importwarningsfromcollectionsimportOrderedDictfromtypingimportAny,Dict,List,Optional,Tupleimporttorchimporttorch.nn.functionalasFfromtorchimportnn,Tensorfrom..._internally_replaced_utilsimportload_state_dict_from_urlfrom...opsimportboxesasbox_opsfrom...utilsimport_log_api_usage_oncefrom..importvggfrom.import_utilsasdet_utilsfrom.anchor_utilsimportDefaultBoxGeneratorfrom.backbone_utilsimport_validate_trainable_layersfrom.transformimportGeneralizedRCNNTransform__all__=["SSD","ssd300_vgg16"]model_urls={"ssd300_vgg16_coco":"https://download.pytorch.org/models/ssd300_vgg16_coco-b556d3b4.pth",}backbone_urls={# We port the features of a VGG16 backbone trained by amdegroot because unlike the one on TorchVision, it uses the# same input standardization method as the paper. Ref: https://s3.amazonaws.com/amdegroot-models/vgg16_reducedfc.pth# Only the `features` weights have proper values, those on the `classifier` module are filled with nans."vgg16_features":"https://download.pytorch.org/models/vgg16_features-amdegroot-88682ab5.pth"}def_xavier_init(conv:nn.Module):forlayerinconv.modules():ifisinstance(layer,nn.Conv2d):torch.nn.init.xavier_uniform_(layer.weight)iflayer.biasisnotNone:torch.nn.init.constant_(layer.bias,0.0)classSSDHead(nn.Module):def__init__(self,in_channels:List[int],num_anchors:List[int],num_classes:int):super().__init__()self.classification_head=SSDClassificationHead(in_channels,num_anchors,num_classes)self.regression_head=SSDRegressionHead(in_channels,num_anchors)defforward(self,x:List[Tensor])->Dict[str,Tensor]:return{"bbox_regression":self.regression_head(x),"cls_logits":self.classification_head(x),}classSSDScoringHead(nn.Module):def__init__(self,module_list:nn.ModuleList,num_columns:int):super().__init__()self.module_list=module_listself.num_columns=num_columnsdef_get_result_from_module_list(self,x:Tensor,idx:int)->Tensor:""" This is equivalent to self.module_list[idx](x), but torchscript doesn't support this yet """num_blocks=len(self.module_list)ifidx<0:idx+=num_blocksout=xfori,moduleinenumerate(self.module_list):ifi==idx:out=module(x)returnoutdefforward(self,x:List[Tensor])->Tensor:all_results=[]fori,featuresinenumerate(x):results=self._get_result_from_module_list(features,i)# Permute output from (N, A * K, H, W) to (N, HWA, K).N,_,H,W=results.shaperesults=results.view(N,-1,self.num_columns,H,W)results=results.permute(0,3,4,1,2)results=results.reshape(N,-1,self.num_columns)# Size=(N, HWA, K)all_results.append(results)returntorch.cat(all_results,dim=1)classSSDClassificationHead(SSDScoringHead):def__init__(self,in_channels:List[int],num_anchors:List[int],num_classes:int):cls_logits=nn.ModuleList()forchannels,anchorsinzip(in_channels,num_anchors):cls_logits.append(nn.Conv2d(channels,num_classes*anchors,kernel_size=3,padding=1))_xavier_init(cls_logits)super().__init__(cls_logits,num_classes)classSSDRegressionHead(SSDScoringHead):def__init__(self,in_channels:List[int],num_anchors:List[int]):bbox_reg=nn.ModuleList()forchannels,anchorsinzip(in_channels,num_anchors):bbox_reg.append(nn.Conv2d(channels,4*anchors,kernel_size=3,padding=1))_xavier_init(bbox_reg)super().__init__(bbox_reg,4)classSSD(nn.Module):""" Implements SSD architecture from `"SSD: Single Shot MultiBox Detector" <https://arxiv.org/abs/1512.02325>`_. The input to the model is expected to be a list of tensors, each of shape [C, H, W], one for each image, and should be in 0-1 range. Different images can have different sizes but they will be resized to a fixed size before passing it to the backbone. The behavior of the model changes depending if it is in training or evaluation mode. During training, the model expects both the input tensors, as well as a targets (list of dictionary), containing: - boxes (``FloatTensor[N, 4]``): the ground-truth boxes in ``[x1, y1, x2, y2]`` format, with ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``. - labels (Int64Tensor[N]): the class label for each ground-truth box The model returns a Dict[Tensor] during training, containing the classification and regression losses. During inference, the model requires only the input tensors, and returns the post-processed predictions as a List[Dict[Tensor]], one for each input image. The fields of the Dict are as follows, where ``N`` is the number of detections: - boxes (``FloatTensor[N, 4]``): the predicted boxes in ``[x1, y1, x2, y2]`` format, with ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``. - labels (Int64Tensor[N]): the predicted labels for each detection - scores (Tensor[N]): the scores for each detection Args: backbone (nn.Module): the network used to compute the features for the model. It should contain an out_channels attribute with the list of the output channels of each feature map. The backbone should return a single Tensor or an OrderedDict[Tensor]. anchor_generator (DefaultBoxGenerator): module that generates the default boxes for a set of feature maps. size (Tuple[int, int]): the width and height to which images will be rescaled before feeding them to the backbone. num_classes (int): number of output classes of the model (including the background). image_mean (Tuple[float, float, float]): mean values used for input normalization. They are generally the mean values of the dataset on which the backbone has been trained on image_std (Tuple[float, float, float]): std values used for input normalization. They are generally the std values of the dataset on which the backbone has been trained on head (nn.Module, optional): Module run on top of the backbone features. Defaults to a module containing a classification and regression module. score_thresh (float): Score threshold used for postprocessing the detections. nms_thresh (float): NMS threshold used for postprocessing the detections. detections_per_img (int): Number of best detections to keep after NMS. iou_thresh (float): minimum IoU between the anchor and the GT box so that they can be considered as positive during training. topk_candidates (int): Number of best detections to keep before NMS. positive_fraction (float): a number between 0 and 1 which indicates the proportion of positive proposals used during the training of the classification head. It is used to estimate the negative to positive ratio. """__annotations__={"box_coder":det_utils.BoxCoder,"proposal_matcher":det_utils.Matcher,}def__init__(self,backbone:nn.Module,anchor_generator:DefaultBoxGenerator,size:Tuple[int,int],num_classes:int,image_mean:Optional[List[float]]=None,image_std:Optional[List[float]]=None,head:Optional[nn.Module]=None,score_thresh:float=0.01,nms_thresh:float=0.45,detections_per_img:int=200,iou_thresh:float=0.5,topk_candidates:int=400,positive_fraction:float=0.25,):super().__init__()_log_api_usage_once(self)self.backbone=backboneself.anchor_generator=anchor_generatorself.box_coder=det_utils.BoxCoder(weights=(10.0,10.0,5.0,5.0))ifheadisNone:ifhasattr(backbone,"out_channels"):out_channels=backbone.out_channelselse:out_channels=det_utils.retrieve_out_channels(backbone,size)assertlen(out_channels)==len(anchor_generator.aspect_ratios)num_anchors=self.anchor_generator.num_anchors_per_location()head=SSDHead(out_channels,num_anchors,num_classes)self.head=headself.proposal_matcher=det_utils.SSDMatcher(iou_thresh)ifimage_meanisNone:image_mean=[0.485,0.456,0.406]ifimage_stdisNone:image_std=[0.229,0.224,0.225]self.transform=GeneralizedRCNNTransform(min(size),max(size),image_mean,image_std,size_divisible=1,fixed_size=size)self.score_thresh=score_threshself.nms_thresh=nms_threshself.detections_per_img=detections_per_imgself.topk_candidates=topk_candidatesself.neg_to_pos_ratio=(1.0-positive_fraction)/positive_fraction# used only on torchscript modeself._has_warned=False@torch.jit.unuseddefeager_outputs(self,losses:Dict[str,Tensor],detections:List[Dict[str,Tensor]])->Tuple[Dict[str,Tensor],List[Dict[str,Tensor]]]:ifself.training:returnlossesreturndetectionsdefcompute_loss(self,targets:List[Dict[str,Tensor]],head_outputs:Dict[str,Tensor],anchors:List[Tensor],matched_idxs:List[Tensor],)->Dict[str,Tensor]:bbox_regression=head_outputs["bbox_regression"]cls_logits=head_outputs["cls_logits"]# Match original targets with default boxesnum_foreground=0bbox_loss=[]cls_targets=[]for(targets_per_image,bbox_regression_per_image,cls_logits_per_image,anchors_per_image,matched_idxs_per_image,)inzip(targets,bbox_regression,cls_logits,anchors,matched_idxs):# produce the matching between boxes and targetsforeground_idxs_per_image=torch.where(matched_idxs_per_image>=0)[0]foreground_matched_idxs_per_image=matched_idxs_per_image[foreground_idxs_per_image]num_foreground+=foreground_matched_idxs_per_image.numel()# Calculate regression lossmatched_gt_boxes_per_image=targets_per_image["boxes"][foreground_matched_idxs_per_image]bbox_regression_per_image=bbox_regression_per_image[foreground_idxs_per_image,:]anchors_per_image=anchors_per_image[foreground_idxs_per_image,:]target_regression=self.box_coder.encode_single(matched_gt_boxes_per_image,anchors_per_image)bbox_loss.append(torch.nn.functional.smooth_l1_loss(bbox_regression_per_image,target_regression,reduction="sum"))# Estimate ground truth for class targetsgt_classes_target=torch.zeros((cls_logits_per_image.size(0),),dtype=targets_per_image["labels"].dtype,device=targets_per_image["labels"].device,)gt_classes_target[foreground_idxs_per_image]=targets_per_image["labels"][foreground_matched_idxs_per_image]cls_targets.append(gt_classes_target)bbox_loss=torch.stack(bbox_loss)cls_targets=torch.stack(cls_targets)# Calculate classification lossnum_classes=cls_logits.size(-1)cls_loss=F.cross_entropy(cls_logits.view(-1,num_classes),cls_targets.view(-1),reduction="none").view(cls_targets.size())# Hard Negative Samplingforeground_idxs=cls_targets>0num_negative=self.neg_to_pos_ratio*foreground_idxs.sum(1,keepdim=True)# num_negative[num_negative < self.neg_to_pos_ratio] = self.neg_to_pos_rationegative_loss=cls_loss.clone()negative_loss[foreground_idxs]=-float("inf")# use -inf to detect positive values that creeped in the samplevalues,idx=negative_loss.sort(1,descending=True)# background_idxs = torch.logical_and(idx.sort(1)[1] < num_negative, torch.isfinite(values))background_idxs=idx.sort(1)[1]<num_negativeN=max(1,num_foreground)return{"bbox_regression":bbox_loss.sum()/N,"classification":(cls_loss[foreground_idxs].sum()+cls_loss[background_idxs].sum())/N,}defforward(self,images:List[Tensor],targets:Optional[List[Dict[str,Tensor]]]=None)->Tuple[Dict[str,Tensor],List[Dict[str,Tensor]]]:ifself.trainingandtargetsisNone:raiseValueError("In training mode, targets should be passed")ifself.training:asserttargetsisnotNonefortargetintargets:boxes=target["boxes"]ifisinstance(boxes,torch.Tensor):iflen(boxes.shape)!=2orboxes.shape[-1]!=4:raiseValueError(f"Expected target boxes to be a tensor of shape [N, 4], got {boxes.shape}.")else:raiseValueError(f"Expected target boxes to be of type Tensor, got {type(boxes)}.")# get the original image sizesoriginal_image_sizes:List[Tuple[int,int]]=[]forimginimages:val=img.shape[-2:]assertlen(val)==2original_image_sizes.append((val[0],val[1]))# transform the inputimages,targets=self.transform(images,targets)# Check for degenerate boxesiftargetsisnotNone:fortarget_idx,targetinenumerate(targets):boxes=target["boxes"]degenerate_boxes=boxes[:,2:]<=boxes[:,:2]ifdegenerate_boxes.any():bb_idx=torch.where(degenerate_boxes.any(dim=1))[0][0]degen_bb:List[float]=boxes[bb_idx].tolist()raiseValueError("All bounding boxes should have positive height and width."f" Found invalid box {degen_bb} for target at index {target_idx}.")# get the features from the backbonefeatures=self.backbone(images.tensors)ifisinstance(features,torch.Tensor):features=OrderedDict([("0",features)])features=list(features.values())# compute the ssd heads outputs using the featureshead_outputs=self.head(features)# create the set of anchorsanchors=self.anchor_generator(images,features)losses={}detections:List[Dict[str,Tensor]]=[]ifself.training:asserttargetsisnotNonematched_idxs=[]foranchors_per_image,targets_per_imageinzip(anchors,targets):iftargets_per_image["boxes"].numel()==0:matched_idxs.append(torch.full((anchors_per_image.size(0),),-1,dtype=torch.int64,device=anchors_per_image.device))continuematch_quality_matrix=box_ops.box_iou(targets_per_image["boxes"],anchors_per_image)matched_idxs.append(self.proposal_matcher(match_quality_matrix))losses=self.compute_loss(targets,head_outputs,anchors,matched_idxs)else:detections=self.postprocess_detections(head_outputs,anchors,images.image_sizes)detections=self.transform.postprocess(detections,images.image_sizes,original_image_sizes)iftorch.jit.is_scripting():ifnotself._has_warned:warnings.warn("SSD always returns a (Losses, Detections) tuple in scripting")self._has_warned=Truereturnlosses,detectionsreturnself.eager_outputs(losses,detections)defpostprocess_detections(self,head_outputs:Dict[str,Tensor],image_anchors:List[Tensor],image_shapes:List[Tuple[int,int]])->List[Dict[str,Tensor]]:bbox_regression=head_outputs["bbox_regression"]pred_scores=F.softmax(head_outputs["cls_logits"],dim=-1)num_classes=pred_scores.size(-1)device=pred_scores.devicedetections:List[Dict[str,Tensor]]=[]forboxes,scores,anchors,image_shapeinzip(bbox_regression,pred_scores,image_anchors,image_shapes):boxes=self.box_coder.decode_single(boxes,anchors)boxes=box_ops.clip_boxes_to_image(boxes,image_shape)image_boxes=[]image_scores=[]image_labels=[]forlabelinrange(1,num_classes):score=scores[:,label]keep_idxs=score>self.score_threshscore=score[keep_idxs]box=boxes[keep_idxs]# keep only topk scoring predictionsnum_topk=min(self.topk_candidates,score.size(0))score,idxs=score.topk(num_topk)box=box[idxs]image_boxes.append(box)image_scores.append(score)image_labels.append(torch.full_like(score,fill_value=label,dtype=torch.int64,device=device))image_boxes=torch.cat(image_boxes,dim=0)image_scores=torch.cat(image_scores,dim=0)image_labels=torch.cat(image_labels,dim=0)# non-maximum suppressionkeep=box_ops.batched_nms(image_boxes,image_scores,image_labels,self.nms_thresh)keep=keep[:self.detections_per_img]detections.append({"boxes":image_boxes[keep],"scores":image_scores[keep],"labels":image_labels[keep],})returndetectionsclassSSDFeatureExtractorVGG(nn.Module):def__init__(self,backbone:nn.Module,highres:bool):super().__init__()_,_,maxpool3_pos,maxpool4_pos,_=(ifori,layerinenumerate(backbone)ifisinstance(layer,nn.MaxPool2d))# Patch ceil_mode for maxpool3 to get the same WxH output sizes as the paperbackbone[maxpool3_pos].ceil_mode=True# parameters used for L2 regularization + rescalingself.scale_weight=nn.Parameter(torch.ones(512)*20)# Multiple Feature maps - page 4, Fig 2 of SSD paperself.features=nn.Sequential(*backbone[:maxpool4_pos])# until conv4_3# SSD300 case - page 4, Fig 2 of SSD paperextra=nn.ModuleList([nn.Sequential(nn.Conv2d(1024,256,kernel_size=1),nn.ReLU(inplace=True),nn.Conv2d(256,512,kernel_size=3,padding=1,stride=2),# conv8_2nn.ReLU(inplace=True),),nn.Sequential(nn.Conv2d(512,128,kernel_size=1),nn.ReLU(inplace=True),nn.Conv2d(128,256,kernel_size=3,padding=1,stride=2),# conv9_2nn.ReLU(inplace=True),),nn.Sequential(nn.Conv2d(256,128,kernel_size=1),nn.ReLU(inplace=True),nn.Conv2d(128,256,kernel_size=3),# conv10_2nn.ReLU(inplace=True),),nn.Sequential(nn.Conv2d(256,128,kernel_size=1),nn.ReLU(inplace=True),nn.Conv2d(128,256,kernel_size=3),# conv11_2nn.ReLU(inplace=True),),])ifhighres:# Additional layers for the SSD512 case. See page 11, footernote 5.extra.append(nn.Sequential(nn.Conv2d(256,128,kernel_size=1),nn.ReLU(inplace=True),nn.Conv2d(128,256,kernel_size=4),# conv12_2nn.ReLU(inplace=True),))_xavier_init(extra)fc=nn.Sequential(nn.MaxPool2d(kernel_size=3,stride=1,padding=1,ceil_mode=False),# add modified maxpool5nn.Conv2d(in_channels=512,out_channels=1024,kernel_size=3,padding=6,dilation=6),# FC6 with atrousnn.ReLU(inplace=True),nn.Conv2d(in_channels=1024,out_channels=1024,kernel_size=1),# FC7nn.ReLU(inplace=True),)_xavier_init(fc)extra.insert(0,nn.Sequential(*backbone[maxpool4_pos:-1],# until conv5_3, skip maxpool5fc,),)self.extra=extradefforward(self,x:Tensor)->Dict[str,Tensor]:# L2 regularization + Rescaling of 1st block's feature mapx=self.features(x)rescaled=self.scale_weight.view(1,-1,1,1)*F.normalize(x)output=[rescaled]# Calculating Feature maps for the rest blocksforblockinself.extra:x=block(x)output.append(x)returnOrderedDict([(str(i),v)fori,vinenumerate(output)])def_vgg_extractor(backbone:vgg.VGG,highres:bool,trainable_layers:int):backbone=backbone.features# Gather the indices of maxpools. These are the locations of output blocks.stage_indices=[0]+[ifori,binenumerate(backbone)ifisinstance(b,nn.MaxPool2d)][:-1]num_stages=len(stage_indices)# find the index of the layer from which we wont freezeassert0<=trainable_layers<=num_stagesfreeze_before=len(backbone)iftrainable_layers==0elsestage_indices[num_stages-trainable_layers]forbinbackbone[:freeze_before]:forparameterinb.parameters():parameter.requires_grad_(False)returnSSDFeatureExtractorVGG(backbone,highres)
[docs]defssd300_vgg16(pretrained:bool=False,progress:bool=True,num_classes:int=91,pretrained_backbone:bool=True,trainable_backbone_layers:Optional[int]=None,**kwargs:Any,):"""Constructs an SSD model with input size 300x300 and a VGG16 backbone. Reference: `"SSD: Single Shot MultiBox Detector" <https://arxiv.org/abs/1512.02325>`_. The input to the model is expected to be a list of tensors, each of shape [C, H, W], one for each image, and should be in 0-1 range. Different images can have different sizes but they will be resized to a fixed size before passing it to the backbone. The behavior of the model changes depending if it is in training or evaluation mode. During training, the model expects both the input tensors, as well as a targets (list of dictionary), containing: - boxes (``FloatTensor[N, 4]``): the ground-truth boxes in ``[x1, y1, x2, y2]`` format, with ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``. - labels (Int64Tensor[N]): the class label for each ground-truth box The model returns a Dict[Tensor] during training, containing the classification and regression losses. During inference, the model requires only the input tensors, and returns the post-processed predictions as a List[Dict[Tensor]], one for each input image. The fields of the Dict are as follows, where ``N`` is the number of detections: - boxes (``FloatTensor[N, 4]``): the predicted boxes in ``[x1, y1, x2, y2]`` format, with ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``. - labels (Int64Tensor[N]): the predicted labels for each detection - scores (Tensor[N]): the scores for each detection Example: >>> model = torchvision.models.detection.ssd300_vgg16(pretrained=True) >>> model.eval() >>> x = [torch.rand(3, 300, 300), torch.rand(3, 500, 400)] >>> predictions = model(x) Args: pretrained (bool): If True, returns a model pre-trained on COCO train2017 progress (bool): If True, displays a progress bar of the download to stderr num_classes (int): number of output classes of the model (including the background) pretrained_backbone (bool): If True, returns a model with backbone pre-trained on Imagenet trainable_backbone_layers (int): number of trainable (not frozen) resnet layers starting from final block. Valid values are between 0 and 5, with 5 meaning all backbone layers are trainable. If ``None`` is passed (the default) this value is set to 4. """if"size"inkwargs:warnings.warn("The size of the model is already fixed; ignoring the argument.")trainable_backbone_layers=_validate_trainable_layers(pretrainedorpretrained_backbone,trainable_backbone_layers,5,4)ifpretrained:# no need to download the backbone if pretrained is setpretrained_backbone=False# Use custom backbones more appropriate for SSDbackbone=vgg.vgg16(pretrained=False,progress=progress)ifpretrained_backbone:state_dict=load_state_dict_from_url(backbone_urls["vgg16_features"],progress=progress)backbone.load_state_dict(state_dict)backbone=_vgg_extractor(backbone,False,trainable_backbone_layers)anchor_generator=DefaultBoxGenerator([[2],[2,3],[2,3],[2,3],[2],[2]],scales=[0.07,0.15,0.33,0.51,0.69,0.87,1.05],steps=[8,16,32,64,100,300],)defaults={# Rescale the input in a way compatible to the backbone"image_mean":[0.48235,0.45882,0.40784],"image_std":[1.0/255.0,1.0/255.0,1.0/255.0],# undo the 0-1 scaling of toTensor}kwargs={**defaults,**kwargs}model=SSD(backbone,anchor_generator,(300,300),num_classes,**kwargs)ifpretrained:weights_name="ssd300_vgg16_coco"ifmodel_urls.get(weights_name,None)isNone:raiseValueError(f"No checkpoint is available for model {weights_name}")state_dict=load_state_dict_from_url(model_urls[weights_name],progress=progress)model.load_state_dict(state_dict)returnmodel
Docs
Access comprehensive developer documentation for PyTorch
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.