Spaces:
Runtime error
Runtime error
File size: 12,598 Bytes
75240e8 37cd563 75240e8 66d2dd4 |
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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
import numpy as np
import gradio as gr
import tensorflow as tf
from tensorflow.keras import applications
import pandas as pd
BATCH_SIZE = 8
EPOCHS = 50
IM_SIZE = (256, 256)
dataframe = pd.DataFrame(columns=["Method", "Loss", "Accuracy", "ROC AUC"])
def gradio_predictor(flag, image, modelname):
image = np.expand_dims(image, axis=0)
if not flag:
if modelname == 'InceptionV3':
model = applications.InceptionV3(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
weights=None,
classes=2,
classifier_activation="softmax",
)
model.load_weights('InceptionV3.hdf5')
result_a = model.predict(image)
result = np.argmax(result_a)
if result == 0:
return 'Day (' + str(result_a) + ')'
else:
return 'Night (' + str(result_a) + ')'
elif modelname == 'MobileNetV2':
model = applications.MobileNetV2(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
weights=None,
classes=2,
classifier_activation="softmax",
)
model.load_weights('MobileNetV2.hdf5')
result_a = model.predict(image)
result = np.argmax(result_a)
if result == 0:
return 'Day (' + str(result_a) + ')'
else:
return 'Night (' + str(result_a) + ')'
else:
model = applications.EfficientNetV2B0(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
weights=None,
classes=2,
classifier_activation="softmax",
)
model.load_weights('EfficientNetV2.hdf5')
result_a = model.predict(image)
result = np.argmax(result_a)
if result == 0:
return 'Day (' + str(result_a) + ')'
else:
return 'Night (' + str(result_a) + ')'
else:
if modelname == 'InceptionV3':
model = applications.InceptionV3(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
weights=None,
classes=2,
classifier_activation="softmax",
)
model.load_weights('InceptionV3_Aug.hdf5')
result_a = model.predict(image)
result = np.argmax(result_a)
if result == 0:
return 'Day (' + str(result_a) + ')'
else:
return 'Night (' + str(result_a) + ')'
elif modelname == 'MobileNetV2':
model = applications.MobileNetV2(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
weights=None,
classes=2,
classifier_activation="softmax",
)
model.load_weights('MobileNetV2_Aug.hdf5')
result_a = model.predict(image)
result = np.argmax(result_a)
if result == 0:
return 'Day (' + str(result_a) + ')'
else:
return 'Night (' + str(result_a) + ')'
else:
model = applications.EfficientNetV2B0(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
weights=None,
classes=2,
classifier_activation="softmax",
)
model.load_weights('EfficientNetV2_Aug.hdf5')
result_a = model.predict(image)
result = np.argmax(result_a)
if result == 0:
return 'Day (' + str(result_a) + ')'
else:
return 'Night (' + str(result_a) + ')'
data = tf.keras.utils.image_dataset_from_directory(
'data_orig/test',
label_mode='int',
image_size=IM_SIZE,
seed=42,
batch_size=BATCH_SIZE,
)
def gradio_tester(flag, modelname):
global dataframe
if not flag:
if modelname == 'InceptionV3':
model = applications.InceptionV3(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
weights=None,
classes=2,
classifier_activation="softmax",
)
model.load_weights('InceptionV3.hdf5')
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0025),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'],
)
[loss, accuracy] = model.evaluate(data)
auc = model.predict(data)
a = tf.keras.metrics.AUC(num_thresholds=3)
heu = np.concatenate([y for x, y in data], axis=0)
a.update_state(heu,tf.math.argmax(auc,-1))
middat = pd.Series({'Method': modelname, 'Loss': loss, 'Accuracy': accuracy, 'ROC AUC': a.result().numpy()})
dataframe = pd.concat([dataframe, middat.to_frame().T], ignore_index=True)
return dataframe
elif modelname == 'MobileNetV2':
model = applications.MobileNetV2(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
weights=None,
classes=2,
classifier_activation="softmax",
)
model.load_weights('MobileNetV2.hdf5')
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0025),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'],
)
[loss, accuracy] = model.evaluate(data)
auc = model.predict(data)
a = tf.keras.metrics.AUC(num_thresholds=3)
heu = np.concatenate([y for x, y in data], axis=0)
a.update_state(heu, tf.math.argmax(auc, -1))
middat = pd.Series({'Method': modelname, 'Loss': loss, 'Accuracy': accuracy, 'ROC AUC': a.result().numpy()})
dataframe = pd.concat([dataframe, middat.to_frame().T], ignore_index=True)
return dataframe
else:
model = applications.EfficientNetV2B0(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
weights=None,
classes=2,
classifier_activation="softmax",
)
model.load_weights('EfficientNetV2.hdf5')
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0025),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'],
)
[loss, accuracy] = model.evaluate(data)
auc = model.predict(data)
a = tf.keras.metrics.AUC(num_thresholds=3)
heu = np.concatenate([y for x, y in data], axis=0)
a.update_state(heu, tf.math.argmax(auc, -1))
middat = pd.Series({'Method': modelname, 'Loss': loss, 'Accuracy': accuracy, 'ROC AUC': a.result().numpy()})
dataframe = pd.concat([dataframe, middat.to_frame().T], ignore_index=True)
return dataframe
else:
if modelname == 'InceptionV3':
model = applications.InceptionV3(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
weights=None,
classes=2,
classifier_activation="softmax",
)
model.load_weights('InceptionV3_Aug.hdf5')
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0025),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'],
)
[loss, accuracy] = model.evaluate(data)
auc = model.predict(data)
a = tf.keras.metrics.AUC(num_thresholds=3)
heu = np.concatenate([y for x, y in data], axis=0)
a.update_state(heu, tf.math.argmax(auc, -1))
middat = pd.Series({'Method': modelname, 'Loss': loss, 'Accuracy': accuracy, 'ROC AUC': a.result().numpy()})
dataframe = pd.concat([dataframe, middat.to_frame().T], ignore_index=True)
return dataframe
elif modelname == 'MobileNetV2':
model = applications.MobileNetV2(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
weights=None,
classes=2,
classifier_activation="softmax",
)
model.load_weights('MobileNetV2_Aug.hdf5')
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0025),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'],
)
[loss, accuracy] = model.evaluate(data)
auc = model.predict(data)
a = tf.keras.metrics.AUC(num_thresholds=3)
heu = np.concatenate([y for x, y in data], axis=0)
a.update_state(heu, tf.math.argmax(auc, -1))
middat = pd.Series({'Method': modelname, 'Loss': loss, 'Accuracy': accuracy, 'ROC AUC': a.result().numpy()})
dataframe = pd.concat([dataframe, middat.to_frame().T], ignore_index=True)
return dataframe
else:
model = applications.EfficientNetV2B0(input_shape=(IM_SIZE[0], IM_SIZE[1], 3),
weights=None,
classes=2,
classifier_activation="softmax",
)
model.load_weights('EfficientNetV2_Aug.hdf5')
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0025),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'],
)
[loss, accuracy] = model.evaluate(data)
auc = model.predict(data)
a = tf.keras.metrics.AUC(num_thresholds=3)
heu = np.concatenate([y for x, y in data], axis=0)
a.update_state(heu, tf.math.argmax(auc, -1))
middat = pd.Series({'Method': modelname, 'Loss': loss, 'Accuracy': accuracy, 'ROC AUC': a.result().numpy()})
dataframe = pd.concat([dataframe, middat.to_frame().T], ignore_index=True)
return dataframe
with gr.Blocks() as interface:
header = gr.Markdown('# Data Augmentation with CycleGAN Images')
image_input = gr.Image(shape=(256, 256), label="Cityscape Image")
model_chooser = gr.Dropdown(choices=['InceptionV3', 'MobileNetV2', 'EfficientNetV2'], value='InceptionV3',
label='Model Chooser')
text_output = gr.Textbox(label='Predicted day/night')
augmenter_btn = gr.Checkbox(label="Use augmented models?")
submit_btn = gr.Button("Predict")
submit_btn.click(fn=gradio_predictor, inputs=[augmenter_btn, image_input, model_chooser], outputs=text_output)
test_btn = gr.Button("Run test on dataset")
text_test = gr.Dataframe(label="Test results")
test_btn.click(fn=gradio_tester, inputs=[augmenter_btn, model_chooser], outputs=text_test)
credits = gr.Markdown('Find the dataset used in kaggle here: https://www.kaggle.com/datasets/heonh0/daynight-cityview . Augmented models (tick the box) have been trained with an additional 750 CycleGAN-generated images. ')
interface.launch()
|