|
| 1 | +import os |
| 2 | +import random |
| 3 | + |
| 4 | +import torch |
| 5 | +import torchvision.transforms as transforms |
| 6 | +from PIL import Image |
| 7 | + |
| 8 | + |
| 9 | +def recalculate_box_and_verify_if_valid(x, y, w, h, image_size, original_image_size, min_box_size): |
| 10 | + scale = image_size / min(original_image_size) |
| 11 | + crop_y = (original_image_size[1] * scale - image_size) // 2 |
| 12 | + crop_x = (original_image_size[0] * scale - image_size) // 2 |
| 13 | + x0 = max(x * scale - crop_x, 0) |
| 14 | + y0 = max(y * scale - crop_y, 0) |
| 15 | + x1 = min((x + w) * scale - crop_x, image_size) |
| 16 | + y1 = min((y + h) * scale - crop_y, image_size) |
| 17 | + if (x1 - x0) * (y1 - y0) / (image_size * image_size) < min_box_size: |
| 18 | + return False, (None, None, None, None) |
| 19 | + return True, (x0, y0, x1, y1) |
| 20 | + |
| 21 | + |
| 22 | +class COCODataset(torch.utils.data.Dataset): |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + data_path, |
| 26 | + image_path, |
| 27 | + image_size=512, |
| 28 | + min_box_size=0.01, |
| 29 | + max_boxes_per_data=8, |
| 30 | + tokenizer=None, |
| 31 | + ): |
| 32 | + super().__init__() |
| 33 | + self.min_box_size = min_box_size |
| 34 | + self.max_boxes_per_data = max_boxes_per_data |
| 35 | + self.image_size = image_size |
| 36 | + self.image_path = image_path |
| 37 | + self.tokenizer = tokenizer |
| 38 | + self.transforms = transforms.Compose( |
| 39 | + [ |
| 40 | + transforms.Resize(image_size, interpolation=transforms.InterpolationMode.BILINEAR), |
| 41 | + transforms.CenterCrop(image_size), |
| 42 | + transforms.ToTensor(), |
| 43 | + transforms.Normalize([0.5], [0.5]), |
| 44 | + ] |
| 45 | + ) |
| 46 | + |
| 47 | + self.data_list = torch.load(data_path, map_location="cpu") |
| 48 | + |
| 49 | + def __getitem__(self, index): |
| 50 | + if self.max_boxes_per_data > 99: |
| 51 | + assert False, "Are you sure setting such large number of boxes per image?" |
| 52 | + |
| 53 | + out = {} |
| 54 | + |
| 55 | + data = self.data_list[index] |
| 56 | + image = Image.open(os.path.join(self.image_path, data["file_path"])).convert("RGB") |
| 57 | + original_image_size = image.size |
| 58 | + out["pixel_values"] = self.transforms(image) |
| 59 | + |
| 60 | + annos = data["annos"] |
| 61 | + |
| 62 | + areas, valid_annos = [], [] |
| 63 | + for anno in annos: |
| 64 | + # x, y, w, h = anno['bbox'] |
| 65 | + x0, y0, x1, y1 = anno["bbox"] |
| 66 | + x, y, w, h = x0, y0, x1 - x0, y1 - y0 |
| 67 | + valid, (x0, y0, x1, y1) = recalculate_box_and_verify_if_valid( |
| 68 | + x, y, w, h, self.image_size, original_image_size, self.min_box_size |
| 69 | + ) |
| 70 | + if valid: |
| 71 | + anno["bbox"] = [x0, y0, x1, y1] |
| 72 | + areas.append((x1 - x0) * (y1 - y0)) |
| 73 | + valid_annos.append(anno) |
| 74 | + |
| 75 | + # Sort according to area and choose the largest N objects |
| 76 | + wanted_idxs = torch.tensor(areas).sort(descending=True)[1] |
| 77 | + wanted_idxs = wanted_idxs[: self.max_boxes_per_data] |
| 78 | + valid_annos = [valid_annos[i] for i in wanted_idxs] |
| 79 | + |
| 80 | + out["boxes"] = torch.zeros(self.max_boxes_per_data, 4) |
| 81 | + out["masks"] = torch.zeros(self.max_boxes_per_data) |
| 82 | + out["text_embeddings_before_projection"] = torch.zeros(self.max_boxes_per_data, 768) |
| 83 | + |
| 84 | + for i, anno in enumerate(valid_annos): |
| 85 | + out["boxes"][i] = torch.tensor(anno["bbox"]) / self.image_size |
| 86 | + out["masks"][i] = 1 |
| 87 | + out["text_embeddings_before_projection"][i] = anno["text_embeddings_before_projection"] |
| 88 | + |
| 89 | + prob_drop_boxes = 0.1 |
| 90 | + if random.random() < prob_drop_boxes: |
| 91 | + out["masks"][:] = 0 |
| 92 | + |
| 93 | + caption = random.choice(data["captions"]) |
| 94 | + |
| 95 | + prob_drop_captions = 0.5 |
| 96 | + if random.random() < prob_drop_captions: |
| 97 | + caption = "" |
| 98 | + caption = self.tokenizer( |
| 99 | + caption, |
| 100 | + max_length=self.tokenizer.model_max_length, |
| 101 | + padding="max_length", |
| 102 | + truncation=True, |
| 103 | + return_tensors="pt", |
| 104 | + ) |
| 105 | + out["caption"] = caption |
| 106 | + |
| 107 | + return out |
| 108 | + |
| 109 | + def __len__(self): |
| 110 | + return len(self.data_list) |
0 commit comments