Anthony-Ml commited on
Commit
6088cd1
·
1 Parent(s): 311d8c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -32
app.py CHANGED
@@ -61,39 +61,38 @@ def predict_image(get_image):
61
  pred, idx, probs = learn.predict(get_image)
62
  return dict(zip(categories, map(float, probs)))
63
 
64
- def interpretation_function(get_image):
65
- # Create or load your PyTorch model
66
- target_layer = learn.layer4[-1]
67
- # Create an instance of GradCAM
68
- cam = GradCAM(model=learn, target_layer=target_layer)
69
- # Load and preprocess your image
70
- image_path = get_image #'your_image.jpg'
71
- image = Image.open(image_path)
72
- preprocess = transforms.Compose([
73
- transforms.Resize((224, 224)),
74
- transforms.ToTensor(),
75
- ])
76
- input_image = preprocess(image).unsqueeze(0) # Add a batch dimension
77
- input_image = input_image.to('cuda' if torch.cuda.is_available() else 'cpu')
78
- # Compute the CAM
79
- cam_image = cam(input_tensor=input_image, target_category=None)
80
- # Compute the CAM
81
- cam_image = cam(input_tensor=input_image, target_category=None)
82
- # Show the CAM on the original image
83
- visualization = show_cam_on_image(input_image, cam_image)
84
- return visualization
85
-
86
- with gr.Blocks() as demo:
87
- with gr.Row():
88
- with gr.Column():
89
- input_img = gr.Image(label="Input Image", shape=(224, 224))
90
- with gr.Row():
91
- interpret = gr.Button("Interpret")
92
- with gr.Column():
93
  label = gr.Label(label="Predicted Class")
94
- with gr.Column():
95
- interpretation = gr.components.Interpretation(input_img)
96
- interpret.click(interpretation_function, input_img, interpretation)
97
 
98
 
99
  #interpretation="default"
 
61
  pred, idx, probs = learn.predict(get_image)
62
  return dict(zip(categories, map(float, probs)))
63
 
64
+ def interpretation_function(image_path, model, target_layer, target_category=None):
65
+ # Load and preprocess the image
66
+ image = Image.open(image_path)
67
+ preprocess = transforms.Compose([
68
+ transforms.Resize((224, 224)),
69
+ transforms.ToTensor(),
70
+ ])
71
+ input_image = preprocess(image).unsqueeze(0) # Add a batch dimension
72
+ input_image = input_image.to('cuda' if torch.cuda.is_available() else 'cpu')
73
+
74
+ # Create an instance of GradCAM
75
+ cam = GradCAM(model=model, target_layer=target_layer)
76
+
77
+ # Compute the CAM
78
+ cam_image = cam(input_tensor=input_image, target_category=target_category)
79
+
80
+ # Show the CAM on the original image
81
+ visualization = show_cam_on_image(input_image, cam_image)
82
+ visualization.show()
83
+
84
+
85
+ with gr.Blocks() as demo:
86
+ with gr.Row():
87
+ with gr.Column():
88
+ input_img = gr.Image(label="Input Image", shape=(224, 224))
89
+ with gr.Row():
90
+ interpret = gr.Button("Interpret")
91
+ with gr.Column():
 
92
  label = gr.Label(label="Predicted Class")
93
+ with gr.Column():
94
+ interpretation = gr.components.Interpretation(input_img)
95
+ interpret.click(interpretation_function(get_image,learn, learn.layer4[-1],target_category=None), input_img, interpretation)
96
 
97
 
98
  #interpretation="default"