Python torchvision.transforms.functional.resized_crop() Examples
The following are 25
code examples of torchvision.transforms.functional.resized_crop().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torchvision.transforms.functional
, or try the search function
.
Example #1
Source File: dataloader.py From Tag2Pix with MIT License | 6 votes |
def __call__(self, img1, img2): img1 = tvF.resize(img1, self.size, interpolation=Image.LANCZOS) img2 = tvF.resize(img2, self.size, interpolation=Image.LANCZOS) if random.random() < 0.5: img1 = tvF.hflip(img1) img2 = tvF.hflip(img2) if random.random() < 0.5: rot = random.uniform(-10, 10) crop_ratio = rot_crop(rot) img1 = tvF.rotate(img1, rot, resample=Image.BILINEAR) img2 = tvF.rotate(img2, rot, resample=Image.BILINEAR) img1 = tvF.center_crop(img1, int(img1.size[0] * crop_ratio)) img2 = tvF.center_crop(img2, int(img2.size[0] * crop_ratio)) i, j, h, w = self.get_params(img1, self.scale, self.ratio) # return the image with the same transformation return (tvF.resized_crop(img1, i, j, h, w, self.size, self.interpolation), tvF.resized_crop(img2, i, j, h, w, self.size, self.interpolation))
Example #2
Source File: Dataloader.py From Text_Segmentation_Image_Inpainting with GNU General Public License v3.0 | 6 votes |
def process_images(self, clean, mask): i, j, h, w = RandomResizedCrop.get_params(clean, scale=(0.5, 2.0), ratio=(3. / 4., 4. / 3.)) clean_img = resized_crop(clean, i, j, h, w, size=self.img_size, interpolation=Image.BICUBIC) mask = resized_crop(mask, i, j, h, w, self.img_size, interpolation=Image.BICUBIC) # get mask before further image augment # mask = self.get_mask(raw_img, clean_img) if self.add_random_masks: mask = random_masks(mask.copy(), size=self.img_size[0], offset=10) mask = np.where(np.array(mask) > brightness_difference * 255, np.uint8(255), np.uint8(0)) mask = cv2.dilate(mask, np.ones((10, 10), np.uint8), iterations=1) mask = np.expand_dims(mask, -1) mask_t = to_tensor(mask) # mask_t = (mask_t > brightness_difference).float() # mask_t, _ = torch.max(mask_t, dim=0, keepdim=True) binary_mask = (1 - mask_t) # valid positions are 1; holes are 0 binary_mask = binary_mask.expand(3, -1, -1) clean_img = self.transformer(clean_img) corrupted_img = clean_img * binary_mask return corrupted_img, binary_mask, clean_img
Example #3
Source File: cvfunctional.py From opencv_transforms_torchvision with MIT License | 6 votes |
def resized_crop(img, i, j, h, w, size, interpolation='BILINEAR'): """Crop the given CV Image and resize it to desired size. Notably used in RandomResizedCrop. Args: img (np.ndarray): Image to be cropped. i: Upper pixel coordinate. j: Left pixel coordinate. h: Height of the cropped image. w: Width of the cropped image. size (sequence or int): Desired output size. Same semantics as ``scale``. interpolation (str, optional): Desired interpolation. Default is ``BILINEAR``. Returns: np.ndarray: Cropped image. """ assert _is_numpy_image(img), 'img should be CV Image' img = crop(img, i, j, h, w) img = resize(img, size, interpolation) return img
Example #4
Source File: cvfunctional.py From opencv_transforms_torchvision with MIT License | 6 votes |
def cv_transform(img): # img = resize(img, size=(100, 300)) # img = to_tensor(img) # img = normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # img = pad(img, padding=(10, 10, 20, 20), fill=(255, 255, 255), padding_mode='constant') # img = pad(img, padding=(100, 100, 100, 100), fill=5, padding_mode='symmetric') # img = crop(img, -40, -20, 1000, 1000) # img = center_crop(img, (310, 300)) # img = resized_crop(img, -10.3, -20, 330, 220, (500, 500)) # img = hflip(img) # img = vflip(img) # tl, tr, bl, br, center = five_crop(img, 100) # img = adjust_brightness(img, 2.1) # img = adjust_contrast(img, 1.5) # img = adjust_saturation(img, 2.3) # img = adjust_hue(img, 0.5) # img = adjust_gamma(img, gamma=3, gain=0.1) # img = rotate(img, 10, resample='BILINEAR', expand=True, center=None) # img = to_grayscale(img, 3) # img = affine(img, 10, (0, 0), 1, 0, resample='BICUBIC', fillcolor=(255,255,0)) # img = gaussion_noise(img) # img = poisson_noise(img) img = salt_and_pepper(img) return to_tensor(img)
Example #5
Source File: cvfunctional.py From opencv_transforms_torchvision with MIT License | 6 votes |
def pil_transform(img): # img = functional.resize(img, size=(100, 300)) # img = functional.to_tensor(img) # img = functional.normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # img = functional.pad(img, padding=(10, 10, 20, 20), fill=(255, 255, 255), padding_mode='constant') # img = functional.pad(img, padding=(100, 100, 100, 100), padding_mode='symmetric') # img = functional.crop(img, -40, -20, 1000, 1000) # img = functional.center_crop(img, (310, 300)) # img = functional.resized_crop(img, -10.3, -20, 330, 220, (500, 500)) # img = functional.hflip(img) # img = functional.vflip(img) # tl, tr, bl, br, center = functional.five_crop(img, 100) # img = functional.adjust_brightness(img, 2.1) # img = functional.adjust_contrast(img, 1.5) # img = functional.adjust_saturation(img, 2.3) # img = functional.adjust_hue(img, 0.5) # img = functional.adjust_gamma(img, gamma=3, gain=0.1) # img = functional.rotate(img, 10, resample=PIL.Image.BILINEAR, expand=True, center=None) # img = functional.to_grayscale(img, 3) # img = functional.affine(img, 10, (0, 0), 1, 0, resample=PIL.Image.BICUBIC, fillcolor=(255,255,0)) return functional.to_tensor(img)
Example #6
Source File: transforms.py From Holocron with MIT License | 6 votes |
def __call__(self, image, target): i, j, h, w = self.get_params(image, self.scale, self.ratio) image = F.resized_crop(image, i, j, h, w, self.size, self.interpolation) # Crop target['boxes'][:, [0, 2]] = target['boxes'][:, [0, 2]].clamp_(j, j + w) target['boxes'][:, [1, 3]] = target['boxes'][:, [1, 3]].clamp_(i, i + h) # Reset origin target['boxes'][:, [0, 2]] -= j target['boxes'][:, [1, 3]] -= i # Remove targets that are out of crop target_filter = (target['boxes'][:, 0] != target['boxes'][:, 2]) & \ (target['boxes'][:, 1] != target['boxes'][:, 3]) target['boxes'] = target['boxes'][target_filter] target['labels'] = target['labels'][target_filter] # Resize target['boxes'][:, [0, 2]] *= self.size[0] / w target['boxes'][:, [1, 3]] *= self.size[1] / h return image, target
Example #7
Source File: random_resized_crop_video.py From torchvideo with Mozilla Public License 2.0 | 5 votes |
def _transform_frame(self, frame: Image, i: int, j: int, h: int, w: int) -> Image: return F.resized_crop(frame, i, j, h, w, self.size, self.interpolation)
Example #8
Source File: transforms.py From ACDRNet with Apache License 2.0 | 5 votes |
def __call__(self, img, mask): i, j, h, w = self.get_params(img, self.scale, self.ratio) return F.resized_crop(img, i, j, h, w, self.size, self.interpolation), \ F.resized_crop(mask, i, j, h, w, self.size, Image.NEAREST)
Example #9
Source File: randomcrop.py From DSC-PyTorch with MIT License | 5 votes |
def __call__(self, img,img_gt): """ Args: img (PIL Image): Image to be cropped and resized. Returns: PIL Image: Randomly cropped and resized image. """ i, j, h, w = self.get_params(img, self.scale, self.ratio) return (F.resized_crop(img, i, j, h, w, self.size, self.interpolation),F.resized_crop(img_gt, i, j, h, w, self.size, self.interpolation))
Example #10
Source File: video_transforms.py From pvse with MIT License | 5 votes |
def __call__(self, img_list): """ Args: img (PIL Image): Image to be cropped and resized. Returns: PIL Image: Randomly cropped and resized image. """ i, j, h, w = self.get_params(img_list[0], self.scale, self.ratio) return [F.resized_crop(img, i, j, h, w, self.size, self.interpolation) for img in img_list]
Example #11
Source File: augmentation.py From nni with MIT License | 5 votes |
def __call__(self, *imgs): i, j, h, w = self.get_params(imgs[0], self.scale, self.ratio) #print(i,j,h,w) return map(lambda x: F.resized_crop(x, i, j, h, w, self.size, self.interpolation), imgs)
Example #12
Source File: transforms.py From RCRNet-Pytorch with MIT License | 5 votes |
def __call__(self, sample): image, label = sample['image'], sample['label'] if self.i is None or self.image_mode: self.i, self.j, self.h, self.w = transforms.RandomResizedCrop.get_params(image, self.scale, self.ratio) image = F.resized_crop(image, self.i, self.j, self.h, self.w, self.size, Image.BILINEAR) label = F.resized_crop(label, self.i, self.j, self.h, self.w, self.size, Image.BILINEAR) sample['image'], sample['label'] = image, label return sample
Example #13
Source File: __init__.py From Torchelie with MIT License | 5 votes |
def __call__(self, img): """ Args: img (PIL Image): Image to be cropped and resized. Returns: PIL Image: Randomly cropped and resized image. """ i, j, h, w = self.get_params(img, self.scale) return F.resized_crop(img, i, j, h, w, self.size, self.interpolation)
Example #14
Source File: transforms.py From pytorch-image-models with Apache License 2.0 | 5 votes |
def __call__(self, img): """ Args: img (PIL Image): Image to be cropped and resized. Returns: PIL Image: Randomly cropped and resized image. """ i, j, h, w = self.get_params(img, self.scale, self.ratio) if isinstance(self.interpolation, (tuple, list)): interpolation = random.choice(self.interpolation) else: interpolation = self.interpolation return F.resized_crop(img, i, j, h, w, self.size, interpolation)
Example #15
Source File: data_loader_stargan.py From adversarial-object-removal with MIT License | 5 votes |
def __getitem__(self, index): # In this situation ignore index and sample classes uniformly if self.balance_classes: currCls = random.choice(self.attToImgId.keys()) index = random.choice(self.attToImgId[currCls]) cid = [self.sattr_to_idx[currCls]] if currCls != 'bg' else [0] else: cid = [0] image = Image.open(os.path.join(self.image_path,self.dataset['images'][index]['filepath'], self.dataset['images'][index]['filename'])) if image.mode != 'RGB': #print image.mode image = image.convert('RGB') bbox, bboxLabel, cbid = self.randomBBoxSample(index) label = self.dataset['images'][index]['label'] # Apply transforms to the image. image = self.transform[0](image) # Now do the flipping if self.randHFlip and random.random()>0.5: image = FN.hflip(image) bbox[0] = 1.0-(bbox[0]+bbox[2]) #Convert BBox to actual co-ordinates bbox = [int(bc*self.out_img_size) for bc in bbox] #print bbox, image.size, cbid #assert bbox[3]>0; #assert bbox[2]>0; #if not ((bbox[0]>=0) and (bbox[0]<128)): # print bbox; # import ipdb;ipdb.set_trace() #assert ((bbox[0]>=0) and (bbox[0]<128)); #assert ((bbox[1]>=0) and (bbox[1]<128)) #Now obtain the crop boxCrop = FN.resized_crop(image, bbox[1], bbox[0], bbox[3],bbox[2], (self.bbox_out_size, self.bbox_out_size)) # Create Mask mask = torch.zeros(1,self.out_img_size,self.out_img_size) mask[0,bbox[1]:bbox[1]+bbox[3],bbox[0]:bbox[0]+bbox[2]] = 1. return self.transform[-1](image), torch.FloatTensor(label), self.transform[-1](boxCrop), torch.FloatTensor(bboxLabel), mask, torch.IntTensor(bbox), torch.LongTensor(cid)
Example #16
Source File: multiscale_crop_video.py From torchvideo with Mozilla Public License 2.0 | 5 votes |
def _transform( self, frames: PILVideo, params: Tuple[ImageShape, Point] ) -> PILVideoI: crop_shape, offset = params for frame in frames: yield F.resized_crop( frame, offset.y, offset.x, crop_shape.height, crop_shape.width, size=self.size, interpolation=self.interpolation, ) pass
Example #17
Source File: transforms.py From ChaLearn_liveness_challenge with MIT License | 5 votes |
def __call__(self, img_dict): """ Args: img (PIL Image): Image to be cropped and resized. Returns: PIL Image: Randomly cropped and resized image. """ keys = ['rgb', 'ir', 'depth'] i, j, h, w = self.get_params(img_dict[keys[0]], self.scale, self.ratio) for key in keys: img_dict[key] = F.resized_crop(img_dict[key], i, j, h, w, self.size, self.interpolation) return img_dict
Example #18
Source File: transforms.py From ChaLearn_liveness_challenge with MIT License | 5 votes |
def __call__(self, img_dict): """ Args: img (PIL Image): Image to be cropped and resized. Returns: PIL Image: Randomly cropped and resized image. """ keys = ['rgb', 'ir', 'depth'] i, j, h, w = self.get_params(img_dict[keys[0]], self.scale, self.ratio) for key in keys: img_dict[key] = F.resized_crop(img_dict[key], i, j, h, w, self.size, self.interpolation) return img_dict
Example #19
Source File: __init__.py From Pytorch_Lightweight_Network with MIT License | 5 votes |
def __call__(self, img, anns): i, j, h, w = self.get_params(img, self.scale, self.ratio) new_anns = HF.resized_crop(anns, j, i, w, h, self.size, self.min_area_frac) if len(new_anns) == 0: return img, anns img = VF.resized_crop(img, i, j, h, w, self.size[::-1], self.interpolation) return img, new_anns
Example #20
Source File: spatial_transforms.py From 3D-ResNets-PyTorch with MIT License | 5 votes |
def __call__(self, img): if self.randomize: self.random_crop = self.get_params(img, self.scale, self.ratio) self.randomize = False i, j, h, w = self.random_crop return F.resized_crop(img, i, j, h, w, self.size, self.interpolation)
Example #21
Source File: Dataloader.py From Text_Segmentation_Image_Inpainting with GNU General Public License v3.0 | 5 votes |
def process_images(self, raw, clean): i, j, h, w = RandomResizedCrop.get_params(raw, scale=(0.5, 2.0), ratio=(3. / 4., 4. / 3.)) raw_img = resized_crop(raw, i, j, h, w, size=self.img_size, interpolation=Image.BICUBIC) clean_img = resized_crop(clean, i, j, h, w, self.img_size, interpolation=Image.BICUBIC) # get mask before further image augment mask = self.get_mask(raw_img, clean_img) mask_t = to_tensor(mask) binary_mask = (1 - mask_t) binary_mask = binary_mask.expand(3, -1, -1) clean_img = self.transformer(clean_img) corrupted_img = clean_img * binary_mask return corrupted_img, binary_mask, clean_img
Example #22
Source File: Dataloader.py From Text_Segmentation_Image_Inpainting with GNU General Public License v3.0 | 5 votes |
def process_images(self, raw, clean): i, j, h, w = RandomResizedCrop.get_params(raw, scale=(0.1, 2), ratio=(3. / 4., 4. / 3.)) raw_img = resized_crop(raw, i, j, h, w, size=self.img_size, interpolation=Image.BICUBIC) raw_img = self.transformer(raw_img) # raw_img = np.array(raw_img) mask_img = resized_crop(clean, i, j, h, w, self.img_size, interpolation=Image.BICUBIC) # mask_img = np.array(mask_img) return to_tensor(raw_img), to_tensor(mask_img)
Example #23
Source File: data_loader_stargan.py From adversarial-object-removal with MIT License | 4 votes |
def __getitem__(self, index): # In this situation ignore index and sample classes uniformly image = Image.open(os.path.join(self.image_path,self.filenames[index])) if image.mode != 'RGB': #print image.mode image = image.convert('RGB') cid = [0] sampbbox, bboxLabel, cbid = self.randomBBoxSample(0.5) extra_boxes = [] if self.n_boxes > 1: # Sample random number of boxes between 1 and n_boxes c_nbox = np.random.randint(0,self.n_boxes) c_area = sampbbox[2]*sampbbox[3] for i in xrange(c_nbox): # Also stop at total area > 50% if c_area < 0.5: bsamp, _, _ = self.randomBBoxSample(0.6-c_area) # Extra 10% to make the sampling easier extra_boxes.append(bsamp) c_area += bsamp[2]*bsamp[3] else: break label = np.zeros(max(len(self.selected_attrs),1)) # Apply transforms to the image. image = self.transform[0](image) # Now do the flipping hflip = 0 if self.randHFlip and random.random()>0.5: hflip = 1 image = FN.hflip(image) sampbbox[0] = 1.0-(sampbbox[0]+sampbbox[2]) #Convert BBox to actual co-ordinates sampbbox = [int(bc*self.out_img_size) for bc in sampbbox] #Now obtain the crop boxCrop = FN.resized_crop(image, sampbbox[1], sampbbox[0], sampbbox[3],sampbbox[2], (self.bbox_out_size, self.bbox_out_size)) # Create Mask mask = torch.zeros(1,self.out_img_size,self.out_img_size) mask[0,sampbbox[1]:sampbbox[1]+sampbbox[3],sampbbox[0]:sampbbox[0]+sampbbox[2]] = 1. if self.n_boxes > 1 and len(extra_boxes): for box in extra_boxes: box = [int(bc*self.out_img_size) for bc in box] mask[0,box[1]:box[1]+box[3],box[0]:box[0]+box[2]] = 1. if self.boxrotate: mask = torch.FloatTensor(np.asarray(self.rotateTrans(Image.fromarray(mask.numpy()[0]))))[None,::] return self.transform[-1](image), torch.FloatTensor(label), self.transform[-1](boxCrop), torch.FloatTensor(bboxLabel), mask, torch.IntTensor(sampbbox), torch.LongTensor(cid)
Example #24
Source File: data_loader_stargan.py From adversarial-object-removal with MIT License | 4 votes |
def getbyIndexAndclass(self, index,cls=None): imgDb = self.dataset[index] image_id = imgDb['image_id'] image = Image.open(os.path.join(self.image_path,imgDb['fpath_img'])) if image.mode != 'RGB': #print image.mode image = image.convert('RGB') cid = [0] sampbbox, bboxLabel, cbid = self.randomBBoxSample(0.5) extra_boxes = [] if self.n_boxes > 1: # Sample random number of boxes between 1 and n_boxes c_nbox = np.random.randint(0,self.n_boxes) c_area = sampbbox[2]*sampbbox[3] for i in xrange(c_nbox): # Also stop at total area > 50% if c_area < 0.5: bsamp, _, _ = self.randomBBoxSample(0.6-c_area) # Extra 10% to make the sampling easier extra_boxes.append(bsamp) c_area += bsamp[2]*bsamp[3] else: break label = np.ones(max(len(self.selected_attrs),1)) # Apply transforms to the image. image = self.transform[0](image) # Now do the flipping hflip = 0 if self.randHFlip and random.random()>0.5: hflip = 1 image = FN.hflip(image) sampbbox[0] = 1.0-(sampbbox[0]+sampbbox[2]) if self.use_gt_mask==1: # Use GT masks as input gtMask = self.getGTMaskInp(index, hflip=hflip) #Convert BBox to actual co-ordinates sampbbox = [int(bc*self.out_img_size) for bc in sampbbox] boxCrop = FN.resized_crop(image, sampbbox[1], sampbbox[0], sampbbox[3],sampbbox[2], (self.bbox_out_size, self.bbox_out_size)) # Create Mask mask = torch.zeros(1,self.out_img_size,self.out_img_size) mask[0,sampbbox[1]:sampbbox[1]+sampbbox[3],sampbbox[0]:sampbbox[0]+sampbbox[2]] = 1. if self.n_boxes > 1 and len(extra_boxes): for box in extra_boxes: box = [int(bc*self.out_img_size) for bc in box] mask[0,box[1]:box[1]+box[3],box[0]:box[0]+box[2]] = 1. if self.boxrotate: mask = torch.FloatTensor(np.asarray(self.rotateTrans(Image.fromarray(mask.numpy()[0]))))[None,::] if self.use_gt_mask: mask = torch.cat([mask, gtMask], dim=0) return self.transform[-1](image), torch.FloatTensor(label), torch.FloatTensor(bboxLabel), torch.FloatTensor(bboxLabel), mask, torch.IntTensor(sampbbox), torch.LongTensor(cid)
Example #25
Source File: myposetrackLoader.py From 3D-HourGlass-Network with MIT License | 4 votes |
def __getitem__(self, index): if split=='train': index = np.random.randint(self.nVideos) whichFrames,frameWiseData = self.annotations[index] numFrames = len(whichFrames) if self.loadConsecutive: startpt = random.randint(1, numFrames - (self.nFramesLoad - 1)) inpFrames = np.zeros((3,self.nFramesLoad,256,256)) outPts_2ds = np.zeros((ref.nJoints,self.nFramesLoad,2)) outOutRegs = np.ones((ref.nJoints,self.nFramesLoad,1)) outPts_3d_monos = np.zeros((ref.nJoints,self.nFramesLoad,3)) outOutMaps = np.zeros((ref.nJoints, self.nFramesLoad, ref.outputRes, ref.outputRes)) nPersons = len(frameWiseData[whichFrames[0]]) personIndex = random.randint(0,nPersons-1) if self.augmentation: angle = random.randint(0,10) scale = 1 + (random.random()-0.5)*0.4 flipOrNot = random.random() > 0 else: angle = 0 scale = 1 flipOrNot = 0 for i in range(self.nFramesLoad): tempFrame = cv2.imread(ref.posetrackDataDir + whichFrames[startpt + i]) frameData = frameWiseData[whichFrames[startpt + i]] bbox, joints = frameData[personIndex] cropedAndResized = F.resized_crop(tempFrame, bbox.mean[0] + bbox.delta, bbox.mean[1] - bbox.delta, bbox.delta, bbox.delta, 224) padded = F.pad(cropedAndResized, 256 - 224) rotated = F.affine(padded,angle,scale) if flipOrNot: flipped = hflip(rotated) thisFrame = flipped else: thisFrame = rotated inpFrames[:,i,:,:] = thisFrame outPts_2ds[:,i,:] = joints for j in range(ref.nJoints): if (joints[j,:] == -1).all(): continue else: outOutMaps[j,i,:,:] = DrawGaussian(outOutMaps[j,i,:,:], pt, ref.hmGauss)