liewchooichin commited on
Commit
45a3297
·
verified ·
1 Parent(s): 5e033e4

Upload image_pretrained.py

Browse files
Files changed (1) hide show
  1. image_pretrained.py +141 -0
image_pretrained.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Spyder Editor
4
+
5
+ Use pretrained model to recognize an image.
6
+
7
+ """
8
+ import os
9
+ import numpy as np
10
+ import pandas as pd
11
+ import PIL
12
+ import tensorflow as tf
13
+ from tensorflow.keras.applications import EfficientNetV2S
14
+ from tensorflow.keras.applications import Xception
15
+ from tensorflow.keras.applications import MobileNetV3Small
16
+
17
+ """
18
+ model = ResNet50(weights='imagenet')
19
+
20
+ img_path = 'elephant.jpg'
21
+ img = keras.utils.load_img(img_path, target_size=(224, 224))
22
+ x = keras.utils.img_to_array(img)
23
+ x = np.expand_dims(x, axis=0)
24
+ x = preprocess_input(x)
25
+
26
+ preds = model.predict(x)
27
+ # decode the results into a list of tuples (class, description, probability)
28
+ # (one such list for each sample in the batch)
29
+ print('Predicted:', decode_predictions(preds, top=3)[0])
30
+ """
31
+
32
+ # Take a pretrained model, its preprocess input functions, prediction function,
33
+ # the image to be predicted.
34
+ # Return the decoded prediction.
35
+
36
+
37
+ def pretrained_model(image, model_name, preprocess_fn,
38
+ decoded_predictions_fn):
39
+ # preprocess with the model's function
40
+ x = preprocess_fn(image)
41
+ # predictions
42
+ pred = model_name.predict(x)
43
+ # decode the results into a list of tuples (class, description, probability)
44
+ decoded_pred = decoded_predictions_fn(pred, top=3)
45
+ # Must put in the [0] first element of the
46
+ # decoded predictions.
47
+ pred_df = pd.DataFrame(
48
+ decoded_pred[0],
49
+ columns=["class", "description", "probability"]
50
+ )
51
+ return pred_df
52
+
53
+ # Load the specific image size for the model
54
+ # path = filepath of the image
55
+ # size = size to be loaded
56
+
57
+
58
+ def load_and_resize_image(img_path, size=(224, 224, 3)):
59
+ img = tf.keras.utils.load_img(
60
+ path=img_path, target_size=size)
61
+ x = tf.keras.utils.img_to_array(img)
62
+ x = np.expand_dims(x, axis=0)
63
+ print(f"Shape of x: {x.shape}")
64
+
65
+ return x
66
+
67
+
68
+ def predict(img_path):
69
+ # Set the data directory
70
+ data_dir = "images"
71
+ image_list = ["cat.jpg", "mrt_train.jpg",
72
+ "apples.jpg", "duck.jpg", "daisy.jpg"]
73
+
74
+ # Xception: (299x299)
75
+ # EfficientNetV2S: (384, 384)
76
+ # MobileNetV2: (224, 224, 3)
77
+ # Load the image
78
+ # img_path = os.path.join(data_dir, image_list[4])
79
+ print(f"image path: {img_path}")
80
+
81
+ # Efficient Net V2 Small
82
+ img = load_and_resize_image(
83
+ img_path,
84
+ size=(384, 384))
85
+ eff_net = EfficientNetV2S(include_top=True,
86
+ weights='imagenet',
87
+ input_shape=(384, 384, 3))
88
+ pred_df_eff = pretrained_model(
89
+ img,
90
+ eff_net,
91
+ tf.keras.applications.efficientnet_v2.preprocess_input,
92
+ tf.keras.applications.efficientnet_v2.decode_predictions,
93
+ )
94
+ print(pred_df_eff[["description", "probability"]])
95
+
96
+ # Mobile Net V3 Small
97
+ img = load_and_resize_image(img_path, size=(224, 224))
98
+ mobile_net = MobileNetV3Small(include_top=True,
99
+ weights="imagenet",
100
+ input_shape=(224, 224, 3))
101
+ pred_df_mob = pretrained_model(
102
+ img,
103
+ eff_net,
104
+ tf.keras.applications.mobilenet_v3.preprocess_input,
105
+ tf.keras.applications.mobilenet_v3.decode_predictions,
106
+ )
107
+ print(pred_df_mob[["description", "probability"]])
108
+
109
+ # Xception
110
+ img = load_and_resize_image(img_path, size=(299, 299))
111
+ xcept = Xception(include_top=True,
112
+ weights="imagenet",
113
+ input_shape=(299, 299, 3))
114
+ pred_df_xcept = pretrained_model(
115
+ img,
116
+ xcept,
117
+ tf.keras.applications.xception.preprocess_input,
118
+ tf.keras.applications.xception.decode_predictions,
119
+ )
120
+ print(pred_df_xcept[["description", "probability"]])
121
+
122
+ # Format the output for gradio label
123
+ dict_eff = dict({
124
+ "Description": pred_df_eff["description"],
125
+ "Probability": pred_df_eff["probability"]
126
+ })
127
+ dict_mob = dict({
128
+ "Description": pred_df_mob["description"],
129
+ "Probability": pred_df_mob["probability"]
130
+ })
131
+ dict_xcept = dict({
132
+ "Description": pred_df_xcept["description"],
133
+ "Probability": pred_df_xcept["probability"]
134
+ })
135
+ return pred_df_eff[["description", "probability"]], \
136
+ pred_df_mob[["description", "probability"]], \
137
+ pred_df_xcept[["description", "probability"]]
138
+
139
+ # Main
140
+ if __name__ == "__main__":
141
+ predict()