HoangTN11 commited on
Commit
c26d41d
·
verified ·
1 Parent(s): d984ad4

Upload inference_utils.py

Browse files
Files changed (1) hide show
  1. inference_utils.py +98 -0
inference_utils.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ import numpy as np
3
+ import cv2
4
+ from PIL import Image
5
+ import json
6
+
7
+ # Constants
8
+ IMG_SIZE = 240
9
+ MODEL_PATH = "model/efficientnetb1_plant_final.weights.h5"
10
+ CLASS_NAMES_PATH = "model/class_names.json"
11
+
12
+ # Load CLASS_NAMES
13
+ with open(CLASS_NAMES_PATH, "r") as f:
14
+ CLASS_NAMES = json.load(f)
15
+
16
+ # Build model EXACTLY like in training
17
+ base_model = tf.keras.applications.EfficientNetB1(
18
+ include_top=False,
19
+ weights="imagenet",
20
+ input_shape=(IMG_SIZE, IMG_SIZE, 3)
21
+ )
22
+ base_model.trainable = True
23
+
24
+ model = tf.keras.Sequential([
25
+ base_model,
26
+ tf.keras.layers.GlobalAveragePooling2D(),
27
+ tf.keras.layers.Dropout(0.2),
28
+ tf.keras.layers.Dense(len(CLASS_NAMES), activation='softmax')
29
+ ])
30
+
31
+ # Load weights
32
+ model.load_weights(MODEL_PATH)
33
+
34
+ # Preprocess image
35
+ def preprocess_image(image_path):
36
+ img = tf.keras.preprocessing.image.load_img(image_path, target_size=(IMG_SIZE, IMG_SIZE))
37
+ img_array = tf.keras.preprocessing.image.img_to_array(img) / 255.0
38
+ return np.expand_dims(img_array, axis=0)
39
+
40
+ # Grad-CAM
41
+ def generate_gradcam(img_path, model, class_index, layer_name="efficientnetb1"):
42
+ img_array = preprocess_image(img_path)
43
+
44
+ grad_model = tf.keras.models.Model(
45
+ [model.inputs],
46
+ [model.get_layer(layer_name).output, model.output]
47
+ )
48
+
49
+ with tf.GradientTape() as tape:
50
+ conv_outputs, predictions = grad_model(img_array)
51
+ loss = predictions[:, class_index]
52
+
53
+ grads = tape.gradient(loss, conv_outputs)[0]
54
+ pooled_grads = tf.reduce_mean(grads, axis=(0, 1))
55
+
56
+ conv_outputs = conv_outputs[0]
57
+ heatmap = tf.reduce_sum(conv_outputs * pooled_grads, axis=-1)
58
+
59
+ heatmap = np.maximum(heatmap, 0)
60
+ heatmap /= tf.math.reduce_max(heatmap) + 1e-6
61
+ heatmap = heatmap.numpy()
62
+
63
+ # Overlay heatmap
64
+ img = cv2.imread(img_path)
65
+ img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
66
+ heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))
67
+ heatmap = np.uint8(255 * heatmap)
68
+ heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
69
+ superimposed_img = heatmap * 0.4 + img
70
+ result_img = Image.fromarray(np.uint8(superimposed_img))
71
+
72
+ return result_img
73
+
74
+ # Inference
75
+ def predict_plant_disease(image_path):
76
+ img_array = preprocess_image(image_path)
77
+ preds = model.predict(img_array)[0]
78
+
79
+ class_index = int(np.argmax(preds))
80
+ confidence = float(preds[class_index])
81
+ label = CLASS_NAMES[class_index]
82
+
83
+ return {label: confidence}
84
+
85
+
86
+ ''' gradcam_img = generate_gradcam(image_path, model, class_index)
87
+ we will disable gradcam for now, we need to rebuild the model in kaggle using functional API to for this to work'''
88
+
89
+ ''' def build_model(num_classes=15):
90
+ inputs = tf.keras.Input(shape=(240, 240, 3))
91
+ base_model = tf.keras.applications.EfficientNetB1(include_top=False, weights='imagenet', input_tensor=inputs)
92
+ x = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)
93
+ x = tf.keras.layers.Dropout(0.2)(x)
94
+ outputs = tf.keras.layers.Dense(num_classes, activation='softmax')(x)
95
+ return tf.keras.Model(inputs=inputs, outputs=outputs)
96
+
97
+ model = build_model()
98
+ model.load_weights("model/efficientnetb1_plant_final.weights.h5")'''