Spaces:
Runtime error
Runtime error
Create app.py
Browse filesCommit to main
app.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import matplotlib.pyplot as plt
|
| 2 |
+
import numpy as np
|
| 3 |
+
import os
|
| 4 |
+
import PIL
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
|
| 7 |
+
from tensorflow import keras
|
| 8 |
+
from tensorflow.keras import layers
|
| 9 |
+
from tensorflow.keras.models import Sequential
|
| 10 |
+
|
| 11 |
+
import pathlib
|
| 12 |
+
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
|
| 13 |
+
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
|
| 14 |
+
data_dir = pathlib.Path(data_dir)
|
| 15 |
+
|
| 16 |
+
image_count = len(list(data_dir.glob('*/*.jpg')))
|
| 17 |
+
print(image_count)
|
| 18 |
+
|
| 19 |
+
print(os.listdir(data_dir))
|
| 20 |
+
|
| 21 |
+
roses = list(data_dir.glob('roses/*'))
|
| 22 |
+
PIL.Image.open(str(roses[1]))
|
| 23 |
+
|
| 24 |
+
daisy = list(data_dir.glob('daisy/*'))
|
| 25 |
+
PIL.Image.open(str(daisy[2]))
|
| 26 |
+
|
| 27 |
+
batch_size = 32
|
| 28 |
+
img_height = 180
|
| 29 |
+
img_width = 180
|
| 30 |
+
|
| 31 |
+
train_ds = tf.keras.utils.image_dataset_from_directory(
|
| 32 |
+
data_dir,
|
| 33 |
+
validation_split=0.2,
|
| 34 |
+
subset="training",
|
| 35 |
+
seed=123,
|
| 36 |
+
image_size=(img_height, img_width),
|
| 37 |
+
batch_size=batch_size)
|
| 38 |
+
|
| 39 |
+
val_ds = tf.keras.utils.image_dataset_from_directory(
|
| 40 |
+
data_dir,
|
| 41 |
+
validation_split=0.2,
|
| 42 |
+
subset="validation",
|
| 43 |
+
seed=123,
|
| 44 |
+
image_size=(img_height, img_width),
|
| 45 |
+
batch_size=batch_size)
|
| 46 |
+
|
| 47 |
+
class_names = train_ds.class_names
|
| 48 |
+
print(class_names)
|
| 49 |
+
|
| 50 |
+
import matplotlib.pyplot as plt
|
| 51 |
+
|
| 52 |
+
plt.figure(figsize=(12, 12))
|
| 53 |
+
for images, labels in train_ds.take(1):
|
| 54 |
+
for i in range(12):
|
| 55 |
+
ax = plt.subplot(3, 4, i + 1)
|
| 56 |
+
plt.imshow(images[i].numpy().astype("uint8"))
|
| 57 |
+
plt.title(class_names[labels[i]])
|
| 58 |
+
plt.axis("off")
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
num_classes = len(class_names)
|
| 63 |
+
|
| 64 |
+
model = Sequential([
|
| 65 |
+
layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
|
| 66 |
+
layers.Conv2D(16, 3, padding='same', activation='relu'),
|
| 67 |
+
layers.MaxPooling2D(),
|
| 68 |
+
layers.Conv2D(32, 3, padding='same', activation='relu'),
|
| 69 |
+
layers.MaxPooling2D(),
|
| 70 |
+
layers.Conv2D(64, 3, padding='same', activation='relu'),
|
| 71 |
+
layers.MaxPooling2D(),
|
| 72 |
+
layers.Flatten(),
|
| 73 |
+
layers.Dense(128, activation='relu'),
|
| 74 |
+
layers.Dense(num_classes,activation='softmax')
|
| 75 |
+
])
|
| 76 |
+
|
| 77 |
+
model.compile(optimizer='adam',
|
| 78 |
+
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
| 79 |
+
metrics=['accuracy'])
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
model.summary()
|
| 83 |
+
|
| 84 |
+
epochs=15
|
| 85 |
+
history = model.fit(
|
| 86 |
+
train_ds,
|
| 87 |
+
validation_data=val_ds,
|
| 88 |
+
epochs=epochs
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
acc = history.history['accuracy']
|
| 92 |
+
val_acc = history.history['val_accuracy']
|
| 93 |
+
|
| 94 |
+
loss = history.history['loss']
|
| 95 |
+
val_loss = history.history['val_loss']
|
| 96 |
+
|
| 97 |
+
epochs_range = range(epochs)
|
| 98 |
+
|
| 99 |
+
plt.figure(figsize=(12, 8))
|
| 100 |
+
plt.subplot(1, 2, 1)
|
| 101 |
+
plt.plot(epochs_range, acc, label='Training Accuracy')
|
| 102 |
+
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
|
| 103 |
+
plt.legend(loc='lower right')
|
| 104 |
+
plt.title('Training and Validation Accuracy')
|
| 105 |
+
|
| 106 |
+
plt.subplot(1, 2, 2)
|
| 107 |
+
plt.plot(epochs_range, loss, label='Training Loss')
|
| 108 |
+
plt.plot(epochs_range, val_loss, label='Validation Loss')
|
| 109 |
+
plt.legend(loc='upper right')
|
| 110 |
+
plt.title('Training and Validation Loss')
|
| 111 |
+
plt.show()
|
| 112 |
+
|
| 113 |
+
def resize_image(input_image):
|
| 114 |
+
img = PIL.Image.fromarray(input_image)
|
| 115 |
+
resized_img = img.resize((180, 180))
|
| 116 |
+
resized_array = np.array(resized_img)
|
| 117 |
+
return resized_array
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def predict_input_image(img):
|
| 121 |
+
img=resize_image(img)
|
| 122 |
+
img_4d=img.reshape(-1,180,180,3)
|
| 123 |
+
prediction=model.predict(img_4d)[0]
|
| 124 |
+
return {class_names[i]: float(prediction[i]) for i in range(5)}
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
import gradio as gr
|
| 128 |
+
gr.Interface(fn=predict_input_image,
|
| 129 |
+
inputs=gr.Image(),
|
| 130 |
+
outputs=gr.Label(num_top_classes=5),
|
| 131 |
+
live=False).launch(debug='True')
|
| 132 |
+
|