generation/data/base_dataset.py [24:115]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class PadToSquare(object):
    """Pad the given PIL Image to square, with the given "pad" value.

    Args:
        padding (int or tuple): Padding on each border. If a single int is provided this
            is used to pad all borders. If tuple of length 2 is provided this is the padding
            on left/right and top/bottom respectively. If a tuple of length 4 is provided
            this is the padding for the left, top, right and bottom borders
            respectively.
        fill: Pixel fill value for constant fill. Default is 0. If a tuple of
            length 3, it is used to fill R, G, B channels respectively.
            This value is only used when the padding_mode is constant
        padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.
            constant: pads with a constant value, this value is specified with fill
            edge: pads with the last value at the edge of the image
            reflect: pads with reflection of image (without repeating the last value on the edge)
                padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode
                will result in [3, 2, 1, 2, 3, 4, 3, 2]
            symmetric: pads with reflection of image (repeating the last value on the edge)
                padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode
                will result in [2, 1, 1, 2, 3, 4, 4, 3]
    """

    def __init__(self, fill=0, padding_mode='constant'):
        assert isinstance(fill, (numbers.Number, str, tuple))
        assert padding_mode in ['constant', 'edge', 'reflect', 'symmetric']

        self.fill = fill
        self.padding_mode = padding_mode

    def __call__(self, img):
        """
        Args:
            img (PIL Image): Image to be padded.

        Returns:
            PIL Image: Padded image.
        """
        w,h = img.size
        if w > h: # Pad vertically
            if int((w-h)/2) > ((w-h)/2):
                padding = (0, int((w-h)/2), 0, int((w-h)/2)-1)
            elif int((w-h)/2) > ((w-h)/2):
                padding = (0, int((w-h)/2), 0, int((w-h)/2)+1)
            else:
                padding = (0, int((w-h)/2), 0, int((w-h)/2))
        elif h > w: # Pad horizontally
            if int((h-w)/2) > ((h-w)/2):
                padding = (int((h-w)/2), 0, int((h-w)/2)-1, 0)
            else:
                padding = (int((h-w)/2), 0, int((h-w)/2)+1, 0)
        else: # No need to pad
            padding = (0, 0, 0, 0)
        return F.pad(img, padding, self.fill)


class BaseDataset(data.Dataset):
    def __init__(self):
        super(BaseDataset, self).__init__()

    def name(self):
        return 'BaseDataset'

    def initialize(self, opt):
        pass

def get_params(opt, size):
    w, h = size
    new_h = h
    new_w = w
    if opt.resize_or_crop == 'resize_and_crop':
        new_h = new_w = opt.loadSize
    elif opt.resize_or_crop == 'scale_width_and_crop':
        new_w = opt.loadSize
        new_h = opt.loadSize * h // w

    x = random.randint(0, np.maximum(0, new_w - opt.fineSize))
    y = random.randint(0, np.maximum(0, new_h - opt.fineSize))

    flip = random.random() > 0.5
    return {'crop_pos': (x, y), 'flip': flip}

def get_transform(opt, params, method=Image.BICUBIC, normalize=True, labimg=False):
    transform_list = []
    if 'pad' in opt.resize_or_crop:
        if labimg:
            transform_list.append(PadToSquare((0, 128, 128))) # Black in Lab space is (0, 128, 128)
        else:
            transform_list.append(PadToSquare(0))

    if 'resize' in opt.resize_or_crop:
        osize = [opt.loadSize, opt.loadSize]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



separate_vae/data/base_dataset.py [24:115]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class PadToSquare(object):
    """Pad the given PIL Image to square, with the given "pad" value.

    Args:
        padding (int or tuple): Padding on each border. If a single int is provided this
            is used to pad all borders. If tuple of length 2 is provided this is the padding
            on left/right and top/bottom respectively. If a tuple of length 4 is provided
            this is the padding for the left, top, right and bottom borders
            respectively.
        fill: Pixel fill value for constant fill. Default is 0. If a tuple of
            length 3, it is used to fill R, G, B channels respectively.
            This value is only used when the padding_mode is constant
        padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.
            constant: pads with a constant value, this value is specified with fill
            edge: pads with the last value at the edge of the image
            reflect: pads with reflection of image (without repeating the last value on the edge)
                padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode
                will result in [3, 2, 1, 2, 3, 4, 3, 2]
            symmetric: pads with reflection of image (repeating the last value on the edge)
                padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode
                will result in [2, 1, 1, 2, 3, 4, 4, 3]
    """

    def __init__(self, fill=0, padding_mode='constant'):
        assert isinstance(fill, (numbers.Number, str, tuple))
        assert padding_mode in ['constant', 'edge', 'reflect', 'symmetric']

        self.fill = fill
        self.padding_mode = padding_mode

    def __call__(self, img):
        """
        Args:
            img (PIL Image): Image to be padded.

        Returns:
            PIL Image: Padded image.
        """
        w,h = img.size
        if w > h: # Pad vertically
            if int((w-h)/2) > ((w-h)/2):
                padding = (0, int((w-h)/2), 0, int((w-h)/2)-1)
            elif int((w-h)/2) > ((w-h)/2):
                padding = (0, int((w-h)/2), 0, int((w-h)/2)+1)
            else:
                padding = (0, int((w-h)/2), 0, int((w-h)/2))
        elif h > w: # Pad horizontally
            if int((h-w)/2) > ((h-w)/2):
                padding = (int((h-w)/2), 0, int((h-w)/2)-1, 0)
            else:
                padding = (int((h-w)/2), 0, int((h-w)/2)+1, 0)
        else: # No need to pad
            padding = (0, 0, 0, 0)
        return F.pad(img, padding, self.fill)


class BaseDataset(data.Dataset):
    def __init__(self):
        super(BaseDataset, self).__init__()

    def name(self):
        return 'BaseDataset'

    def initialize(self, opt):
        pass

def get_params(opt, size):
    w, h = size
    new_h = h
    new_w = w
    if opt.resize_or_crop == 'resize_and_crop':
        new_h = new_w = opt.loadSize
    elif opt.resize_or_crop == 'scale_width_and_crop':
        new_w = opt.loadSize
        new_h = opt.loadSize * h // w

    x = random.randint(0, np.maximum(0, new_w - opt.fineSize))
    y = random.randint(0, np.maximum(0, new_h - opt.fineSize))

    flip = random.random() > 0.5
    return {'crop_pos': (x, y), 'flip': flip}

def get_transform(opt, params, method=Image.BICUBIC, normalize=True, labimg=False):
    transform_list = []
    if 'pad' in opt.resize_or_crop:
        if labimg:
            transform_list.append(PadToSquare((0, 128, 128))) # Black in Lab space is (0, 128, 128)
        else:
            transform_list.append(PadToSquare(0))

    if 'resize' in opt.resize_or_crop:
        osize = [opt.loadSize, opt.loadSize]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



