示例#1
0
    def test_random_crop(self):
        img = np.random.uniform(size=(3, 48, 32))

        out, slices = random_crop(img, (48, 32), return_slices=True)
        np.testing.assert_equal(out, img)
        self.assertEqual(slices[0], slice(0, 48))
        self.assertEqual(slices[1], slice(0, 32))

        out = random_crop(img, (24, 12))
        self.assertEqual(out.shape[1:], (24, 12))
    def test_random_crop(self):
        img = np.random.uniform(size=(3, 48, 32))

        out, param = random_crop(img, (48, 32), return_param=True)
        y_slice = param['y_slice']
        x_slice = param['x_slice']
        np.testing.assert_equal(out, img)
        self.assertEqual(y_slice, slice(0, 48))
        self.assertEqual(x_slice, slice(0, 32))

        out = random_crop(img, (24, 12))
        self.assertEqual(out.shape[1:], (24, 12))
示例#3
0
    def test_random_crop(self):
        img = np.random.uniform(size=(3, 48, 32))

        out, param = random_crop(img, (48, 32), return_param=True)
        y_slice = param['y_slice']
        x_slice = param['x_slice']
        np.testing.assert_equal(out, img)
        self.assertEqual(y_slice, slice(0, 48))
        self.assertEqual(x_slice, slice(0, 32))

        out = random_crop(img, (24, 12))
        self.assertEqual(out.shape[1:], (24, 12))
示例#4
0
    def __call__(self, in_data):
        img, label = in_data
        _, height, width = img.shape

        scale = np.random.uniform(self.scale_range[0], self.scale_range[1])

        # Scale
        scaled_height = int(scale * height)
        scaled_width = int(scale * width)
        img = transforms.resize(img, (scaled_height, scaled_width),
                                PIL.Image.BICUBIC)
        label = transforms.resize(label[None], (scaled_height, scaled_width),
                                  PIL.Image.NEAREST)[0]

        # Crop
        if (scaled_height < self.crop_size[0]) or (scaled_width <
                                                   self.crop_size[1]):
            shorter_side = min(img.shape[1:])
            img, param = transforms.random_crop(img,
                                                (shorter_side, shorter_side),
                                                True)
        else:
            img, param = transforms.random_crop(img, self.crop_size, True)
        label = label[param['y_slice'], param['x_slice']]

        # Rotate
        angle = np.random.uniform(-10, 10)
        img = transforms.rotate(img, angle, expand=False)
        label = transforms.rotate(label[None],
                                  angle,
                                  expand=False,
                                  interpolation=PIL.Image.NEAREST,
                                  fill=-1)[0]

        # Resize
        if ((img.shape[1] < self.crop_size[0])
                or (img.shape[2] < self.crop_size[1])):
            img = transforms.resize(img, self.crop_size, PIL.Image.BICUBIC)
        if ((label.shape[0] < self.crop_size[0])
                or (label.shape[1] < self.crop_size[1])):
            label = transforms.resize(label[None].astype(np.float32),
                                      self.crop_size, PIL.Image.NEAREST)
            label = label.astype(np.int32)[0]

        # heightorizontal flip
        if self.horizontal_flip and np.random.rand() > 0.5:
            img = transforms.flip(img, x_flip=True)
            label = transforms.flip(label[None], x_flip=True)[0]

        # Mean subtraction
        img = img - self.mean
        return img, label
示例#5
0
def transform(inputs,
              mean,
              std,
              random_angle=15.,
              expand_ratio=1.0,
              crop_size=(32, 32),
              train=True):
    img, label = inputs
    img = img.copy()

    # Random rotate
    if random_angle != 0:
        angle = np.random.uniform(-random_angle, random_angle)
        img = cv_rotate(img, angle)

    # Cut out
    img = cut_out(img)

    # Standardization
    img -= mean[:, None, None]
    img /= std[:, None, None]

    if train:
        # Random flip
        img = transforms.random_flip(img, x_random=True)
        # Random expand
        if expand_ratio > 1:
            img = transforms.random_expand(img, max_ratio=expand_ratio)
        # Random crop
        if tuple(crop_size) != (32, 32):
            img = transforms.random_crop(img, tuple(crop_size))

    return img, label
