Commit 7be960f5 authored by Alykhan Tejani's avatar Alykhan Tejani Committed by Francisco Massa
Browse files

Add docs to FiveCrop + TenCrop (#294)

* add docs to FiveCrop + TenCrop

* fix typo in docstring
parent 174dbbd4
...@@ -312,16 +312,28 @@ def five_crop(img, size): ...@@ -312,16 +312,28 @@ def five_crop(img, size):
"""Crop the given PIL Image into four corners and the central crop. """Crop the given PIL Image into four corners and the central crop.
.. Note:: .. Note::
This transform returns a tuple of images and there may be a This transform returns a tuple of images and there may be a mismatch in the number of
mismatch in the number of inputs and targets your ``Dataset`` returns. inputs and targets your Dataset returns. See below for an example of how to deal with
this.
Args: Args:
size (sequence or int): Desired output size of the crop. If size is an img (PIL Image): Image to be cropped.
int instead of sequence like (h, w), a square crop (size, size) is size (sequence or int): Desired output size of the crop. If size is an ``int``
made. instead of sequence like (h, w), a square crop of size (size, size) is made.
Returns: Returns:
tuple: tuple (tl, tr, bl, br, center) corresponding top left, tuple: tuple (tl, tr, bl, br, center) corresponding top left,
top right, bottom left, bottom right and center crop. top right, bottom left, bottom right and center crop.
Example:
>>> def transform(img):
>>> crops = five_crop(img, size) # this is a list of PIL Images
>>> return torch.stack([to_tensor(crop) for crop in crops)]) # returns a 4D tensor
>>> #In your test loop you can do the following:
>>> input, target = batch # input is a 5d tensor, target is 2d
>>> bs, ncrops, c, h, w = input.size()
>>> result = model(input.view(-1, c, h, w)) # fuse batch size and ncrops
>>> result_avg = result.view(bs, ncrops, -1).mean(1) # avg over crops
""" """
if isinstance(size, numbers.Number): if isinstance(size, numbers.Number):
size = (int(size), int(size)) size = (int(size), int(size))
...@@ -342,24 +354,35 @@ def five_crop(img, size): ...@@ -342,24 +354,35 @@ def five_crop(img, size):
def ten_crop(img, size, vertical_flip=False): def ten_crop(img, size, vertical_flip=False):
"""Crop the given PIL Image into four corners and the central crop plus the """Crop the given PIL Image into four corners and the central crop plus the flipped version of
flipped version of these (horizontal flipping is used by default). these (horizontal flipping is used by default).
.. Note:: .. Note::
This transform returns a tuple of images and there may be a This transform returns a tuple of images and there may be a mismatch in the number of
mismatch in the number of inputs and targets your ``Dataset`` returns. inputs and targets your Dataset returns. See below for an example of how to deal with
this.
Args: Args:
size (sequence or int): Desired output size of the crop. If size is an img (PIL Image): Image to be cropped.
int instead of sequence like (h, w), a square crop (size, size) is size (sequence or int): Desired output size of the crop. If size is an ``int``
made. instead of sequence like (h, w), a square crop of size (size, size) is made.
vertical_flip (bool): Use vertical flipping instead of horizontal vertical_flip (bool): Use vertical flipping instead of horizontal.
Returns: Returns:
tuple: tuple (tl, tr, bl, br, center, tl_flip, tr_flip, bl_flip, tuple: tuple (tl, tr, bl, br, center, tl_flip, tr_flip, bl_flip,
br_flip, center_flip) corresponding top left, top right, br_flip, center_flip) corresponding top left, top right,
bottom left, bottom right and center crop and same for the bottom left, bottom right and center crop and same for the
flipped image. flipped image.
Example:
>>> def transform(img):
>>> crops = ten_crop(img, size) # this is a list of PIL Images
>>> return torch.stack([to_tensor(crop) for crop in crops)]) # returns a 4D tensor
>>> #In your test loop you can do the following:
>>> input, target = batch # input is a 5d tensor, target is 2d
>>> bs, ncrops, c, h, w = input.size()
>>> result = model(input.view(-1, c, h, w)) # fuse batch size and ncrops
>>> result_avg = result.view(bs, ncrops, -1).mean(1) # avg over crops
""" """
if isinstance(size, numbers.Number): if isinstance(size, numbers.Number):
size = (int(size), int(size)) size = (int(size), int(size))
...@@ -906,15 +929,27 @@ class RandomSizedCrop(RandomResizedCrop): ...@@ -906,15 +929,27 @@ class RandomSizedCrop(RandomResizedCrop):
class FiveCrop(object): class FiveCrop(object):
"""Crop the given PIL Image into four corners and the central crop.abs """Crop the given PIL Image into four corners and the central crop
Note: this transform returns a tuple of images and there may be a mismatch in the number of .. Note::
inputs and targets your `Dataset` returns. This transform returns a tuple of images and there may be a mismatch in the number of
inputs and targets your Dataset returns. See below for an example of how to deal with
this.
Args: Args:
size (sequence or int): Desired output size of the crop. If size is an size (sequence or int): Desired output size of the crop. If size is an ``int``
int instead of sequence like (h, w), a square crop (size, size) is instead of sequence like (h, w), a square crop of size (size, size) is made.
made.
Example:
>>> transform = Compose([
>>> FiveCrop(size), # this is a list of PIL Images
>>> Lambda(lambda crops: torch.stack([ToTensor()(crop) for crop in crops])) # returns a 4D tensor
>>> ])
>>> #In your test loop you can do the following:
>>> input, target = batch # input is a 5d tensor, target is 2d
>>> bs, ncrops, c, h, w = input.size()
>>> result = model(input.view(-1, c, h, w)) # fuse batch size and ncrops
>>> result_avg = result.view(bs, ncrops, -1).mean(1) # avg over crops
""" """
def __init__(self, size): def __init__(self, size):
...@@ -930,17 +965,30 @@ class FiveCrop(object): ...@@ -930,17 +965,30 @@ class FiveCrop(object):
class TenCrop(object): class TenCrop(object):
"""Crop the given PIL Image into four corners and the central crop plus the """Crop the given PIL Image into four corners and the central crop plus the flipped version of
flipped version of these (horizontal flipping is used by default) these (horizontal flipping is used by default)
Note: this transform returns a tuple of images and there may be a mismatch in the number of .. Note::
inputs and targets your `Dataset` returns. This transform returns a tuple of images and there may be a mismatch in the number of
inputs and targets your Dataset returns. See below for an example of how to deal with
this.
Args: Args:
size (sequence or int): Desired output size of the crop. If size is an size (sequence or int): Desired output size of the crop. If size is an
int instead of sequence like (h, w), a square crop (size, size) is int instead of sequence like (h, w), a square crop (size, size) is
made. made.
vertical_flip(bool): Use vertical flipping instead of horizontal vertical_flip(bool): Use vertical flipping instead of horizontal
Example:
>>> transform = Compose([
>>> TenCrop(size), # this is a list of PIL Images
>>> Lambda(lambda crops: torch.stack([ToTensor()(crop) for crop in crops])) # returns a 4D tensor
>>> ])
>>> #In your test loop you can do the following:
>>> input, target = batch # input is a 5d tensor, target is 2d
>>> bs, ncrops, c, h, w = input.size()
>>> result = model(input.view(-1, c, h, w)) # fuse batch size and ncrops
>>> result_avg = result.view(bs, ncrops, -1).mean(1) # avg over crops
""" """
def __init__(self, size, vertical_flip=False): def __init__(self, size, vertical_flip=False):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment