|
| 1 | +import torch |
| 2 | +import torchvision |
| 3 | +from torchvision.models.detection import MaskRCNN |
| 4 | +from torchvision.models.detection.anchor_utils import AnchorGenerator |
| 5 | +from torchvision.models.detection.backbone_utils import resnet_fpn_backbone |
| 6 | +from torchvision.transforms import functional as F |
| 7 | +import os |
| 8 | +import numpy as np |
| 9 | +from PIL import Image |
| 10 | +import requests |
| 11 | +import zipfile |
| 12 | +from tqdm import tqdm |
| 13 | +import sys |
| 14 | + |
| 15 | +# Add project root to path |
| 16 | +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| 17 | +from src.model import SimCLR |
| 18 | + |
| 19 | +DEVICE = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu') |
| 20 | + |
| 21 | +def download_sample_dataset(root): |
| 22 | + """ |
| 23 | + Downloads Penn-Fudan Database for Pedestrian Detection and Segmentation |
| 24 | + as a sample dataset to verify the pipeline. |
| 25 | + User should replace this with WHU Building Dataset or similar for Remote Sensing. |
| 26 | + """ |
| 27 | + url = "https://www.cis.upenn.edu/~jshi/ped_html/PennFudanPed.zip" |
| 28 | + target_dir = os.path.join(root, "PennFudanPed") |
| 29 | + |
| 30 | + if os.path.exists(target_dir): |
| 31 | + print("Sample dataset already exists.") |
| 32 | + return target_dir |
| 33 | + |
| 34 | + print(f"Downloading sample dataset from {url}...") |
| 35 | + zip_path = os.path.join(root, "PennFudanPed.zip") |
| 36 | + r = requests.get(url, stream=True) |
| 37 | + with open(zip_path, 'wb') as f: |
| 38 | + for chunk in r.iter_content(chunk_size=1024): |
| 39 | + if chunk: |
| 40 | + f.write(chunk) |
| 41 | + |
| 42 | + print("Extracting...") |
| 43 | + with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
| 44 | + zip_ref.extractall(root) |
| 45 | + |
| 46 | + return target_dir |
| 47 | + |
| 48 | +class RemoteSensingDataset(torch.utils.data.Dataset): |
| 49 | + def __init__(self, root, transforms=None): |
| 50 | + self.root = root |
| 51 | + self.transforms = transforms |
| 52 | + # Setup for PennFudan structure. Modify for WHU/Standard format. |
| 53 | + self.imgs = list(sorted(os.listdir(os.path.join(root, "PNGImages")))) |
| 54 | + self.masks = list(sorted(os.listdir(os.path.join(root, "PedMasks")))) |
| 55 | + |
| 56 | + def __getitem__(self, idx): |
| 57 | + # Load image and mask |
| 58 | + img_path = os.path.join(self.root, "PNGImages", self.imgs[idx]) |
| 59 | + mask_path = os.path.join(self.root, "PedMasks", self.masks[idx]) |
| 60 | + |
| 61 | + img = Image.open(img_path).convert("RGB") |
| 62 | + # Mask is usually 1-channel, where each pixel value is the instance ID |
| 63 | + mask = Image.open(mask_path) |
| 64 | + mask = np.array(mask) |
| 65 | + |
| 66 | + # Instance IDs |
| 67 | + obj_ids = np.unique(mask) |
| 68 | + # First ID is background, remove it |
| 69 | + obj_ids = obj_ids[1:] |
| 70 | + |
| 71 | + masks = mask == obj_ids[:, None, None] |
| 72 | + |
| 73 | + num_objs = len(obj_ids) |
| 74 | + boxes = [] |
| 75 | + for i in range(num_objs): |
| 76 | + pos = np.where(masks[i]) |
| 77 | + xmin = np.min(pos[1]) |
| 78 | + xmax = np.max(pos[1]) |
| 79 | + ymin = np.min(pos[0]) |
| 80 | + ymax = np.max(pos[0]) |
| 81 | + boxes.append([xmin, ymin, xmax, ymax]) |
| 82 | + |
| 83 | + boxes = torch.as_tensor(boxes, dtype=torch.float32) |
| 84 | + labels = torch.ones((num_objs,), dtype=torch.int64) # Single class (e.g., Building) |
| 85 | + masks = torch.as_tensor(masks, dtype=torch.uint8) |
| 86 | + |
| 87 | + image_id = torch.tensor([idx]) |
| 88 | + area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0]) |
| 89 | + iscrowd = torch.zeros((num_objs,), dtype=torch.int64) |
| 90 | + |
| 91 | + target = {} |
| 92 | + target["boxes"] = boxes |
| 93 | + target["labels"] = labels |
| 94 | + target["masks"] = masks |
| 95 | + target["image_id"] = image_id |
| 96 | + target["area"] = area |
| 97 | + target["iscrowd"] = iscrowd |
| 98 | + |
| 99 | + if self.transforms is not None: |
| 100 | + img = F.to_tensor(img) |
| 101 | + # Add other transforms here if needed |
| 102 | + |
| 103 | + return img, target |
| 104 | + |
| 105 | + def __len__(self): |
| 106 | + return len(self.imgs) |
| 107 | + |
| 108 | +def get_model_instance_segmentation(num_classes, pretrained_simclr_path=None): |
| 109 | + # 1. Create ResNet-101 FPN backbone |
| 110 | + # trainable_layers=3 means we train the top 3 blocks. |
| 111 | + backbone = resnet_fpn_backbone('resnet101', weights=None, trainable_layers=3) |
| 112 | + |
| 113 | + # 2. Load SimCLR Weights |
| 114 | + if pretrained_simclr_path and os.path.exists(pretrained_simclr_path): |
| 115 | + print(f"Loading SimCLR weights from {pretrained_simclr_path} into Mask R-CNN backbone...") |
| 116 | + try: |
| 117 | + # SimCLR state dict keys: backbone.conv1.weight, backbone.layer1.0.conv1.weight... |
| 118 | + # FPN state dict keys: body.conv1.weight, body.layer1.0.conv1.weight... |
| 119 | + state_dict = torch.load(pretrained_simclr_path, map_location='cpu') |
| 120 | + |
| 121 | + # If saved as full model/wrapper |
| 122 | + if 'state_dict' in state_dict: |
| 123 | + state_dict = state_dict['state_dict'] |
| 124 | + |
| 125 | + new_state_dict = {} |
| 126 | + for k, v in state_dict.items(): |
| 127 | + if k.startswith('backbone.'): |
| 128 | + # Replace 'backbone.' with 'body.' to match FPN structure |
| 129 | + new_key = k.replace('backbone.', 'body.') |
| 130 | + # FPN backbone doesn't have 'fc' layer, so ignore it |
| 131 | + if 'fc' not in new_key: |
| 132 | + new_state_dict[new_key] = v |
| 133 | + |
| 134 | + # Load 'body' weights |
| 135 | + # strict=False because FPN has extra layers (fpn_inner, fpn_layer) that are not in ResNet |
| 136 | + missing, unexpected = backbone.load_state_dict(new_state_dict, strict=False) |
| 137 | + print(f"Weights loaded. Missing (expected for FPN layers): {len(missing)}, Unexpected: {len(unexpected)}") |
| 138 | + except Exception as e: |
| 139 | + print(f"Failed to load SimCLR weights: {e}") |
| 140 | + print("Using random initialization for backbone.") |
| 141 | + |
| 142 | + # 3. Create Mask R-CNN |
| 143 | + model = MaskRCNN(backbone, num_classes=num_classes) |
| 144 | + return model |
| 145 | + |
| 146 | +def main(): |
| 147 | + # Paths |
| 148 | + DATA_DIR = "./data" |
| 149 | + SIMCLR_PATH = "simclr_model_RN101.pth" |
| 150 | + |
| 151 | + # 1. Prepare Data |
| 152 | + data_path = download_sample_dataset(DATA_DIR) |
| 153 | + dataset = RemoteSensingDataset(data_path) |
| 154 | + |
| 155 | + # Split |
| 156 | + indices = torch.randperm(len(dataset)).tolist() |
| 157 | + dataset_train = torch.utils.data.Subset(dataset, indices[:-50]) |
| 158 | + dataset_test = torch.utils.data.Subset(dataset, indices[-50:]) |
| 159 | + |
| 160 | + data_loader = torch.utils.data.DataLoader( |
| 161 | + dataset_train, batch_size=2, shuffle=True, num_workers=0, collate_fn=lambda x: tuple(zip(*x)) |
| 162 | + ) |
| 163 | + |
| 164 | + # 2. Setup Model |
| 165 | + # num_classes = 2 (background + building/pedestrian) |
| 166 | + model = get_model_instance_segmentation(num_classes=2, pretrained_simclr_path=SIMCLR_PATH) |
| 167 | + model.to(DEVICE) |
| 168 | + |
| 169 | + # 3. Optimization |
| 170 | + params = [p for p in model.parameters() if p.requires_grad] |
| 171 | + optimizer = torch.optim.SGD(params, lr=0.005, momentum=0.9, weight_decay=0.0005) |
| 172 | + lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1) |
| 173 | + |
| 174 | + # 4. Training Loop (Simple) |
| 175 | + num_epochs = 2 # Demo |
| 176 | + print("Starting Training...") |
| 177 | + |
| 178 | + for epoch in range(num_epochs): |
| 179 | + model.train() |
| 180 | + i = 0 |
| 181 | + for images, targets in tqdm(data_loader): |
| 182 | + images = list(image.to(DEVICE) for image in images) |
| 183 | + targets = [{k: v.to(DEVICE) for k, v in t.items()} for t in targets] |
| 184 | + |
| 185 | + loss_dict = model(images, targets) |
| 186 | + losses = sum(loss for loss in loss_dict.values()) |
| 187 | + |
| 188 | + optimizer.zero_grad() |
| 189 | + losses.backward() |
| 190 | + optimizer.step() |
| 191 | + |
| 192 | + if i % 10 == 0: |
| 193 | + print(f"Epoch {epoch}, Iter {i}, Loss: {losses.item():.4f}") |
| 194 | + i += 1 |
| 195 | + |
| 196 | + lr_scheduler.step() |
| 197 | + print(f"Epoch {epoch} complete.") |
| 198 | + |
| 199 | + # Save |
| 200 | + torch.save(model.state_dict(), "maskrcnn_simclr_tuned.pth") |
| 201 | + print("Model saved to maskrcnn_simclr_tuned.pth") |
| 202 | + |
| 203 | +if __name__ == "__main__": |
| 204 | + main() |
0 commit comments