shady4real commited on
Commit
8596595
1 Parent(s): c61020c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """shadman_Image_classification.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1DCVsfqR-kOz3CHdB99IT75IHBzUtIf3M
8
+ """
9
+
10
+ import tensorflow as tf
11
+ import PIL
12
+ import matplotlib.pyplot as plt
13
+ from tensorflow.keras import layers
14
+ import os
15
+
16
+ import pathlib
17
+
18
+ flower_dataset = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
19
+ dataset_path = tf.keras.utils.get_file('flower_photos',origin=flower_dataset,untar=True)
20
+ dataset_path = pathlib.Path(dataset_path)
21
+
22
+ roses = list(dataset_path.glob('roses/*'))
23
+ daisy = list(dataset_path.glob('daisy/*'))
24
+ print(roses[1])
25
+
26
+ PIL.Image.open(roses[10])
27
+ PIL.Image.open(daisy[10])
28
+
29
+ # training the dataset
30
+
31
+ training_images = tf.keras.preprocessing.image_dataset_from_directory(
32
+ dataset_path,
33
+ subset = "training",
34
+ validation_split = 0.25,
35
+ seed = 123,
36
+ image_size = (180, 180),
37
+ batch_size = 32
38
+
39
+ )
40
+
41
+ # validation of images
42
+
43
+
44
+ validation_images = tf.keras.preprocessing.image_dataset_from_directory(
45
+ dataset_path,
46
+ subset = "validation",
47
+ validation_split = 0.25,
48
+ seed = 123,
49
+ image_size = (180, 180),
50
+ batch_size = 32
51
+
52
+ )
53
+
54
+ flower_classes = training_images.class_names
55
+ print(flower_classes)
56
+
57
+ from tensorflow.python.framework.func_graph import flatten
58
+ # if there are 5 classes then
59
+
60
+ dataset_classes = 5
61
+
62
+ from tensorflow.keras.models import Sequential
63
+
64
+ model = Sequential([
65
+ #rescaling
66
+ layers.experimental.preprocessing.Rescaling(1./255, input_shape = (180,180,3)),
67
+ layers.Conv2D(16, 3 , padding='same' , activation='relu'),
68
+ layers.MaxPooling2D(),
69
+ layers.Conv2D(32, 3, padding ='same' , activation = 'relu'),
70
+ layers.MaxPooling2D(),
71
+ layers.Conv2D(64 , 3, padding='same', activation='relu'),
72
+ layers.MaxPooling2D(),
73
+ layers.Flatten(),
74
+ layers.Dense(128, activation='relu'),
75
+ layers.Dense(dataset_classes, activation='softmax')
76
+ ])
77
+
78
+ model.compile(
79
+ optimizer='adam',
80
+ loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
81
+ metrics=['accuracy'])
82
+
83
+ mymodel = model.fit(
84
+ training_images,
85
+ validation_data=validation_images,
86
+ epochs=10
87
+ )
88
+
89
+ def predict_input_image(img):
90
+ img_4d=img.reshape(-1,180,180,3)
91
+ prediction=model.predict(img_4d)[0]
92
+ return {flower_classes[i]: float(prediction[i]) for i in range(5)}
93
+
94
+ ! pip install gradio
95
+
96
+ import gradio as gr
97
+ def predict_input_image(img):
98
+ img_4d=img.reshape(-1,180,180,3)
99
+ prediction=model.predict(img_4d)[0]
100
+ return {flower_classes[i]: float(prediction[i]) for i in range(5)}
101
+
102
+ image = gr.inputs.Image(shape=(180,180))
103
+ label = gr.outputs.Label(num_top_classes=5)
104
+
105
+ gr.Interface(fn=predict_input_image, inputs=image, outputs=label,interpretation='default').launch(debug='True')