MissingBreath commited on
Commit
e210c55
·
verified ·
1 Parent(s): 842cc6e

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +58 -0
api.py CHANGED
@@ -30,6 +30,7 @@ from sklearn.svm import SVC
30
  import joblib
31
 
32
  model = tf.keras.models.load_model('946_.keras', compile=False)
 
33
 
34
  file_path = "Algeria Plant Disease Treatment Plan.docx"
35
  def docx_to_knowledge_base(file_path):
@@ -287,6 +288,63 @@ async def classify(image: UploadFile = File(...)):
287
  return {"prediction": predicted_class_idx,"gradcam": base64_image,"ration":ratio}
288
  else:
289
  return {"error": "No image provided"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
290
 
291
 
292
  yolomodel = YOLO("yolo11m.pt")
 
30
  import joblib
31
 
32
  model = tf.keras.models.load_model('946_.keras', compile=False)
33
+ modelPalm = tf.keras.models.load_model('palm_model.h5', compile=False)
34
 
35
  file_path = "Algeria Plant Disease Treatment Plan.docx"
36
  def docx_to_knowledge_base(file_path):
 
288
  return {"prediction": predicted_class_idx,"gradcam": base64_image,"ration":ratio}
289
  else:
290
  return {"error": "No image provided"}
291
+
292
+
293
+ def make_gradcam_heatmap_Palm(img_array, model, base_model_name, last_conv_layer_name, pred_index=None):
294
+ base_model = model.get_layer(base_model_name)
295
+ last_conv_layer = base_model.get_layer(last_conv_layer_name)
296
+
297
+ grad_model = tf.keras.models.Model(
298
+ inputs=[model.inputs],
299
+ outputs=[last_conv_layer.output, model.output]
300
+ )
301
+
302
+ with tf.GradientTape() as tape:
303
+ conv_outputs, predictions = grad_model(img_array)
304
+ if pred_index is None:
305
+ pred_index = tf.argmax(predictions[0])
306
+ loss = predictions[:, pred_index]
307
+
308
+ grads = tape.gradient(loss, conv_outputs)
309
+ pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
310
+ conv_outputs = conv_outputs[0]
311
+ heatmap = conv_outputs @ pooled_grads[..., tf.newaxis]
312
+ heatmap = tf.squeeze(heatmap)
313
+ heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
314
+ return heatmap.numpy()
315
+
316
+
317
+ @app.post("/palmclassify")
318
+ async def palmclassify(image: UploadFile = File(...)):
319
+ if image is not None:
320
+ img = Image.open(io.BytesIO(await image.read()))
321
+ img = img.resize((64,64))
322
+ img_array = np.array(img) / 255.0
323
+ img_array = np.expand_dims(img_array, axis=0)
324
+ predictions = modelPalm.predict(img_array)
325
+ predicted_class_idx = np.argmax(predictions)
326
+ predicted_class_idx = int(predicted_class_idx)
327
+
328
+ last_mb = "Conv_1"
329
+
330
+ # heatmap = make_gradcam_heatmap(img_array, modelPalm, last_mb)
331
+ heatmap = make_gradcam_heatmap_Palm(
332
+ img_array,
333
+ modelPalm,
334
+ base_model_name='mobilenetv2_1.00_224',
335
+ last_conv_layer_name='Conv_1'
336
+ )
337
+
338
+ base64_image = display_gradcam( np.array(img), heatmap)
339
+
340
+ # Return the base64 encoded image in the response
341
+
342
+ # Calculate and print the activation ratio
343
+ ratio = calculate_activation_ratio(heatmap)
344
+
345
+ return {"prediction": predicted_class_idx,"gradcam": base64_image,"ration":ratio}
346
+ else:
347
+ return {"error": "No image provided"}
348
 
349
 
350
  yolomodel = YOLO("yolo11m.pt")