Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e26c42a
NIR processing
mvpatel2000 Jun 28, 2017
fa8aea6
added sloth utils
Jun 28, 2017
7641bdb
fixed bug with sloth utils
Jun 28, 2017
b43f1e5
update notebook
mvpatel2000 Jun 29, 2017
595aa11
added augment utility
Jun 29, 2017
bed09ee
Merge branch 'deeplearning' of github.com:MPBA/wv2017project into dee…
Jun 29, 2017
67981fb
raster saving
mvpatel2000 Jun 29, 2017
cc7d0b5
Merge branch 'deeplearning' of github.com:MPBA/wv2017project into dee…
mvpatel2000 Jun 29, 2017
1441bbb
fixing notebook push issues
mvpatel2000 Jun 29, 2017
fa584d9
updated calib conversion
mvpatel2000 Jun 29, 2017
d00c0db
fixed sloth utils bugs
Jun 29, 2017
6d8b0b4
Merge branch 'deeplearning' of github.com:MPBA/wv2017project into dee…
Jun 29, 2017
e61c6bb
Function to convert sloth files into h5 files
smundada Jun 29, 2017
84b6891
Update hdf5_conversion.py
smundada Jun 29, 2017
e1ac7ac
automating NIR stuff
mvpatel2000 Jun 29, 2017
50e5ba5
Raster transformation
mvpatel2000 Jun 30, 2017
531b3fb
json to csv for faster-RCNN
mvpatel2000 Jul 1, 2017
935c3b0
naming conventions
mvpatel2000 Jul 1, 2017
3c829e8
Added spaces
Jul 1, 2017
4634ad2
Implemented augmentated images generator from ssd_keras
Jul 1, 2017
19b8302
some utils and edits to the Generator
Jul 1, 2017
52ceaab
Merge branch 'deeplearning' of github.com:MPBA/wv2017project into dee…
Jul 1, 2017
6ac5654
folder for images to hdf5 not hardcoded anymore
Jul 1, 2017
4b5d694
Renamed file
Jul 1, 2017
dde25ef
jsonsloth_to_csv parametrized
Jul 1, 2017
bf85baa
Moved classes into right folder
Jul 1, 2017
8283f10
Merge branch 'deeplearning' of github.com:MPBA/wv2017project into dee…
Jul 1, 2017
3a2423e
useful scripts for h5
Jul 1, 2017
a49e6d8
Renamed files
Jul 1, 2017
2ba8c4b
multiGPU
mvpatel2000 Jul 3, 2017
b207fa9
added pipeline to create the csv for frcnn
Jul 3, 2017
d17fea4
pipeline now allows for multiple json files in the same folder
Jul 3, 2017
3ca770f
added train test split
Jul 3, 2017
294a889
Added initial script for preprocessing grapes
Jul 4, 2017
27a1e72
Update grapes script
Jul 4, 2017
821fd8d
Added script for converting images from any resolution and relative j…
Jul 4, 2017
f1cdddc
Updated convert img
Jul 4, 2017
4bc709f
sliding windows backup
mvpatel2000 Jul 4, 2017
a4bf29f
frcnn stuff
Jul 6, 2017
9b570a7
changes to the utils
Jul 6, 2017
3ef044e
upload windows
mvpatel2000 Jul 6, 2017
2636663
Merge branch 'deeplearning' of github.com:MPBA/wv2017project into dee…
mvpatel2000 Jul 6, 2017
c60b5f8
classification code upload
mvpatel2000 Jul 10, 2017
813519b
test
mvpatel2000 Jul 11, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
256 changes: 256 additions & 0 deletions NIR/MAPIR_calibration.ipynb

Large diffs are not rendered by default.

283 changes: 283 additions & 0 deletions NIR/RastNotebook.ipynb

Large diffs are not rendered by default.

256 changes: 256 additions & 0 deletions NIR/optimus

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions frcnn/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# IPython Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# dotenv
.env

# virtualenv
venv/
ENV/

# Spyder project settings
.spyderproject

# Rope project settings
.ropeproject
57 changes: 57 additions & 0 deletions frcnn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# keras-frcnn
Keras implementation of Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks.


USAGE:
- Both theano and tensorflow backends are supported. However compile times are very high in theano, and tensorflow is highly recommended.
- `train_frcnn.py` can be used to train a model. To train on Pascal VOC data, simply do:
`python train_frcnn.py -p /path/to/pascalvoc/`.
- the Pascal VOC data set (images and annotations for bounding boxes around the classified objects) can be obtained from: http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
- simple_parser.py provides an alternative way to input data, using a text file. Simply provide a text file, with each
line containing:

