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