Example #1
0
def array_to_img(x, data_format=None, scale=True, dtype=None):
    if data_format is None:
        data_format = backend.image_data_format()
    if 'dtype' in generic_utils.getargspec(image.array_to_img).args:
        if dtype is None:
            dtype = backend.floatx()
        return image.array_to_img(x,
                                  data_format=data_format,
                                  scale=scale,
                                  dtype=dtype)
    return image.array_to_img(x,
                              data_format=data_format,
                              scale=scale)
Example #2
0
def array_to_img(x, data_format=None, scale=True, dtype=None):
  """Converts a 3D Numpy array to a PIL Image instance.

  Arguments:
      x: Input Numpy array.
      data_format: Image data format.
          either "channels_first" or "channels_last".
      scale: Whether to rescale image values
          to be within `[0, 255]`.
      dtype: Dtype to use.

  Returns:
      A PIL Image instance.

  Raises:
      ImportError: if PIL is not available.
      ValueError: if invalid `x` or `data_format` is passed.
  """

  if data_format is None:
    data_format = backend.image_data_format()
  kwargs = {}
  if 'dtype' in tf_inspect.getfullargspec(image.array_to_img)[0]:
    if dtype is None:
      dtype = backend.floatx()
    kwargs['dtype'] = dtype
  return image.array_to_img(x, data_format=data_format, scale=scale, **kwargs)
                             zoom_range=0.2,
                             horizontal_flip=True,
                             fill_mode='nearest')

fnames = [
    os.path.join(train_cats_dir, fname) for fname in os.listdir(train_cats_dir)
]

img_path = fnames[3]
img = image.load_img(img_path, target_size=(150, 150))
x = image.img_to_array(img)
x = x.reshape((1, ) + x.shape)
i = 0
for batch in datagen.flow(x, batch_size=1):
    plt.figure(i)
    imgplot = plt.imshow(image.array_to_img(batch[0]))
    i += 1
    if i % 4 == 0:
        break
plt.show()

train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
)
    def test_img_utils(self):
        height, width = 10, 8

        # Test th data format
        # Test RGB 3D
        x = np.random.random((3, height, width))
        img = image.array_to_img(x, data_format='channels_first')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_first')
        assert x.shape == (3, height, width)
        # Test RGBA 3D
        x = np.random.random((4, height, width))
        img = image.array_to_img(x, data_format='channels_first')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_first')
        assert x.shape == (4, height, width)
        # Test 2D
        x = np.random.random((1, height, width))
        img = image.array_to_img(x, data_format='channels_first')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_first')
        assert x.shape == (1, height, width)

        # Test tf data format
        # Test RGB 3D
        x = np.random.random((height, width, 3))
        img = image.array_to_img(x, data_format='channels_last')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_last')
        assert x.shape == (height, width, 3)
        # Test RGBA 3D
        x = np.random.random((height, width, 4))
        img = image.array_to_img(x, data_format='channels_last')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_last')
        assert x.shape == (height, width, 4)
        # Test 2D
        x = np.random.random((height, width, 1))
        img = image.array_to_img(x, data_format='channels_last')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_last')
        assert x.shape == (height, width, 1)

        # Test invalid use case
        with pytest.raises(ValueError):
            x = np.random.random((height, width))  # not 3D
            img = image.array_to_img(x, data_format='channels_first')
        with pytest.raises(ValueError):
            x = np.random.random((height, width, 3))
            # unknown data_format
            img = image.array_to_img(x, data_format='channels')
        with pytest.raises(ValueError):
            # neither RGB, RGBA, or gray-scale
            x = np.random.random((height, width, 5))
            img = image.array_to_img(x, data_format='channels_last')
        with pytest.raises(ValueError):
            x = np.random.random((height, width, 3))
            # unknown data_format
            img = image.img_to_array(x, data_format='channels')
        with pytest.raises(ValueError):
            # neither RGB, RGBA, or gray-scale
            x = np.random.random((height, width, 5, 3))
            img = image.img_to_array(x, data_format='channels_last')
Example #5
0
    def test_load_img(self, tmpdir):
        filename = str(tmpdir / 'image.png')
        filename_16bits = str(tmpdir / 'image.tif')

        original_im_array = np.array(255 * np.random.rand(100, 100, 3),
                                     dtype=np.uint8)
        original_im = image.array_to_img(original_im_array, scale=False)
        original_im.save(filename)

        original_im_array_16bits = np.array(65535 *
                                            np.random.rand(100, 100, 1),
                                            dtype=np.uint16)
        original_im_16bits = image.array_to_img(original_im_array, scale=False)
        original_im_16bits.save(filename_16bits)

        # Test that loaded 8 bits image is exactly equal to original.

        loaded_im = image.load_img(filename)
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == original_im_array.shape
        assert np.all(loaded_im_array == original_im_array)

        loaded_im = image.load_img(filename, grayscale=True)
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (original_im_array.shape[0],
                                         original_im_array.shape[1], 1)

        # Test that loaded 16bits image is exactly equal to original.

        loaded_im = image.load_img(filename_16bits)
        loaded_im_array = image.img_to_array(loaded_im, colormode='grayscale')
        assert loaded_im_array.shape == original_im_array_16bits.shape
        assert np.all(loaded_im_array == original_im_array_16bits)

        # Test that nothing is changed when target size is equal to original.

        loaded_im = image.load_img(filename, target_size=(100, 100))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == original_im_array.shape
        assert np.all(loaded_im_array == original_im_array)

        loaded_im = image.load_img(filename,
                                   grayscale=True,
                                   target_size=(100, 100))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (original_im_array.shape[0],
                                         original_im_array.shape[1], 1)

        # Test down-sampling with bilinear interpolation.

        loaded_im = image.load_img(filename, target_size=(25, 25))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (25, 25, 3)

        loaded_im = image.load_img(filename,
                                   grayscale=True,
                                   target_size=(25, 25))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (25, 25, 1)

        # Test down-sampling with nearest neighbor interpolation.

        loaded_im_nearest = image.load_img(filename,
                                           target_size=(25, 25),
                                           interpolation="nearest")
        loaded_im_array_nearest = image.img_to_array(loaded_im_nearest)
        assert loaded_im_array_nearest.shape == (25, 25, 3)
        assert np.any(loaded_im_array_nearest != loaded_im_array)

        # Check that exception is raised if interpolation not supported.

        loaded_im = image.load_img(filename, interpolation="unsupported")
        with pytest.raises(ValueError):
            loaded_im = image.load_img(filename,
                                       target_size=(25, 25),
                                       interpolation="unsupported")
Example #6
0
File: GAN.py Project: ljldgup/ml
        labels = np.concatenate([np.ones((batch_size, 1)),
                                 np.zeros((batch_size, 1))])
        # 向标签中添加随机噪声,这是一个很重要的技巧
        labels += 0.05 * np.random.random(labels.shape)

        # 训练判别器
        d_loss = discriminator.train_on_batch(combined_images, labels)
        random_latent_vectors = np.random.normal(size=(batch_size,
                                                       latent_dim))

        #合并标签,全部是“真实图像”(这是在撒谎)
        misleading_targets = np.zeros((batch_size, 1))

        # 通过gan 模型来训练生成器( 此时冻结判别器权重)
        a_loss = gan.train_on_batch(random_latent_vectors,
                                    misleading_targets)
        start += batch_size
        if start > len(x_train) - batch_size:
            start = 0

        if step % 100 == 0:
            gan.save_weights('gan.h5')
            print('discriminator loss:', d_loss)
            print('adversarial loss:', a_loss)
            img = array_to_img(generated_images[0] * 255., scale=False)
            img.save(os.path.join(save_dir,
                                  'generated_frog' + str(step) + '.png'))
            img = array_to_img(real_images[0] * 255., scale=False)
            img.save(os.path.join(save_dir,
                                  'real_frog' + str(step) + '.png'))