`filepath,x1,y1,x2,y2,class_name`

For example:

/data/imgs/img_001.jpg,837,346,981,456,cow

/data/imgs/img_002.jpg,215,312,279,391,cat

The classes will be inferred from the file. To use the simple parser instead of the default pascal voc style parser,
use the command line option `-o simple`. For example `python train_frcnn.py -o simple -p my_data.txt`.

- Running `train_frcnn.py` will write weights to disk to an hdf5 file, as well as all the setting of the training run to a `pickle` file. These
settings can then be loaded by `test_frcnn.py` for any testing.

- test_frcnn.py can be used to perform inference, given pretrained weights and a config file. Specify a path to the folder containing
images:
`python test_frcnn.py -p /path/to/test_data/`
- Data augmentation can be applied by specifying `--hf` for horizontal flips, `--vf` for vertical flips and `--rot` for 90 degree rotations



NOTES:
- config.py contains all settings for the train or test run. The default settings match those in the original Faster-RCNN
paper. The anchor box sizes are [128, 256, 512] and the ratios are [1:1, 1:2, 2:1].
- The theano backend by default uses a 7x7 pooling region, instead of 14x14 as in the frcnn paper. This cuts down compiling time slightly.
- The tensorflow backend performs a resize on the pooling region, instead of max pooling. This is much more efficient and has little impact on results.


Example output:

