Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
import PIL
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
|
| 8 |
+
from tensorflow import keras
|
| 9 |
+
from tensorflow.keras import layers
|
| 10 |
+
from tensorflow.keras.models import Sequential
|
| 11 |
+
|
| 12 |
+
import pathlib
|
| 13 |
+
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
|
| 14 |
+
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
|
| 15 |
+
data_dir = pathlib.Path(data_dir)
|
| 16 |
+
|
| 17 |
+
roses = list(data_dir.glob('roses/*'))
|
| 18 |
+
print(roses[0])
|
| 19 |
+
PIL.Image.open(str(roses[0]))
|
| 20 |
+
|
| 21 |
+
img_height,img_width=180,180
|
| 22 |
+
batch_size=32
|
| 23 |
+
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
| 24 |
+
data_dir,
|
| 25 |
+
validation_split=0.2,
|
| 26 |
+
subset="training",
|
| 27 |
+
seed=123,
|
| 28 |
+
image_size=(img_height, img_width),
|
| 29 |
+
batch_size=batch_size)
|
| 30 |
+
|
| 31 |
+
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
| 32 |
+
data_dir,
|
| 33 |
+
validation_split=0.2,
|
| 34 |
+
subset="validation",
|
| 35 |
+
seed=123,
|
| 36 |
+
image_size=(img_height, img_width),
|
| 37 |
+
batch_size=batch_size)
|
| 38 |
+
|
| 39 |
+
class_names = train_ds.class_names
|
| 40 |
+
print(class_names)
|
| 41 |
+
|
| 42 |
+
import matplotlib.pyplot as plt
|
| 43 |
+
|
| 44 |
+
plt.figure(figsize=(10, 10))
|
| 45 |
+
for images, labels in train_ds.take(1):
|
| 46 |
+
for i in range(9):
|
| 47 |
+
ax = plt.subplot(3, 3, i + 1)
|
| 48 |
+
plt.imshow(images[i].numpy().astype("uint8"))
|
| 49 |
+
plt.title(class_names[labels[i]])
|
| 50 |
+
plt.axis("off")
|
| 51 |
+
|
| 52 |
+
num_classes = 5
|
| 53 |
+
|
| 54 |
+
model = Sequential([
|
| 55 |
+
layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
|
| 56 |
+
layers.Conv2D(16, 3, padding='same', activation='relu'),
|
| 57 |
+
layers.MaxPooling2D(),
|
| 58 |
+
layers.Conv2D(32, 3, padding='same', activation='relu'),
|
| 59 |
+
layers.MaxPooling2D(),
|
| 60 |
+
layers.Conv2D(64, 3, padding='same', activation='relu'),
|
| 61 |
+
layers.MaxPooling2D(),
|
| 62 |
+
layers.Flatten(),
|
| 63 |
+
layers.Dense(128, activation='relu'),
|
| 64 |
+
layers.Dense(num_classes,activation='softmax')
|
| 65 |
+
])
|
| 66 |
+
|
| 67 |
+
model.compile(optimizer='adam',
|
| 68 |
+
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
| 69 |
+
metrics=['accuracy'])
|
| 70 |
+
|
| 71 |
+
epochs=1
|
| 72 |
+
history = model.fit(
|
| 73 |
+
train_ds,
|
| 74 |
+
validation_data=val_ds,
|
| 75 |
+
epochs=epochs,
|
| 76 |
+
verbose=1
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
def predict_image(img):
|
| 80 |
+
img_4d=img.reshape(-1,180,180,3)
|
| 81 |
+
prediction=model.predict(img_4d)[0]
|
| 82 |
+
return {class_names[i]: float(prediction[i]) for i in range(5)}
|
| 83 |
+
|
| 84 |
+
image = gr.Image(height=180,width=180)
|
| 85 |
+
#label = gr.outputs.Label(num_top_classes=5)
|
| 86 |
+
label = gr.Label(num_top_classes=5)
|
| 87 |
+
#gr.Interface(fn=predict_image, inputs=image, outputs=label,interpretation='default').launch(debug='True')
|
| 88 |
+
|
| 89 |
+
gr.Interface(predict_image, inputs=image, outputs=label )
|
| 90 |
+
|
| 91 |
+
iface.launch(debug=True)
|