예제 #1
0
    def _get_batches_of_transformed_samples(self, index_array):
        V_red = self.image_data_generator.V_red
        batch_x = np.zeros(
            (len(index_array), V_red.shape[1]), dtype=self.dtype
        )  #tuple([len(index_array)] + list(self.x.shape)[1:]),
        # dtype=self.dtype)
        for i, j in enumerate(index_array):
            x = self.x[j]
            params = self.image_data_generator.get_random_transform(x.shape)
            x = self.image_data_generator.apply_transform(
                x.astype(self.dtype), params)
            # x = self.image_data_generator.standardize(x)
            batch_x[i] = np.dot(np.reshape(x, (1, 2916)), V_red)

        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(1e4),
                    format=self.save_format)
                img.save(os.path.join(self.save_to_dir, fname))
        batch_x_miscs = [xx[index_array] for xx in self.x_misc]
        output = (batch_x if batch_x_miscs == [] else [batch_x] +
                  batch_x_miscs, )
        if self.y is None:
            return output[0]
        output += (self.y[index_array], )
        if self.sample_weight is not None:
            output += (self.sample_weight[index_array], )
        return output
    def _get_batches_of_transformed_samples(self, index_array):

        # build batch of image data
        batch_x = np.array([self.x[j] for j in index_array])

        # transform the image data
        batch_x = np.array(
            [self.image_data_generator.transform_image(x) for x in batch_x])

        if self.y is not None:
            batch_y = np.array([self.y[j] for j in index_array])

        else:
            batch_y = np.array([])

        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(1e4),
                    format=self.save_format)
                img.save(os.path.join(self.save_to_dir, fname))
        batch_x_miscs = [xx[index_array] for xx in self.x_misc]
        output = (batch_x if batch_x_miscs == [] else [batch_x] +
                  batch_x_miscs, )
        if self.y is None:
            return output[0]

        output += (batch_y, )
        if self.sample_weight is not None:
            output += (self.sample_weight[index_array], )
        return output
