MCPcopy Create free account
hub / github.com/pytorch/tutorials / RandomCrop

Class RandomCrop

beginner_source/data_loading_tutorial.py:244–274  ·  view source on GitHub ↗

Crop randomly the image in a sample. Args: output_size (tuple or int): Desired output size. If int, square crop is made.

Source from the content-addressed store, hash-verified

242
243
244class RandomCrop(object):
245 """Crop randomly the image in a sample.
246
247 Args:
248 output_size (tuple or int): Desired output size. If int, square crop
249 is made.
250 """
251
252 def __init__(self, output_size):
253 assert isinstance(output_size, (int, tuple))
254 if isinstance(output_size, int):
255 self.output_size = (output_size, output_size)
256 else:
257 assert len(output_size) == 2
258 self.output_size = output_size
259
260 def __call__(self, sample):
261 image, landmarks = sample['image'], sample['landmarks']
262
263 h, w = image.shape[:2]
264 new_h, new_w = self.output_size
265
266 top = np.random.randint(0, h - new_h + 1)
267 left = np.random.randint(0, w - new_w + 1)
268
269 image = image[top: top + new_h,
270 left: left + new_w]
271
272 landmarks = landmarks - [left, top]
273
274 return {'image': image, 'landmarks': landmarks}
275
276
277class ToTensor(object):

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected