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


Python functional.crop方法代碼示例

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


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

示例1: get_params

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import crop [as 別名]
def get_params(img_size, crop_size):
        """Get parameters for ``crop`` for a random crop.

        Args:
            img_size (tuple): size of image to be cropped.
            output_size (tuple): Expected output size of the crop.

        Returns:
            tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
        """
        w, h = img_size
        th = crop_size
        tw = crop_size
        if w == tw and h == th:
            return 0, 0, h, w

        i = random.randint(0, h - th)
        j = random.randint(0, w - tw)
        return i, j, th, tw 
開發者ID:DIVA-DIA,項目名稱:DeepDIVA,代碼行數:21,代碼來源:transforms.py

示例2: _random_crop

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import crop [as 別名]
def _random_crop(self, img_list):
        """Performs random square crop of fixed size.
        Works with list so that all items get the same cropped window (e.g. for buffers).
        """

        w, h = img_list[0].size
        assert w >= self.crop_size and h >= self.crop_size, \
            f'Error: Crop size: {self.crop_size}, Image size: ({w}, {h})'
        cropped_imgs = []
        i = np.random.randint(0, h - self.crop_size + 1)
        j = np.random.randint(0, w - self.crop_size + 1)

        for img in img_list:
            # Resize if dimensions are too small
            if min(w, h) < self.crop_size:
                img = tvF.resize(img, (self.crop_size, self.crop_size))

            # Random crop
            cropped_imgs.append(tvF.crop(img, i, j, self.crop_size, self.crop_size))

        return cropped_imgs 
開發者ID:joeylitalien,項目名稱:noise2noise-pytorch,代碼行數:23,代碼來源:datasets.py

示例3: __getitem__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import crop [as 別名]
def __getitem__(self, index):
        """Retrieves image from folder and corrupts it."""

        # Load PIL image
        img_path = os.path.join(self.root_dir, self.imgs[index])
        img =  Image.open(img_path).convert('RGB')

        # Random square crop
        if self.crop_size != 0:
            img = self._random_crop([img])[0]

        # Corrupt source image
        tmp = self._corrupt(img)
        source = tvF.to_tensor(self._corrupt(img))

        # Corrupt target image, but not when clean targets are requested
        if self.clean_targets:
            target = tvF.to_tensor(img)
        else:
            target = tvF.to_tensor(self._corrupt(img))

        return source, target 
開發者ID:joeylitalien,項目名稱:noise2noise-pytorch,代碼行數:24,代碼來源:datasets.py

示例4: get_params

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import crop [as 別名]
def get_params(img, output_size):
        """Get parameters for ``crop`` for a random crop.
        Args:
            img (PIL Image): Image to be cropped.
            output_size (tuple): Expected output size of the crop.
        Returns:
            tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
        """
        w, h = img.size
        th, tw = output_size
        if w == tw and h == th:
            return 0, 0, h, w

        i = random.randint(0, h - th)
        j = random.randint(0, w - tw)
        return i, j, th, tw 
開發者ID:yolomax,項目名稱:person-reid-lib,代碼行數:18,代碼來源:transforms.py

示例5: _instance_process

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import crop [as 別名]
def _instance_process(self, img, params):
        if params is None:
            img.img = img.img.resize((self.width, self.height), self.interpolation)

            if img.x is not None:
                img.x = img.x.resize((self.width, self.height), self.interpolation)
            if img.y is not None:
                img.y = img.y.resize((self.width, self.height), self.interpolation)

        else:
            new_width, new_height, x1, y1 = params
            img.img = img.img.resize((new_width, new_height), self.interpolation)
            img.img = img.img.crop((x1, y1, x1 + self.width, y1 + self.height))

            if img.x is not None:
                img.x = img.x.resize((new_width, new_height), self.interpolation)
                img.x = img.x.crop((x1, y1, x1 + self.width, y1 + self.height))

            if img.y is not None:
                img.y = img.y.resize((new_width, new_height), self.interpolation)
                img.y = img.y.crop((x1, y1, x1 + self.width, y1 + self.height))

        return img 
開發者ID:yolomax,項目名稱:person-reid-lib,代碼行數:25,代碼來源:transforms.py

示例6: __call__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import crop [as 別名]
def __call__(self, img):
        image_width = img.size[0]
        image_height = img.size[1]

        h, w = (self.size, self.size)
        if self.crop_position == 'c':
            i = int(round((image_height - h) / 2.))
            j = int(round((image_width - w) / 2.))
        elif self.crop_position == 'tl':
            i = 0
            j = 0
        elif self.crop_position == 'tr':
            i = 0
            j = image_width - self.size
        elif self.crop_position == 'bl':
            i = image_height - self.size
            j = 0
        elif self.crop_position == 'br':
            i = image_height - self.size
            j = image_width - self.size

        img = F.crop(img, i, j, h, w)

        return img 
開發者ID:kenshohara,項目名稱:3D-ResNets-PyTorch,代碼行數:26,代碼來源:spatial_transforms.py

示例7: get_params

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import crop [as 別名]
def get_params(data, output_size):
        """Get parameters for ``crop`` for a random crop.
        Args:
            img (PIL Image): Image to be cropped.
            output_size (tuple): Expected output size of the crop.
        Returns:
            tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
        """
        hr, lr = data
        w, h = hr.size
        th, tw = output_size
        if w == tw or h == th:
            return 0, 0, h, w

        if w < tw or h < th:
            th, tw = h//2, w//2

        i = random.randint(0, h - th)
        j = random.randint(0, w - tw)
        return i, j, th, tw 
