forked from XYangXRay/dxlearn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working on the docs; adding jupyter notebook for image classify
- Loading branch information
Showing
15 changed files
with
927 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ API reference | |
.. toctree:: | ||
|
||
api/convnet.transform | ||
api/convnet.classify | ||
api/convnet.utils | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,7 @@ | |
check_random_state | ||
extract_patches | ||
reconstruct_patches | ||
img_window | ||
extract_3d | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters