basementparking commited on
Commit
926bc73
·
verified ·
1 Parent(s): ae195de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -26
app.py CHANGED
@@ -24,7 +24,7 @@ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
24
  def setup_model():
25
  cfg = get_cfg()
26
  cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
27
- #cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
28
  cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml")
29
 
30
  cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # Set a threshold for this model
@@ -99,15 +99,6 @@ def overlay_heatmap_on_image(heatmap, image, alpha=0.4, colormap=cv2.COLORMAP_JE
99
  overlayed_img = cv2.addWeighted(image, alpha, heatmap, 1 - alpha, 0)
100
  return overlayed_img
101
 
102
- # # Set up the model
103
- # def setup_model():
104
- # cfg = get_cfg()
105
- # cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
106
- # cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model
107
- # cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
108
- # cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # Use GPU if available
109
- # predictor = DefaultPredictor(cfg)
110
- # return predictor
111
 
112
  # Function to segment image
113
  def segment_image(image):
@@ -213,28 +204,28 @@ def segment_image(image):
213
  modernity_scores = calculate_modernity_scores(modernity_output, year_categories).item()
214
 
215
 
216
- target_layer = modernity_model.layer4[-1]
217
 
218
- modernity_cam = GradCAM(modernity_model, target_layer)
219
- modernity_heatmap = modernity_cam(input_tensor, class_idx=torch.argmax(modernity_output).item())
220
 
221
- target_layer = typicality_model.layer4[-1]
222
- typicality_cam = GradCAM(typicality_model, target_layer)
223
- typicality_heatmap = typicality_cam(input_tensor, class_idx=torch.argmax(typicality_output).item())
224
 
225
- # Convert the input image to a format suitable for overlaying
226
- img_np = np.array(cropped_car_pil)
227
- img_np = cv2.resize(img_np, (224, 224))
228
 
229
- # Overlay the heatmap on the image
230
- overlayed_img_modernity = overlay_heatmap_on_image(modernity_heatmap, img_np)
231
- overlayed_img_typicality = overlay_heatmap_on_image(typicality_heatmap, img_np)
232
 
233
- # Convert overlayed images back to PIL for saving
234
- overlayed_img_modernity_pil = Image.fromarray(cv2.cvtColor(overlayed_img_modernity, cv2.COLOR_BGR2RGB))
235
- overlayed_img_typicality_pil = Image.fromarray(cv2.cvtColor(overlayed_img_typicality, cv2.COLOR_BGR2RGB))
236
 
237
- return "Automobiles detected in the image", cropped_car_pil, modernity_scores, typicality_scores , most_similar_group, overlayed_img_modernity_pil, overlayed_img_typicality_pil
238
 
239
 
240
  # Create Gradio interface
 
24
  def setup_model():
25
  cfg = get_cfg()
26
  cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
27
+ cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml")
28
  cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml")
29
 
30
  cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # Set a threshold for this model
 
99
  overlayed_img = cv2.addWeighted(image, alpha, heatmap, 1 - alpha, 0)
100
  return overlayed_img
101
 
 
 
 
 
 
 
 
 
 
102
 
103
  # Function to segment image
104
  def segment_image(image):
 
204
  modernity_scores = calculate_modernity_scores(modernity_output, year_categories).item()
205
 
206
 
207
+ # target_layer = modernity_model.layer4[-1]
208
 
209
+ # modernity_cam = GradCAM(modernity_model, target_layer)
210
+ # modernity_heatmap = modernity_cam(input_tensor, class_idx=torch.argmax(modernity_output).item())
211
 
212
+ # target_layer = typicality_model.layer4[-1]
213
+ # typicality_cam = GradCAM(typicality_model, target_layer)
214
+ # typicality_heatmap = typicality_cam(input_tensor, class_idx=torch.argmax(typicality_output).item())
215
 
216
+ # # Convert the input image to a format suitable for overlaying
217
+ # img_np = np.array(cropped_car_pil)
218
+ # img_np = cv2.resize(img_np, (224, 224))
219
 
220
+ # # Overlay the heatmap on the image
221
+ # overlayed_img_modernity = overlay_heatmap_on_image(modernity_heatmap, img_np)
222
+ # overlayed_img_typicality = overlay_heatmap_on_image(typicality_heatmap, img_np)
223
 
224
+ # # Convert overlayed images back to PIL for saving
225
+ # overlayed_img_modernity_pil = Image.fromarray(cv2.cvtColor(overlayed_img_modernity, cv2.COLOR_BGR2RGB))
226
+ # overlayed_img_typicality_pil = Image.fromarray(cv2.cvtColor(overlayed_img_typicality, cv2.COLOR_BGR2RGB))
227
 
228
+ return "Automobiles detected in the image", cropped_car_pil, modernity_scores, typicality_scores , most_similar_group#, overlayed_img_modernity_pil, overlayed_img_typicality_pil
229
 
230
 
231
  # Create Gradio interface