Update app.py
Browse files
app.py
CHANGED
|
@@ -3,22 +3,83 @@ import tensorflow as tf
|
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
inception_net = tf.keras.applications.MobileNetV2() # load the model
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
labels = response.text.split("\n")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
title = "Gradio Image Classifiction + Interpretation Example"
|
| 24 |
gr.Interface(
|
|
|
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
|
|
|
|
| 6 |
|
| 7 |
+
def classify_image(inp):
|
| 8 |
+
# Dataset link: https://www.kaggle.com/pranavraikokte/covid19-image-dataset
|
|
|
|
| 9 |
|
| 10 |
+
import tensorflow as tf
|
| 11 |
+
from tensorflow import keras
|
| 12 |
+
from tensorflow.keras.models import Sequential
|
| 13 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
| 14 |
|
| 15 |
+
import matplotlib.pyplot as plt
|
| 16 |
+
#import app
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
batch = 4
|
| 20 |
+
|
| 21 |
+
generator = ImageDataGenerator(
|
| 22 |
+
rotation_range=40,
|
| 23 |
+
width_shift_range=0.2,
|
| 24 |
+
height_shift_range=0.2,
|
| 25 |
+
rescale=1./255,
|
| 26 |
+
shear_range=0.2,
|
| 27 |
+
zoom_range=0.2,
|
| 28 |
+
horizontal_flip=True,
|
| 29 |
+
fill_mode='nearest')
|
| 30 |
+
|
| 31 |
+
train_iterator = generator.flow_from_directory("C:/Users/Lyall Stewart/Documents/Coding/NeuralNetwork/COVID-19 Classification/data/train",
|
| 32 |
+
batch_size=batch,
|
| 33 |
+
color_mode='grayscale',
|
| 34 |
+
class_mode='sparse')
|
| 35 |
+
test_iterator = generator.flow_from_directory("C:/Users/Lyall Stewart/Documents/Coding/NeuralNetwork/COVID-19 Classification/data/test",
|
| 36 |
+
batch_size=batch,
|
| 37 |
+
color_mode='grayscale',
|
| 38 |
+
class_mode='sparse')
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def design_model():
|
| 42 |
+
model = Sequential()
|
| 43 |
+
model.add(tf.keras.Input(shape=(256, 256, 1)))
|
| 44 |
+
|
| 45 |
+
model.add(tf.keras.layers.Conv2D(2, 5, strides=3, activation="relu"))
|
| 46 |
+
model.add(tf.keras.layers.MaxPooling2D(pool_size=(5, 5), strides=(5,5)))
|
| 47 |
+
model.add(tf.keras.layers.Conv2D(4, 3, strides=1, activation="relu"))
|
| 48 |
+
model.add(tf.keras.layers.MaxPooling2D(pool_size=(3,2), strides=(2,2)))
|
| 49 |
+
|
| 50 |
+
model.add(tf.keras.layers.Flatten())
|
| 51 |
+
#model.add(tf.keras.layers.Dense(8, activation="relu"))
|
| 52 |
+
#model.add(tf.keras.layers.Dropout(.20))
|
| 53 |
+
model.add(tf.keras.layers.Dense(4, activation='softmax'))
|
| 54 |
+
|
| 55 |
+
model.summary()
|
| 56 |
+
|
| 57 |
+
callback = tf.keras.callbacks.EarlyStopping(monitor='accuracy', patience=5, restore_best_weights=True, verbose=1)
|
| 58 |
+
|
| 59 |
+
print("Model designed")
|
| 60 |
+
return model, callback
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
model, es_callback = design_model()
|
| 64 |
+
|
| 65 |
+
model.compile(loss='sparse_categorical_crossentropy',
|
| 66 |
+
optimizer=keras.optimizers.Adam(learning_rate=0.01),
|
| 67 |
+
metrics=['accuracy'])
|
| 68 |
|
| 69 |
+
history = model.fit_generator(train_iterator,
|
| 70 |
+
epochs=50,
|
| 71 |
+
steps_per_epoch=50,
|
| 72 |
+
validation_data=test_iterator,
|
| 73 |
+
validation_steps=50,
|
| 74 |
+
callbacks=[es_callback],
|
| 75 |
+
verbose=1)
|
| 76 |
|
| 77 |
+
plt.plot(history.history['accuracy'])
|
| 78 |
+
plt.title('model accuracy')
|
| 79 |
+
plt.ylabel('accuracy')
|
| 80 |
+
plt.xlabel('epoch')
|
| 81 |
+
plt.legend(['train'], loc='upper left')
|
| 82 |
+
plt.show()
|
| 83 |
|
| 84 |
title = "Gradio Image Classifiction + Interpretation Example"
|
| 85 |
gr.Interface(
|