예제 #3
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.
        """
        # build batch of image data
        # self.filepaths is dynamic, is better to call it once outside the loop
        filepaths = self.filepaths

        # build batch of image data
        batch_x = np.array([
            load_img(filepaths[x],
                     color_mode=self.color_mode,
                     target_size=self.target_size,
                     interpolation=self.interpolation) for x in index_array
        ])
        # transform the image data
        batch_x = np.array(
            [self.image_data_generator.transform_image(x) for x in batch_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), len(self.class_indices)),
                               dtype=self.dtype)
            for i, n_observation in enumerate(index_array):
                batch_y[i, self.classes[n_observation]] = 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]
예제 #4
0
def test_array_to_img_and_img_to_array():
    height, width = 10, 8

    # Test the data format
    # Test RGB 3D
    x = np.random.random((3, height, width))
    img = utils.array_to_img(x, data_format='channels_first')
    assert img.size == (width, height)

    x = utils.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 = utils.array_to_img(x, data_format='channels_first')
    assert img.size == (width, height)

    x = utils.img_to_array(img, data_format='channels_first')
    assert x.shape == (4, height, width)

    # Test 2D
    x = np.random.random((1, height, width))
    img = utils.array_to_img(x, data_format='channels_first')
    assert img.size == (width, height)

    x = utils.img_to_array(img, data_format='channels_first')
    assert x.shape == (1, height, width)

    # grayscale 32-bit signed integer
    x = np.array(
        np.random.randint(-2147483648, 2147483647, (1, height, width)),
        dtype=np.int32
    )
    img = utils.array_to_img(x, data_format='channels_first')
    assert img.size == (width, height)

    x = utils.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 = utils.array_to_img(x, data_format='channels_last')
    assert img.size == (width, height)

    x = utils.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 = utils.array_to_img(x, data_format='channels_last')
    assert img.size == (width, height)

    x = utils.img_to_array(img, data_format='channels_last')
    assert x.shape == (height, width, 4)

    # Test 2D
    x = np.random.random((height, width, 1))
    img = utils.array_to_img(x, data_format='channels_last')
    assert img.size == (width, height)

    x = utils.img_to_array(img, data_format='channels_last')
    assert x.shape == (height, width, 1)

    # grayscale 16-bit signed integer
    x = np.array(
        np.random.randint(-2147483648, 2147483647, (height, width, 1)),
        dtype=np.int16
    )
    img = utils.array_to_img(x, data_format='channels_last')
    assert img.size == (width, height)

    x = utils.img_to_array(img, data_format='channels_last')
    assert x.shape == (height, width, 1)

    # grayscale 32-bit signed integer
    x = np.array(
        np.random.randint(-2147483648, 2147483647, (height, width, 1)),
        dtype=np.int32
    )
    img = utils.array_to_img(x, data_format='channels_last')
    assert img.size == (width, height)

    x = utils.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 = utils.array_to_img(x, data_format='channels_first')

    with pytest.raises(ValueError):
        x = np.random.random((height, width, 3))
        # unknown data_format
        img = utils.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 = utils.array_to_img(x, data_format='channels_last')

    with pytest.raises(ValueError):
        x = np.random.random((height, width, 3))
        # unknown data_format
        img = utils.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 = utils.img_to_array(x, data_format='channels_last')
예제 #5
0
def test_load_img(tmpdir):
    filename_rgb = str(tmpdir / 'rgb_utils.png')
    filename_rgba = str(tmpdir / 'rgba_utils.png')
    filename_grayscale_8bit = str(tmpdir / 'grayscale_8bit_utils.png')
    filename_grayscale_16bit = str(tmpdir / 'grayscale_16bit_utils.tiff')
    filename_grayscale_32bit = str(tmpdir / 'grayscale_32bit_utils.tiff')

    original_rgb_array = np.array(255 * np.random.rand(100, 100, 3),
                                  dtype=np.uint8)
    original_rgb = utils.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 = utils.array_to_img(original_rgba_array, scale=False)
    original_rgba.save(filename_rgba)

    original_grayscale_8bit_array = np.array(255 * np.random.rand(100, 100, 1),
                                             dtype=np.uint8)
    original_grayscale_8bit = utils.array_to_img(original_grayscale_8bit_array,
                                                 scale=False)
    original_grayscale_8bit.save(filename_grayscale_8bit)

    original_grayscale_16bit_array = np.array(
        np.random.randint(-2147483648, 2147483647, (100, 100, 1)), dtype=np.int16
    )
    original_grayscale_16bit = utils.array_to_img(original_grayscale_16bit_array,
                                                  scale=False, dtype='int16')
    original_grayscale_16bit.save(filename_grayscale_16bit)

    original_grayscale_32bit_array = np.array(
        np.random.randint(-2147483648, 2147483647, (100, 100, 1)), dtype=np.int32
    )
    original_grayscale_32bit = utils.array_to_img(original_grayscale_32bit_array,
                                                  scale=False, dtype='int32')
    original_grayscale_32bit.save(filename_grayscale_32bit)

    # Test that loaded image is exactly equal to original.

    loaded_im = utils.load_img(filename_rgb)
    loaded_im_array = utils.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 = utils.load_img(filename_rgba, color_mode='rgba')
    loaded_im_array = utils.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 = utils.load_img(filename_rgb, color_mode='grayscale')
    loaded_im_array = utils.img_to_array(loaded_im)
    assert loaded_im_array.shape == (original_rgb_array.shape[0],
                                     original_rgb_array.shape[1], 1)

    loaded_im = utils.load_img(filename_grayscale_8bit, color_mode='grayscale')
    loaded_im_array = utils.img_to_array(loaded_im)
    assert loaded_im_array.shape == original_grayscale_8bit_array.shape
    assert np.all(loaded_im_array == original_grayscale_8bit_array)

    loaded_im = utils.load_img(filename_grayscale_16bit, color_mode='grayscale')
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int16')
    assert loaded_im_array.shape == original_grayscale_16bit_array.shape
    assert np.all(loaded_im_array == original_grayscale_16bit_array)
    # test casting int16 image to float32
    loaded_im_array = utils.img_to_array(loaded_im)
    assert np.allclose(loaded_im_array, original_grayscale_16bit_array)

    loaded_im = utils.load_img(filename_grayscale_32bit, color_mode='grayscale')
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int32')
    assert loaded_im_array.shape == original_grayscale_32bit_array.shape
    assert np.all(loaded_im_array == original_grayscale_32bit_array)
    # test casting int32 image to float32
    loaded_im_array = utils.img_to_array(loaded_im)
    assert np.allclose(loaded_im_array, original_grayscale_32bit_array)

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

    loaded_im = utils.load_img(filename_rgb, target_size=(100, 100))
    loaded_im_array = utils.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 = utils.load_img(filename_rgba, color_mode='rgba',
                               target_size=(100, 100))
    loaded_im_array = utils.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 = utils.load_img(filename_rgb, color_mode='grayscale',
                               target_size=(100, 100))
    loaded_im_array = utils.img_to_array(loaded_im)
    assert loaded_im_array.shape == (original_rgba_array.shape[0],
                                     original_rgba_array.shape[1], 1)

    loaded_im = utils.load_img(filename_grayscale_8bit, color_mode='grayscale',
                               target_size=(100, 100))
    loaded_im_array = utils.img_to_array(loaded_im)
    assert loaded_im_array.shape == original_grayscale_8bit_array.shape
    assert np.all(loaded_im_array == original_grayscale_8bit_array)

    loaded_im = utils.load_img(filename_grayscale_16bit, color_mode='grayscale',
                               target_size=(100, 100))
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int16')
    assert loaded_im_array.shape == original_grayscale_16bit_array.shape
    assert np.all(loaded_im_array == original_grayscale_16bit_array)

    loaded_im = utils.load_img(filename_grayscale_32bit, color_mode='grayscale',
                               target_size=(100, 100))
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int32')
    assert loaded_im_array.shape == original_grayscale_32bit_array.shape
    assert np.all(loaded_im_array == original_grayscale_32bit_array)

    # Test down-sampling with bilinear interpolation.

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

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

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

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

    loaded_im = utils.load_img(filename_grayscale_16bit, color_mode='grayscale',
                               target_size=(25, 25))
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int16')
    assert loaded_im_array.shape == (25, 25, 1)

    loaded_im = utils.load_img(filename_grayscale_32bit, color_mode='grayscale',
                               target_size=(25, 25))
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int32')
    assert loaded_im_array.shape == (25, 25, 1)

    # Test down-sampling with nearest neighbor interpolation.

    loaded_im_nearest = utils.load_img(filename_rgb, target_size=(25, 25),
                                       interpolation="nearest")
    loaded_im_array_nearest = utils.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 = utils.load_img(filename_rgba, color_mode='rgba',
                                       target_size=(25, 25),
                                       interpolation="nearest")
    loaded_im_array_nearest = utils.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)

    loaded_im = utils.load_img(filename_grayscale_8bit, color_mode='grayscale',
                               target_size=(25, 25), interpolation="nearest")
    loaded_im_array = utils.img_to_array(loaded_im)
    assert loaded_im_array.shape == (25, 25, 1)

    loaded_im = utils.load_img(filename_grayscale_16bit, color_mode='grayscale',
                               target_size=(25, 25), interpolation="nearest")
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int16')
    assert loaded_im_array.shape == (25, 25, 1)

    loaded_im = utils.load_img(filename_grayscale_32bit, color_mode='grayscale',
                               target_size=(25, 25), interpolation="nearest")
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int32')
    assert loaded_im_array.shape == (25, 25, 1)

    # Check that exception is raised if interpolation not supported.

    loaded_im = utils.load_img(filename_rgb, interpolation="unsupported")
    with pytest.raises(ValueError):
        loaded_im = utils.load_img(filename_rgb, target_size=(25, 25),
                                   interpolation="unsupported")
    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 in one-hot-encoded format.
        """
        additional_bg_class = 0
        if self.include_background:
            additional_bg_class = 1
        batch_x = np.zeros(
            (len(index_array), ) + (self.target_size[0], self.target_size[1],
                                    self.num_classes + additional_bg_class),
            dtype=self.dtype)
        # build batch of image data
        # self.filepaths is dynamic, is better to call it once outside the loop
        filepaths = self.filepaths
        known_label_keys = self.dropped_labels_memory.keys()
        known_label_drops = {}
        unset_label_drops = []
        for i, j in enumerate(index_array):
            if j in known_label_keys:
                known_label_drops[i] = j
            else:
                unset_label_drops.append(i)
            one_hot_map = np.zeros((self.target_size[0], self.target_size[1],
                                    self.num_classes + additional_bg_class),
                                   dtype=np.float32)
            # Iterate over all classes
            params = None
            reserved_pixels = None
            for k in range(self.num_classes):
                filepath = os.path.join(self.directory, self._mask_classes[k],
                                        filepaths[j])
                img = load_img(filepath,
                               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()
                if self.image_data_generator:
                    # Params need to be set once for every image (not for every mask)
                    if params is None:
                        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)
                if reserved_pixels is None:
                    reserved_pixels = np.zeros(x.shape)
                x = np.where(reserved_pixels, 0, x)
                reserved_pixels += x
                one_hot_map += self.get_one_hot_map(
                    x,
                    k,
                    background=self.background_color,
                    additional_bg_class=additional_bg_class)
            # If one_hot_map has a max value >1 whe have overlapping classes -> prohibited
            one_hot_map = one_hot_map.numpy()
            if one_hot_map.max() > 1:
                raise ValueError(
                    'Mask mismatch: classes are not mutually exclusive (multiple class definitions for '
                    'one pixel).')
            if self.include_background:
                # Background class is everywhere, where the one-hot encoding has only zeros
                one_hot_map = tf.where(
                    tf.repeat(tf.reshape(
                        tf.math.count_nonzero(
                            one_hot_map == tf.zeros(self.num_classes + 1),
                            axis=2), [img.height, img.width, 1]),
                              self.num_classes + 1,
                              axis=2) == self.num_classes + 1,
                    tf.one_hot(
                        tf.constant(self.num_classes,
                                    shape=one_hot_map.shape[:2]),
                        self.num_classes + 1), one_hot_map)
            batch_x[i] = one_hot_map
        # 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))

        # Only where labels are not already known
        if len(self.heterogeneously_labeled_masks) > 0:
            batch_x[unset_label_drops] = self.remove_masks(
                np.take(batch_x, unset_label_drops, axis=0))

            # Known labels
            for item in known_label_drops.items():
                index_in_batch = item[0]
                index_in_memory = item[1]
                memory_binary_mask = self.dropped_labels_memory.get(
                    index_in_memory)
                batch_x[index_in_batch, :, :, :][
                    memory_binary_mask] = DELETED_MASK_IDENTIFIER

            # Extend Memory of known deletion masks.
            delete_mask = np.where(batch_x == DELETED_MASK_IDENTIFIER, True,
                                   False)
            for i in range(len(index_array)):
                self.dropped_labels_memory[index_array[i]] = delete_mask[
                    i, :, :, :]
        return batch_x
    def _get_batches_of_transformed_samples(self, index_array):
        # IMPORTANT: the next line is changed with respect to the original
        # keras implementation
        # Change: self.target_shape <-- list(self.x.shape)[1:]
        # Additionally: support for more than one augmentation per image
        batch_x = np.zeros(tuple([len(index_array) * self.n_aug] \
                                 + self.target_shape), dtype=self.dtype)
        batch_y_id = np.zeros([len(index_array) * self.n_aug] * 2,
                              dtype=np.uint8)
        for i, j in enumerate(index_array):
            batch_y_id[i * self.n_aug:i * self.n_aug + self.n_aug,
                       i * self.n_aug:i * self.n_aug + self.n_aug] = 1
            for k in range(self.n_aug):
                x = self.x[j]
                params = self.image_data_generator.get_random_transform(
                    x.shape)
                x = self.image_data_generator.apply_transform(
                    x.astype(self.dtype), params)
                x = self.image_data_generator.standardize(x)
                batch_x[i * self.n_aug + k] = x
        if self.n_aug > 1:
            batch_y_id[np.tril_indices(batch_y_id.shape[0])] = 0

        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(1e4),
                    format=self.save_format)
                img.save(os.path.join(self.save_to_dir, fname))

        # Re-shuffle, in order to avoid contiguous augmented images
        if (self.n_aug > 1) & (self.shuffle):
            if self.seed is not None:
                np.random.seed(self.seed + self.total_batches_seen)
            idx = np.random.permutation(batch_x.shape[0])
        else:
            idx = np.arange(batch_x.shape[0])

        batch_x = batch_x[idx]
        batch_x_miscs = [xx[index_array][idx] for xx in self.x_misc]

        output = (batch_x if batch_x_miscs == [] else [batch_x] +
                  batch_x_miscs, )

        if self.y is None:
            return output[0]

        batch_y_cat = np.repeat(self.y[index_array], self.n_aug, axis=0)[idx]
        batch_y_id = batch_y_id[idx][:, idx]
        batch_y = [batch_y_cat, batch_y_id]
        batch_y_miscs = [yy[indey_array][idx] for yy in self.y_misc]
        output += (batch_y if batch_y_miscs == [] else batch_y +
                   batch_y_miscs, )

        if self.sample_weight is not None:
            output += (np.repeat(self.sample_weight[index_array],
                                 self.n_aug,
                                 axis=0)[idx], )
        return output