Example #7
0
    def test_load_img(self, tmpdir):
        filename_rgb = str(tmpdir / 'rgb_image.png')
        filename_rgba = str(tmpdir / 'rgba_image.png')

        original_rgb_array = np.array(255 * np.random.rand(100, 100, 3),
                                      dtype=np.uint8)
        original_rgb = image.array_to_img(original_rgb_array, scale=False)
        original_rgb.save(filename_rgb)

        original_rgba_array = np.array(255 * np.random.rand(100, 100, 4),
                                       dtype=np.uint8)
        original_rgba = image.array_to_img(original_rgba_array, scale=False)
        original_rgba.save(filename_rgba)

        # Test that loaded image is exactly equal to original.

        loaded_im = image.load_img(filename_rgb)
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == original_rgb_array.shape
        assert np.all(loaded_im_array == original_rgb_array)

        loaded_im = image.load_img(filename_rgba, color_mode='rgba')
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == original_rgba_array.shape
        assert np.all(loaded_im_array == original_rgba_array)

        loaded_im = image.load_img(filename_rgb, color_mode='grayscale')
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (original_rgb_array.shape[0],
                                         original_rgb_array.shape[1], 1)

        # Test that nothing is changed when target size is equal to original.

        loaded_im = image.load_img(filename_rgb, target_size=(100, 100))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == original_rgb_array.shape
        assert np.all(loaded_im_array == original_rgb_array)

        loaded_im = image.load_img(filename_rgba,
                                   color_mode='rgba',
                                   target_size=(100, 100))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == original_rgba_array.shape
        assert np.all(loaded_im_array == original_rgba_array)

        loaded_im = image.load_img(filename_rgb,
                                   color_mode='grayscale',
                                   target_size=(100, 100))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (original_rgba_array.shape[0],
                                         original_rgba_array.shape[1], 1)

        # Test down-sampling with bilinear interpolation.

        loaded_im = image.load_img(filename_rgb, target_size=(25, 25))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (25, 25, 3)

        loaded_im = image.load_img(filename_rgba,
                                   color_mode='rgba',
                                   target_size=(25, 25))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (25, 25, 4)

        loaded_im = image.load_img(filename_rgb,
                                   color_mode='grayscale',
                                   target_size=(25, 25))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (25, 25, 1)

        # Test down-sampling with nearest neighbor interpolation.

        loaded_im_nearest = image.load_img(filename_rgb,
                                           target_size=(25, 25),
                                           interpolation="nearest")
        loaded_im_array_nearest = image.img_to_array(loaded_im_nearest)
        assert loaded_im_array_nearest.shape == (25, 25, 3)
        assert np.any(loaded_im_array_nearest != loaded_im_array)

        loaded_im_nearest = image.load_img(filename_rgba,
                                           color_mode='rgba',
                                           target_size=(25, 25),
                                           interpolation="nearest")
        loaded_im_array_nearest = image.img_to_array(loaded_im_nearest)
        assert loaded_im_array_nearest.shape == (25, 25, 4)
        assert np.any(loaded_im_array_nearest != loaded_im_array)

        # Check that exception is raised if interpolation not supported.

        loaded_im = image.load_img(filename_rgb, interpolation="unsupported")
        with pytest.raises(ValueError):
            loaded_im = image.load_img(filename_rgb,
                                       target_size=(25, 25),
                                       interpolation="unsupported")