示例#6
0
    def _add_noise(self, x, expand, angle, offset_range, s_range, r_width):
        ratio = random.uniform(1, expand)
        out_h, out_w = int(self.crop_size[0] * ratio), int(self.crop_size[1] *
                                                           ratio)
        x = cv2.resize(x.transpose(1, 2, 0), (out_h, out_w))
        if x.shape[2] == 1:
            x = cv2.cvtColor(x, cv2.COLOR_GRAY2RGB)

        x = x.transpose((2, 0, 1))
        # Color augmentation
        #            if self.pca_sigma != 0:
        #                            x = transforms.pca_lighting(x, self.pca_sigma)
        # Random rotate
        angle = np.random.uniform(-angle, angle)
        x = cv_rotate(x, angle)

        # Random flip
        x = transforms.random_flip(x, x_random=True, y_random=True)
        # Random expand
        #if expand_ratio > 1:
        #    img = transforms.random_expand(img, max_ratio=expand_ratio)
        # Random crop
        x = transforms.random_crop(x, self.crop_size)
        x = random_erasing(x, offset_range, s_range, r_width)
        return x
示例#7
0
def transform(inputs):
    img, label = inputs
    img = img.copy()
    img = pad(img, pad=4)
    img = transforms.random_flip(img, x_random=True)
    img = transforms.random_crop(img, (32, 32))
    return img, label
示例#8
0
    def get_example(self, index):
        x, y = super().get_example(index)
        # random_expandをする代わりに、最初に target_size * random_expandの大きさにリサイズしておいて、
        # あとでrandom croppingすればいいのではないかと思いました
        ratio = random.uniform(1, self.expand_ratio)
        out_h, out_w = int(
            self.crop_size[0] * ratio), int(self.crop_size[1] * ratio)
        x = cv2.resize(x.transpose(1, 2, 0), (out_h, out_w))
#        x = cv2.resize(x.transpose(1,2,0), (128, 128))

        if x.shape[2] == 1:
            x = cv2.cvtColor(x, cv2.COLOR_GRAY2RGB)

        x = x.transpose((2, 0, 1))

        if self.train:
            # Color augmentation
            #            if self.pca_sigma != 0:
            #                            x = transforms.pca_lighting(x, self.pca_sigma)
            # Random rotate
            if self.random_angle != 0:
                angle = np.random.uniform(-self.random_angle,
                                          self.random_angle)
                x = cv_rotate(x, angle)

            # Random flip
            x = transforms.random_flip(x, x_random=True, y_random=True)
            # Random expand
            # if expand_ratio > 1:
            #    img = transforms.random_expand(img, max_ratio=expand_ratio)
            # Random crop
            x = transforms.random_crop(x, self.crop_size)

        x /= 255.0
        return x, y
    def get_example(self, i):
        image = super().get_example(i)
        if image.shape[0] == 1:
            image = numpy.tile(image, (3, 1, 1))

        if self.augmentations is not None and self.use_imgaug:
            image = numpy.transpose(image, (1, 2, 0))
            image = image.astype(numpy.uint8)
            image = self.augmentations.augment_images([image])[0]
            image = image.astype(numpy.float32)
            image = numpy.transpose(image, (2, 0, 1))
        elif random.random() < self.transform_probability:
            if self.crop_always or random.random() <= 0.5:
                crop_ratio = random.uniform(self.min_crop_ratio,
                                            self.max_crop_ratio)
                image = transforms.random_crop(
                    image,
                    tuple(
                        [int(size * crop_ratio) for size in image.shape[-2:]]))
            image = transforms.random_flip(image, x_random=True)

        if self.image_size is not None:
            image = resize_image(image,
                                 self.image_size,
                                 image_mode=self.image_mode)

        if len(image.shape) == 2:
            image = image[None, ...]

        return image / 255
