Skip to content

Commit

Permalink
working on the docs; adding jupyter notebook for image classify
Browse files Browse the repository at this point in the history
  • Loading branch information
decarlof committed Jun 23, 2016
1 parent aa2f559 commit 6f74bb3
Show file tree
Hide file tree
Showing 15 changed files with 927 additions and 12 deletions.
4 changes: 3 additions & 1 deletion convnet/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
__all__ = ['nor_data',
'check_random_state',
'extract_patches',
'reconstruct_patches']
'reconstruct_patches',
'img_window',
'extract_3d']


def nor_data(img):
Expand Down
237 changes: 237 additions & 0 deletions doc/demo/classify_evaluate.ipynb

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions doc/demo/classify_evaluate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Example script
"""

from __future__ import print_function
import dxchange
import numpy as np
from convnet.utils import nor_data
from convnet.utils import extract_3d
from convnet.utils import img_window
from convnet.classify import model
import matplotlib.pyplot as plt
import time
import glob

np.random.seed(1337)

dim_img = 128
patch_size = (dim_img, dim_img)
batch_size = 50
nb_classes = 2
nb_epoch = 12

# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3
nb_evl = 100

start_time = time.time()
fnames = glob.glob('/local/decarlo/data/databank/xiaogang/center/test_05/Hornby_APS_1000_1058_1/*.tiff')
fnames = np.sort(fnames)

mdl = model(dim_img, nb_filters, nb_conv, nb_classes)

mdl.load_weights('classify_training_weights.h5')
print('The model loading time is %s seconds'%(time.time()-start_time))
start_time = time.time()
Y_score = np.zeros((len(fnames)))

for i in range(len(fnames)):
img = dxchange.read_tiff(fnames[i])
img = nor_data(img)
X_evl = np.zeros((nb_evl, dim_img, dim_img))

for j in range(nb_evl):
X_evl[j] = img_window(img[360:1460, 440:1440], dim_img)
X_evl = X_evl.reshape(X_evl.shape[0], 1, dim_img, dim_img)
Y_evl = mdl.predict(X_evl, batch_size=batch_size)
Y_score[i] = sum(np.dot(Y_evl, [0, 1]))
#print('The evaluate score is:', Y_score[i])
#Y_score = sum(np.round(Y_score))/len(Y_score)


ind_max = np.argmax(Y_score)
print('The well-centered reconstruction is:', fnames[ind_max])
print('The prediction runs for %s seconds'%(time.time()-start_time))
plt.plot(Y_score)
plt.show()


387 changes: 387 additions & 0 deletions doc/demo/classify_train.ipynb

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions doc/demo/classify_train.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Example script
"""

from __future__ import print_function
import dxchange
import numpy as np
from convnet.utils import nor_data
from convnet.utils import extract_3d
from convnet.utils import img_window
from convnet.classify import train

np.random.seed(1337)

dim_img = 128
patch_size = (dim_img, dim_img)
batch_size = 50
nb_classes = 2
nb_epoch = 12

# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3


fname = '/local/decarlo/data/databank/xiaogang/center/test_01/Blakely_SLS_1038_1058_1/1038.tiff'
ind_uncenter1 = range(1038, 1047)
ind_uncenter2 = range(1049, 1057)
uncenter1 = dxchange.read_tiff_stack(fname, ind=ind_uncenter1, digit=4)
uncenter2 = dxchange.read_tiff_stack(fname, ind=ind_uncenter2, digit=4)
uncenter = np.concatenate((uncenter1, uncenter2), axis=0)
uncenter = nor_data(uncenter)
print (uncenter.shape)
uncenter = img_window(uncenter[:, 360:1460, 440:1440], 200)
print (uncenter.shape)
uncenter_patches = extract_3d(uncenter, patch_size, 1)
np.random.shuffle(uncenter_patches)
print (uncenter_patches.shape)
# print uncenter_patches.shape
center_img = dxchange.read_tiff('/local/decarlo/data/databank/xiaogang/center/test_01/Blakely_SLS_1038_1058_1/1048.tiff')
center_img = nor_data(center_img)
print (center_img.shape)
center_img = img_window(center_img[360:1460, 440:1440], 400)
center_patches = extract_3d(center_img, patch_size, 1)
np.random.shuffle(center_patches)
print (center_patches.shape)
# plt.imshow(center_img, cmap='gray', interpolation= None)
# plt.show()

x_train = np.concatenate((uncenter_patches[0:50000], center_patches[0:50000]), axis=0)
x_test = np.concatenate((uncenter_patches[50000:60000], center_patches[50000:60000]), axis=0)
x_train = x_train.reshape(x_train.shape[0], 1, dim_img, dim_img)
x_test = x_test.reshape(x_test.shape[0], 1, dim_img, dim_img)
y_train = np.zeros(100000)
y_train[50000:99999] = 1
y_test = np.zeros(20000)
y_test[10000:19999] = 1

model = train(x_train, y_train, x_test, y_test, dim_img, nb_filters, nb_conv, batch_size, nb_epoch, nb_classes)
model.save_weights('classify_training_weights.h5')

1 change: 1 addition & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ API reference
.. toctree::

api/convnet.transform
api/convnet.classify
api/convnet.utils


Expand Down
15 changes: 15 additions & 0 deletions doc/source/api/convnet.classify.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
:mod:`convnet.classify`
========================

.. automodule:: convnet.classify
:members:
:show-inheritance:
:undoc-members:

.. rubric:: **Functions:**

.. autosummary::

model
train

3 changes: 3 additions & 0 deletions doc/source/api/convnet.utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
check_random_state
extract_patches
reconstruct_patches
img_window
extract_3d


9 changes: 2 additions & 7 deletions doc/source/demo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ scripts from `here <https://github.com/XiaogangYangAPS/convnet/tree/master/doc/d

.. toctree::

ipynb/transform_train.rst
ipynb/transform_predict.rst

.. automodule:: convnet
:members:
:undoc-members:
:show-inheritance:
ipynb/transform.rst
ipynb/classify.rst

12 changes: 12 additions & 0 deletions doc/source/ipynb/classify.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Classify
--------

.. toctree::

classify_train.rst
classify_evaluate.rst

.. automodule:: convnet
:members:
:undoc-members:
:show-inheritance:
57 changes: 57 additions & 0 deletions doc/source/ipynb/classify_evaluate.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Evaluate
--------

Here is an example on how to use an already trained convolutional neural netwrok
on a new image. The output will be estract features based on the training received.

You can download the python scritp :download:`here<../../../doc/demo/transform_predict.py>`
or the Jupyter notebook :download:`here<../../../doc/demo/transform_predict.ipynb>`

.. code:: python
%pylab inline
.. parsed-literal::
Populating the interactive namespace from numpy and matplotlib
.. code:: python
import dxchange
Image data I/O in ConvNet is supported by
`DXchange <http://dxchange.readthedocs.io>`__.

.. code:: python
import matplotlib.pyplot as plt
matplotlib provide plotting of the result in this notebook.

`Install <http://convnet.readthedocs.io/en/latest/install.html>`__ ConvNet
then:

.. code:: python
import numpy as np
from convnet.utils import nor_data
from convnet.utils import extract_3d
from convnet.utils import img_window
from convnet.classify import model
import matplotlib.pyplot as plt
import time
import glob
.. code:: python
np.random.seed(1337)
dim_img = 128
patch_size = (dim_img, dim_img)
batch_size = 50
nb_classes = 2
nb_epoch = 12
to be completed
61 changes: 61 additions & 0 deletions doc/source/ipynb/classify_train.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Train
-----

Here is an example on how to train a convolutional neural network.

You can download the python scritp :download:`here<../../../doc/demo/transform_train.py>`
or the Jupyter notebook :download:`here<../../../doc/demo/transform_train.ipynb>`

.. code:: python
%pylab inline
.. parsed-literal::
Populating the interactive namespace from numpy and matplotlib
.. code:: python
import dxchange
Image data I/O in ConvNet is supported by
`DXchange <http://dxchange.readthedocs.io>`__.

.. code:: python
import matplotlib.pyplot as plt
matplotlib provide plotting of the result in this notebook.

`Install <http://convnet.readthedocs.io/en/latest/install.html>`__ ConvNet
then:

.. code:: python
import numpy as np
from convnet.utils import nor_data
from convnet.utils import extract_3d
from convnet.utils import img_window
from convnet.classify import train
.. code:: python
np.random.seed(1337)
dim_img = 128
patch_size = (dim_img, dim_img)
batch_size = 50
nb_classes = 2
nb_epoch = 12
.. code:: python
nb_filters = 32
nb_pool = 2
nb_conv = 3
img_y = dxchange.read_tiff('../../test/test_data/training_output.tiff')
to be completed
12 changes: 12 additions & 0 deletions doc/source/ipynb/transform.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Transform
---------

.. toctree::

transform_train.rst
transform_predict.rst

.. automodule:: convnet
:members:
:undoc-members:
:show-inheritance:
4 changes: 2 additions & 2 deletions doc/source/ipynb/transform_predict.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Transform Predict
-----------------
Predict
-------

Here is an example on how to use an already trained convolutional neural netwrok
on a new image. The output will be estract features based on the training received.
Expand Down
4 changes: 2 additions & 2 deletions doc/source/ipynb/transform_train.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Transform Training
------------------
Train
-----

Here is an example on how to train a convolutional neural network.

Expand Down

0 comments on commit 6f74bb3

Please sign in to comment.