Update app.py
Browse files
app.py
CHANGED
@@ -1,91 +1,54 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
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 |
-
|
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 |
-
|
18 |
-
print(roses[0])
|
19 |
-
PIL.Image.open(str(roses[0]))
|
20 |
|
21 |
-
|
22 |
-
|
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 |
-
|
32 |
-
|
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 |
-
|
40 |
-
|
|
|
41 |
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
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 |
-
|
|
|
|
|
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 |
-
|
69 |
-
|
70 |
|
71 |
-
epochs=
|
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 |
-
|
81 |
-
|
82 |
-
|
|
|
83 |
|
84 |
-
|
85 |
-
|
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 |
-
iface = gr.Interface(predict_image, inputs=
|
90 |
|
91 |
-
iface.launch(
|
|
|
1 |
import gradio as gr
|
2 |
+
from gradio import Interface
|
|
|
|
|
|
|
3 |
import tensorflow as tf
|
|
|
4 |
from tensorflow import keras
|
5 |
+
from tensorflow.keras import datasets, layers, models
|
6 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
(X_train, y_train) , (X_test, y_test) = keras.datasets.mnist.load_data()
|
|
|
|
|
9 |
|
10 |
+
X_train = np.concatenate((X_train, X_test))
|
11 |
+
y_train = np.concatenate((y_train, y_test))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
X_train = X_train / 255
|
14 |
+
X_test = X_test / 255
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
data_augmentation = keras.Sequential([
|
17 |
+
tf.keras.layers.experimental.preprocessing.RandomRotation(0.2, input_shape=(28, 28, 1)),
|
18 |
+
])
|
19 |
|
20 |
+
model = models.Sequential([
|
21 |
+
data_augmentation,
|
22 |
+
|
23 |
+
#cnn
|
24 |
+
layers.Conv2D(filters=32, kernel_size=(3,3), padding='same', activation='relu'),
|
25 |
+
layers.MaxPooling2D((2,2)),
|
26 |
+
layers.Conv2D(filters=32, kernel_size=(3,3), padding='same', activation='relu'),
|
27 |
+
layers.MaxPooling2D((2,2)),
|
28 |
|
29 |
+
#dense
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
layers.Flatten(),
|
32 |
+
layers.Dense(32, activation='relu'),
|
33 |
+
layers.Dense(10, activation='softmax'),
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
])
|
36 |
|
37 |
model.compile(optimizer='adam',
|
38 |
+
loss='sparse_categorical_crossentropy',
|
39 |
+
metrics=['accuracy'])
|
40 |
|
41 |
+
model.fit(X_train, y_train, epochs=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
def predict_image(img):
|
44 |
+
img_3d = img.reshape(-1, 28,28)
|
45 |
+
img_scaled = img_3d/255
|
46 |
+
prediction = model.predict(img_scaled)
|
47 |
+
pred = np.argmax(prediction)
|
48 |
|
49 |
+
return pred.item()
|
50 |
+
|
|
|
|
|
51 |
|
52 |
+
iface = gr.Interface(predict_image, inputs='sketchpad', outputs='label', title='Digit Recognition Model By Debamrita Paul', description='Draw a single digit(0 to 9)', __gradio_theme='dark')
|
53 |
|
54 |
+
iface.launch()
|