![ex1](http://i.imgur.com/7Lmb2RC.png)
![ex2](http://i.imgur.com/h58kCIV.png)
![ex3](http://i.imgur.com/EbvGBaG.png)
![ex4](http://i.imgur.com/i5UAgLb.png)

ISSUES:

- If you get this error:
`ValueError: There is a negative shape in the graph!`
than update keras to the newest version

- Make sure to use `python2`, not `python3`. If you get this error:
`TypeError: unorderable types: dict() < dict()` you are using python3

- If you run out of memory, try reducing the number of ROIs that are processed simultaneously. Try passing a lower `-n` to `train_frcnn.py`. Alternatively, try reducing the image size from the default value of 600 (this setting is found in `config.py`.
Binary file added frcnn/config.pickle
Binary file not shown.
Binary file added frcnn/config2.pickle
Binary file not shown.
83 changes: 83 additions & 0 deletions frcnn/keras_frcnn/FixedBatchNormalization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from keras.engine import Layer, InputSpec
from keras import initializers, regularizers
from keras import backend as K


class FixedBatchNormalization(Layer):

def __init__(self, epsilon=1e-3, axis=-1,
weights=None, beta_init='zero', gamma_init='one',
gamma_regularizer=None, beta_regularizer=None, **kwargs):

self.supports_masking = True
self.beta_init = initializers.get(beta_init)
self.gamma_init = initializers.get(gamma_init)
self.epsilon = epsilon
self.axis = axis
self.gamma_regularizer = regularizers.get(gamma_regularizer)
self.beta_regularizer = regularizers.get(beta_regularizer)
self.initial_weights = weights
super(FixedBatchNormalization, self).__init__(**kwargs)

def build(self, input_shape):
self.input_spec = [InputSpec(shape=input_shape)]
shape = (input_shape[self.axis],)

self.gamma = self.add_weight(shape,
initializer=self.gamma_init,
regularizer=self.gamma_regularizer,
name='{}_gamma'.format(self.name),
trainable=False)
self.beta = self.add_weight(shape,
initializer=self.beta_init,
regularizer=self.beta_regularizer,
name='{}_beta'.format(self.name),
trainable=False)
self.running_mean = self.add_weight(shape, initializer='zero',
name='{}_running_mean'.format(self.name),
trainable=False)
self.running_std = self.add_weight(shape, initializer='one',
name='{}_running_std'.format(self.name),
trainable=False)

if self.initial_weights is not None:
self.set_weights(self.initial_weights)
del self.initial_weights

self.built = True

def call(self, x, mask=None):

assert self.built, 'Layer must be built before being called'
input_shape = K.int_shape(x)

reduction_axes = list(range(len(input_shape)))
del reduction_axes[self.axis]
broadcast_shape = [1] * len(input_shape)
broadcast_shape[self.axis] = input_shape[self.axis]

if sorted(reduction_axes) == range(K.ndim(x))[:-1]:
x_normed = K.batch_normalization(
x, self.running_mean, self.running_std,
self.beta, self.gamma,
epsilon=self.epsilon)
else:
# need broadcasting
broadcast_running_mean = K.reshape(self.running_mean, broadcast_shape)
broadcast_running_std = K.reshape(self.running_std, broadcast_shape)
broadcast_beta = K.reshape(self.beta, broadcast_shape)
broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
x_normed = K.batch_normalization(
x, broadcast_running_mean, broadcast_running_std,
broadcast_beta, broadcast_gamma,
epsilon=self.epsilon)

return x_normed

def get_config(self):
config = {'epsilon': self.epsilon,
'axis': self.axis,
'gamma_regularizer': self.gamma_regularizer.get_config() if self.gamma_regularizer else None,
'beta_regularizer': self.beta_regularizer.get_config() if self.beta_regularizer else None}
base_config = super(FixedBatchNormalization, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
116 changes: 116 additions & 0 deletions frcnn/keras_frcnn/RoiPoolingConv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
from keras.engine.topology import Layer
import keras.backend as K

if K.backend() == 'tensorflow':
import tensorflow as tf

class RoiPoolingConv(Layer):
'''ROI pooling layer for 2D inputs.
See Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition,
K. He, X. Zhang, S. Ren, J. Sun
# Arguments
pool_size: int
Size of pooling region to use. pool_size = 7 will result in a 7x7 region.
num_rois: number of regions of interest to be used
# Input shape
list of two 4D tensors [X_img,X_roi] with shape:
X_img:
`(1, channels, rows, cols)` if dim_ordering='th'
or 4D tensor with shape:
`(1, rows, cols, channels)` if dim_ordering='tf'.
X_roi:
`(1,num_rois,4)` list of rois, with ordering (x,y,w,h)
# Output shape
3D tensor with shape:
`(1, num_rois, channels, pool_size, pool_size)`
'''
def __init__(self, pool_size, num_rois, **kwargs):

self.dim_ordering = K.image_dim_ordering()
assert self.dim_ordering in {'tf', 'th'}, 'dim_ordering must be in {tf, th}'

self.pool_size = pool_size
self.num_rois = num_rois

super(RoiPoolingConv, self).__init__(**kwargs)

def build(self, input_shape):
if self.dim_ordering == 'th':
self.nb_channels = input_shape[0][1]
elif self.dim_ordering == 'tf':
self.nb_channels = input_shape[0][3]

def compute_output_shape(self, input_shape):
if self.dim_ordering == 'th':
return None, self.num_rois, self.nb_channels, self.pool_size, self.pool_size
else:
return None, self.num_rois, self.pool_size, self.pool_size, self.nb_channels

def call(self, x, mask=None):

assert(len(x) == 2)

img = x[0]
rois = x[1]

input_shape = K.shape(img)

outputs = []

for roi_idx in range(self.num_rois):

x = rois[0, roi_idx, 0]
y = rois[0, roi_idx, 1]
w = rois[0, roi_idx, 2]
h = rois[0, roi_idx, 3]

row_length = w / float(self.pool_size)
col_length = h / float(self.pool_size)

num_pool_regions = self.pool_size

#NOTE: the RoiPooling implementation differs between theano and tensorflow due to the lack of a resize op
# in theano. The theano implementation is much less efficient and leads to long compile times

if self.dim_ordering == 'th':
for jy in range(num_pool_regions):
for ix in range(num_pool_regions):
x1 = x + ix * row_length
x2 = x1 + row_length
y1 = y + jy * col_length
y2 = y1 + col_length

x1 = K.cast(x1, 'int32')
x2 = K.cast(x2, 'int32')
y1 = K.cast(y1, 'int32')
y2 = K.cast(y2, 'int32')

x2 = x1 + K.maximum(1,x2-x1)
y2 = y1 + K.maximum(1,y2-y1)

new_shape = [input_shape[0], input_shape[1],
y2 - y1, x2 - x1]

x_crop = img[:, :, y1:y2, x1:x2]
xm = K.reshape(x_crop, new_shape)
pooled_val = K.max(xm, axis=(2, 3))
outputs.append(pooled_val)

elif self.dim_ordering == 'tf':
x = K.cast(x, 'int32')
y = K.cast(y, 'int32')
w = K.cast(w, 'int32')
h = K.cast(h, 'int32')

rs = tf.image.resize_images(img[:, y:y+h, x:x+w, :], (self.pool_size, self.pool_size))
outputs.append(rs)

final_output = K.concatenate(outputs, axis=0)
final_output = K.reshape(final_output, (1, self.num_rois, self.pool_size, self.pool_size, self.nb_channels))

if self.dim_ordering == 'th':
final_output = K.permute_dimensions(final_output, (0, 1, 4, 2, 3))
else:
final_output = K.permute_dimensions(final_output, (0, 1, 2, 3, 4))

return final_output
Empty file added frcnn/keras_frcnn/__init__.py
Empty file.
Loading