From 395b205f45ccf73746123ac1ade2b5417c680581 Mon Sep 17 00:00:00 2001 From: Aditya Oke <47158509+oke-aditya@users.noreply.github.com> Date: Fri, 11 Dec 2020 19:49:22 +0530 Subject: [PATCH] Fixes requirements to be installable, fixes CUDA Tests (#88) * fixes requirements to be installable * fixes CI and req * retry install * explicit to cpu --- .github/workflows/ci-testing.yml | 1 - requirements-test.txt | 5 ++++- requirements.txt | 2 ++ tests/test_cnn.py | 9 +++++++++ tests/test_detr.py | 6 ++++++ tests/test_frcnn.py | 9 +++++++++ tests/test_retinanet.py | 10 +++++++++- 7 files changed, 39 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci-testing.yml b/.github/workflows/ci-testing.yml index aa66f5a..0960474 100644 --- a/.github/workflows/ci-testing.yml +++ b/.github/workflows/ci-testing.yml @@ -28,7 +28,6 @@ jobs: run: | python -m pip install --upgrade pip pip install flake8 pytest pytest-cov - pip install torch==1.7.1+cpu torchvision==0.8.2+cpu -f https://download.pytorch.org/whl/torch_stable.html pip install -r requirements-test.txt pip install -e . - name: Lint with flake8 diff --git a/requirements-test.txt b/requirements-test.txt index 0e38af7..16de9e0 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,4 +1,7 @@ -timm +--find-links https://download.pytorch.org/whl/torch_stable.html +torch==1.7.1+cpu +torchvision==0.8.2+cpu pytorch_lightning==1.1.0 scipy==1.5.4 +timm pandas diff --git a/requirements.txt b/requirements.txt index 07e7fc7..bb5827b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ +torch==1.7.1 +torchvision==0.8.2 pytorch_lightning==1.1.0 scipy==1.5.4 diff --git a/tests/test_cnn.py b/tests/test_cnn.py index eb34d84..a27114b 100644 --- a/tests/test_cnn.py +++ b/tests/test_cnn.py @@ -48,6 +48,7 @@ def test_train(self): self.assertEqual(tensor.ndim, 4) for model_name in supported_tv_models: model = cnn.create_vision_cnn(model_name, 10, pretrained=None) + model = model.cpu() out = model(tensor) self.assertEqual(out.shape[1], 10) self.assertEqual(out.ndim, 2) @@ -59,6 +60,7 @@ def test_infer(self): self.assertEqual(tensor.ndim, 4) for model_name in supported_tv_models: model = cnn.create_vision_cnn(model_name, 10, pretrained=None) + model = model.cpu() model = model.eval() out = model(tensor) self.assertEqual(out.shape[1], 10) @@ -67,6 +69,7 @@ def test_infer(self): def test_train_step(self): for model_name in supported_tv_models: model = cnn.create_vision_cnn(model_name, 10, pretrained=None) + model = model.cpu() opt = torch.optim.Adam(model.parameters(), lr=1e-3) loss = nn.CrossEntropyLoss() # scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer=opt, base_lr=1e-4, max_lr=1e-3, mode="min") @@ -95,6 +98,7 @@ def test_train_step_cuda(self): def test_val_step(self): for model_name in supported_tv_models: model = cnn.create_vision_cnn(model_name, 10, pretrained=None) + model = model.cpu() loss = nn.CrossEntropyLoss() val_metrics = cnn.val_step(model, val_loader, loss, "cpu", num_batches=10) self.assertIsInstance(val_metrics, Dict) @@ -105,6 +109,7 @@ def test_val_step(self): def test_fit(self): for model_name in supported_tv_models: model = cnn.create_vision_cnn(model_name, 10, pretrained=None) + model = model.cpu() opt = torch.optim.Adam(model.parameters(), lr=1e-3) loss = nn.CrossEntropyLoss() # scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer=opt, base_lr=1e-4, max_lr=1e-3, mode="min") @@ -143,6 +148,7 @@ def test_fit_cuda(self): def test_fit_swa(self): for model_name in supported_tv_models: model = cnn.create_vision_cnn(model_name, 10, pretrained=None) + model = model.cpu() opt = torch.optim.Adam(model.parameters(), lr=1e-3) loss = nn.CrossEntropyLoss() scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(opt, T_max=300) @@ -186,6 +192,7 @@ def test_fit_swa_cuda(self): def test_train_sanity_fit(self): for model_name in supported_tv_models: model = cnn.create_vision_cnn(model_name, 10, pretrained=None) + model = model.cpu() opt = torch.optim.Adam(model.parameters(), lr=1e-3) loss = nn.CrossEntropyLoss() res = cnn.train_sanity_fit(model, train_loader, loss, "cpu", num_batches=10) @@ -203,6 +210,7 @@ def test_train_sanity_fit_cuda(self): def test_val_sanity_fit(self): for model_name in supported_tv_models: model = cnn.create_vision_cnn(model_name, 10, pretrained=None) + model = model.cpu() loss = nn.CrossEntropyLoss() res = cnn.val_sanity_fit(model, val_loader, loss, "cpu", num_batches=10) self.assertTrue(res) @@ -210,6 +218,7 @@ def test_val_sanity_fit(self): def test_sanity_fit(self): for model_name in supported_tv_models: model = cnn.create_vision_cnn(model_name, 10, pretrained=None) + model = model.cpu() loss = nn.CrossEntropyLoss() res = cnn.sanity_fit(model, train_loader, val_loader, loss, "cpu", num_batches=10) self.assertTrue(res) diff --git a/tests/test_detr.py b/tests/test_detr.py index 69120f6..9002bfb 100644 --- a/tests/test_detr.py +++ b/tests/test_detr.py @@ -85,6 +85,7 @@ def test_train_step(self): backbone = detr.create_detr_backbone(bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) detr_model = detr.create_vision_detr(num_classes=3, num_queries=5, backbone=backbone) + detr_model = detr_model.cpu() self.assertTrue(isinstance(detr_model, nn.Module)) opt = torch.optim.SGD(detr_model.parameters(), lr=1e-3) matcher = detr_loss.HungarianMatcher() @@ -121,6 +122,7 @@ def test_val_step(self): backbone = detr.create_detr_backbone(bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) detr_model = detr.create_vision_detr(num_classes=3, num_queries=5, backbone=backbone) + detr_model = detr_model.cpu() self.assertTrue(isinstance(detr_model, nn.Module)) matcher = detr_loss.HungarianMatcher() weight_dict = {"loss_ce": 1, "loss_bbox": 1, "loss_giou": 1} @@ -137,6 +139,7 @@ def test_fit(self): backbone = detr.create_detr_backbone(bbone, pretrained="coco") self.assertTrue(isinstance(backbone, nn.Module)) detr_model = detr.create_vision_detr(num_classes=3, num_queries=5, backbone=backbone) + detr_model = detr_model.cpu() self.assertTrue(isinstance(detr_model, nn.Module)) matcher = detr_loss.HungarianMatcher() weight_dict = {"loss_ce": 1, "loss_bbox": 1, "loss_giou": 1} @@ -174,6 +177,7 @@ def test_train_sanity_fit(self): backbone = detr.create_detr_backbone(bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) detr_model = detr.create_vision_detr(num_classes=3, num_queries=5, backbone=backbone) + detr_model = detr_model.cpu() self.assertTrue(isinstance(detr_model, nn.Module)) matcher = detr_loss.HungarianMatcher() weight_dict = {"loss_ce": 1, "loss_bbox": 1, "loss_giou": 1} @@ -201,6 +205,7 @@ def test_val_sanity_fit(self): backbone = detr.create_detr_backbone(bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) detr_model = detr.create_vision_detr(num_classes=3, num_queries=5, backbone=backbone) + detr_model = detr_model.cpu() self.assertTrue(isinstance(detr_model, nn.Module)) matcher = detr_loss.HungarianMatcher() weight_dict = {"loss_ce": 1, "loss_bbox": 1, "loss_giou": 1} @@ -214,6 +219,7 @@ def test_sanity_fit(self): backbone = detr.create_detr_backbone(bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) detr_model = detr.create_vision_detr(num_classes=3, num_queries=5, backbone=backbone) + detr_model = detr_model.cpu() self.assertTrue(isinstance(detr_model, nn.Module)) matcher = detr_loss.HungarianMatcher() weight_dict = {"loss_ce": 1, "loss_bbox": 1, "loss_giou": 1} diff --git a/tests/test_frcnn.py b/tests/test_frcnn.py index 6d45757..8cee71e 100644 --- a/tests/test_frcnn.py +++ b/tests/test_frcnn.py @@ -66,6 +66,7 @@ def test_train(self): 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) + frcnn_model = frcnn_model.cpu() out = frcnn_model(img_tensor, targets) self.assertIsInstance(out, Dict) self.assertIsInstance(out["loss_classifier"], torch.Tensor) @@ -79,6 +80,7 @@ def test_infer(self): tensor = im2tensor(image) self.assertEqual(tensor.ndim, 4) frcnn_model = faster_rcnn.create_vision_fastercnn() + frcnn_model = frcnn_model.cpu() frcnn_model.eval() out = frcnn_model(tensor) self.assertIsInstance(out, list) @@ -92,6 +94,7 @@ def test_train_step_fpn(self): 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) + frcnn_model = frcnn_model.cpu() 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) @@ -119,6 +122,7 @@ def test_val_step_fpn(self): 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) + frcnn_model = frcnn_model.cpu() 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) @@ -131,6 +135,7 @@ def test_fit(self): 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) + frcnn_model = frcnn_model.cpu() 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, "cpu", opt, num_batches=10) @@ -172,6 +177,7 @@ def test_train_sanity_fit(self): 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) + frcnn_model = frcnn_model.cpu() self.assertTrue(isinstance(frcnn_model, nn.Module)) result = faster_rcnn.train_sanity_fit(frcnn_model, train_loader, "cpu", num_batches=10) self.assertTrue(result) @@ -191,6 +197,7 @@ def test_val_sanity_fit(self): 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) + frcnn_model = frcnn_model.cpu() self.assertTrue(isinstance(frcnn_model, nn.Module)) result = faster_rcnn.val_sanity_fit(frcnn_model, val_loader, "cpu", num_batches=10) self.assertTrue(result) @@ -200,6 +207,7 @@ def test_sanity_fit(self): 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) + frcnn_model = frcnn_model.cpu() self.assertTrue(isinstance(frcnn_model, nn.Module)) result = faster_rcnn.sanity_fit(frcnn_model, train_loader, val_loader, "cpu", num_batches=10) self.assertTrue(result) @@ -210,6 +218,7 @@ def test_sanity_fit_cuda(self): 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) + frcnn_model = frcnn_model.cpu() 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) diff --git a/tests/test_retinanet.py b/tests/test_retinanet.py index 902d1ff..9bbe9ac 100644 --- a/tests/test_retinanet.py +++ b/tests/test_retinanet.py @@ -65,6 +65,7 @@ def test_train(self): labels = torch.tensor([1, 2, 3, 4], dtype=torch.int64) targets = [{"boxes": boxes, "labels": labels}] retina_model = retinanet.create_vision_retinanet(num_classes=5) + retina_model = retina_model.cpu() out = retina_model(img_tensor, targets) self.assertIsInstance(out, Dict) self.assertIsInstance(out["classification"], torch.Tensor) @@ -76,6 +77,7 @@ def test_infer(self): tensor = im2tensor(image) self.assertEqual(tensor.ndim, 4) retina_model = retinanet.create_vision_retinanet() + retina_model = retina_model.cpu() retina_model.eval() out = retina_model(tensor) self.assertIsInstance(out, list) @@ -89,6 +91,7 @@ def test_train_step_fpn(self): backbone = retinanet.create_retinanet_backbone(backbone=bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) retina_model = retinanet.create_vision_retinanet(num_classes=3, backbone=backbone) + retina_model = retina_model.cpu() self.assertTrue(isinstance(retina_model, nn.Module)) opt = torch.optim.SGD(retina_model.parameters(), lr=1e-3) train_metrics = retinanet.train_step(retina_model, train_loader, "cpu", opt, num_batches=4) @@ -116,6 +119,7 @@ def test_val_step_fpn(self): backbone = retinanet.create_retinanet_backbone(backbone=bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) retina_model = retinanet.create_vision_retinanet(num_classes=3, backbone=backbone) + retina_model = retina_model.cpu() self.assertTrue(isinstance(retina_model, nn.Module)) val_metrics = retinanet.val_step(retina_model, train_loader, "cpu", num_batches=4) self.assertIsInstance(val_metrics, Dict) @@ -128,6 +132,7 @@ def test_fit(self): backbone = retinanet.create_retinanet_backbone(backbone=bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) retina_model = retinanet.create_vision_retinanet(num_classes=3, backbone=backbone) + retina_model = retina_model.cpu() self.assertTrue(isinstance(retina_model, nn.Module)) opt = torch.optim.SGD(retina_model.parameters(), lr=1e-3) history = retinanet.fit(retina_model, 1, train_loader, val_loader, "cpu", opt, num_batches=4) @@ -155,6 +160,7 @@ def test_train_sanity_fit(self): backbone = retinanet.create_retinanet_backbone(backbone=bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) retina_model = retinanet.create_vision_retinanet(num_classes=3, backbone=backbone) + retina_model = retina_model.cpu() self.assertTrue(isinstance(retina_model, nn.Module)) result = retinanet.train_sanity_fit(retina_model, train_loader, "cpu", num_batches=10) self.assertTrue(result) @@ -174,6 +180,7 @@ def test_val_sanity_fit(self): backbone = retinanet.create_retinanet_backbone(backbone=bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) retina_model = retinanet.create_vision_retinanet(num_classes=3, backbone=backbone) + retina_model = retina_model.cpu() self.assertTrue(isinstance(retina_model, nn.Module)) result = retinanet.val_sanity_fit(retina_model, val_loader, "cpu", num_batches=10) self.assertTrue(result) @@ -183,6 +190,7 @@ def test_sanity_fit(self): backbone = retinanet.create_retinanet_backbone(backbone=bbone, pretrained=None) self.assertTrue(isinstance(backbone, nn.Module)) retina_model = retinanet.create_vision_retinanet(num_classes=3, backbone=backbone) + retina_model = retina_model.cpu() self.assertTrue(isinstance(retina_model, nn.Module)) result = retinanet.sanity_fit(retina_model, train_loader, val_loader, "cpu", num_batches=10) self.assertTrue(result) @@ -209,7 +217,7 @@ def test_lit_retinanet_fpn(self): self.assertTrue(flag) @unittest.skipIf(not torch.cuda.is_available(), "CUDA unavailable") - def test_lit_cnn_cuda(self): + def test_lit_retina_cuda(self): flag = False for bbone in fpn_supported_models: model = retinanet.lit_retinanet(num_classes=3, backbone=bbone, fpn=True, pretrained_backbone=False,)