def test_frcnn_nonfpn(self): for bbone in non_fpn_supported_models: backbone = faster_rcnn.create_fastercnn_backbone(backbone=bbone, pretrained=None, fpn=False) self.assertTrue(isinstance(backbone, nn.Module)) frcnn_model = faster_rcnn.create_vision_fastercnn(num_classes=3, backbone=backbone) self.assertTrue(isinstance(frcnn_model, nn.Module))
def test_sanity_fit_cuda(self): for bbone in fpn_supported_models: backbone = faster_rcnn.create_fastercnn_backbone(backbone=bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) frcnn_model = faster_rcnn.create_vision_fastercnn(num_classes=3, backbone=backbone) self.assertTrue(isinstance(frcnn_model, nn.Module)) result = faster_rcnn.sanity_fit(frcnn_model, train_loader, val_loader, "cuda", num_batches=10, fp16=True) self.assertTrue(result)
def test_val_step_fpn(self): for bbone in fpn_supported_models: backbone = faster_rcnn.create_fastercnn_backbone(backbone=bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) frcnn_model = faster_rcnn.create_vision_fastercnn(num_classes=3, backbone=backbone) self.assertTrue(isinstance(frcnn_model, nn.Module)) val_metrics = faster_rcnn.val_step(frcnn_model, train_loader, "cpu", num_batches=10) self.assertIsInstance(val_metrics, Dict) exp_keys = ("iou", "giou") for exp_k in exp_keys: self.assertTrue(exp_k in val_metrics.keys())
def test_train_step_fpn(self): for bbone in fpn_supported_models: backbone = faster_rcnn.create_fastercnn_backbone(backbone=bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) frcnn_model = faster_rcnn.create_vision_fastercnn(num_classes=3, backbone=backbone) self.assertTrue(isinstance(frcnn_model, nn.Module)) opt = torch.optim.SGD(frcnn_model.parameters(), lr=1e-3) train_metrics = faster_rcnn.train_step(frcnn_model, train_loader, "cpu", opt, num_batches=10) self.assertIsInstance(train_metrics, Dict) exp_keys = ("loss_classifier", "loss_box_reg", "loss_objectness", "loss_rpn_box_reg") for exp_k in exp_keys: self.assertTrue(exp_k in train_metrics.keys())
def test_infer(self): # Infer over an image image = Image.open("tests/assets/grace_hopper_517x606.jpg") tensor = im2tensor(image) self.assertEqual(tensor.ndim, 4) frcnn_model = faster_rcnn.create_vision_fastercnn() frcnn_model.eval() out = frcnn_model(tensor) self.assertIsInstance(out, list) self.assertIsInstance(out[0], Dict) self.assertIsInstance(out[0]["boxes"], torch.Tensor) self.assertIsInstance(out[0]["labels"], torch.Tensor) self.assertIsInstance(out[0]["scores"], torch.Tensor)
def test_fit_cuda(self): for bbone in fpn_supported_models: backbone = faster_rcnn.create_fastercnn_backbone(backbone=bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) frcnn_model = faster_rcnn.create_vision_fastercnn(num_classes=3, backbone=backbone) self.assertTrue(isinstance(frcnn_model, nn.Module)) opt = torch.optim.SGD(frcnn_model.parameters(), lr=1e-3) history = faster_rcnn.fit(frcnn_model, 1, train_loader, val_loader, "cuda", opt, num_batches=4, fp16=True) self.assertIsInstance(history, Dict) exp_keys = ("train", "val") for exp_k in exp_keys: self.assertTrue(exp_k in history.keys())
def test_train(self): # Read Image using PIL Here # Do forward over image image = Image.open("tests/assets/grace_hopper_517x606.jpg") img_tensor = im2tensor(image) self.assertEqual(img_tensor.ndim, 4) boxes = torch.tensor([[0, 0, 100, 100], [0, 1, 2, 2], [10, 15, 30, 35], [23, 35, 93, 95]], dtype=torch.float) labels = torch.tensor([1, 2, 3, 4], dtype=torch.int64) targets = [{"boxes": boxes, "labels": labels}] frcnn_model = faster_rcnn.create_vision_fastercnn(num_classes=5) out = frcnn_model(img_tensor, targets) self.assertIsInstance(out, Dict) self.assertIsInstance(out["loss_classifier"], torch.Tensor) self.assertIsInstance(out["loss_box_reg"], torch.Tensor) self.assertIsInstance(out["loss_objectness"], torch.Tensor) self.assertIsInstance(out["loss_rpn_box_reg"], torch.Tensor)
# And now let's train the model for 10 epochs, evaluating at the end of every epoch. # Quickvision to Train Detection Models ! # Training Faster RCNN backbone = faster_rcnn.create_fastercnn_backbone( "resnet101", fpn=True, pretrained="imagenet", trainable_backbone_layers=3) # our dataset has two classes only - background and person num_classes = 2 model = faster_rcnn.create_vision_fastercnn(num_classes=num_classes, backbone=backbone) # - Quickvision supports Mixed Precision training as well ! # Let's use Mixed Precision training scaler = amp.GradScaler() params = [p for p in model.parameters() if p.requires_grad] optimizer = torch.optim.SGD(params, lr=0.005, momentum=0.9, weight_decay=0.0005) # and a learning rate scheduler which decreases the learning rate by 10x every 3 epochs lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=3,