File size: 13,700 Bytes
0a96ac9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from transformers import AutoModelForObjectDetection, AutoImageProcessor
from torch.utils.data import Dataset, DataLoader
import os
from tqdm import tqdm
from PIL import Image
from pathlib import Path
from ultralytics.models.fastsam import FastSAMPredictor
import supervision as sv
import torch
import numpy as np
import cv2
from typing import List, Tuple, Dict, Any, Optional
from supervision.dataset.utils import approximate_mask_with_polygons
from supervision.detection.utils import (
    contains_holes,
    contains_multiple_segments,
)

detector = AutoModelForObjectDetection.from_pretrained("Yifeng-Liu/rt-detr-finetuned-for-satellite-image-roofs-detection")
detector_processor = AutoImageProcessor.from_pretrained("Yifeng-Liu/rt-detr-finetuned-for-satellite-image-roofs-detection")


overrides = dict(conf=0.25, task="segment", mode="predict", model="FastSAM-x.pt", save=False)
segment_predictor = FastSAMPredictor(overrides=overrides)

# IMG_FORMATS = {"bmp", "dng", "jpeg", "jpg", "mpo", "png", "tif", "tiff", "webp", "pfm"}  # image suffixes


class ImageInferenceDataset(Dataset):
    def __init__(self, image_paths: Path, image_processor):
        """
        A custom dataset class for image inference without annotations or masks.

        Args:
            image_folder (Path): The path to the folder containing images.
            image_processor: A callable for processing images (usually a transformer or feature extractor).
            image_formats (set): A set of supported image formats to be filtered.
        """
        self.image_processor = image_processor
        # Filter out files that are not supported image formats
        self.image_files = image_paths

    def __len__(self) -> int:
        return len(self.image_files)

    def __getitem__(self, idx: int) -> Tuple[torch.Tensor, str]:
        """
        Get an image from the dataset at the specified index.

        Args:
            idx (int): The index of the image.

        Returns:
            Tuple[torch.Tensor, str]: A tuple containing the processed image tensor and the image file path.
        """
        image_path = self.image_files[idx]
        # Open image using PIL and process it using the provided image processor
        with Image.open(image_path) as img:
            orig_size = img.size
            img = img.convert("RGB")  # Ensure all images are in RGB format for consistency
            processed_img = self.image_processor(images=img, return_tensors="pt")["pixel_values"].squeeze(0)

        return processed_img, str(image_path), orig_size


def collate_fn_inference(batch: List[Tuple[torch.Tensor, str]]) -> dict:
    """
    Collate function for batching images for inference.

    Args:
        batch (List[Tuple[torch.Tensor, str]]): A list of tuples where each tuple contains
                                                the processed image tensor and image path.

    Returns:
        dict: A dictionary containing the batched image tensors and corresponding image file paths.
    """
    pixel_values = [item[0] for item in batch]  # Extract processed images
    image_paths = [item[1] for item in batch]   # Extract image paths
    orig_sizes = [item[2] for item in batch]

    # Pad the images to match the largest image in the batch
    encoding = detector_processor.pad(pixel_values, return_tensors="pt")

    return {
        'pixel_values': encoding['pixel_values'],
        'pixel_mask': encoding['pixel_mask'],  # Padding mask (if needed by the model)
        'image_paths': image_paths,
        'orig_sizes': orig_sizes
    }


class ModelInference:
    def __init__(self, detector, detector_processor, segment_predictor, id2label, CONFIDENCE_TRESHOLD):
        self.detector = detector
        self.detector_processor = detector_processor
        self.segment_predictor = segment_predictor
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.CONFIDENCE_TRESHOLD = CONFIDENCE_TRESHOLD
        self.id2label = id2label
        self.mask_annotator = sv.MaskAnnotator()
        self.detector.to(self.device)

    def predict_one(self, image_path):
        image = cv2.imread(image_path)
        with torch.no_grad():

            # load image and predict
            inputs = self.detector_processor(images=image, return_tensors='pt').to(self.device)
            outputs = self.detector(**inputs)

            # post-process
            target_sizes = torch.tensor([image.shape[:2]]).to(self.device)
            results = detector_processor.post_process_object_detection(
                outputs=outputs,
                threshold=self.CONFIDENCE_TRESHOLD,
                target_sizes=target_sizes
            )[0]
            if results['boxes'].numel() == 0:
                print("No bounding box detected")
                return None, None
            else:
                det_detections = sv.Detections.from_transformers(transformers_results=results).with_nms(threshold=0.5)

            everything_results = self.segment_predictor(image)
        if everything_results[0].masks is not None:
            bbox_results = self.segment_predictor.prompt(everything_results, det_detections.xyxy.tolist())[0]
            seg_detections = sv.Detections.from_ultralytics(bbox_results)
            seg_detections = self.filter_small_masks(seg_detections)

            max_length = max(len(name) for name in self.id2label.values())

            # Create a new NumPy array with the appropriate dtype based on the longest string
            seg_detections.data['class_name'] = np.array(seg_detections.data['class_name'], dtype=f'<U{max_length}')

            for idx, class_name in enumerate(seg_detections.data['class_name']):
                if class_name == 'object':
                    seg_detections.data['class_name'][idx] = self.id2label[seg_detections.class_id[idx]]

            annotated_frame = image.copy()
            annotated_frame = self.mask_annotator.annotate(scene=annotated_frame, detections=seg_detections)

            return seg_detections, annotated_frame
        else:
            print("No segmentation mask generated")
            return None, None

    def predict_folder(self, image_paths, batch_size=4):
        dataset = ImageInferenceDataset(image_paths=image_paths, image_processor=detector_processor)

        # Create DataLoader instance with the custom collate function
        dataloader = DataLoader(dataset, batch_size=batch_size, collate_fn=collate_fn_inference)

        detector_failed_list = []
        segmentor_failed_list = []

        id2label = {0: 'building'}
        max_length = max(len(name) for name in id2label.values())

        all_image_paths = []

        all_results = []

        for idx, batch in enumerate(tqdm(dataloader)):
            pixel_values = batch["pixel_values"].to(self.device)
            pixel_mask = batch["pixel_mask"].to(self.device)
            image_paths = batch["image_paths"]
            orig_sizes = batch["orig_sizes"]

            orig_target_sizes = torch.tensor(orig_sizes, device=self.device)

            with torch.no_grad():
                outputs = self.detector(
                    pixel_values=pixel_values, pixel_mask=pixel_mask)

            # orig_target_sizes = torch.stack([target["orig_size"] for target in labels], dim=0)

            detector_results = detector_processor.post_process_object_detection(
                outputs,
                target_sizes=orig_target_sizes)

            detector_detections = []
            detector_to_remove = []

            for idx, detector_result in enumerate(detector_results):
                if detector_result['boxes'].numel() == 0:
                    # The tensor is empty
                    detector_to_remove.append(idx)
                else:
                    detector_detections.append(sv.Detections.from_transformers(transformers_results=detector_result))

            if detector_to_remove is not None:
                # Remove items from detector_results and image_ids by reversing the indices to avoid index shifting
                for idx in sorted(detector_to_remove, reverse=True):
                    detector_failed_list.append(image_paths[idx])
                    del image_paths[idx]

            images_raw = [cv2.imread(image_path) for image_path in image_paths]

            boxes = [detections.xyxy.tolist() for detections in detector_detections]

            results = []

            to_remove_seg = []

            for idx, (image_path, image, box) in enumerate(zip(image_paths, images_raw, boxes)):
                try:
                    with torch.no_grad():
                        # segmentation_result = segment_model(image, bboxes=box)[0]
                        everything_results = self.segment_predictor(image)

                        if everything_results[0].masks is not None:
                            bbox_results = self.segment_predictor.prompt(everything_results, box)[0]
                            seg_detections = sv.Detections.from_ultralytics(bbox_results)
                            seg_detections = self.filter_small_masks(seg_detections)
                            seg_detections.data['class_name'] = np.array(seg_detections.data['class_name'], dtype=f'<U{max_length}')
                            for idx, class_name in enumerate(seg_detections.data['class_name']):
                                if class_name == 'object':
                                    seg_detections.data['class_name'][idx] = id2label[seg_detections.class_id[idx]]
                            results.append(seg_detections)
                        else:
                            to_remove_seg.append(idx)
                except Exception as e:
                    print(f"An error occurred: {e}")
                    print(f"box: {box}")
                    print(f"image id: {image_path}")
                # result = sv.Detections.from_ultralytics(segmentation_result)
                # results.append(result)

            if to_remove_seg is not None:
                for idx in sorted(to_remove_seg, reverse=True):
                    segmentor_failed_list.append(image_paths[idx])
                    del image_paths[idx]

            if len(results) != len(image_paths):
                print(f"Length of results ({len(results)}) does not match the length of image_ids ({len(image_paths)})")
                continue

            all_image_paths.extend(image_paths)
            all_results.extend(results)

            annotated_frame = cv2.imread(all_image_paths[0]).copy()
            annotated_frame = self.mask_annotator.annotate(scene=annotated_frame, detections=all_results[0])

        return all_image_paths, all_results, annotated_frame, detector_failed_list, segmentor_failed_list

    def filter_small_masks(self, detections: sv.Detections) -> sv.Detections:
        valid_indices = []
        min_image_area_percentage = 0.002
        max_image_area_percentage = 0.80
        approximation_percentage = 0.75
        for i, mask in enumerate(detections.mask):

            # Check for structural issues in the mask
            if not (contains_holes(mask) or contains_multiple_segments(mask)):
                # Check if the mask can be approximated to a polygon successfully
                if not approximate_mask_with_polygons(mask=mask,
                                                      min_image_area_percentage=min_image_area_percentage,
                                                      max_image_area_percentage=max_image_area_percentage,
                                                      approximation_percentage=approximation_percentage,
                                                      ):
                    print(f"Skipping mask {i} due to structural issues")
                    continue

            # If all checks pass, add index to valid_indices
            valid_indices.append(i)

        filtered_xyxy = detections.xyxy[valid_indices]
        filtered_mask = detections.mask[valid_indices]
        filtered_confidence = detections.confidence[valid_indices]
        filtered_class_id = detections.class_id[valid_indices]
        filtered_class_name = detections.data['class_name'][valid_indices]

        detections.xyxy = filtered_xyxy
        detections.mask = filtered_mask
        detections.confidence = filtered_confidence
        detections.class_id = filtered_class_id
        detections.data['class_name'] = filtered_class_name
        return detections

    def get_dict(
        self,
        image_paths: List[Any],
        detections: List[Any]
    ) -> Dict[str, Any]:

        detections_dict = {}

        for idx, image_path in enumerate(image_paths):
            detections_dict[image_path] = detections[idx]

        return detections_dict

    def save_annotations(self,
                         image_paths,
                         detections,
                         class_names,
                         annotation_path,
                         MIN_IMAGE_AREA_PERCENTAGE=0.002,
                         MAX_IMAGE_AREA_PERCENTAGE=0.80,
                         APPROXIMATION_PERCENTAGE=0.75):
        # image_dir = annotation_path.parent
        detections_dict = self.get_dict(image_paths, detections)
        sv.DetectionDataset(
            classes=class_names,
            images=image_paths,
            annotations=detections_dict
        ).as_coco(
            images_directory_path=None,
            annotations_path=annotation_path,
            min_image_area_percentage=MIN_IMAGE_AREA_PERCENTAGE,
            max_image_area_percentage=MAX_IMAGE_AREA_PERCENTAGE,
            approximation_percentage=APPROXIMATION_PERCENTAGE
        )

        return