Example #8
0
    def _get_batches_of_transformed_samples(self, index_array):
        """Gets a batch of transformed samples.
        # Arguments
            index_array: Array of sample indices to include in batch.
        # Returns
            A batch of transformed samples.
        """
        TimeSteps = self.image_data_generator.time_steps
        batch_x = np.zeros(
            (len(index_array), ) + (TimeSteps, ) + self.image_shape,
            dtype=self.dtype)  #KJ
        # build batch of image data
        # self.filepaths is dynamic, is better to call it once outside the loop
        filepaths = self.filepaths
        for i, j in enumerate(index_array):
            for k in reversed(range(0, TimeSteps)):
                try:
                    img = load_img(filepaths[j - k],
                                   color_mode=self.color_mode,
                                   target_size=self.target_size,
                                   interpolation=self.interpolation)
                    x = img_to_array(img, data_format=self.data_format)
                    # Pillow images should be closed after `load_img`,
                    # but not PIL images.
                    if hasattr(img, 'close'):
                        img.close()
                except:
                    pass

                if self.image_data_generator:
                    params = self.image_data_generator.get_random_transform(
                        x.shape)
                    x = self.image_data_generator.apply_transform(x, params)
                    x = self.image_data_generator.standardize(x)
                batch_x[i][k] = x
        # optionally save augmented images to disk for debugging purposes
        if self.save_to_dir:
            for i, j in enumerate(index_array):
                img = array_to_img(batch_x[i], self.data_format, scale=True)
                fname = '{prefix}_{index}_{hash}.{format}'.format(
                    prefix=self.save_prefix,
                    index=j,
                    hash=np.random.randint(1e7),
                    format=self.save_format)
                img.save(os.path.join(self.save_to_dir, fname))
        # build batch of labels
        if self.class_mode == 'input':
            batch_y = batch_x.copy()
        elif self.class_mode in {'binary', 'sparse'}:
            batch_y = np.empty(len(batch_x), dtype=self.dtype)
            for i, n_observation in enumerate(index_array):
                batch_y[i] = self.classes[n_observation]
        elif self.class_mode == 'categorical':
            batch_y = np.zeros(
                (len(batch_x), TimeSteps, len(self.class_indices)),
                dtype=self.dtype)
            for i, n_observation in enumerate(index_array):
                for q in reversed(range(0, TimeSteps)):
                    batch_y[i, q, self.classes[n_observation - q]] = 1.
        elif self.class_mode == 'multi_output':
            batch_y = [output[index_array] for output in self.labels]
        elif self.class_mode == 'raw':
            batch_y = self.labels[index_array]
        else:
            return batch_x
        if self.sample_weight is None:
            return batch_x, batch_y
        else:
            return batch_x, batch_y, self.sample_weight[index_array]
Example #9
0
    #,   shear_range = 0.2
    #,   zoom_range = 0.2
    #,   horizontal_flip = True
    #,   fill_mode = 'nearest'
)

valid_test_data_gen = ImageDataGenerator()

# here is some code showing an example of what the image augmentation is doing:
my_img = load_img(
    'C://Users//jbolton//Documents//naughty//deep_tagger//images//prod_35.png')
my_img = img_to_array(my_img)
my_img = my_img.reshape((1, ) + my_img.shape)
i = 0
for batch_i in train_data_gen.flow(my_img, batch_size=1):
    array_to_img(batch_i[0]).show()
    i += 1
    if i > 5:
        break

img_size_for_model = (224, 224)

group_proportions = {'train': 0.5, 'validate': 0.3, 'test': 0.2}
group_assignments = np.random.choice(a=['train', 'validate', 'test'],
                                     size=len(data_dict_df),
                                     replace=True,
                                     p=[
                                         group_proportions['train'],
                                         group_proportions['validate'],
                                         group_proportions['test']
                                     ])
Example #10
0
def save_image_from_tensor(save_directory: str, file_name: str,
                           tensor_image: tf.Tensor) -> None:
    save_path = join(save_directory, file_name)
    image = array_to_img(tensor_image[0])
    image.save(save_path)
Example #11
0
    def transform_fn(self, image, mask):
        if self.num_classes == 1:
            ### Converts a torch.*Tensor of shape C x H x W or a numpy ndarray of shape H x W x C to a PIL Image while preserving the value range.
            image = array_to_img(image, data_format="channels_last")
            mask = array_to_img(mask, data_format="channels_last")
            ## Input type float32 is not supported

            ##!!!
            ## the preprocess funcions from Keras are very convenient
            ##!!!

            # Resize
            # resize = transforms.Resize(size=(520, 520))
            # image = resize(image)
            # mask = resize(mask)

            # Random crop
            # i, j, h, w = transforms.RandomCrop.get_params(
            #    image, output_size=(512, 512))
            # image = TF.crop(image, i, j, h, w)
            # mask = TF.crop(mask, i, j, h, w)

            ## https://pytorch.org/docs/stable/torchvision/transforms.html
            ## https://github.com/pytorch/vision/blob/master/torchvision/transforms/functional.py
            # Random horizontal flipping
            if random.random() > 0.5:
                image = TF.hflip(image)
                mask = TF.hflip(mask)

            # Random vertical flipping
            if random.random() > 0.5:
                image = TF.vflip(image)
                mask = TF.vflip(mask)

            # Random to_grayscale
            # if random.random() > 0.6:
            #     image = TF.to_grayscale(image, num_output_channels=3)

            angle = random.randint(0, 90)
            translate = (random.uniform(0, 100), random.uniform(0, 100))
            scale = random.uniform(0.5, 2)
            shear = random.uniform(-10, 10)
            image = TF.affine(image, angle, translate, scale, shear)
            mask = TF.affine(mask, angle, translate, scale, shear)

            # Random adjust_brightness
            image = TF.adjust_brightness(image,
                                         brightness_factor=random.uniform(
                                             0.8, 1.2))

            # Random adjust_saturation
            image = TF.adjust_saturation(image,
                                         saturation_factor=random.uniform(
                                             0.8, 1.2))

            # Random adjust_hue
            # `hue_factor` is the amount of shift in H channel and must be in the
            #     interval `[-0.5, 0.5]`.
            # image = TF.adjust_hue(image, hue_factor=random.uniform(-0.2, 0.2))

            # image = TF.adjust_gamma(image, gamma=random.uniform(0.8, 1.5), gain=1)

            angle = random.randint(0, 90)
            image = TF.rotate(image, angle)
            mask = TF.rotate(mask, angle)

            # Transform to tensor
            image = img_to_array(image, data_format="channels_last")
            mask = img_to_array(mask, data_format="channels_last")

        else:
            ### Converts a torch.*Tensor of shape C x H x W or a numpy ndarray of shape H x W x C to a PIL Image while preserving the value range.
            image = array_to_img(image, data_format="channels_last")
            mask_pil_array = [None] * mask.shape[-1]
            for i in range(mask.shape[-1]):
                mask_pil_array[i] = array_to_img(mask[:, :, i, np.newaxis],
                                                 data_format="channels_last")

            ## https://pytorch.org/docs/stable/torchvision/transforms.html
            ## https://github.com/pytorch/vision/blob/master/torchvision/transforms/functional.py
            # Random horizontal flipping
            if random.random() > 0.5:
                image = TF.hflip(image)
                for i in range(mask.shape[-1]):
                    mask_pil_array[i] = TF.hflip(mask_pil_array[i])

            # Random vertical flipping
            if random.random() > 0.5:
                image = TF.vflip(image)
                for i in range(mask.shape[-1]):
                    mask_pil_array[i] = TF.vflip(mask_pil_array[i])

            # Random to_grayscale
            # if random.random() > 0.6:
            #     image = TF.to_grayscale(image, num_output_channels=3)

            angle = random.randint(0, 90)
            translate = (random.uniform(0, 100), random.uniform(0, 100))
            scale = random.uniform(0.5, 2)
            shear = random.uniform(0, 0)
            image = TF.affine(image, angle, translate, scale, shear)
            for i in range(mask.shape[-1]):
                mask_pil_array[i] = TF.affine(mask_pil_array[i], angle,
                                              translate, scale, shear)

            # Random adjust_brightness
            image = TF.adjust_brightness(image,
                                         brightness_factor=random.uniform(
                                             0.8, 1.2))

            # Random adjust_saturation
            image = TF.adjust_saturation(image,
                                         saturation_factor=random.uniform(
                                             0.8, 1.2))

            # Random adjust_hue
            # `hue_factor` is the amount of shift in H channel and must be in the
            #     interval `[-0.5, 0.5]`.
            # image = TF.adjust_hue(image, hue_factor=random.uniform(-0.2, 0.2))

            # image = TF.adjust_gamma(image, gamma=random.uniform(0.8, 1.5), gain=1)

            # angle = random.randint(0, 90)
            # image = TF.rotate(image, angle)
            # for i in range(mask.shape[-1]):
            #    mask_pil_array[i] = TF.rotate(mask_pil_array[i], angle)

            # Transform to tensor
            image = img_to_array(image, data_format="channels_last")
            for i in range(mask.shape[-1]):
                # img_to_array(mask_pil_array[i], data_format="channels_last"): 512, 512, 1
                mask[:, :, i] = img_to_array(
                    mask_pil_array[i],
                    data_format="channels_last")[:, :, 0].astype('uint8')

        ### img_to_array will scale the image to (0,255)
        ### when use img_to_array, the image and mask will in (0,255)
        image = (image / 255.0).astype('float32')
        mask = (mask / 255.0).astype('uint8')
        # print(11)
        return image, mask