pallabi commited on
Commit
8b7dc8a
Β·
verified Β·
1 Parent(s): 85e0f33

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ !pip install keras
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
+ image_count = len(list(data_dir.glob('*/*.jpg')))
18
+ print(image_count)
19
+
20
+ print(os.listdir(data_dir))
21
+
22
+ roses = list(data_dir.glob('roses/*'))
23
+ PIL.Image.open(str(roses[1]))
24
+
25
+ daisy = list(data_dir.glob('daisy/*'))
26
+ PIL.Image.open(str(daisy[2]))
27
+
28
+ batch_size = 32
29
+ img_height = 180
30
+ img_width = 180
31
+
32
+ train_ds = tf.keras.utils.image_dataset_from_directory(
33
+ data_dir,
34
+ validation_split=0.2,
35
+ subset="training",
36
+ seed=123,
37
+ image_size=(img_height, img_width),
38
+ batch_size=batch_size)
39
+
40
+ val_ds = tf.keras.utils.image_dataset_from_directory(
41
+ data_dir,
42
+ validation_split=0.2,
43
+ subset="validation",
44
+ seed=123,
45
+ image_size=(img_height, img_width),
46
+ batch_size=batch_size)
47
+
48
+ class_names = train_ds.class_names
49
+ print(class_names)
50
+
51
+ import matplotlib.pyplot as plt
52
+
53
+ plt.figure(figsize=(12, 12))
54
+ for images, labels in train_ds.take(1):
55
+ for i in range(12):
56
+ ax = plt.subplot(3, 4, i + 1)
57
+ plt.imshow(images[i].numpy().astype("uint8"))
58
+ plt.title(class_names[labels[i]])
59
+ plt.axis("off")
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
+ model.summary()
82
+
83
+ epochs=15
84
+ history = model.fit(
85
+ train_ds,
86
+ validation_data=val_ds,
87
+ epochs=epochs
88
+ )
89
+
90
+ acc = history.history['accuracy']
91
+ val_acc = history.history['val_accuracy']
92
+
93
+ loss = history.history['loss']
94
+ val_loss = history.history['val_loss']
95
+
96
+ epochs_range = range(epochs)
97
+
98
+ plt.figure(figsize=(12, 8))
99
+ plt.subplot(1, 2, 1)
100
+ plt.plot(epochs_range, acc, label='Training Accuracy')
101
+ plt.plot(epochs_range, val_acc, label='Validation Accuracy')
102
+ plt.legend(loc='lower right')
103
+ plt.title('Training and Validation Accuracy')
104
+
105
+ plt.subplot(1, 2, 2)
106
+ plt.plot(epochs_range, loss, label='Training Loss')
107
+ plt.plot(epochs_range, val_loss, label='Validation Loss')
108
+ plt.legend(loc='upper right')
109
+ plt.title('Training and Validation Loss')
110
+ plt.show()
111
+
112
+ def resize_image(input_image):
113
+ img = PIL.Image.fromarray(input_image)
114
+ resized_img = img.resize((180, 180))
115
+ resized_array = np.array(resized_img)
116
+ return resized_array
117
+
118
+ def predict_input_image(img):
119
+ img=resize_image(img)
120
+ img_4d=img.reshape(-1,180,180,3)
121
+ prediction=model.predict(img_4d)[0]
122
+ return {class_names[i]: float(prediction[i]) for i in range(5)}
123
+
124
+ !pip install gradio
125
+
126
+ import gradio as gr
127
+
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')