-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfruit_recognition_layer_visualization.py
79 lines (61 loc) · 2.34 KB
/
fruit_recognition_layer_visualization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import fruit_recognition_model
import tflearn
from tflearn.data_utils import build_hdf5_image_dataset
import h5py
import time
root_directory = '/home/emooo/PycharmProjects/machine_learning/'
image_directory = root_directory + 'dataset/test/'
dataset_file = root_directory + 'trained_data/predict_dataset.h5'
imageWidth = 80
imageHeight = 80
numClasses = 2
network = fruit_recognition_model.getEkalchevNet(imageWidth, imageHeight, numClasses)
model = tflearn.DNN(network)
model.load("_fruit_recognition_model.tfl")
print("Predicted values:")
build_hdf5_image_dataset(image_directory, image_shape=(imageWidth, imageHeight), mode='folder', output_path=dataset_file, categorical_labels=True, normalize=True)
h5f = h5py.File(dataset_file, 'r')
X = h5f['X']
print("Predict started")
start = time.perf_counter()
print(model.predict(X))
end = time.perf_counter()
print("Predict completed", end - start, "sec")
#plot hidden layers
from mpl_toolkits.axes_grid1 import make_axes_locatable
def nice_imshow(ax, data, vmin=None, vmax=None, cmap=None):
"""Wrapper around pl.imshow"""
if cmap is None:
cmap = cm.jet
if vmin is None:
vmin = data.min()
if vmax is None:
vmax = data.max()
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
im = ax.imshow(data, vmin=vmin, vmax=vmax, interpolation='nearest', cmap=cmap)
pl.colorbar(im, cax=cax)
import numpy.ma as ma
def make_mosaic(imgs, nrows, ncols, border=1):
"""
Given a set of images with all the same shape, makes a
mosaic with nrows and ncols
"""
nimgs = imgs.shape[0]
imshape = imgs.shape[1:]
mosaic = ma.masked_all((nrows * imshape[0] + (nrows - 1) * border,
ncols * imshape[1] + (ncols - 1) * border),
dtype=np.float32)
paddedh = imshape[0] + border
paddedw = imshape[1] + border
for i in xrange(nimgs):
row = int(np.floor(i / ncols))
col = i % ncols
mosaic[row * paddedh:row * paddedh + imshape[0],
col * paddedw:col * paddedw + imshape[1]] = imgs[i]
return mosaic
W_visu = model.get_weights(conv1.W) # Here share the weights you want, for example your layer conv1
W_visu = np.squeeze(W_visu)
pl.figure(figsize=(15, 15))
pl.title('conv1 weights')
nice_imshow(pl.gca(), make_mosaic(W, 6, 6), cmap=cm.binary)