Skip to content

Commit

Permalink
Fixes requirements to be installable, fixes CUDA Tests (#88)
Browse files Browse the repository at this point in the history
* fixes requirements to be installable

* fixes CI and req

* retry install

* explicit to cpu
  • Loading branch information
oke-aditya authored Dec 11, 2020
1 parent d098527 commit 395b205
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 3 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
torch==1.7.1
torchvision==0.8.2
pytorch_lightning==1.1.0
scipy==1.5.4
9 changes: 9 additions & 0 deletions tests/test_cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -203,13 +210,15 @@ 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)

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)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_detr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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}
Expand All @@ -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}
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand All @@ -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}
Expand Down
9 changes: 9 additions & 0 deletions tests/test_frcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion tests/test_retinanet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,)
Expand Down

0 comments on commit 395b205

Please sign in to comment.