示例#10
0
def transform(
        inputs, mean, std, random_angle=15., pca_sigma=25.5, expand_ratio=1.2,
        crop_size=(28, 28), train=True):
    img = inputs
    img = img.copy()

    # Random rotate
    if random_angle != 0:
        angle = np.random.uniform(-random_angle, random_angle)
        img = cv_rotate(img, angle)

    # Color augmentation
    if train and pca_sigma != 0:
        img = transforms.pca_lighting(img, pca_sigma)

    """
    # Standardization
    img -= mean[:, None, None]
    img /= std[:, None, None]
    """

    if train:
        # Random flip
        img = transforms.random_flip(img, x_random=True)
        # Random expand
        if expand_ratio > 1:
            img = transforms.random_expand(img, max_ratio=expand_ratio)
        # Random crop
        if tuple(crop_size) != (32, 32):
            img = transforms.random_crop(img, tuple(crop_size))

    return img
示例#11
0
def transform_cifar10(data: Tuple[np.ndarray, np.ndarray],
                      mean: np.ndarray,
                      std: np.ndarray,
                      cutout_length: int,
                      crop_size=(32, 32),
                      train=True):
    img, label = data
    img = img.copy()

    # Standardization
    img -= mean[:, None, None]
    img /= std[:, None, None]

    if type(crop_size) == int:
        crop_size = (crop_size, crop_size)

    if train:
        # padding
        img = padding(img, pad=4)
        # Random flip
        img = transforms.random_flip(img, x_random=True)
        # Random crop
        if crop_size != (32, 32):
            img = transforms.random_crop(img, tuple(crop_size))
        # cutout
        img = cutout(img, cutout_length)

    return img.astype(np.float32), label
 def get_example(self, i):
     if self.imgtype == "npy":
         img = np.load(self.get_img_path(i))
         img = 2 * (np.clip(img, self.base, self.base + self.range) -
                    self.base) / self.range - 1.0
         if len(img.shape) == 2:
             img = img[np.newaxis, ]
     else:
         ref_dicom = dicom.read_file(self.get_img_path(i), force=True)
         #        print(ref_dicom)
         #        ref_dicom.file_meta.TransferSyntaxUID = dicom.uid.ImplicitVRLittleEndian
         img = ref_dicom.pixel_array + ref_dicom.RescaleIntercept
         img = self.img2var(img)
         img = img[np.newaxis, :, :]
     if self.scale_to > 0:
         img = resize(img, (self.scale_to, self.scale_to))
     H, W = self.crop
     #        print(img.shape)
     if img.shape[1] < H + 2 * self.random or img.shape[
             2] < W + 2 * self.random:
         p = max(H + 2 * self.random - img.shape[1],
                 W + 2 * self.random - img.shape[2])
         img = np.pad(img, ((0, 0), (p, p), (p, p)), 'edge')
     if H + self.random < img.shape[1] and W + self.random < img.shape[2]:
         img = center_crop(img, (H + self.random, W + self.random))
         img = random_crop(img, self.crop)
     return img
示例#13
0
def transform(images):
    input, answer = copy.deepcopy(images)

    # rotate
    deg = np.random.choice(DEG_RANGE)
    input = rotate_image(input, deg)
    answer = rotate_image(answer, deg)

    # resize
    H, W = input.shape[1:]
    h_resize = int(np.random.uniform(240, H * 2.0))
    w_resize = int(np.random.uniform(320, W * 2.0))
    input = chainercv.transforms.resize(input, (h_resize, w_resize))
    answer = chainercv.transforms.resize(answer, (h_resize, w_resize))

    # crop
    input, slice = transforms.random_crop(input, (240, 320), return_param=True)
    answer = answer[:, slice["y_slice"], slice['x_slice']]

    # flip
    input, param = transforms.random_flip(input,
                                          x_random=True,
                                          return_param=True)
    if param['x_flip']:
        transforms.flip(answer, x_flip=True)

    # pca_lighting:
    input = transforms.pca_lighting(input, sigma=5)

    return resize((input, answer))
示例#14
0
    def get_example(self, i):
        if self.imgtype == "npy":
            img = np.load(self.get_img_path(i))
            img = 2 * (np.clip(img, self.base, self.base + self.range) -
                       self.base) / self.range - 1.0
            if len(img.shape) == 2:
                img = img[np.newaxis, ]
        else:
            img = self.img2var(
                read_image(self.get_img_path(i), color=self.color))

