fightglory's picture
Update app.py
66d2dd4
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()