Bijan k commited on
Commit
7317b71
·
1 Parent(s): e0df8aa

Testing a different configuration

Browse files
Files changed (2) hide show
  1. app.py +39 -41
  2. requirements.txt +2 -3
app.py CHANGED
@@ -1,74 +1,72 @@
1
  import os
2
  import gradio as gr
3
  import tensorflow as tf
4
- from tensorflow.keras.applications import EfficientNetB0
5
- from tensorflow.keras.models import Model
6
- from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Input
7
-
8
- # Create the model using TensorFlow's native EfficientNet implementation
9
- input_tensor = Input(shape=(32, 32, 1))
10
- # Use a preprocessing layer to handle grayscale to RGB conversion
11
- x = tf.keras.layers.Rescaling(1.0 / 255)(input_tensor)
12
- # Repeat the grayscale channel to create a 3-channel input compatible with EfficientNet
13
- x = tf.keras.layers.Concatenate(axis=-1)([x, x, x])
14
-
15
-
16
- base_model = EfficientNetB0(include_top=False, weights=None, input_tensor=x)
17
- x = GlobalAveragePooling2D()(base_model.output)
18
- x = Dense(10, activation="softmax")(x)
19
- model = Model(inputs=input_tensor, outputs=x)
20
 
21
  model.compile(
22
- optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
23
- loss=tf.keras.losses.SparseCategoricalCrossentropy(),
24
  metrics=["accuracy"],
25
  )
26
 
27
- # Load weights if you have them
28
- try:
29
- model.load_weights("my_model.h5")
30
- except:
31
- print("Could not load weights. Using untrained model.")
32
 
33
 
34
  def classify_image(image):
35
  # Preprocess the image
36
  image_gray = tf.image.rgb_to_grayscale(image)
37
- image_tensor = tf.image.resize(image_gray, (32, 32))
38
 
39
- # Add a batch dimension
 
 
 
 
 
 
40
  image_tensor = tf.expand_dims(image_tensor, 0)
41
 
42
- # Get the prediction
 
 
 
43
  prediction = model.predict(image_tensor)
44
 
45
- # Convert the prediction to a dictionary of label-confidence pairs
46
- top_indices = tf.argsort(prediction[0])[-3:][::-1] # Top 3 indices
47
- confidences = {str(idx): float(prediction[0][idx]) for idx in top_indices}
48
 
49
- return confidences
50
 
51
 
52
- title = "MNIST Model 98% Accuracy"
53
- description = "Model trained on MNIST dataset using EfficientNet to classify handwritten digits with 98% accuracy"
54
- article = "For source code, visit [my GitHub](https://github.com/Bijan-K/Tensorflow-MNIST-98Acc.git)"
55
 
56
- example_list = []
57
- if os.path.exists("examples"):
58
- example_list = [
59
- ["examples/" + example]
60
- for example in os.listdir("examples")
61
- if os.path.isfile(os.path.join("examples", example))
62
- ]
63
 
64
  interface = gr.Interface(
65
  fn=classify_image,
66
  inputs=gr.Image(type="pil"),
67
- outputs=gr.Label(num_top_classes=3),
68
  examples=example_list,
69
  title=title,
70
  description=description,
71
  article=article,
72
  )
73
 
 
74
  interface.launch()
 
1
  import os
2
  import gradio as gr
3
  import tensorflow as tf
4
+ from tensorflow import keras
5
+ from tensorflow.keras.models import load_model
6
+ from tensorflow.keras import layers
7
+ from efficientnet.tfkeras import EfficientNetB0
8
+
9
+ model = keras.Sequential(
10
+ [
11
+ layers.Input(shape=(32, 32, 1)),
12
+ layers.experimental.preprocessing.Rescaling(1.0 / 255),
13
+ EfficientNetB0(include_top=False, weights=None, input_shape=(32, 32, 1)),
14
+ layers.GlobalAveragePooling2D(),
15
+ layers.Dropout(0.2),
16
+ layers.Dense(10),
17
+ ]
18
+ )
 
19
 
20
  model.compile(
21
+ optimizer=keras.optimizers.Adam(learning_rate=1e-3),
22
+ loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
23
  metrics=["accuracy"],
24
  )
25
 
26
+ model = load_model("my_model.h5")
 
 
 
 
27
 
28
 
29
  def classify_image(image):
30
  # Preprocess the image
31
  image_gray = tf.image.rgb_to_grayscale(image)
32
+ image_tensor = tf.convert_to_tensor(image_gray)
33
 
34
+ # Resize the image to 28x28.
35
+ image_tensor = tf.image.resize(image_tensor, (32, 32))
36
+
37
+ # Cast the data to float32.
38
+ image_tensor = tf.cast(image_tensor, tf.float32)
39
+
40
+ # Add a batch dimension.
41
  image_tensor = tf.expand_dims(image_tensor, 0)
42
 
43
+ # Normalize the data.
44
+ image_tensor = image_tensor / 255.0
45
+
46
+ # Get the prediction.
47
  prediction = model.predict(image_tensor)
48
 
49
+ # Convert the prediction to a string label.
50
+ prediction_label = str(prediction.argmax())
 
51
 
52
+ return prediction_label
53
 
54
 
55
+ title = "MNIST Model 98%acc"
56
+ description = "Model trained on MNIST dataset using efficientnet to classify MNIST images with 98% accuracy"
57
+ article = "for source code you can visit [my github](https://github.com/Bijan-K/Tensorflow-MNIST-98Acc.git) (gradio + training code)."
58
 
59
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
 
 
 
 
 
 
60
 
61
  interface = gr.Interface(
62
  fn=classify_image,
63
  inputs=gr.Image(type="pil"),
64
+ outputs=gr.Label(num_top_classes=3, label="Predictions"),
65
  examples=example_list,
66
  title=title,
67
  description=description,
68
  article=article,
69
  )
70
 
71
+
72
  interface.launch()
requirements.txt CHANGED
@@ -1,4 +1,3 @@
1
- tensorflow==2.9.1
2
- keras==2.9.0
3
  efficientnet==1.1.1
4
- gradio==3.32.0
 
1
+ tensorflow==2.6.0
 
2
  efficientnet==1.1.1
3
+ gradio