#        img = resize(img, (self.resize_to, self.resize_to))
        if self.crop:
            H, W = self.crop
        else:
            H, W = (16 * ((img.shape[1] - 2 * self.random) // 16),
                    16 * ((img.shape[2] - 2 * self.random) // 16))
        if img.shape[1] < H + 2 * self.random or img.shape[
                2] < W + 2 * self.random:
            p = max(H + 2 * self.random - img.shape[1],
                    W + 2 * self.random - img.shape[2])
            img = np.pad(img, ((0, 0), (p, p), (p, p)), 'edge')
        img = random_crop(
            center_crop(img, (H + 2 * self.random, W + 2 * self.random)),
            (H, W))
        if self.random:
            img = random_flip(img, x_random=True)
        return img.astype(self.dtype)
示例#15
0
def transform(
        inputs, mean, std, random_angle=15., pca_sigma=255., expand_ratio=1.0,
        crop_size=(32, 32), train=True):
    img, label = inputs
    img = img.copy()

    # Random rotate
    if random_angle != 0:
        angle = np.random.uniform(-random_angle, random_angle)
        img = cv_rotate(img, angle)

    # Color augmentation
    if train and pca_sigma != 0:
        img = transforms.pca_lighting(img, pca_sigma)

    # Standardization
    img -= mean[:, None, None]
    img /= std[:, None, None]

    if train:
        # Random flip
        img = transforms.random_flip(img, x_random=True)
        # Random expand
        if expand_ratio > 1:
            img = transforms.random_expand(img, max_ratio=expand_ratio)
        # Random crop
        if tuple(crop_size) != (32, 32):
            img = transforms.random_crop(img, tuple(crop_size))

    return img, label
示例#16
0
 def get_example(self, i):
     img = read_image(self.get_img_path(i), color=self.color)
     img = img * 2 / 255.0 - 1.0  # [-1, 1)
     #        img = resize(img, (self.resize_to, self.resize_to))
     img = random_crop(img, self.crop)
     if self.flip:
         img = random_flip(img, x_random=True)
     return img.astype(self.dtype)
示例#17
0
 def preprocess(self, image):
     image = self.normalize(image, self.mean, self.std)
     if not self.train:
         return image
     else:
         image = T.random_flip(image, x_random=True)
         image = padding(image, 4)
         image = T.random_crop(image, (32, 32))
         return image
示例#18
0
 def random_crop(self, im, exp):
     image_set = []
     for i in range(exp):
         image, param = transforms.random_crop(
             np.moveaxis(im, 2,0),
             size=(224, 224),
             return_param=True
         )
         image_set.append(np.moveaxis(image, 0, 2))
     return image_set
示例#19
0
 def __call__(self, img):
     img = random_crop(img=img, size=self.resize_value)
     img = random_flip(img=img, x_random=True)
     img = pca_lighting(img=img, sigma=25.5)
     img = scale(img=img, size=self.resize_value)
     img = center_crop(img, self.input_image_size)
     img /= 255.0
     img -= self.mean
     img /= self.std
     return img
示例#20
0
def transform(inputs,
              mean=None,
              std=None,
              random_angle=15.,
              pca_sigma=255.,
              expand_ratio=1.0,
              crop_size=(32, 32),
              cutout=None,
              flip=True,
              train=True,
              old_test_method=False):
    img, label = inputs
    img = img.copy()

    if train:
        # Random rotate
        if random_angle != 0:
            angle = np.random.uniform(-random_angle, random_angle)
            img = cv_rotate(img, angle)

        # Color augmentation
        if pca_sigma != 0:
            img = transforms.pca_lighting(img, pca_sigma)

    elif old_test_method:
        # There was a bug in prior versions, here it is reactivated to preserve
        # the same test accuracy
        if random_angle != 0:
            angle = np.random.uniform(-random_angle, random_angle)
            img = cv_rotate(img, angle)

    # Standardization
    if mean is not None:
        img -= mean[:, None, None]
        img /= std[:, None, None]

    if train:
        # Random flip
        if flip:
            img = transforms.random_flip(img, x_random=True)

        # Random expand
        if expand_ratio > 1:
            img = transforms.random_expand(img, max_ratio=expand_ratio)

        # Random crop
        if tuple(crop_size) != (32, 32) or expand_ratio > 1:
            img = transforms.random_crop(img, tuple(crop_size))

        # Cutout
        if cutout is not None:
            h0, w0 = np.random.randint(0, 32 - cutout, size=(2, ))
            img[:, h0:h0 + cutout, w0:w0 + cutout].fill(0.0)

    return img, label
示例#21
0
    def __call__(self, in_data):
        img, label = in_data

        img = np.pad(img, ((0, 0), (4, 4), (4, 4)), 'constant')
        img = random_crop(img, (32, 32))
        img = random_flip(img, x_random=True)
        img = (img - CIFAR_MEAN[:, None, None]) / CIFAR_STD[:, None, None]

        if self.use_cutout:
            img = Cutout(self.cutout_length)(img)
        return img, label
示例#22
0
    def get_example(self, i):
        img = read_image(self.get_img_path(i))
        img = img.astype('f')
        img = img * 2 / 255.0 - 1.0  # [-1, 1)

        img = resize(img, (self.resize_to, self.resize_to))
        if self.resize_to > self.crop_to:
            img = random_crop(img, (self.crop_to, self.crop_to))
        if self.flip:
            img = random_flip(img, x_random=True)
        return img
示例#23
0
def train_transform_office(in_data):
    mean = np.load('imagenet_mean.npy').reshape(3, 256, 256).astype('f')
    crop_size = 227

    img, label = in_data
    # subtract the mean file
    img = img - mean
    # random crop image to 227x227
    img = random_crop(img, (crop_size, crop_size))
    # random mirror the image
    img = random_flip(img, x_random=True)
    return img, label
示例#24
0
    def transform(in_data):
        img, label = in_data
        img = img.copy()

        img -= mean[:, None, None]
        img /= std[:, None, None]

        img = T.resize_contain(img, (40, 40))
        img = T.random_crop(img, (32, 32))
        img = T.random_flip(img, x_random=True)

        return img, label
 def __call__(self, in_data):
     img, label = in_data
     #         img = transforms.resize(img, self.size)
     img = transforms.random_crop(img, self.size)
     img = transforms.flip(img, y_flip=self.y_flip, x_flip=self.x_flip)
     img = transforms.random_rotate(img)
     ######################## perimage normalize #############
     ms_im = img.reshape((img.shape[0], -1))
     mean = np.mean(ms_im, axis=1)[:, np.newaxis, np.newaxis]
     std = np.std(ms_im, axis=1)[:, np.newaxis, np.newaxis]
     img = (img - mean) / (std + 0.0000001)
     ####################################################
     return img, label
示例#26
0
def augment_data(image, resize_width, resize_height, use_random_x_flip,
                 use_random_y_flip, use_random_rotate, use_pca_lighting,
                 crop_edit, crop_width, crop_height):
    image = transforms.random_flip(image, use_random_x_flip, use_random_y_flip)
    if use_random_rotate:
        image = transforms.random_rotate(image)
    image = transforms.pca_lighting(image, sigma=use_pca_lighting)

    if crop_edit == 'Center Crop':
        image = transforms.center_crop(image, (crop_width, crop_height))
    elif crop_edit == 'Random Crop':
        image = transforms.random_crop(image, (crop_width, crop_height))
    image = transforms.resize(image, (resize_width, resize_height))
    return image
示例#27
0
 def __call__(self, inputs, train=True):
     img, label = inputs
     img = img.copy()
     # Color augmentation
     if train and self.pca:
         img = transforms.pca_lighting(img, 76.5)
     # Standardization
     img -= self.mean
     img *= self.invstd
     # Random crop
     if train and self.trans:
         img = transforms.random_flip(img, x_random=True)
         img = transforms.random_expand(img, max_ratio=1.5)
         img = transforms.random_crop(img, (28, 28))
     return img, label
def transform_img(inputs,
                  mean,
                  std,
                  pca_sigma=0,
                  random_angle=0,
                  x_random_flip=False,
                  y_random_flip=False,
                  expand_ratio=1.,
                  random_crop_size=(224, 224),
                  random_erase=False,
                  output_size=(224, 224),
                  train=False):
    x, lab = inputs
    x = x.copy()
    # Color augmentation
    if train and pca_sigma != 0:
        x = transforms.pca_lighting(x, pca_sigma)
    x -= mean[:, None, None]
    x /= std[:, None, None]
    x = x[::-1]
    if train:
        # Random rotate
        if random_angle != 0:
            angle = np.random.uniform(-random_angle, random_angle)
            x = cv_rotate(x, angle)

        # Random flip
        if x_random_flip or y_random_flip:
            x = transforms.random_flip(x,
                                       x_random=x_random_flip,
                                       y_random=y_random_flip)

        # Random expand
        if expand_ratio > 1:
            x = transforms.random_expand(x, max_ratio=expand_ratio)

        if all(random_crop_size) > 0:
            x = transforms.random_crop(x, random_crop_size)
        else:
            if random_erase:
                x = random_erasing(x)

    if all(random_crop_size) > 0:
        x = transforms.resize(x, random_crop_size)
    else:
        x = transforms.resize(x, output_size)

    return x, lab
示例#29
0
    def __call__(self, chw):
        chw = chw.astype(np.uint8)
        if self.p_blur > 0:
            chw = random_blur(chw, self.p_blur, self.blur_max_ksize)
        if self.p_add_lines > 0:
            chw = add_random_lines(chw, self.p_add_lines, self.max_num_lines)
        chw = transforms.random_flip(chw, False, True)
        chw, param_stretch = random_stretch(chw,
                                            self.max_horizontal_factor,
                                            return_param=True)
        chw = inscribed_center_crop(chw)
        chw = transforms.scale(chw, self.scaled_size)
        #        chw = transforms.center_crop(chw, (256, 256))
        chw = transforms.random_crop(chw, (self.crop_size, self.crop_size))
        chw = chw.astype(np.float32) / 256.0

        return chw, param_stretch['log_ar'].astype(np.float32)
示例#30
0
    def get_example(self, i):
        """ Pad input with length 4, random crop to 32, together with random horizontal flipping. """
        x, y = self.pairs[i]
        # label
        y = np.array(y, dtype=np.int32)

        # padding with length-4 zeros
        pad_x = np.zeros((3, 40, 40), dtype=x.dtype)
        pad_x[:, 4:36, 4:36] = x
        # random cropping
        x = transforms.random_crop(pad_x, (32, 32))
        # random horizontal flipping
        x = transforms.random_flip(x, x_random=True)
        # normalize
        x = (x - self.mean) / self.std

        return x, y
示例#31
0
def transform(inputs, mean, std, crop_size=(32, 32), train=True):
    img, label = inputs
    img = img.copy()

    # Standardization
    img -= mean[:, None, None]
    img /= std[:, None, None]

    if train:
        # Random flip
        img = transforms.random_flip(img, x_random=True)
        # zero_pad
        img = np.pad(img, ((0, ), (4, ), (4, )), 'constant')
        # Random crop
        if tuple(crop_size) != (40, 40):
            img = transforms.random_crop(img, tuple(crop_size))

    return img, label
 def __call__(self, in_data):
     img, label = in_data
     #         img = transforms.resize(img, self.size)
     img = transforms.random_crop(img, self.size)
     img = transforms.random_rotate(img)
     img = transforms.random_flip(img,
                                  x_random=True,
                                  y_random=True,
                                  return_param=False)
     #         aug = RandomBrightnessContrast(p=0.5) # WIP
     # #         aug = GaussNoise(p=0.5) # WIP
     #         img = aug(image=img)['image']
     ######################## perimage normalize #############
     ms_im = img.reshape((img.shape[0], -1))
     mean = np.mean(ms_im, axis=1)[:, np.newaxis, np.newaxis]
     std = np.std(ms_im, axis=1)[:, np.newaxis, np.newaxis]
     img = (img - mean) / (std + 0.0000001)
     ####################################################
     return img, label