NORLIE JHON MALAGDAO commited on
Commit
b4a3a35
·
verified ·
1 Parent(s): 5d260c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import gradio as gr
2
  import matplotlib.pyplot as plt
3
  import numpy as np
@@ -93,24 +95,23 @@ data_augmentation = keras.Sequential([
93
  layers.RandomFlip("horizontal", input_shape=(img_height, img_width, 3)),
94
  layers.RandomRotation(0.1),
95
  layers.RandomZoom(0.1),
96
- layers.RandomContrast(0.1),
97
  ])
98
 
99
- # Load a pretrained model and fine-tune it
100
- base_model = tf.keras.applications.MobileNetV2(input_shape=(img_height, img_width, 3),
101
- include_top=False,
102
- weights='imagenet')
103
- base_model.trainable = False # Freeze the base model
104
 
105
- # Add custom layers on top of the pretrained model
106
  model = Sequential([
107
  data_augmentation,
108
  layers.Rescaling(1./255),
109
- base_model,
110
- layers.GlobalAveragePooling2D(),
 
 
 
 
111
  layers.Dropout(0.2),
 
112
  layers.Dense(128, activation='relu'),
113
- layers.Dense(len(class_names), name="outputs")
114
  ])
115
 
116
  model.compile(optimizer='adam',
@@ -126,19 +127,14 @@ history = model.fit(
126
 
127
  def predict_image(img):
128
  img = np.array(img)
129
- img_resized = tf.image.resize(img, (img_height, img_width))
130
  img_4d = tf.expand_dims(img_resized, axis=0)
131
  prediction = model.predict(img_4d)[0]
132
- max_index = np.argmax(prediction)
133
- max_probability = tf.nn.softmax(prediction)[max_index]
134
-
135
- if max_probability == 1.0:
136
- return {class_names[max_index]: float(max_probability)}
137
- else:
138
- return {"None": "No class with 100% confidence"}
139
 
140
  image = gr.Image()
141
- label = gr.Label(num_top_classes=1)
142
 
143
  gr.Interface(
144
  fn=predict_image,
 
1
+
2
+
3
  import gradio as gr
4
  import matplotlib.pyplot as plt
5
  import numpy as np
 
95
  layers.RandomFlip("horizontal", input_shape=(img_height, img_width, 3)),
96
  layers.RandomRotation(0.1),
97
  layers.RandomZoom(0.1),
 
98
  ])
99
 
100
+ num_classes = 12
 
 
 
 
101
 
 
102
  model = Sequential([
103
  data_augmentation,
104
  layers.Rescaling(1./255),
105
+ layers.Conv2D(16, 3, padding='same', activation='relu'),
106
+ layers.MaxPooling2D(),
107
+ layers.Conv2D(32, 3, padding='same', activation='relu'),
108
+ layers.MaxPooling2D(),
109
+ layers.Conv2D(64, 3, padding='same', activation='relu'),
110
+ layers.MaxPooling2D(),
111
  layers.Dropout(0.2),
112
+ layers.Flatten(),
113
  layers.Dense(128, activation='relu'),
114
+ layers.Dense(num_classes, name="outputs")
115
  ])
116
 
117
  model.compile(optimizer='adam',
 
127
 
128
  def predict_image(img):
129
  img = np.array(img)
130
+ img_resized = tf.image.resize(img, (180, 180))
131
  img_4d = tf.expand_dims(img_resized, axis=0)
132
  prediction = model.predict(img_4d)[0]
133
+ probabilities = tf.nn.softmax(prediction).numpy()
134
+ return {class_names[i]: float(probabilities[i]) for i in range(len(class_names))}
 
 
 
 
 
135
 
136
  image = gr.Image()
137
+ label = gr.Label(num_top_classes=12)
138
 
139
  gr.Interface(
140
  fn=predict_image,