Rual113GJ commited on
Commit
d86b7ae
1 Parent(s): 4c75e11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -19
app.py CHANGED
@@ -6,23 +6,20 @@ import numpy as np
6
  from tensorflow.keras.preprocessing.image import img_to_array, load_img
7
 
8
  # Function to load the model with custom objects
9
- def load_model_with_hub():
10
- # Load the KerasLayer from TensorFlow Hub
11
- keras_layer = hub.KerasLayer("https://tfhub.dev/google/imagenet/resnet_v2_50/classification/5", trainable=False)
12
-
13
- # Define the input layer
14
- input_layer = tf.keras.layers.Input(shape=(224, 224, 3))
15
-
16
- # Pass the input through the KerasLayer
17
- x = keras_layer(input_layer)
18
-
19
- # Build the model
20
- model = tf.keras.models.Model(inputs=input_layer, outputs=x)
21
-
22
  return model
23
 
24
- # Loading model with custom objects
25
- model = load_model_with_hub(model_cat_dog.h5)
26
 
27
  def predict(input_image):
28
  try:
@@ -63,10 +60,7 @@ iface = gr.Interface(
63
  fn=predict,
64
  inputs=gr.inputs.Image(shape=(224, 224)),
65
  outputs="text",
66
- title = '🐱 x 🐶 Image Recognition - Cats vs Dogs with Resnet 101 V2 🐱 x 🐶',
67
- description="""<br> This model was trained to predict whether an image contains a cat or a dog. <br>
68
- <br> You can see how this model was trained on the following <a href = "https://www.kaggle.com/lusfernandotorres/computer-vision-cats-vs-dogs-w-resnet-v2-101">Kaggle Notebook</a>.
69
- <br>Upload a photo to see the how the model predicts!""",
70
  examples = examples
71
  )
72
 
 
6
  from tensorflow.keras.preprocessing.image import img_to_array, load_img
7
 
8
  # Function to load the model with custom objects
9
+ def load_model_with_hub(model_path):
10
+ # Load the model architecture without weights
11
+ model = tf.keras.models.load_model(model_path, compile=False)
12
+
13
+ # Define the KerasLayer from TensorFlow Hub
14
+ keras_layer = hub.KerasLayer("https://tfhub.dev/google/imagenet/resnet_v2_101/feature_vector/5", trainable=False)
15
+
16
+ # Add the KerasLayer to the model
17
+ model.add(keras_layer)
18
+
 
 
 
19
  return model
20
 
21
+ # Loading saved model with custom objects
22
+ model = load_model_with_hub('model_cat_dog.h5')
23
 
24
  def predict(input_image):
25
  try:
 
60
  fn=predict,
61
  inputs=gr.inputs.Image(shape=(224, 224)),
62
  outputs="text",
63
+ title = 'Image Recognition - Cats vs Dogs',
 
 
 
64
  examples = examples
65
  )
66