本文整理汇总了Python中maskrcnn_benchmark.structures.image_list.to_image_list方法的典型用法代码示例。如果您正苦于以下问题:Python image_list.to_image_list方法的具体用法?Python image_list.to_image_list怎么用?Python image_list.to_image_list使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类maskrcnn_benchmark.structures.image_list
的用法示例。
在下文中一共展示了image_list.to_image_list方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: im_detect_bbox_hflip
# 需要导入模块: from maskrcnn_benchmark.structures import image_list [as 别名]
# 或者: from maskrcnn_benchmark.structures.image_list import to_image_list [as 别名]
def im_detect_bbox_hflip(model, images, target_scale, target_max_size, device):
"""
Performs bbox detection on the horizontally flipped image.
Function signature is the same as for im_detect_bbox.
"""
transform = TT.Compose([
T.Resize(target_scale, target_max_size),
TT.RandomHorizontalFlip(1.0),
TT.ToTensor(),
T.Normalize(
mean=cfg.INPUT.PIXEL_MEAN, std=cfg.INPUT.PIXEL_STD, to_bgr255=cfg.INPUT.TO_BGR255
)
])
images = [transform(image) for image in images]
images = to_image_list(images, cfg.DATALOADER.SIZE_DIVISIBILITY)
boxlists = model(images.to(device))
# Invert the detections computed on the flipped image
boxlists_inv = [boxlist.transpose(0) for boxlist in boxlists]
return boxlists_inv
示例2: forward
# 需要导入模块: from maskrcnn_benchmark.structures import image_list [as 别名]
# 或者: from maskrcnn_benchmark.structures.image_list import to_image_list [as 别名]
def forward(self, images, targets=None):
"""
Arguments:
images (list[Tensor] or ImageList): images to be processed
targets (list[BoxList]): ground-truth boxes present in the image (optional)
"""
if self.training and targets is None:
raise ValueError("In training mode, targets should be passed")
images = to_image_list(images)
features = self.backbone(images.tensors)
proposals, proposal_losses = self.rpn(images, features, targets)
if self.cfg.MODEL.RPN_ONLY:
x = features
result = proposals
detector_losses = {}
else:
x, result, detector_losses = self.roi_heads(features, proposals, targets)
if self.training:
losses = {}
losses.update(detector_losses)
losses.update(proposal_losses)
return losses
return result
示例3: forward
# 需要导入模块: from maskrcnn_benchmark.structures import image_list [as 别名]
# 或者: from maskrcnn_benchmark.structures.image_list import to_image_list [as 别名]
def forward(self, images, targets=None, features=None):
"""
Arguments:
images (list[Tensor] or ImageList): images to be processed
targets (list[BoxList]): ground-truth boxes present in the image (optional)
features (list[Tensor]): encoder output features (optional)
Returns:
result (list[BoxList] or dict[Tensor]): the output from the model.
During training, it returns a dict[Tensor] which contains the losses.
During testing, it returns list[BoxList] contains additional fields
like `scores`, `labels` and `mask` (for Mask R-CNN models).
"""
if self.training and targets is None:
raise ValueError("In training mode, targets should be passed")
if features is None:
images = to_image_list(images)
features = self.encoder(images.tensors)
return self.decoder(images, features, targets)
示例4: __call__
# 需要导入模块: from maskrcnn_benchmark.structures import image_list [as 别名]
# 或者: from maskrcnn_benchmark.structures.image_list import to_image_list [as 别名]
def __call__(self, batch):
transposed_batch = list(zip(*batch))
images = to_image_list(transposed_batch[0], self.size_divisible)
targets = transposed_batch[1]
img_ids = transposed_batch[2]
return images, targets, img_ids
示例5: forward
# 需要导入模块: from maskrcnn_benchmark.structures import image_list [as 别名]
# 或者: from maskrcnn_benchmark.structures.image_list import to_image_list [as 别名]
def forward(self, images, targets=None):
"""
Arguments:
images (list[Tensor] or ImageList): images to be processed
targets (list[BoxList]): ground-truth boxes present in the image (optional)
Returns:
result (list[BoxList] or dict[Tensor]): the output from the model.
During training, it returns a dict[Tensor] which contains the losses.
During testing, it returns list[BoxList] contains additional fields
like `scores`, `labels` and `mask` (for Mask R-CNN models).
"""
if self.training and targets is None:
raise ValueError("In training mode, targets should be passed")
images = to_image_list(images)
features = self.backbone(images.tensors)
proposals, proposal_losses = self.rpn(images, features, targets)
if self.roi_heads:
x, result, detector_losses = self.roi_heads(features, proposals, targets)
else:
# RPN-only models don't have roi_heads
x = features
result = proposals
detector_losses = {}
if self.training:
losses = {}
losses.update(detector_losses)
losses.update(proposal_losses)
return losses
return result
示例6: compute_prediction
# 需要导入模块: from maskrcnn_benchmark.structures import image_list [as 别名]
# 或者: from maskrcnn_benchmark.structures.image_list import to_image_list [as 别名]
def compute_prediction(self, original_image):
"""
Arguments:
original_image (np.ndarray): an image as returned by OpenCV
Returns:
prediction (BoxList): the detected objects. Additional information
of the detection properties can be found in the fields of
the BoxList via `prediction.fields()`
"""
# apply pre-processing to image
image = self.transforms(original_image)
# convert to an ImageList, padded so that it is divisible by
# cfg.DATALOADER.SIZE_DIVISIBILITY
image_list = to_image_list(image, self.cfg.DATALOADER.SIZE_DIVISIBILITY)
image_list = image_list.to(self.device)
# compute predictions
with torch.no_grad():
predictions = self.model(image_list)
predictions = [o.to(self.cpu_device) for o in predictions]
# always single image is passed at a time
prediction = predictions[0]
# reshape prediction (a BoxList) into the original image size
height, width = original_image.shape[:-1]
prediction = prediction.resize((width, height))
if prediction.has_field("mask"):
# if we have masks, paste the masks in the right position
# in the image, as defined by the bounding boxes
masks = prediction.get_field("mask")
# always single image is passed at a time
masks = self.masker([masks], [prediction])[0]
prediction.add_field("mask", masks)
return prediction
示例7: compute_prediction
# 需要导入模块: from maskrcnn_benchmark.structures import image_list [as 别名]
# 或者: from maskrcnn_benchmark.structures.image_list import to_image_list [as 别名]
def compute_prediction(self, original_image):
"""
Arguments:
original_image (np.ndarray): an image as returned by OpenCV
Returns:
prediction (BoxList): the detected objects. Additional information
of the detection properties can be found in the fields of
the BoxList via `prediction.fields()`
"""
# apply pre-processing to image
image = self.transforms(original_image)
# convert to an ImageList, padded so that it is divisible by
# cfg.DATALOADER.SIZE_DIVISIBILITY
image_list = to_image_list(image, self.cfg.DATALOADER.SIZE_DIVISIBILITY)
image_list = image_list.to(self.device)
# compute predictions
with torch.no_grad():
predictions = self.model(image_list)
predictions = [o.to(self.cpu_device) for o in predictions]
# always single image is passed at a time
prediction = predictions[0]
# reshape prediction (a BoxList) into the original image size
height, width = original_image.shape[:-1]
prediction = prediction.resize((width, height))
return prediction
示例8: im_detect_bbox
# 需要导入模块: from maskrcnn_benchmark.structures import image_list [as 别名]
# 或者: from maskrcnn_benchmark.structures.image_list import to_image_list [as 别名]
def im_detect_bbox(model, images, target_scale, target_max_size, device):
"""
Performs bbox detection on the original image.
"""
transform = TT.Compose([
T.Resize(target_scale, target_max_size),
TT.ToTensor(),
T.Normalize(
mean=cfg.INPUT.PIXEL_MEAN, std=cfg.INPUT.PIXEL_STD, to_bgr255=cfg.INPUT.TO_BGR255
)
])
images = [transform(image) for image in images]
images = to_image_list(images, cfg.DATALOADER.SIZE_DIVISIBILITY)
return model(images.to(device))
示例9: forward
# 需要导入模块: from maskrcnn_benchmark.structures import image_list [as 别名]
# 或者: from maskrcnn_benchmark.structures.image_list import to_image_list [as 别名]
def forward(self, images, targets=None):
"""
Arguments:
images (list[Tensor] or ImageList): images to be processed
targets (list[BoxList]): ground-truth boxes present in the image (optional)
Returns:
result (list[BoxList] or dict[Tensor]): the output from the model.
During training, it returns a dict[Tensor] which contains the losses.
During testing, it returns list[BoxList] contains additional fields
like `scores`, `labels` and `mask` (for Mask R-CNN models).
"""
if self.training and targets is None:
raise ValueError("In training mode, targets should be passed")
images = to_image_list(images)
features = self.backbone(images.tensors)
proposals, proposal_losses = self.rpn(images, features, targets)
#print(features[0].shape)
if self.roi_heads:
x, result, detector_losses = self.roi_heads(features, proposals, targets)
else:
# RPN-only models don't have roi_heads
x = features
result = proposals
detector_losses = {}
if self.training:
losses = {}
losses.update(detector_losses)
losses.update(proposal_losses)
return losses
#print(proposals)
#@print(x.shape)
#print(proposals[0].bbox)
#print(x)
#print(result[0].get_field('orig_inds'))
#print(result[0])
return result
示例10: compute_features_from_bbox
# 需要导入模块: from maskrcnn_benchmark.structures import image_list [as 别名]
# 或者: from maskrcnn_benchmark.structures.image_list import to_image_list [as 别名]
def compute_features_from_bbox(self, original_image, gt_boxes):
"""
Extracts features given the ground-truth boxes
assume ground-truth boxes are list of boxes in xyxy format
Arguments:
original_image (np.ndarray): an image as returned by OpenCV
Returns:
features (BoxList): the ground truth boxes with features
accessible using features.get_field()
"""
# Convert gt boxes to BoxList
gt_box_list = BoxList(
gt_boxes, (original_image.shape[1], original_image.shape[0]), mode='xyxy').to(self.device)
# Convert image as in `run_on_opencv_image`
image = self.transforms(original_image)
# Convert gt boxes for a single image to a list
#print(image.size(1))
gt_box_list = [gt_box_list.resize((image.size(2), image.size(1)))]
image_list = to_image_list(
image, self.cfg.DATALOADER.SIZE_DIVISIBILITY)
image_list = image_list.to(self.device)
with torch.no_grad():
features = self.feat_extractor(image_list, gt_box_list)
#print(features)
# sanity check
#assert len(features) == len(gt_box_list[0].bbox)
#feats = gt_box_list[0]
#feats.add_field('features', features)
return features[0].cpu().detach().numpy()[0]
示例11: forward
# 需要导入模块: from maskrcnn_benchmark.structures import image_list [as 别名]
# 或者: from maskrcnn_benchmark.structures.image_list import to_image_list [as 别名]
def forward(self, images, targets=None, rngs=None):
"""
Arguments:
images (list[Tensor] or ImageList): images to be processed
targets (list[BoxList]): ground-truth boxes present in the image (optional)
Returns:
result (list[BoxList] or dict[Tensor]): the output from the model.
During training, it returns a dict[Tensor] which contains the losses.
During testing, it returns list[BoxList] contains additional fields
like `scores`, `labels` and `mask` (for Mask R-CNN models).
"""
if self.training and targets is None:
raise ValueError("In training mode, targets should be passed")
images = to_image_list(images)
if rngs is None:
features = self.backbone(images.tensors)
else:
features = self.backbone(images.tensors, rngs)
features = self.fpn(features)
proposals, proposal_losses = self.rpn(images, features, targets)
if self.roi_heads:
x, result, detector_losses = self.roi_heads(features, proposals, targets)
else:
# RPN-only models don't have roi_heads
x = features
result = proposals
detector_losses = {}
if self.training:
losses = {}
losses.update(detector_losses)
losses.update(proposal_losses)
return losses
return result
示例12: forward
# 需要导入模块: from maskrcnn_benchmark.structures import image_list [as 别名]
# 或者: from maskrcnn_benchmark.structures.image_list import to_image_list [as 别名]
def forward(self, images, targets=None):
"""
Arguments:
images (list[Tensor] or ImageList): images to be processed
targets (list[BoxList]): ground-truth boxes present in the image (optional)
Returns:
result (list[BoxList] or dict[Tensor]): the output from the model.
During training, it returns a dict[Tensor] which contains the losses.
During testing, it returns list[BoxList] contains additional fields
like `scores`, `labels` and `mask` (for Mask R-CNN models).
"""
if self.training and targets is None:
raise ValueError("In training mode, targets should be passed")
images = to_image_list(images)
features = self.backbone(images.tensors)
if self.fp4p_on:
# get you C4
proposals, proposal_losses = self.rpn(images, (features[-1],), targets)
else:
proposals, proposal_losses = self.rpn(images, features, targets)
# features = [feature.detach() for feature in features]
if self.roi_heads:
x, result, detector_losses = self.roi_heads(features, proposals, targets)
else:
# RPN-only models don't have roi_heads
x = features
result = proposals
detector_losses = {}
if self.training:
losses = {}
losses.update(detector_losses)
losses.update(proposal_losses)
return losses
return result