開發者ID:jacobgil,項目名稱:pytorch-zssr,代碼行數:22,代碼來源:source_target_transforms.py

示例8: get_params

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import crop [as 別名]
def get_params(img, output_size):
        """Get parameters for ``crop`` for a random crop.

        Args:
            img (PIL Image): Image to be cropped.
            output_size (tuple): Expected output size of the crop.

        Returns:
            tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
        """
        w, h = img.size
        th, tw = output_size
        if w == tw and h == th:
            return 0, 0, h, w

        i = random.randint(0, h - th)
        j = random.randint(0, w - tw)
        return i, j, th, tw 
開發者ID:qixuxiang,項目名稱:Pytorch_Lightweight_Network,代碼行數:20,代碼來源:__init__.py

示例9: __call__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import crop [as 別名]
def __call__(self, img, target):
        w, h = img.size
        w_range = w - self.size[0]
        h_range = h - self.size[1]
        if w_range > 0:
            left = random.randint(0, w_range - 1)

        else:
            left = 0

        if h_range > 0:
            top = random.randint(0, h_range - 1)

        else:
            top = 0
            
        height = min(h - top, self.size[1])
        width = min(w - left, self.size[0])

        img = F.crop(img, top, left, height, width)
        target = F.crop(target, top, left, height, width)

        return img, target 
開發者ID:rosinality,項目名稱:ocr-pytorch,代碼行數:25,代碼來源:transform.py

示例10: resized_crop

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import 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

示例11: pil_transform

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import 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

示例12: __call__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import crop [as 別名]
def __call__(self, img_dict):
        keys = ['rgb', 'ir', 'depth']
        for k in keys:
            img = img_dict[k]
            w, h = img.size
            crop_h, crop_w = self.size
            if crop_w > w or crop_h > h:
                raise ValueError("Requested crop size {} is bigger than input size {}".format(self.size,
                                                                                              (h, w)))
            if self.crop_index == 0:
                img_dict[k] = F.center_crop(img, (crop_h, crop_w))
            elif self.crop_index == 1:
                img_dict[k] = img.crop((0, 0, crop_w, crop_h))
            elif self.crop_index == 2:
                img_dict[k] = img.crop((w - crop_w, 0, w, crop_h))
            elif self.crop_index == 3:
                img_dict[k] = img.crop((0, h - crop_h, crop_w, h))
            elif self.crop_index == 4:
                img_dict[k] = img.crop((w - crop_w, h - crop_h, w, h))
            else:
                raise ValueError("Requested crop index is not in range(5)")
        return img_dict 
開發者ID:AlexanderParkin,項目名稱:ChaLearn_liveness_challenge,代碼行數:24,代碼來源:transforms.py

示例13: get_params

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import crop [as 別名]
def get_params(frames, output_size):
        """Get parameters for ``crop`` for a random crop.
        Args:
            frames: a list of PIL Image
            output_size (tuple): Expected output size of the crop.
        Returns:
            tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
        """
        w, h = frames[0].size
        th, tw = output_size
        if w == tw and h == th:
            return 0, 0, h, w

        i = random.randint(0, h - th)
        j = random.randint(0, w - tw)
        return i, j, th, tw 
開發者ID:hangzhaomit,項目名稱:Sound-of-Pixels,代碼行數:18,代碼來源:video_transforms.py

示例14: __call__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import crop [as 別名]
def __call__(self, frames):
        """
        Args:
            frames: a list of PIL Image
        Returns:
            a list of PIL Image: Cropped images.
        """

        i, j, h, w = self.get_params(frames, self.size)

        out_frames = []
        for frame in frames:
            if self.padding is not None:
                frame = F.pad(frame, self.padding, self.fill, self.padding_mode)

            # pad the width if needed
            if self.pad_if_needed and frame.size[0] < self.size[1]:
                frame = F.pad(frame, (int((1 + self.size[1] - frame.size[0]) / 2), 0), self.fill, self.padding_mode)
            # pad the height if needed
            if self.pad_if_needed and frame.size[1] < self.size[0]:
                frame = F.pad(frame, (0, int((1 + self.size[0] - frame.size[1]) / 2)), self.fill, self.padding_mode)

            out_frames.append(F.crop(frame, i, j, h, w))
        return out_frames 
開發者ID:hangzhaomit,項目名稱:Sound-of-Pixels,代碼行數:26,代碼來源:video_transforms.py

示例15: __call__

# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import crop [as 別名]
def __call__(self, img):
        """
        Args:
            img (PIL Image): Image to be cropped.

        Returns:
            PIL Image: Cropped image.
        """
        if self.padding is not None:
            img = F.pad(img, self.padding, self.fill, self.padding_mode)

        # pad the width if needed
        if self.pad_if_needed and img.size[0] < self.size[1]:
            img = F.pad(img, (self.size[1] - img.size[0], 0), self.fill, self.padding_mode)
        # pad the height if needed
        if self.pad_if_needed and img.size[1] < self.size[0]:
            img = F.pad(img, (0, self.size[0] - img.size[1]), self.fill, self.padding_mode)

        i, j, h, w = self.get_params(img, self.size)

        return F.crop(img, i, j, h, w) 
開發者ID:yalesong,項目名稱:pvse,代碼行數:23,代碼來源:video_transforms.py


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