當前位置: 首頁>>代碼示例>>Python>>正文


Python functional.resized_crop方法代碼示例

本文整理匯總了Python中torchvision.transforms.functional.resized_crop方法的典型用法代碼示例。如果您正苦於以下問題:Python functional.resized_crop方法的具體用法?Python functional.resized_crop怎麽用?Python functional.resized_crop使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在torchvision.transforms.functional的用法示例。


在下文中一共展示了functional.resized_crop方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: process_images

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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 
開發者ID:yu45020,項目名稱:Text_Segmentation_Image_Inpainting,代碼行數:25,代碼來源:Dataloader.py

示例2: resized_crop

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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 
開發者ID:YU-Zhiyang,項目名稱:opencv_transforms_torchvision,代碼行數:21,代碼來源:cvfunctional.py

示例3: cv_transform

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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) 
開發者ID:YU-Zhiyang,項目名稱:opencv_transforms_torchvision,代碼行數:26,代碼來源:cvfunctional.py

示例4: pil_transform

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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) 
開發者ID:YU-Zhiyang,項目名稱:opencv_transforms_torchvision,代碼行數:24,代碼來源:cvfunctional.py

示例5: __call__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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 
開發者ID:frgfm,項目名稱:Holocron,代碼行數:21,代碼來源:transforms.py

示例6: __call__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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)) 
開發者ID:blandocs,項目名稱:Tag2Pix,代碼行數:21,代碼來源:dataloader.py

示例7: __call__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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) 
開發者ID:kenshohara,項目名稱:3D-ResNets-PyTorch,代碼行數:9,代碼來源:spatial_transforms.py

示例8: __call__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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 
開發者ID:qixuxiang,項目名稱:Pytorch_Lightweight_Network,代碼行數:9,代碼來源:__init__.py

示例9: __call__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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 
開發者ID:AlexanderParkin,項目名稱:ChaLearn_liveness_challenge,代碼行數:16,代碼來源:transforms.py

示例10: _transform

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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 
開發者ID:torchvideo,項目名稱:torchvideo,代碼行數:17,代碼來源:multiscale_crop_video.py

示例11: _transform_frame

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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) 
開發者ID:torchvideo,項目名稱:torchvideo,代碼行數:4,代碼來源:random_resized_crop_video.py

示例12: __getitem__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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) 
開發者ID:rakshithShetty,項目名稱:adversarial-object-removal,代碼行數:43,代碼來源:data_loader_stargan.py

示例13: __call__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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) 
開發者ID:rwightman,項目名稱:pytorch-image-models,代碼行數:16,代碼來源:transforms.py

示例14: __call__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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) 
開發者ID:Vermeille,項目名稱:Torchelie,代碼行數:11,代碼來源:__init__.py

示例15: __call__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import resized_crop [as 別名]
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 
開發者ID:Kinpzz,項目名稱:RCRNet-Pytorch,代碼行數:10,代碼來源:transforms.py


注:本文中的torchvision.transforms.functional.resized_crop方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。