fightglory commited on
Commit
75240e8
·
1 Parent(s): 89245fb

First commit

Browse files
Files changed (1) hide show
  1. app.py +247 -0
app.py ADDED
@@ -0,0 +1,247 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ import tensorflow as tf
4
+ from tensorflow.keras import applications
5
+ import pandas as pd
6
+
7
+ BATCH_SIZE = 8
8
+ EPOCHS = 50
9
+ IM_SIZE = (256, 256)
10
+
11
+ dataframe = pd.DataFrame(columns=["Method", "Loss", "Accuracy", "ROC AUC"])
12
+
13
+
14
+ def gradio_predictor(flag, image, modelname):
15
+ image = np.expand_dims(image, axis=0)
16
+ if not flag:
17
+ if modelname == 'InceptionV3':
18
+ model = applications.InceptionV3(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
19
+ weights=None,
20
+ classes=2,
21
+ classifier_activation="softmax",
22
+ )
23
+ model.load_weights('InceptionV3.hdf5')
24
+ result_a = model.predict(image)
25
+ result = np.argmax(result_a)
26
+ if result == 0:
27
+ return 'Day (' + str(result_a) + ')'
28
+ else:
29
+ return 'Night (' + str(result_a) + ')'
30
+
31
+ elif modelname == 'MobileNetV2':
32
+ model = applications.MobileNetV2(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
33
+ weights=None,
34
+ classes=2,
35
+ classifier_activation="softmax",
36
+ )
37
+ model.load_weights('MobileNetV2.hdf5')
38
+ result_a = model.predict(image)
39
+ result = np.argmax(result_a)
40
+ if result == 0:
41
+ return 'Day (' + str(result_a) + ')'
42
+ else:
43
+ return 'Night (' + str(result_a) + ')'
44
+ else:
45
+ model = applications.EfficientNetV2B0(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
46
+ weights=None,
47
+ classes=2,
48
+ classifier_activation="softmax",
49
+ )
50
+
51
+ model.load_weights('EfficientNetV2.hdf5')
52
+ result_a = model.predict(image)
53
+ result = np.argmax(result_a)
54
+ if result == 0:
55
+ return 'Day (' + str(result_a) + ')'
56
+ else:
57
+ return 'Night (' + str(result_a) + ')'
58
+ else:
59
+ if modelname == 'InceptionV3':
60
+ model = applications.InceptionV3(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
61
+ weights=None,
62
+ classes=2,
63
+ classifier_activation="softmax",
64
+ )
65
+ model.load_weights('InceptionV3_Aug.hdf5')
66
+ result_a = model.predict(image)
67
+ result = np.argmax(result_a)
68
+ if result == 0:
69
+ return 'Day (' + str(result_a) + ')'
70
+ else:
71
+ return 'Night (' + str(result_a) + ')'
72
+
73
+ elif modelname == 'MobileNetV2':
74
+ model = applications.MobileNetV2(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
75
+ weights=None,
76
+ classes=2,
77
+ classifier_activation="softmax",
78
+ )
79
+ model.load_weights('MobileNetV2_Aug.hdf5')
80
+ result_a = model.predict(image)
81
+ result = np.argmax(result_a)
82
+ if result == 0:
83
+ return 'Day (' + str(result_a) + ')'
84
+ else:
85
+ return 'Night (' + str(result_a) + ')'
86
+ else:
87
+ model = applications.EfficientNetV2B0(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
88
+ weights=None,
89
+ classes=2,
90
+ classifier_activation="softmax",
91
+ )
92
+
93
+ model.load_weights('EfficientNetV2_Aug.hdf5')
94
+ result_a = model.predict(image)
95
+ result = np.argmax(result_a)
96
+ if result == 0:
97
+ return 'Day (' + str(result_a) + ')'
98
+ else:
99
+ return 'Night (' + str(result_a) + ')'
100
+
101
+ data = tf.keras.utils.image_dataset_from_directory(
102
+ 'data_orig/test',
103
+ label_mode='int',
104
+ image_size=IM_SIZE,
105
+ seed=42,
106
+ batch_size=BATCH_SIZE,
107
+ )
108
+
109
+
110
+ def gradio_tester(flag, modelname):
111
+ global dataframe
112
+ if not flag:
113
+ if modelname == 'InceptionV3':
114
+ model = applications.InceptionV3(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
115
+ weights=None,
116
+ classes=2,
117
+ classifier_activation="softmax",
118
+ )
119
+ model.load_weights('InceptionV3.hdf5')
120
+ model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0025),
121
+ loss=tf.keras.losses.SparseCategoricalCrossentropy(),
122
+ metrics=['accuracy'],
123
+ )
124
+ [loss, accuracy] = model.evaluate(data)
125
+ auc = model.predict(data)
126
+ a = tf.keras.metrics.AUC(num_thresholds=3)
127
+ heu = np.concatenate([y for x, y in data], axis=0)
128
+ a.update_state(heu,tf.math.argmax(auc,-1))
129
+ middat = pd.Series({'Method': modelname, 'Loss': loss, 'Accuracy': accuracy, 'ROC AUC': a.result().numpy()})
130
+ dataframe = pd.concat([dataframe, middat.to_frame().T], ignore_index=True)
131
+ return dataframe
132
+
133
+ elif modelname == 'MobileNetV2':
134
+ model = applications.MobileNetV2(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
135
+ weights=None,
136
+ classes=2,
137
+ classifier_activation="softmax",
138
+ )
139
+ model.load_weights('MobileNetV2.hdf5')
140
+ model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0025),
141
+ loss=tf.keras.losses.SparseCategoricalCrossentropy(),
142
+ metrics=['accuracy'],
143
+ )
144
+ [loss, accuracy] = model.evaluate(data)
145
+ auc = model.predict(data)
146
+ a = tf.keras.metrics.AUC(num_thresholds=3)
147
+ heu = np.concatenate([y for x, y in data], axis=0)
148
+ a.update_state(heu, tf.math.argmax(auc, -1))
149
+ middat = pd.Series({'Method': modelname, 'Loss': loss, 'Accuracy': accuracy, 'ROC AUC': a.result().numpy()})
150
+ dataframe = pd.concat([dataframe, middat.to_frame().T], ignore_index=True)
151
+ return dataframe
152
+ else:
153
+ model = applications.EfficientNetV2B0(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
154
+ weights=None,
155
+ classes=2,
156
+ classifier_activation="softmax",
157
+ )
158
+
159
+ model.load_weights('EfficientNetV2.hdf5')
160
+ model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0025),
161
+ loss=tf.keras.losses.SparseCategoricalCrossentropy(),
162
+ metrics=['accuracy'],
163
+ )
164
+ [loss, accuracy] = model.evaluate(data)
165
+ auc = model.predict(data)
166
+ a = tf.keras.metrics.AUC(num_thresholds=3)
167
+ heu = np.concatenate([y for x, y in data], axis=0)
168
+ a.update_state(heu, tf.math.argmax(auc, -1))
169
+ middat = pd.Series({'Method': modelname, 'Loss': loss, 'Accuracy': accuracy, 'ROC AUC': a.result().numpy()})
170
+ dataframe = pd.concat([dataframe, middat.to_frame().T], ignore_index=True)
171
+ return dataframe
172
+ else:
173
+ if modelname == 'InceptionV3':
174
+ model = applications.InceptionV3(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
175
+ weights=None,
176
+ classes=2,
177
+ classifier_activation="softmax",
178
+ )
179
+ model.load_weights('InceptionV3_Aug.hdf5')
180
+ model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0025),
181
+ loss=tf.keras.losses.SparseCategoricalCrossentropy(),
182
+ metrics=['accuracy'],
183
+ )
184
+ [loss, accuracy] = model.evaluate(data)
185
+ auc = model.predict(data)
186
+ a = tf.keras.metrics.AUC(num_thresholds=3)
187
+ heu = np.concatenate([y for x, y in data], axis=0)
188
+ a.update_state(heu, tf.math.argmax(auc, -1))
189
+ middat = pd.Series({'Method': modelname, 'Loss': loss, 'Accuracy': accuracy, 'ROC AUC': a.result().numpy()})
190
+ dataframe = pd.concat([dataframe, middat.to_frame().T], ignore_index=True)
191
+ return dataframe
192
+
193
+ elif modelname == 'MobileNetV2':
194
+ model = applications.MobileNetV2(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
195
+ weights=None,
196
+ classes=2,
197
+ classifier_activation="softmax",
198
+ )
199
+ model.load_weights('MobileNetV2_Aug.hdf5')
200
+ model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0025),
201
+ loss=tf.keras.losses.SparseCategoricalCrossentropy(),
202
+ metrics=['accuracy'],
203
+ )
204
+ [loss, accuracy] = model.evaluate(data)
205
+ auc = model.predict(data)
206
+ a = tf.keras.metrics.AUC(num_thresholds=3)
207
+ heu = np.concatenate([y for x, y in data], axis=0)
208
+ a.update_state(heu, tf.math.argmax(auc, -1))
209
+ middat = pd.Series({'Method': modelname, 'Loss': loss, 'Accuracy': accuracy, 'ROC AUC': a.result().numpy()})
210
+ dataframe = pd.concat([dataframe, middat.to_frame().T], ignore_index=True)
211
+ return dataframe
212
+ else:
213
+ model = applications.EfficientNetV2B0(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
214
+ weights=None,
215
+ classes=2,
216
+ classifier_activation="softmax",
217
+ )
218
+
219
+ model.load_weights('EfficientNetV2_Aug.hdf5')
220
+ model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0025),
221
+ loss=tf.keras.losses.SparseCategoricalCrossentropy(),
222
+ metrics=['accuracy'],
223
+ )
224
+ [loss, accuracy] = model.evaluate(data)
225
+ auc = model.predict(data)
226
+ a = tf.keras.metrics.AUC(num_thresholds=3)
227
+ heu = np.concatenate([y for x, y in data], axis=0)
228
+ a.update_state(heu, tf.math.argmax(auc, -1))
229
+ middat = pd.Series({'Method': modelname, 'Loss': loss, 'Accuracy': accuracy, 'ROC AUC': a.result().numpy()})
230
+ dataframe = pd.concat([dataframe, middat.to_frame().T], ignore_index=True)
231
+ return dataframe
232
+
233
+
234
+ with gr.Blocks() as interface:
235
+ header = gr.Markdown('# Data Augmentation with CycleGAN Images')
236
+ image_input = gr.Image(shape=(256, 256), label="Cityscape Image")
237
+ model_chooser = gr.Dropdown(choices=['InceptionV3', 'MobileNetV2', 'EfficientNetV2'], value='InceptionV3',
238
+ label='Model Chooser')
239
+ text_output = gr.Textbox(label='Predicted day/night')
240
+ augmenter_btn = gr.Checkbox(label="Use augmented models?")
241
+ submit_btn = gr.Button("Predict")
242
+ submit_btn.click(fn=gradio_predictor, inputs=[augmenter_btn, image_input, model_chooser], outputs=text_output)
243
+ test_btn = gr.Button("Run test on dataset")
244
+ text_test = gr.Dataframe(label="Test results")
245
+ test_btn.click(fn=gradio_tester, inputs=[augmenter_btn, model_chooser], outputs=text_test)
246
+
247
+ interface.launch(share=True)