윤영준 윤영준 09-12
bug fix : wrong operation on segmentation masks (resize on entire proto mask?;;;)
@9b92e6481b61d0fab11babee734d23e45844e653
yoloseg/inference_gpu_.py
--- yoloseg/inference_gpu_.py
+++ yoloseg/inference_gpu_.py
@@ -166,10 +166,10 @@
             # for now, plural batch operation is not supported, and this is the point where you should start.
             # instead of hardcoded proto_masks[0], do some iterative/vectorize operation
             mask = np.tensordot(coeffs, proto_masks[0], axes=[0, 0])  # Dot product along the number of prototypes
-
+            resized_mask = cv2.resize(mask,(image_shape[0], image_shape[1]))
             # Resize mask to the bounding box size, using sigmoid to normalize
-            resized_mask = cv2.resize(mask, (w, h))
-            resized_mask = self.sigmoid(resized_mask)
+            cropped_mask = resized_mask[y1:y1+h, x1:x1+w]
+            resized_mask = self.sigmoid(cropped_mask)
 
             # Threshold to create a binary mask
             final_mask = (resized_mask > 0.5).astype(np.uint8)
@@ -250,14 +250,15 @@
 
 def test():
     import time
-
+    import glob
+    import os
     # Path to your ONNX model and classes text file
     model_path = 'yoloseg/weight/best.onnx'
     classes_txt_file = 'config_files/yolo_config.txt'
     # image_path = 'yoloseg/img3.jpg'
-    image_path = 'yoloseg/img3.jpg'
+    image_path = 'yoloseg/img.jpg'
 
-    model_input_shape = (640, 640)
+    model_input_shape = (480, 480)
     inference_engine = Inference(
         onnx_model_path=model_path,
         model_input_shape=model_input_shape,
@@ -273,36 +274,44 @@
     img = cv2.resize(img, model_input_shape)
     # Run inference
 
-    for i in range(10):
+    # for i in range(10):
+    #     t1 = time.time()
+    #     detections, mask_maps = inference_engine.run_inference(img)
+    #     t2 = time.time()
+    #     print(t2 - t1)
+
+    images = glob.glob("/home/juni/사진/flood/out-/*.jpg")
+    images = sorted(images)
+    for k, image in enumerate(images):
+        image = cv2.imread(image)
         t1 = time.time()
-        detections, mask_maps = inference_engine.run_inference(img)
+        image = cv2.resize(image, model_input_shape)
+        detections, mask_maps = inference_engine.run_inference(image)
+
+        # Display results
+        for detection in detections:
+            x, y, w, h = detection['box']
+            class_name = detection['class_name']
+            confidence = detection['confidence']
+            cv2.rectangle(image, (x, y), (x+w, y+h), detection['color'], 2)
+            label = f"{class_name}: {confidence:.2f}"
+            cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, detection['color'], 2)
+        if len(mask_maps) != 0:
+            for i in range(mask_maps.shape[2]):  # Iterate over each mask
+                seg_image = overlay_mask(image, mask_maps[:, :, i], color=(0, 255, 0), alpha=0.3)
+                # cv2.imshow(f"Segmentation {i + 1}", seg_image)
+            # cv2.waitKey(0)  # Wait for a key press before showing the next mask
+            # cv2.destroyAllWindows()
         t2 = time.time()
         print(t2 - t1)
-
-
-
-    # Display results
-    for detection in detections:
-        x, y, w, h = detection['box']
-        class_name = detection['class_name']
-        confidence = detection['confidence']
-        cv2.rectangle(img, (x, y), (x+w, y+h), detection['color'], 2)
-        label = f"{class_name}: {confidence:.2f}"
-        cv2.putText(img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, detection['color'], 2)
+        cv2.imwrite( f"/home/juni/사진/flood/infer/{k}.jpg", seg_image)
 
     # Show the image_binary
     # cv2.imshow('Detections', img)
     # cv2.waitKey(0)
     # cv2.destroyAllWindows()
 
-    # If you also want to display segmentation maps, you would need additional handling here
-    # Example for displaying first mask if available:
-    if len(mask_maps) != 0:
 
-        seg_image = overlay_mask(img, mask_maps[:,:,0], color=(0, 255, 0), alpha=0.3)
-        cv2.imshow("segmentation", seg_image)
-        cv2.waitKey(0)
-        cv2.destroyAllWindows()
 
 
 if __name__ == "__main__":
Add a comment
List