mrdbourke commited on
Commit
e70cacd
ยท
verified ยท
1 Parent(s): 7f54c8c

add dog photos and app.py

Browse files
Files changed (5) hide show
  1. app.py +69 -0
  2. dog-photo-1.jpeg +0 -0
  3. dog-photo-2.jpeg +0 -0
  4. dog-photo-3.jpeg +0 -0
  5. dog-photo-4.jpeg +0 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+
4
+ # Load model
5
+ model_save_path = "dog_vision_model_demo.keras"
6
+ loaded_model_for_demo = tf.keras.models.load_model(model_save_path)
7
+
8
+ # Load labels
9
+ with open("stanford_dogs_class_names.txt", "r") as f:
10
+ class_names = [line.strip() for line in f.readlines()]
11
+
12
+ # Create prediction function
13
+ def pred_on_custom_image(image, # input image (preprocessed by Gradio's Image input to be numpy.array)
14
+ model: tf.keras.Model =loaded_model_for_demo, # Trained TensorFlow model for prediction
15
+ target_size: int = 224, # Desired size of the image for input to the model
16
+ class_names: list = class_names): # List of class names
17
+ """
18
+ Loads an image, preprocesses it, makes a prediction using a provided model,
19
+ and returns a dictionary of prediction probabilities per class name.
20
+
21
+ Args:
22
+ image: Input image.
23
+ model: Trained TensorFlow model for prediction.
24
+ target_size (int, optional): Desired size of the image for input to the model. Defaults to 224.
25
+ class_names (list, optional): List of class names for plotting. Defaults to None.
26
+
27
+ Returns:
28
+ Dict[str: float]: A dictionary of string class names and their respective prediction probability.
29
+ """
30
+
31
+ # Note: gradio.inputs.Image handles opening the image
32
+ # # Prepare and load image
33
+ # custom_image = tf.keras.utils.load_img(
34
+ # path=image_path,
35
+ # color_mode="rgb",
36
+ # target_size=target_size,
37
+ # )
38
+
39
+ # Create resizing layer to resize the image
40
+ resize = tf.keras.layers.Resizing(height=target_size,
41
+ width=target_size)
42
+
43
+ # Turn the image into a tensor and resize it
44
+ custom_image_tensor = resize(tf.keras.utils.img_to_array(image))
45
+
46
+ # Add a batch dimension to the target tensor (e.g. (224, 224, 3) -> (1, 224, 224, 3))
47
+ custom_image_tensor = tf.expand_dims(custom_image_tensor, axis=0)
48
+
49
+ # Make a prediction with the target model
50
+ pred_probs = model.predict(custom_image_tensor)[0]
51
+
52
+ # Predictions get returned as a dictionary of {label: pred_prob}
53
+ pred_probs_dict = {class_names[i]: float(pred_probs[i]) for i in range(len(class_names))}
54
+
55
+ return pred_probs_dict
56
+
57
+ # Create Gradio interface
58
+ interface_title = "Dog Vision ๐Ÿถ๐Ÿ‘๏ธ"
59
+ interface_description = "Identify different dogs in images with deep learning. Model trained with TensorFlow/Keras."
60
+ interface = gr.Interface(fn=pred_on_custom_image,
61
+ inputs=gr.Image(),
62
+ outputs=gr.Label(num_top_classes=3),
63
+ examples=["dog-photo-1.jpeg",
64
+ "dog-photo-2.jpeg",
65
+ "dog-photo-3.jpeg",
66
+ "dog-photo-4.jpeg"],
67
+ title=interface_title,
68
+ description=interface_description)
69
+ interface.launch(debug=True)
dog-photo-1.jpeg ADDED
dog-photo-2.jpeg ADDED
dog-photo-3.jpeg ADDED
dog-photo-4.jpeg ADDED