예제 #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.
        """
        index_array = self.X[index_array]
        batch_x = np.zeros((len(index_array), ) + self.image_shape,
                           dtype=self.dtype)
        # 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):
            img = load_img(filepaths[j],
                           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()
            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] = 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), len(self.class_indices)),
                               dtype=self.dtype)
            for i, n_observation in enumerate(index_array):
                batch_y[i, self.classes[n_observation]] = 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]
예제 #9
0
    def sample(self, X=None, sample_size=None, standardize=True):
        """ Retrived fix-sized image tersors

        Parameters
        ----------
        X : 2D-array. Default is None
            Expanded sub-index array of the dataframe.
            If None, X = np.arange(n_samples)[:, np.newaxis].
        sample_size : int. Default is None.
            The number of samples to be retrieved.
            If None, sample_size = X.shape[0]
        standardize : bool. Default is True.
            Whether to transform the image tersor data.
            If False, return direct results of `img_to_array`.
        """
        if X is None:
            X = np.arange(self.dataframe.shape[0])[:, np.newaxis]
        if not sample_size:
            sample_size = X.shape[0]

        retrieved_X = np.zeros((sample_size, ) + self.image_shape,
                               dtype=self.dtype)

        filepaths = self.filepaths
        indices = np.squeeze(X)
        sample_index = self.rng_.choice(indices,
                                        size=sample_size,
                                        replace=False)
        for i, j in enumerate(sample_index):
            img = load_img(filepaths[j],
                           color_mode=self.color_mode,
                           target_size=self.target_size,
                           interpolation=self.interpolation)

            x = img_to_array(img, data_format=self.data_format)
            if hasattr(img, 'close'):
                img.close()

            if not standardize:
                retrieved_X[i] = x
                continue
            params = self.get_random_transform(x.shape)
            x = self.apply_transform(x, params)
            x = self.standardize(x)
            retrieved_X[i] = x

        if self.save_to_dir:
            for i, j in enumerate(sample_index):
                img = array_to_img(retrieved_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))

        # retrieve labels
        if self.class_mode == 'input':
            retrieved_y = retrieved_X.copy()
        elif self.class_mode in {'binary', 'sparse'}:
            retrieved_y = np.empty(sample_size, dtype=self.dtype)
            for i, n_observation in enumerate(sample_index):
                retrieved_y[i] = self.classes[n_observation]
        elif self.class_mode == 'categorical':
            retrieved_y = np.zeros((sample_size, len(self.class_indices)),
                                   dtype=self.dtype)
            for i, n_observation in enumerate(sample_index):
                retrieved_y[i, self.classes[n_observation]] = 1.
        elif self.class_mode == 'multi_output':
            retrieved_y = [output[sample_index] for output in self.labels]
        elif self.class_mode == 'raw':
            retrieved_y = self.labels[sample_index]
        else:
            return retrieved_X

        return retrieved_X, retrieved_y
예제 #10
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.
        """
        y = self.classes[index_array]

        # creo una matrice indice - classe
        batch = np.concatenate(
            [np.expand_dims(index_array, 1),
             np.expand_dims(y, 1)], axis=1)
        batch = pd.DataFrame(batch, columns=['idx', 'class'])
        batch = batch.groupby('class')

        # Per ogni classe genero K sample
        # batch totale 128
        try:
            batch = batch.apply(
                lambda _x: _x.sample(18).reset_index(drop=True))
        except ValueError:
            batch = batch.apply(
                lambda _x: _x.sample(18, replace=True).reset_index(drop=True))
            print("This batch is garbage")

        index_array = np.array(batch['idx'])
        index_array = np.random.permutation(index_array)

        batch_x = np.zeros((len(index_array), ) + self.image_shape,
                           dtype=self.dtype)
        # 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):
            img = load_img(filepaths[j],
                           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()
            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] = 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), len(self.class_indices)),
                               dtype=self.dtype)
            for i, n_observation in enumerate(index_array):
                batch_y[i, self.classes[n_observation]] = 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_x, batch_x, np.argmax(batch_y, axis=1)],\
                   [np.zeros((len(batch_x), 4096), dtype='float16'), batch_y]
        else:
            return batch_x, batch_y, self.sample_weight[index_array]
