hb-setosys commited on
Commit
f1b1bc2
·
verified ·
1 Parent(s): 08f7af1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -1,36 +1,32 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
- from tensorflow.keras.applications.resnet import ResNet152, preprocess_input, decode_predictions
 
4
  from tensorflow.keras.preprocessing.image import img_to_array
5
  from PIL import Image
6
  import numpy as np
7
 
8
- # Load the pre-trained ResNet152 model
9
- MODEL_PATH = "resnet152-image-classifier.h5" # Path to the saved model
10
- try:
11
- model = tf.keras.models.load_model(MODEL_PATH)
12
- except Exception as e:
13
- print(f"Error loading model: {e}")
14
- exit()
15
 
16
  def predict_image(image):
17
  """
18
- Process the uploaded image and return the top 3 predictions.
19
  """
20
  try:
21
  # Preprocess the image
22
- image = image.resize((224, 224)) # ResNet152 expects 224x224 input
23
  image_array = img_to_array(image)
24
  image_array = preprocess_input(image_array) # Normalize the image
25
  image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
26
 
27
  # Get predictions
28
  predictions = model.predict(image_array)
29
- decoded_predictions = decode_predictions(predictions, top=3)[0]
30
 
31
- # Format predictions as a list of tuples (label, confidence)
32
- results = [(label, float(confidence)) for _, label, confidence in decoded_predictions]
33
- return dict(results)
34
 
35
  except Exception as e:
36
  return {"Error": str(e)}
@@ -39,12 +35,12 @@ def predict_image(image):
39
  interface = gr.Interface(
40
  fn=predict_image,
41
  inputs=gr.Image(type="pil"), # Accepts an image input
42
- outputs=gr.Label(num_top_classes=3), # Shows top 3 predictions with confidence
43
- title="ResNet152 Image Classifier",
44
- description="Upload an image, and the model will predict what's in the image.",
45
- examples=["dog.jpg", "cat.jpg"], # Example images for users to test
46
  )
47
 
48
  # Launch the Gradio app
49
  if __name__ == "__main__":
50
- interface.launch()
 
1
  import gradio as gr
2
  import tensorflow as tf
3
+ from tensorflow.keras.applications import EfficientNetV2L
4
+ from tensorflow.keras.applications.efficientnet_v2 import preprocess_input, decode_predictions
5
  from tensorflow.keras.preprocessing.image import img_to_array
6
  from PIL import Image
7
  import numpy as np
8
 
9
+ # Load a stronger pretrained model (EfficientNetV2L)
10
+ model = EfficientNetV2L(weights="imagenet")
 
 
 
 
 
11
 
12
  def predict_image(image):
13
  """
14
+ Process the uploaded image and return the top 5 predictions.
15
  """
16
  try:
17
  # Preprocess the image
18
+ image = image.resize((480, 480)) # EfficientNetV2L expects 480x480 input
19
  image_array = img_to_array(image)
20
  image_array = preprocess_input(image_array) # Normalize the image
21
  image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
22
 
23
  # Get predictions
24
  predictions = model.predict(image_array)
25
+ decoded_predictions = decode_predictions(predictions, top=5)[0]
26
 
27
+ # Format predictions as a dictionary (label -> confidence)
28
+ results = {label: float(confidence) for _, label, confidence in decoded_predictions}
29
+ return results
30
 
31
  except Exception as e:
32
  return {"Error": str(e)}
 
35
  interface = gr.Interface(
36
  fn=predict_image,
37
  inputs=gr.Image(type="pil"), # Accepts an image input
38
+ outputs=gr.Label(num_top_classes=2), # Shows top 5 predictions with confidence
39
+ title="Image Classifier",
40
+ description="Upload an image, and the model will predict what's in the image with higher accuracy.",
41
+ examples=["dog.jpg", "cat.jpg", "building.jpg", "tree.jpg"], # Example images for testing
42
  )
43
 
44
  # Launch the Gradio app
45
  if __name__ == "__main__":
46
+ interface.launch()