From 60443862d54945db4a23f513bed241cbccdd0e6f Mon Sep 17 00:00:00 2001 From: prajjwal1 Date: Fri, 20 Dec 2019 18:17:06 +0530 Subject: [PATCH 1/4] added sample code for fasterrcnn_resnet50_fpn (optional) --- intermediate_source/torchvision_tutorial.rst | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/intermediate_source/torchvision_tutorial.rst b/intermediate_source/torchvision_tutorial.rst index 7e700f9bd1b..f171705469d 100644 --- a/intermediate_source/torchvision_tutorial.rst +++ b/intermediate_source/torchvision_tutorial.rst @@ -304,6 +304,29 @@ be using Mask R-CNN: That’s it, this will make ``model`` be ready to be trained and evaluated on your custom dataset. +Checking the model with random tensors (Optional) +--------------------------- + +Before iterating over the dataset, it's always good to see what the model +expects during training and inference time with random tensors. + +.. code:: python + model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True) + images,boxes,labels = torch.rand(4,3,600,1200), torch.rand(4,11,4), torch.rand(4,11) # For Training + images = list(image for image in images) # This is handled by GeneralizedRCNNTransform + targets = [] + for i in range(len(images)): + d = {} + d['boxes'] = boxes[i] + d['labels'] = labels[i].type(torch.int64) + targets.append(d) + output = model(images,targets) + + model.eval() # For inference + x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)] + predictions = model(x) + + Putting everything together --------------------------- From 676c17d8ef2b21f022dbd0a61054bd05710c6891 Mon Sep 17 00:00:00 2001 From: prajjwal1 Date: Fri, 20 Dec 2019 19:06:55 +0530 Subject: [PATCH 2/4] added sample code for fasterrcnn_resnet50_fpn (optional) --- intermediate_source/torchvision_tutorial.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/intermediate_source/torchvision_tutorial.rst b/intermediate_source/torchvision_tutorial.rst index f171705469d..20cdaa9fc7a 100644 --- a/intermediate_source/torchvision_tutorial.rst +++ b/intermediate_source/torchvision_tutorial.rst @@ -311,6 +311,7 @@ Before iterating over the dataset, it's always good to see what the model expects during training and inference time with random tensors. .. code:: python + model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True) images,boxes,labels = torch.rand(4,3,600,1200), torch.rand(4,11,4), torch.rand(4,11) # For Training images = list(image for image in images) # This is handled by GeneralizedRCNNTransform @@ -320,11 +321,11 @@ expects during training and inference time with random tensors. d['boxes'] = boxes[i] d['labels'] = labels[i].type(torch.int64) targets.append(d) - output = model(images,targets) + output = model(images,targets) # Returns losses and detections model.eval() # For inference x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)] - predictions = model(x) + predictions = model(x) # Returns predictions Putting everything together From 88b6d4531e6c49cd0bbf26b65662768a3e0f75b8 Mon Sep 17 00:00:00 2001 From: prajjwal1 Date: Fri, 20 Dec 2019 19:19:05 +0530 Subject: [PATCH 3/4] removed comment --- intermediate_source/torchvision_tutorial.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/intermediate_source/torchvision_tutorial.rst b/intermediate_source/torchvision_tutorial.rst index 20cdaa9fc7a..2629fde1255 100644 --- a/intermediate_source/torchvision_tutorial.rst +++ b/intermediate_source/torchvision_tutorial.rst @@ -314,7 +314,7 @@ expects during training and inference time with random tensors. model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True) images,boxes,labels = torch.rand(4,3,600,1200), torch.rand(4,11,4), torch.rand(4,11) # For Training - images = list(image for image in images) # This is handled by GeneralizedRCNNTransform + images = list(image for image in images) targets = [] for i in range(len(images)): d = {} From d5df20a5bc0806f4c27c254236736ce75e61f9fc Mon Sep 17 00:00:00 2001 From: prajjwal1 Date: Sat, 4 Jan 2020 11:40:39 +0530 Subject: [PATCH 4/4] changed heading and removed random init data in favor of sample data from dataset --- intermediate_source/torchvision_tutorial.rst | 48 ++++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/intermediate_source/torchvision_tutorial.rst b/intermediate_source/torchvision_tutorial.rst index 2629fde1255..c82b8097e93 100644 --- a/intermediate_source/torchvision_tutorial.rst +++ b/intermediate_source/torchvision_tutorial.rst @@ -304,30 +304,6 @@ be using Mask R-CNN: That’s it, this will make ``model`` be ready to be trained and evaluated on your custom dataset. -Checking the model with random tensors (Optional) ---------------------------- - -Before iterating over the dataset, it's always good to see what the model -expects during training and inference time with random tensors. - -.. code:: python - - model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True) - images,boxes,labels = torch.rand(4,3,600,1200), torch.rand(4,11,4), torch.rand(4,11) # For Training - images = list(image for image in images) - targets = [] - for i in range(len(images)): - d = {} - d['boxes'] = boxes[i] - d['labels'] = labels[i].type(torch.int64) - targets.append(d) - output = model(images,targets) # Returns losses and detections - - model.eval() # For inference - x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)] - predictions = model(x) # Returns predictions - - Putting everything together --------------------------- @@ -351,6 +327,30 @@ transformation: transforms.append(T.RandomHorizontalFlip(0.5)) return T.Compose(transforms) + +Testing ``forward()`` method (Optional) +--------------------------------------- + +Before iterating over the dataset, it's good to see what the model +expects during training and inference time on sample data. + +.. code:: python + + model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True) + dataset = PennFudanDataset('PennFudanPed', get_transform(train=True)) + data_loader = torch.utils.data.DataLoader( + dataset, batch_size=2, shuffle=True, num_workers=4, + collate_fn=utils.collate_fn) + # For Training + images,targets = next(iter(data_loader)) + images = list(image for image in images) + targets = [{k: v for k, v in t.items()} for t in targets] + output = model(images,targets) # Returns losses and detections + # For inference + model.eval() + x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)] + predictions = model(x) # Returns predictions + Let’s now write the main function which performs the training and the validation: