5
5
"""
6
6
7
7
######################################################################
8
+ #
9
+ # .. tip::
10
+ #
11
+ # To get the most of this tutorial, we suggest using this
12
+ # `Colab Version <https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads/torchvision_finetuning_instance_segmentation.ipynb>`__.
13
+ # This will allow you to experiment with the information presented below.
14
+ #
15
+ #
8
16
# For this tutorial, we will be finetuning a pre-trained `Mask
9
17
# R-CNN <https://arxiv.org/abs/1703.06870>`__ model on the `Penn-Fudan
10
18
# Database for Pedestrian Detection and
17
25
# .. note ::
18
26
#
19
27
# This tutorial works only with torchvision version >=0.16 or nightly.
28
+ # If you're using torchvision<=0.15, please follow
29
+ # `this tutorial instead <https://github.com/pytorch/tutorials/blob/d686b662932a380a58b7683425faa00c06bcf502/intermediate_source/torchvision_tutorial.rst>`_.
20
30
#
21
31
#
22
32
# Defining the Dataset
@@ -252,8 +262,10 @@ def __len__(self):
252
262
# ratios. We have a Tuple[Tuple[int]] because each feature
253
263
# map could potentially have different sizes and
254
264
# aspect ratios
255
- anchor_generator = AnchorGenerator (sizes = ((32 , 64 , 128 , 256 , 512 ),),
256
- aspect_ratios = ((0.5 , 1.0 , 2.0 ),))
265
+ anchor_generator = AnchorGenerator (
266
+ sizes = ((32 , 64 , 128 , 256 , 512 ),),
267
+ aspect_ratios = ((0.5 , 1.0 , 2.0 ),)
268
+ )
257
269
258
270
# let's define what are the feature maps that we will
259
271
# use to perform the region of interest cropping, as well as
@@ -262,15 +274,19 @@ def __len__(self):
262
274
# be [0]. More generally, the backbone should return an
263
275
# ``OrderedDict[Tensor]``, and in ``featmap_names`` you can choose which
264
276
# feature maps to use.
265
- roi_pooler = torchvision .ops .MultiScaleRoIAlign (featmap_names = ['0' ],
266
- output_size = 7 ,
267
- sampling_ratio = 2 )
277
+ roi_pooler = torchvision .ops .MultiScaleRoIAlign (
278
+ featmap_names = ['0' ],
279
+ output_size = 7 ,
280
+ sampling_ratio = 2
281
+ )
268
282
269
283
# put the pieces together inside a Faster-RCNN model
270
- model = FasterRCNN (backbone ,
271
- num_classes = 2 ,
272
- rpn_anchor_generator = anchor_generator ,
273
- box_roi_pool = roi_pooler )
284
+ model = FasterRCNN (
285
+ backbone ,
286
+ num_classes = 2 ,
287
+ rpn_anchor_generator = anchor_generator ,
288
+ box_roi_pool = roi_pooler
289
+ )
274
290
275
291
######################################################################
276
292
# Object detection and instance segmentation model for PennFudan Dataset
@@ -301,9 +317,11 @@ def get_model_instance_segmentation(num_classes):
301
317
in_features_mask = model .roi_heads .mask_predictor .conv5_mask .in_channels
302
318
hidden_layer = 256
303
319
# and replace the mask predictor with a new one
304
- model .roi_heads .mask_predictor = MaskRCNNPredictor (in_features_mask ,
305
- hidden_layer ,
306
- num_classes )
320
+ model .roi_heads .mask_predictor = MaskRCNNPredictor (
321
+ in_features_mask ,
322
+ hidden_layer ,
323
+ num_classes
324
+ )
307
325
308
326
return model
309
327
@@ -477,6 +495,7 @@ def get_transform(train):
477
495
predictions = model ([x , ])
478
496
pred = predictions [0 ]
479
497
498
+
480
499
image = (255.0 * (image - image .min ()) / (image .max () - image .min ())).to (torch .uint8 )
481
500
image = image [:3 , ...]
482
501
pred_labels = [f"pedestrian: { score :.3f} " for label , score in zip (pred ["labels" ], pred ["scores" ])]
@@ -486,7 +505,8 @@ def get_transform(train):
486
505
masks = (pred ["masks" ] > 0.7 ).squeeze (1 )
487
506
output_image = draw_segmentation_masks (output_image , masks , alpha = 0.5 , colors = "blue" )
488
507
489
- plt .figure ()
508
+
509
+ plt .figure (figsize = (12 , 12 ))
490
510
plt .imshow (output_image .permute (1 , 2 , 0 ))
491
511
492
512
######################################################################
@@ -506,3 +526,5 @@ def get_transform(train):
506
526
# training, check ``references/detection/train.py``, which is present in
507
527
# the torchvision repository.
508
528
#
529
+ # You can download a full source file for this tutorial
530
+ # `here <https://pytorch.org/tutorials/_static/tv-training-code.py>`__.
0 commit comments