forked from dandxy89/ImageModels
/
Train.py
235 lines (167 loc) · 6.58 KB
/
Train.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""
My Current Environment has been setup to use: Theano
Script provides a number of functions:
Demonstrates that the Compile Model can process an Output
Demonstrates who to export the Tensorflow Model (Issue)
Demonstrates how to visualise slices of the Convolutional Layers
Saves model summary and Image to the Model folder
Created 13/5/2016
"""
import numpy as np
from keras.models import Model
from AlexNet import check_print as AlexNetModelcheck_print
from AlexNet import create_model as AlexNetModel
from AlexNet_Original import check_print as AlexNetOrigModelcheck_print
from AlexNet_Original import create_model as AlexNetOrigModel
from CaffeNet import check_print as CaffeNetModelcheck_print
from CaffeNet import create_model as CaffeNetModel
from GoogLeNet import check_print as GoogleNetModelcheck_print
from GoogLeNet import create_model as GoogleNetModel
from VGG19 import check_print as VGG19Modelcheck_print
from VGG19 import create_model as VGG19Model
model_choice = dict(AlexNet=AlexNetModel,
AlexNetOrig=AlexNetOrigModel,
CaffeNet=CaffeNetModel,
GoogLeNet=GoogleNetModel,
VGG19=VGG19Model)
model_val = 'AlexNet'
test_batch = False
get_graph = False
show_activation = True
show_cmd_output = False
if not show_cmd_output:
"""
Compiles the respective Model of choice
"""
print('Building : {}'.format(model_val))
x, img_input, CONCAT_AXIS, INP_SHAPE, DIM_ORDERING = model_choice[
model_val]()
# Create a Keras Model - Functional API
model = Model(input=img_input,
output=[x])
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy')
if test_batch:
"""
Setup:
test_batch = True
get_graph = False
show_activation = False
show_cmd_output = False
"""
print('Testing Model Output with Dummy Data...')
# Check that it can predict...
batch_n = 5 # Test Batch Size
test_images = np.random.rand(batch_n, 3, 224, 224)
# Get Model Prediction
output = model.predict(x=test_images,
batch_size=batch_n)
# Get the Softmax Prediction per Image
print(np.argmax(output, axis=1))
if get_graph:
"""
Having some difficulty with the installation of Tensorflow Serving on Mac!
AHH!
Setup:
test_batch = False
get_graph = True
show_activation = False
show_cmd_output = False
"""
from keras import backend as K
# all new operations will be in test mode from now on
K.set_learning_phase(0)
# serialize the model and get its weights, for quick re-building
config = model.get_config()
weights = model.get_weights()
# re-build a model where the learning phase is now hard-coded to 0
from keras.models import model_from_config
new_model = model_from_config(config)
new_model.set_weights(weights)
import tensorflow as tf
import sys
sys.path.insert(0, '/Users/dan.dixey/Desktop/QBiz/serving')
# Unable to Import THIS!! why?
from tensorflow_serving.session_bundle import exporter
sess = K.get_session()
export_path = './Serving' # where to save the exported graph
export_version = 0o0000001 # version number (integer)
saver = tf.train.Saver(sharded=True)
model_exporter = exporter.Exporter(saver)
signature = exporter.classification_signature(input_tensor=model.input,
scores_tensor=model.output)
model_exporter.init(sess.graph.as_graph_def(),
default_graph_signature=signature)
model_exporter.export(export_path, tf.constant(export_version), sess)
if show_activation:
"""
Experimenting with different the Cov Net Layers to visulise their outputs
This section will push data through the layer and record the activations
Normalise (Min/Max) the Data in the whole 3D array, slice for one layer of the 3D Array
Plot using Maplotlib the Output of 10 slice...
Setup:
test_batch = False
get_graph = False
show_activation = True
show_cmd_output = False
"""
from keras import backend as K
import matplotlib.pyplot as plt
from time import sleep
n_layers = len(model.layers)
print('Number of Layers to choose from: {}'.format(n_layers))
zip(model.layers, range(n_layers))
layer_n = input('Enter a Number for the Layer to Viz: ')
def get_activations(model, layer, X_batch):
# Credit for this function belongs to *damaha* (Github Username)
# https://github.com/fchollet/keras/issues/41
# from keras import backend as K
get_activations = K.function(
[model.layers[0].input, K.learning_phase()], model.layers[layer].output)
activations = get_activations([X_batch, 0])
return activations
# Check that it can predict...
batch_n = 5 # Test Batch Size
test_images = np.random.rand(batch_n, 3, 224, 224)
layer_output = get_activations(
model=model,
layer=layer_n,
X_batch=test_images)
# Visualisation
def normalise_3d(data):
min, max = data.min(), data.max()
return (data - min) / (max - min)
data = normalise_3d(data=layer_output[1, :, :, :])
for slice_v in range(10):
H = data[slice_v, :, :]
fig = plt.figure(figsize=(6, 3.2))
ax = fig.add_subplot(111)
ax.set_title('Visualise slice {} within the Layer'.format(slice_v))
plt.imshow(H)
ax.set_aspect('equal')
cax = fig.add_axes([0, 1, 0, 1])
cax.get_xaxis().set_visible(False)
cax.get_yaxis().set_visible(False)
cax.patch.set_alpha(0)
cax.set_frame_on(False)
plt.colorbar(orientation='vertical')
plt.show()
sleep(2)
if show_cmd_output:
"""
Save the Command Line Output to a text file
Example: python Train.py > Model/AlexNet.txt
Setup:
test_batch = False
get_graph = False
show_activation = False
show_cmd_output = True
"""
print('Model : {}'.format(model_val))
model_print = dict(AlexNet=AlexNetModelcheck_print,
AlexNetOrig=AlexNetOrigModelcheck_print,
CaffeNet=CaffeNetModelcheck_print,
GoogLeNet=GoogleNetModelcheck_print,
VGG19=VGG19Modelcheck_print)
# Run Function to get Output
model_print[model_val]()