예제 #11
0
def write_sample_image(tmpdir):
    im = utils.array_to_img(np.random.rand(1, 1, 3))
    path = str(tmpdir / 'sample_image.png')
    utils.save_img(path, im)
    return path
예제 #12
0
def test_load_img(tmpdir):
    filename_rgb = str(tmpdir / 'rgb_utils.png')
    filename_rgba = str(tmpdir / 'rgba_utils.png')
    filename_grayscale_8bit = str(tmpdir / 'grayscale_8bit_utils.png')
    filename_grayscale_16bit = str(tmpdir / 'grayscale_16bit_utils.tiff')
    filename_grayscale_32bit = str(tmpdir / 'grayscale_32bit_utils.tiff')

    original_rgb_array = np.array(255 * np.random.rand(100, 100, 3),
                                  dtype=np.uint8)
    original_rgb = utils.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 = utils.array_to_img(original_rgba_array, scale=False)
    original_rgba.save(filename_rgba)

    original_grayscale_8bit_array = np.array(255 * np.random.rand(100, 100, 1),
                                             dtype=np.uint8)
    original_grayscale_8bit = utils.array_to_img(original_grayscale_8bit_array,
                                                 scale=False)
    original_grayscale_8bit.save(filename_grayscale_8bit)

    original_grayscale_16bit_array = np.array(np.random.randint(
        -2147483648, 2147483647, (100, 100, 1)),
                                              dtype=np.int16)
    original_grayscale_16bit = utils.array_to_img(
        original_grayscale_16bit_array, scale=False, dtype='int16')
    original_grayscale_16bit.save(filename_grayscale_16bit)

    original_grayscale_32bit_array = np.array(np.random.randint(
        -2147483648, 2147483647, (100, 100, 1)),
                                              dtype=np.int32)
    original_grayscale_32bit = utils.array_to_img(
        original_grayscale_32bit_array, scale=False, dtype='int32')
    original_grayscale_32bit.save(filename_grayscale_32bit)

    # Test that loaded image is exactly equal to original.

    loaded_im = utils.load_img(filename_rgb)
    loaded_im_array = utils.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 = utils.load_img(filename_rgba, color_mode='rgba')
    loaded_im_array = utils.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 = utils.load_img(filename_rgb, color_mode='grayscale')
    loaded_im_array = utils.img_to_array(loaded_im)
    assert loaded_im_array.shape == (original_rgb_array.shape[0],
                                     original_rgb_array.shape[1], 1)

    loaded_im = utils.load_img(filename_grayscale_8bit, color_mode='grayscale')
    loaded_im_array = utils.img_to_array(loaded_im)
    assert loaded_im_array.shape == original_grayscale_8bit_array.shape
    assert np.all(loaded_im_array == original_grayscale_8bit_array)

    loaded_im = utils.load_img(filename_grayscale_16bit,
                               color_mode='grayscale')
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int16')
    assert loaded_im_array.shape == original_grayscale_16bit_array.shape
    assert np.all(loaded_im_array == original_grayscale_16bit_array)
    # test casting int16 image to float32
    loaded_im_array = utils.img_to_array(loaded_im)
    assert np.allclose(loaded_im_array, original_grayscale_16bit_array)

    loaded_im = utils.load_img(filename_grayscale_32bit,
                               color_mode='grayscale')
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int32')
    assert loaded_im_array.shape == original_grayscale_32bit_array.shape
    assert np.all(loaded_im_array == original_grayscale_32bit_array)
    # test casting int32 image to float32
    loaded_im_array = utils.img_to_array(loaded_im)
    assert np.allclose(loaded_im_array, original_grayscale_32bit_array)

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

    loaded_im = utils.load_img(filename_rgb, target_size=(100, 100))
    loaded_im_array = utils.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 = utils.load_img(filename_rgba,
                               color_mode='rgba',
                               target_size=(100, 100))
    loaded_im_array = utils.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 = utils.load_img(filename_rgb,
                               color_mode='grayscale',
                               target_size=(100, 100))
    loaded_im_array = utils.img_to_array(loaded_im)
    assert loaded_im_array.shape == (original_rgba_array.shape[0],
                                     original_rgba_array.shape[1], 1)

    loaded_im = utils.load_img(filename_grayscale_8bit,
                               color_mode='grayscale',
                               target_size=(100, 100))
    loaded_im_array = utils.img_to_array(loaded_im)
    assert loaded_im_array.shape == original_grayscale_8bit_array.shape
    assert np.all(loaded_im_array == original_grayscale_8bit_array)

    loaded_im = utils.load_img(filename_grayscale_16bit,
                               color_mode='grayscale',
                               target_size=(100, 100))
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int16')
    assert loaded_im_array.shape == original_grayscale_16bit_array.shape
    assert np.all(loaded_im_array == original_grayscale_16bit_array)

    loaded_im = utils.load_img(filename_grayscale_32bit,
                               color_mode='grayscale',
                               target_size=(100, 100))
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int32')
    assert loaded_im_array.shape == original_grayscale_32bit_array.shape
    assert np.all(loaded_im_array == original_grayscale_32bit_array)

    # Test down-sampling with bilinear interpolation.

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

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

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

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

    loaded_im = utils.load_img(filename_grayscale_16bit,
                               color_mode='grayscale',
                               target_size=(25, 25))
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int16')
    assert loaded_im_array.shape == (25, 25, 1)

    loaded_im = utils.load_img(filename_grayscale_32bit,
                               color_mode='grayscale',
                               target_size=(25, 25))
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int32')
    assert loaded_im_array.shape == (25, 25, 1)

    # Test down-sampling with nearest neighbor interpolation.

    loaded_im_nearest = utils.load_img(filename_rgb,
                                       target_size=(25, 25),
                                       interpolation="nearest")
    loaded_im_array_nearest = utils.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 = utils.load_img(filename_rgba,
                                       color_mode='rgba',
                                       target_size=(25, 25),
                                       interpolation="nearest")
    loaded_im_array_nearest = utils.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)

    loaded_im = utils.load_img(filename_grayscale_8bit,
                               color_mode='grayscale',
                               target_size=(25, 25),
                               interpolation="nearest")
    loaded_im_array = utils.img_to_array(loaded_im)
    assert loaded_im_array.shape == (25, 25, 1)

    loaded_im = utils.load_img(filename_grayscale_16bit,
                               color_mode='grayscale',
                               target_size=(25, 25),
                               interpolation="nearest")
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int16')
    assert loaded_im_array.shape == (25, 25, 1)

    loaded_im = utils.load_img(filename_grayscale_32bit,
                               color_mode='grayscale',
                               target_size=(25, 25),
                               interpolation="nearest")
    loaded_im_array = utils.img_to_array(loaded_im, dtype='int32')
    assert loaded_im_array.shape == (25, 25, 1)

    # Check that exception is raised if interpolation not supported.

    loaded_im = utils.load_img(filename_rgb, interpolation="unsupported")
    with pytest.raises(ValueError):
        loaded_im = utils.load_img(filename_rgb,
                                   target_size=(25, 25),
                                   interpolation="unsupported")

    # Check that the aspect ratio of a square is the same

    filename_red_square = str(tmpdir / 'red_square_utils.png')
    A = np.zeros((50, 100, 3), dtype=np.uint8)  # rectangle image 100x50
    A[20:30, 45:55, 0] = 255  # red square 10x10
    red_square_array = np.array(A)
    red_square = utils.array_to_img(red_square_array, scale=False)
    red_square.save(filename_red_square)

    loaded_im = utils.load_img(filename_red_square,
                               target_size=(25, 25),
                               keep_aspect_ratio=True)
    loaded_im_array = utils.img_to_array(loaded_im)
    assert loaded_im_array.shape == (25, 25, 3)

    red_channel_arr = loaded_im_array[:, :, 0].astype(np.bool)
    square_width = np.sum(np.sum(red_channel_arr, axis=0))
    square_height = np.sum(np.sum(red_channel_arr, axis=1))
    aspect_ratio_result = square_width / square_height

    # original square had 1:1 ratio
    assert aspect_ratio_result == pytest.approx(1.0)