def augment_input(img,
                  mask,
                  aug_dict,
                  batch_size=32,
                  random_crop_size=(256, 256),
                  only_crop=False):
    LX, LY, LZ = img.shape
    image_datagen = ImageDataGenerator(**aug_dict)

    out_imgs = np.zeros(
        (batch_size, random_crop_size[0], random_crop_size[1], 1))
    out_masks = np.zeros(
        (batch_size, random_crop_size[0], random_crop_size[1], 1))
    for b in range(batch_size):
        seed = np.random.randint(1e9)
        crop_img, crop_mask = random_crop(img, mask, random_crop_size)
        if not only_crop:
            out_imgs[b, ...] = image_datagen.random_transform(crop_img,
                                                              seed=seed)
            out_masks[b, ...] = image_datagen.random_transform(crop_mask,
                                                               seed=seed)
        else:
            out_imgs[b, ...] = crop_img
            out_masks[b, ...] = crop_mask

    return out_imgs, out_masks
示例#2
0
class Augmentor(object):
    def __init__(self, rotation_range=30, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2,
                 zoom_range=0.2, horizontal_flip=True, fill_mode='reflect'):

        self.image_augmentor = ImageDataGenerator(
            rotation_range=rotation_range,
            width_shift_range=width_shift_range,
            height_shift_range=height_shift_range,
            shear_range=shear_range,
            zoom_range=zoom_range,
            horizontal_flip=horizontal_flip,
            fill_mode=fill_mode)

    def __call__(self, image_batch, mask_batch=None):
        state = np.random.get_state()
        aug_image_batch = np.array(image_batch)
        for i in range(image_batch.shape[0]):
            aug_image_batch[i] = self.image_augmentor.random_transform(image_batch[i])

        if mask_batch is not None:
            np.random.set_state(state)
            aug_mask_batch = np.array(mask_batch)
            for i in range(mask_batch.shape[0]):
                aug_mask_batch[i] = self.image_augmentor.random_transform(mask_batch[i])

            return aug_image_batch, aug_mask_batch
        else:
            return aug_image_batch
示例#3
0
    def compute_dist(self, images, labels, sample_size=None):
        """Compute distances for pairs

        sample_size: None or integer, number of pairs as all pairs can be large number.
                        If None, by default all possible pairs are considered.

        Returns:
        dist_embed:  array of distances
        actual_issame: array of booleans, True if positive pair, False if negative pair
        """
        # Run forward pass to calculate embeddings
        print('Generating pairs and computing embeddings...')

        # Define generator to loop over data
        gen = ImageDataGenerator(
            data_format=K.image_data_format(),
            preprocessing_function=self.backend_class.normalize,
        )
        features_shape = self.model.get_output_shape_at(0)[1:]

        if sample_size is None:
            n_pairs = comb(images.shape[0], 2, exact=True)
        else:
            n_pairs = int(sample_size)

        embeddings = np.zeros(shape=(2, n_pairs) + features_shape)
        actual_issame = np.full(shape=(n_pairs, ),
                                fill_value=True,
                                dtype=np.bool)

        # Create all possible combinations of images (no repeats)
        idx = 0
        for i in range(images.shape[0]):
            for j in range(i):
                img_1 = gen.random_transform(images[i].astype(K.floatx()))
                img_1 = gen.preprocessing_function(img_1)

                img_2 = gen.random_transform(images[j].astype(K.floatx()))
                img_2 = gen.preprocessing_function(img_2)

                embeddings[0, idx] = self.model.predict_on_batch(
                    np.expand_dims(img_1, 0))
                embeddings[1, idx] = self.model.predict_on_batch(
                    np.expand_dims(img_2, 0))

                if labels[i] != labels[j]:
                    actual_issame[idx] = False
                idx += 1

                if idx >= n_pairs:
                    break

            if idx >= n_pairs:
                break

        print('Number of pairs in evaluation {}, number of positive {}'.format(
            len(actual_issame), np.sum(actual_issame)))
        dist_emb = distance(embeddings[0], embeddings[1], distance_metric=0)
        return dist_emb, actual_issame
def random_augment(img, mask, aug_dict):
    image_datagen = ImageDataGenerator(**aug_dict)
    seed = np.random.randint(1e9)

    out_img = image_datagen.random_transform(img, seed=seed)
    out_mask = image_datagen.random_transform(mask, seed=seed)

    return out_img, out_mask
示例#5
0
def augment(inpts, targets, num_aug=5):
    datagen = ImageDataGenerator()
    X_set, y_set = [], []
    for inpt, target in zip(inpts, targets):
        inpt = np.expand_dims(inpt/255., axis=2)
        target = np.expand_dims(target/255., axis=2)
        for _ in range(num_aug):
            seed = int(time())
            x = datagen.random_transform(inpt, seed=seed)
            y = datagen.random_transform(target, seed=seed)
            X_set.append(x)
            y_set.append(y)
    return X_set, y_set
class ImagePreprocessor(Preprocessor):
    _IMAGE_SIZE = (299, 299)

    def __init__(self, image_augmentation=False):
        super().__init__()
        self._image_augmentation = image_augmentation
        self._image_data_generator = ImageDataGenerator(rotation_range=40,
                                                        width_shift_range=0.2,
                                                        height_shift_range=0.2,
                                                        shear_range=0.2,
                                                        zoom_range=0.2,
                                                        horizontal_flip=True,
                                                        fill_mode='nearest')

    def preprocess_images(self, img_paths):
        return map(self.preprocess_image, img_paths)

    def preprocess_image(self, img_path):
        img = load_img(img_path, target_size=self._IMAGE_SIZE)
        img_array = img_to_array(img)
        if self._image_augmentation:
            img_array = self._image_data_generator.random_transform(img_array)
        img_array = inception_v3.preprocess_input(img_array)

        return img_array
示例#7
0
def data_generator(batch_size, imgs, pos_inp, targets, image_augmentation=False):
    total_num = imgs.shape[0]
    shuffled_indeces = np.arange(total_num)
    prep = ImageDataGenerator(width_shift_range=20, height_shift_range=20, zoom_range=0.1, rotation_range=22)
    while True:
        np.random.shuffle(shuffled_indeces)
        for i in range(total_num):
            current_indeces = shuffled_indeces[i*batch_size:(i+1)*batch_size]
            current_images = imgs[current_indeces]
            current_pos    = pos_inp[current_indeces]
            if current_images.shape[0]!=batch_size:
                continue

            batch_pos = np.zeros([current_images.shape[0],3])
            batch_images = np.zeros_like(current_images)
            for j in range(current_images.shape[0]):
                if image_augmentation:
                    trans_img = prep.random_transform(current_images[j])
                else:
                    trans_img = current_images[j]
                batch_images[j] =  trans_img
                batch_pos[j] =  current_pos[j]

            batch_targets = to_categorical(targets[current_indeces], num_classes=1160)

            yield [batch_images, batch_pos], batch_targets
示例#8
0
class BasicDataAugmenter(object):
    """
    This class wraps the Keras imageDataGenerator for easy image-data augmentation.
    """
    def __init__(self,
                 rotation_range: int = 0,
                 width_shift_range: float = 0.0,
                 height_shift_range: float = 0.0,
                 intensity_shift: float = 0.0,
                 shear_range: float = 0.0,
                 zoom_range: float = 0.0):
        self._intensity_shift = intensity_shift
        self._gen = ImageDataGenerator(rotation_range=rotation_range,
                                       width_shift_range=width_shift_range,
                                       height_shift_range=height_shift_range,
                                       shear_range=shear_range,
                                       zoom_range=zoom_range,
                                       fill_mode='reflect',
                                       cval=0.0)

    def process(self, x):

        if self._intensity_shift != 0.0:
            if rnd.choice([True, False, True, False]):
                x = x * rnd.uniform(1.0 - self._intensity_shift,
                                    1.0 + self._intensity_shift)
                x = np.clip(x, 0.0, 255.0)
        return self._gen.random_transform(x)
示例#9
0
class FERGenerator(Sequence):
    def __init__(self, fer_reader, x_set, y_set, batch_size):
        self.fer_reader = fer_reader
        self.x = x_set
        self.y_set = y_set
        self.batch_size = batch_size
        self.y = fer_reader.generate_emotions(y_set)
        self.datagen = ImageDataGenerator(featurewise_center=False,
                                          featurewise_std_normalization=False,
                                          rotation_range=10,
                                          width_shift_range=0.1,
                                          height_shift_range=0.1,
                                          zoom_range=0.1,
                                          horizontal_flip=True)
        self.datagen.fit(self.x)

    def __len__(self):
        return int(np.ceil(len(self.x) / float(self.batch_size)))

    def __getitem__(self, idx):
        batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size]
        batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]

        batch_x_modified = [
            self.datagen.random_transform(item) for item in batch_x
        ]
        return np.array(batch_x_modified), batch_y

    def on_epoch_end(self):
        self.y = self.fer_reader.generate_emotions(self.y_set)
class ImagePreprocessor(object):
    """A Inception v3 image preprocessor. Implements an image augmentation
    as well."""
    IMAGE_SIZE = (299, 299)

    def __init__(self, image_augmentation=None):
        self._image_data_generator = ImageDataGenerator(rotation_range=40,
                                                        width_shift_range=0.2,
                                                        height_shift_range=0.2,
                                                        shear_range=0.2,
                                                        zoom_range=0.2,
                                                        horizontal_flip=True,
                                                        fill_mode='nearest')
        if image_augmentation is None:
            self._image_augmentation_switch = active_config().image_augmentation
        else:
            self._image_augmentation_switch = image_augmentation

    def preprocess_images(self, img_paths, random_transform=True):
        return map(partial(self._preprocess_an_image,
                           random_transform=random_transform),
                   img_paths)

    def preprocess_batch(self, img_list):
        return np.array(img_list)

    def _preprocess_an_image(self, img_path, random_transform=True):
        img = load_img(img_path, target_size=self.IMAGE_SIZE)
        img_array = img_to_array(img)
        if self._image_augmentation_switch and random_transform:
            img_array = self._image_data_generator.random_transform(img_array)
        img_array = inception_v3.preprocess_input(img_array)

        return img_array
示例#11
0
    def preprocess_img(self, img_path):
        img = Image.open(img_path)
        resize_scale = self.img_size[0] / max(img.size[:2])
        img = img.resize(
            (int(img.size[0] * resize_scale), int(img.size[1] * resize_scale)))
        img = img.convert('RGB')
        img = np.array(img)

        # 数据增强
        if self.use:
            # plt.imshow((img).astype(np.uint8))
            # pylab.show()
            img = self.eraser(img)
            datagen = ImageDataGenerator(
                # rotation_range=90,
                # shear_range=0.1,
                # zoom_range=0.1,
                # brightness_range=(1, 1.3),
                horizontal_flip=True,
                vertical_flip=True,
            )
            img = datagen.random_transform(img)
        #     print("train set")
        # if not self.use:
        #     print("val set")
        # plt.imshow((img).astype(np.uint8))
        # pylab.show()

        img = img[:, :, ::-1]
        img = self.center_img(img, self.img_size[0])
        return img
class ImagePreprocessor(object):
    """A Inception v3 image preprocessor. Implements an image augmentation
    as well."""
    IMAGE_SIZE = (299, 299)

    def __init__(self, image_augmentation=None):
        self._image_data_generator = ImageDataGenerator(rotation_range=40,
                                                        width_shift_range=0.2,
                                                        height_shift_range=0.2,
                                                        shear_range=0.2,
                                                        zoom_range=0.2,
                                                        horizontal_flip=True,
                                                        fill_mode='nearest')
        if image_augmentation is None:
            self._image_augmentation_switch = active_config().image_augmentation
        else:
            self._image_augmentation_switch = image_augmentation

    def preprocess_images(self, img_paths, random_transform=True):
        return map(partial(self._preprocess_an_image,
                           random_transform=random_transform),
                   img_paths)

    def preprocess_batch(self, img_list):
        return np.array(img_list)

    def _preprocess_an_image(self, img_path, random_transform=True):
        img = load_img(img_path, target_size=self.IMAGE_SIZE)
        img_array = img_to_array(img)
        if self._image_augmentation_switch and random_transform:
            img_array = self._image_data_generator.random_transform(img_array)
        img_array = inception_v3.preprocess_input(img_array)

        return img_array
示例#13
0
def _generator(filename, batch_size=32, index=0, augment=False):
    f = h5py.File(filename, 'r')
    pairs = f['pairs']
    idg = ImageDataGenerator(rotation_range=20,
                             horizontal_flip=True,
                             vertical_flip=True,
                             zoom_range=0.05)
    while 1:
        data = pairs[index:index + int(batch_size / 2)]
        if augment:
            data = np.array(
                [[idg.random_transform(x[0]),
                  idg.random_transform(x[1])] for x in data])

        left = data[:, 0]
        right = data[:, 1]
        gen_batch = np.empty((left.shape[0] + right.shape[0], *left.shape[1:]),
                             dtype=left.dtype)
        gen_batch[0::2] = left
        gen_batch[1::2] = right
        yield gen_batch
        index += batch_size
        index = index % len(pairs)
        # print(index)

    f.close()
示例#14
0
    def preprocess_img(self, img_path):
        """
        image preprocessing
        you can add your special preprocess method here
        """
        img = Image.open(img_path)
        resize_scale = self.img_size[0] / max(img.size[:2])
        img = img.resize(
            (int(img.size[0] * resize_scale), int(img.size[1] * resize_scale)))
        img = img.convert('RGB')
        img = np.array(img)

        # 数据增强
        if self.use:

            img = self.eraser(img)
            datagen = ImageDataGenerator(
                width_shift_range=0.05,
                height_shift_range=0.05,
                # rotation_range=90,
                # shear_range=0.1,
                # zoom_range=0.1,
                # brightness_range=(1, 1.3),
                horizontal_flip=True,
                vertical_flip=True,
            )
            img = datagen.random_transform(img)

        img = img[:, :, ::-1]

        img = self.center_img(img, self.img_size[0])
        # print(img)
        return img
示例#15
0
def _generator(filename, batch_size=32, index=0, augment=False):
    f = h5py.File(filename, 'r')
    print(filename)
    pairs = f['pairs']
    total_pairs = pairs.shape[0]

    idg = ImageDataGenerator(rotation_range=20,
                             horizontal_flip=True,
                             vertical_flip=True,
                             zoom_range=0.05)
    while 1:
        if index + batch_size > total_pairs:
            data = pairs[index:]
            index = 0
        else:
            data = pairs[index:index + batch_size]
            index += batch_size
            index %= total_pairs

        if augment:
            data = np.array(
                [[idg.random_transform(x[0]),
                  idg.random_transform(x[1])] for x in data])
        yield [data[:, 0], data[:, 1]], [1, 0] * int(len(data[:, 0]) / 2)
    f.close()
示例#16
0
def generate_data_1(directory, augmentation, batchsize, file_list, label_1):
    i = 0
    while True:
        image_batch = []
        label_1_batch = []
        for b in range(batchsize):
            if i == (len(file_list)):
                i = 0
            img = image.load_img(directory + '/' + file_list.iloc[i] + '.jpg',
                                 grayscale=False,
                                 target_size=(384, 384))
            img = image.img_to_array(img)

            if augmentation:
                datagen = ImageDataGenerator(rotation_range=360,
                                             width_shift_range=0.1,
                                             height_shift_range=0.1,
                                             shear_range=0.2,
                                             zoom_range=0.2,
                                             channel_shift_range=20,
                                             horizontal_flip=True,
                                             vertical_flip=True,
                                             fill_mode="nearest")
                img = datagen.random_transform(img)
                img = img / 255.0
            else:
                img = img / 255.0

            image_batch.append(img)
            label_1_batch.append(label_1.iloc[i])
            i = i + 1

        yield (np.asarray(image_batch), np.asarray(label_1_batch))
class BeautyClassificator(object):
    img_width, img_height = 299, 299
    batch_size = 32

    def __init__(self, model):
        self.model = model
        self.image_data_generator = ImageDataGenerator(rescale=1. / 255)

    def is_beautiful(self, img: Image):
        """
        Scores how beautiful is picture.
        :param img: Image object
        :return: class, it's probability
        where:
            class - 0(not beautiful) or 1(beautiful)
            probability - how probable is that class
        """
        try:
            x = img_to_array(img, dim_ordering=K.image_dim_ordering())
            x = self.image_data_generator.random_transform(x)
            x = self.image_data_generator.standardize(x)
            y_probabilities = self.model.predict(x=np.array([x]),
                                                 batch_size=self.batch_size)
            y_classes = probas_to_classes(y_probabilities)
            return y_classes[0], y_probabilities.max()
        except Exception as e:
            print(e)
            return 0, 1
    def __data_generation(self, Beat_array_IDs_temp, Labels_temp):
        # Generates data containing batch_size samples # X : (n_samples, *dim, n_channels)
        # Initialization
        X = np.empty((self.batch_size, *self.dim, self.n_channels))
        Y = np.empty((self.batch_size, self.n_classes), dtype=int)
        datagen = ImageDataGenerator(zoom_range=(0.9, 1.1),
                                     width_shift_range=0.1,
                                     height_shift_range=0.1)
        # Now we need to create a batch of 5 beat sample arrays

        for j in range(self.batch_size):

            # This gives us a sample name to use
            Beat_ID = Beat_array_IDs_temp[j]

            # Set the j'th value of Y as the label for the five beat array
            Y[j] = Labels_temp[j]

            # Load a 5 beat sample from the folder
            # First find the right filename by opening the pickle of samples and selecting the index
            # Then opening this from the whole data set
            filename = (dir_segments_CWT.format(Beat_ID))
            # Now load in a 5 beat sample and set as first element in input array

            temp1 = np.load(filename, allow_pickle=True)
            add = np.zeros((128, 128, 1))
            new = np.concatenate((temp1, add), axis=-1)
            ran_aug = datagen.random_transform(new, seed=None)
            X[j] = ran_aug[:, :, :2]

        # POTENTIALLY CHANGE THIS PART IF WE ARE NOT USING CATEGORICAL LABELS. But it might be good to keep this
        # as it's more general so can easily adapt if we want to try and identify different types of Cardiac
        # arrhythmia's
#         return X, keras.utils.to_categorical(Y, num_classes=self.n_classes)
        return X, Y
示例#19
0
def get_augment_predictions(inputs, augment_times):
    augmented_inputs = []
    augmented_predictions = {"category": [], "probability": []}

    if (augment_times > 1):
        idg = ImageDataGenerator(rotation_range=30.,
                                 shear_range=0.2,
                                 zoom_range=0.2,
                                 horizontal_flip=True)

        # 对于每个输入进行augment_times倍数据增强
        for i, input in enumerate(inputs):
            augmented_single = []
            for j in range(augment_times):
                temp = idg.random_transform(x=input)
                augmented_single.append(temp)
            augmented_inputs.append(augmented_single)

        # 进行集成判决
        for i, augmented_input in enumerate(augmented_inputs):
            out = model.predict(np.array(augmented_input))
            single_predictions_cat = np.argmax(out, axis=1)
            single_predictions_pro = np.max(out, axis=1)
            max_prediction_cat = Counter(single_predictions_cat).most_common(
                1)[0][0]
            max_prediction_pro = np.array(single_predictions_pro[
                single_predictions_cat == max_prediction_cat]).mean()
            augmented_predictions["category"].append(max_prediction_cat)
            augmented_predictions["probability"].append(max_prediction_pro)
    else:
        out = model.predict(np.array(inputs))
        augmented_predictions["category"] = np.argmax(out, axis=1)
        augmented_predictions["probability"] = np.max(out, axis=1)

    return augmented_predictions
示例#20
0
    def test_tta(self):
        ''' test '''
        self.model.load_weights('../weights/best_weights_{}.hdf5'.format(
            self.base_model))
        submission_df = pd.read_csv(data_dir + "sample_submission.csv")
        submission_df.head()

        test_datagen = ImageDataGenerator(
            preprocessing_function=preprocess_input)
        data = bson.decode_file_iter(open(test_bson_path, "rb"))

        with tqdm(total=num_test_products) as pbar:
            for c, d in enumerate(data):
                product_id = d["_id"]
                num_imgs = len(d["imgs"])

                batch_x = np.zeros((num_imgs, self.height, self.width, 3),
                                   dtype=K.floatx())

                prediction = 0
                for _ in range(num_fold_tta):
                    for i in range(num_imgs):
                        bson_img = d["imgs"][i]["picture"]

                        # Load and preprocess the image.
                        img = load_img(
                            io.BytesIO(bson_img)
                        )  #, target_size=(self.height, self.width))
                        x = img_to_array(img)

                        # x = random_crop(x, (self.height, self.width))

                        x = test_datagen.random_transform(x)

                        x = x[np.newaxis, ...]

                        x = test_datagen.standardize(x)

                        x = x[0]
                        # Add the image to the batch.
                        batch_x[i] = x

                    # prediction += self.model.predict(batch_x, batch_size=num_imgs) / float(num_fold_tta)
                    temp = self.model.predict(
                        batch_x, batch_size=num_imgs) / float(num_fold_tta)
                    print(temp * num_fold_tta)
                    print(temp.shape)
                    prediction += temp
                    print("[{}] prediction: ".format(j), prediction.shape)
                print(prediction)
                avg_pred = prediction.mean(axis=0)
                cat_idx = np.argmax(avg_pred)

                submission_df.iloc[c]["category_id"] = self.idx2cat[cat_idx]
                pbar.update()

        submission_df.to_csv("../submit/my_submission.csv.gz",
                             compression="gzip",
                             index=False)
示例#21
0
def gen2(list_tuples, person_to_images_map, batch_size=16, resize_picture=()):
    aug = ImageDataGenerator(
        width_shift_range=0.1,  # 水平平移幅度
        height_shift_range=0.1,  # 上下平移幅度
        brightness_range=(0.9, 1.1),  # 曝光范围
        zoom_range=0.1,  # 随机缩放的比例,小于1时表示范围 [1-, 1+]
        horizontal_flip=True,  # 水平翻转
        fill_mode='nearest'  # 变换超出边界的处理
    )
    ppl = list(person_to_images_map.keys())
    while True:
        np.random.shuffle(list_tuples)
        batches = chunker(list_tuples, batch_size // 2)
        for bat in batches:
            labels = [1] * len(bat)
            # create a batch where annotation 0 means no relation and 1 means has, the data is (p1,p2).
            # Therefore the data-annotation(element of a batch) pair have such form (p1,p2)-0,(p3,p4)-1 .....
            while len(bat) < batch_size:
                # randomly choose 2 persons' ID
                p1 = choice(ppl)  # person's ID
                p2 = choice(ppl)
                # if 2 persons don't have relation then execute
                # link all persons' without relation together with label 0
                if p1 != p2 and (p1, p2) not in list_tuples and (
                        p2, p1) not in list_tuples:
                    bat.append((p1, p2))
                    labels.append(0)
                    # after that, labels = [1,1,1....1, 0,0,0,...]
            # if person x[0] doesn't have picture(or directly call doesn't exist), then print his ID
            for x in bat:
                if not len(person_to_images_map[x[0]]):
                    print(x[0])
            # print(bat)
            # print(labels)
            # randomly choose a picture of every person in this batch
            X1 = [choice(person_to_images_map[x[0]]) for x in bat]
            X1 = np.array([read_img(x, resize_picture) for x in X1])

            X2 = [choice(person_to_images_map[x[1]]) for x in bat]
            X2 = np.array([read_img(x, resize_picture) for x in X2])
            for x1 in X1:
                aug.random_transform(x1)
            for x2 in X2:
                aug.random_transform(x2)
            # print(X1.shape, X2.shape)
            yield [X1, X2], labels
示例#22
0
class DataSequence(Sequence):
    def __init__(self, data_path, label):
        self.batch = 5
        self.data_file_path = data_path
        self.datagen = ImageDataGenerator(rotation_range=30,
                                          width_shift_range=0.2,
                                          height_shift_range=0.2,
                                          zoom_range=0.5)
        d_list = os.listdir(self.data_file_path)
        self.f_list = []
        for dir in d_list:
            if dir == 'empty': continue
            for f in os.listdir(self.data_file_path + '/' + dir):
                self.f_list.append(self.data_file_path + '/' + dir + '/' + f)
        self.label = label
        self.length = len(self.f_list)

        gamma = 0.5
        self.lookUpTable = np.zeros((256, 1), dtype='uint8')
        for i in range(256):
            self.lookUpTable[i][0] = 255 * pow(float(i) / 255, 1.0 / gamma)

    def __getitem__(self, idx):
        warp = self.batch
        aug_time = 3
        datas, labels = [], []
        label_dict = self.label

        # for f in random.sample(self.f_list, warp):
        for f in self.f_list[warp * idx:warp * (idx + 1)]:
            img = cv2.imread(f)
            img_src = cv2.resize(img, (224, 224))
            img = img_src.astype(np.float32) / 255.0
            datas.append(img)
            label = f.split('/')[2].split('_')[-1]
            labels.append(label_dict[label])

            # Augmentation image
            for num in range(aug_time):
                tmp = self.datagen.random_transform(img)
                datas.append(tmp)
                labels.append(label_dict[label])
            img = cv2.LUT(img_src, self.lookUpTable)
            img = img_src.astype(np.float32) / 255.0
            datas.append(img)
            labels.append(label_dict[label])

        datas = np.asarray(datas)
        labels = pd.DataFrame(labels)
        labels = np_utils.to_categorical(labels, len(label_dict))
        return datas, labels

    def __len__(self):
        return self.length

    def on_epoch_end(self):
        ''' 何もしない'''
        pass
示例#23
0
class AEDataGenerator(Sequence):
    def __init__(self,
                 list_IDs,
                 data_path,
                 name_format,
                 batch_size=32,
                 n_channel=3,
                 shuffle=True,
                 augment=True):
        self.list_IDs = list_IDs
        self.batch_size = batch_size
        self.n_channel = n_channel
        self.shuffle = shuffle
        self.augment = augment
        self.prng = np.random.RandomState(10)
        self.datagen = ImageDataGenerator(rotation_range=90,
                                          width_shift_range=0.15,
                                          height_shift_range=0.15,
                                          shear_range=0.01,
                                          fill_mode='constant',
                                          cval=0,
                                          zoom_range=0.10)
        self.data_path = data_path
        self.name_format = name_format
        self.on_epoch_end()

    def on_epoch_end(self):
        self.indexes = np.arange(len(self.list_IDs))
        if self.shuffle == True:
            self.prng.shuffle(self.indexes)

    def __len__(self):
        return int(np.floor(len(self.list_IDs) / self.batch_size))

    def __getitem__(self, index):
        indexes = self.indexes[index * self.batch_size:(index + 1) *
                               self.batch_size]
        list_IDs_temp = [self.list_IDs[k] for k in indexes]
        return self.__data_generation(list_IDs_temp)

    def __data_generation(self, list_IDs_temp):
        X = np.empty((self.batch_size, 256, 256, self.n_channel))
        Y = np.empty((self.batch_size, 256, 256, self.n_channel))

        for i, ID in enumerate(list_IDs_temp):
            if not self.augment:
                X[i] = np.expand_dims(imread(
                    os.path.join(self.data_path, self.name_format.format(ID))),
                                      axis=0)
            else:
                seed = self.prng.randint(0, 1000000)
                X[i] = np.expand_dims(self.datagen.random_transform(imread(
                    os.path.join(self.data_path, self.name_format.format(ID))),
                                                                    seed=seed),
                                      axis=0)
        np.copyto(Y, X)
        return (preprocess_input(X), Y)
示例#24
0
def generate_data(directory, mode, shuffle, batch_size, file_list, label):
    i = 0
    shuff_file_list = []
    shuff_label = []
    indexes = list(range(len(file_list)))
    if shuffle == True:
        random.shuffle(indexes)
    for index in indexes:
        shuff_file_list.append(file_list[index])
        shuff_label.append(label[index])
    while True:
        image_batch = []
        label_batch = []
        for b in range(batch_size):
            if i == (len(file_list)):
                i = 0
                if shuffle == True:
                    new_file_list = []
                    new_label = []
                    indexes = list(range(len(shuff_file_list)))
                    random.shuffle(indexes)

                    for index in indexes:
                        new_file_list.append(shuff_file_list[index])
                        new_label.append(shuff_label[index])
                    shuff_file_list = new_file_list
                    shuff_label = new_label
            sample = shuff_file_list[i]

            img = image.load_img(directory + shuff_file_list[i] + '.jpg',
                                 grayscale=False,
                                 target_size=(384, 384))
            img = image.img_to_array(img)
            if mode == 'augmentation':
                datagen = ImageDataGenerator(rotation_range=360,
                                             width_shift_range=0.1,
                                             height_shift_range=0.1,
                                             shear_range=0.2,
                                             zoom_range=0.2,
                                             channel_shift_range=20,
                                             horizontal_flip=True,
                                             vertical_flip=True,
                                             fill_mode="nearest")
                img = datagen.random_transform(img)
                img = img / 255.0
                #img = image.random_rotation(img, rg=360, row_axis=0, col_axis=1, channel_axis=2)
                #img = image.random_shift(img,wrg=0.1, hrg=0.1, row_axis=0, col_axis=1, channel_axis=2)
                #img = image.random_shear(img, intensity=0.2,row_axis=0, col_axis=1, channel_axis=2)
                #img = image.random_zoom(img, (0.4,0.6),row_axis=0, col_axis=1, channel_axis=2)
                #img = image.random_channel_shift(img, 20, channel_axis=2)
            if mode == 'rescale':
                img = img / 255.0
            image_batch.append(img)
            label_batch.append(shuff_label[i])
            i = i + 1
        yield (np.asarray(image_batch), np.asarray(label_batch))
示例#25
0
class ImageDataLoader:
    def __init__(self,
                 rotation_range=40,
                 width_shift_range=0.15,
                 height_shift_range=0.15,
                 shear_range=0.2,
                 zoom_range=0.2,
                 channel_shift_range=20.,
                 horizontal_flip=True,
                 fill_mode='nearest'):
        self.interpolation = fill_mode
        self.data_gen = ImageDataGenerator(
            rotation_range=rotation_range,
            width_shift_range=width_shift_range,
            height_shift_range=height_shift_range,
            shear_range=shear_range,
            zoom_range=zoom_range,
            channel_shift_range=channel_shift_range,
            horizontal_flip=horizontal_flip,
            fill_mode=fill_mode)

    def reshape_img(self, img, target_size):
        if target_size is not None:
            width_height_tuple = (target_size[1], target_size[0])
            if img.size != width_height_tuple:
                if self.interpolation not in _PIL_INTERPOLATION_METHODS:
                    raise ValueError(
                        'Invalid interpolation method {} specified. Supported '
                        'methods are {}'.format(
                            self.interpolation,
                            ', '.join(_PIL_INTERPOLATION_METHODS.keys())))
                resample = _PIL_INTERPOLATION_METHODS[self.interpolation]
                img = img.resize(width_height_tuple, resample)
        return img

    def process(self, img):
        img_tensor = img_to_array(img)
        augmented = self.data_gen.random_transform(img_tensor)
        extended = np.expand_dims(augmented, axis=0)
        normalized = extended / 255.
        return normalized

    def load_web_img(self, url, target_size=TARGET_SIZE):
        response = requests.get(url)
        img = Image.open(BytesIO(response.content))
        img = self.reshape_img(img, target_size)
        augmented = self.process(img)
        return augmented

    def load_local_img(self, path, target_size=TARGET_SIZE):
        img = load_img(path,
                       target_size=target_size,
                       interpolation=self.interpolation)
        augmented = self.process(img)
        return augmented
示例#26
0
def get_set_of_imgs(img, replication_factor):
    ans = [img]
    datagen = ImageDataGenerator(rotation_range=15,
                                 width_shift_range=0.1,
                                 height_shift_range=0.1,
                                 shear_range=0.1,
                                 zoom_range=0.1,
                                 fill_mode='nearest')
    for i in range(replication_factor):
        ans.append(datagen.random_transform(img))
    return ans
示例#27
0
def random_transform(image,
                     mask,
                     seed,
                     rotation_range=5,
                     width_shift_range=0.05,
                     height_shift_range=0.05,
                     horizontal_flip=True,
                     u=0.5):
    if np.random.random() < u:
        data_gen_args = dict(rotation_range=rotation_range,
                             width_shift_range=width_shift_range,
                             height_shift_range=height_shift_range,
                             horizontal_flip=horizontal_flip)
        image_data_gen = ImageDataGenerator(**data_gen_args)
        mask_data_gen = ImageDataGenerator(**data_gen_args)

        image = image_data_gen.random_transform(image, seed)
        mask = mask_data_gen.random_transform(mask, seed)

    return image, mask
示例#28
0
def image_generator(filepaths, setname=None, batch_size=2, input_shape=(128, 128),
                    output_shape=(128, 128), aug=False):
    if aug:
        aug_gen = ImageDataGenerator(shear_range=0.25, rotation_range=30,
                                     width_shift_range=0.25,
                                     height_shift_range=0.25, zoom_range=0.75,
                                     horizontal_flip=True, vertical_flip=True)
    while True:
        trainfiles = filepaths.copy()
        if setname == 'train':
            trainfiles = trainfiles[:int(len(trainfiles) * 0.8)]
        elif setname == 'val':
            trainfiles = trainfiles[int(len(trainfiles) * 0.8):]

        random.shuffle(trainfiles)
        trainfiles_masks = [re.sub('/train', '/train_masks', f) for f in trainfiles]
        trainfiles_masks = [re.sub('\.jpg$', '_mask.gif', f) for f in trainfiles_masks]

        for i in range(0, len(trainfiles), batch_size):
            if (i + 1) * batch_size > len(trainfiles):
                batch_files = trainfiles[(len(trainfiles) - batch_size):]
                batch_filesmask = trainfiles_masks[(len(trainfiles) - batch_size):]
            else:
                batch_files = trainfiles[(i * batch_size):((i + 1) * batch_size)]
                batch_filesmask = trainfiles_masks[(i * batch_size):((i + 1) * batch_size)]

            batch_imgs = np.zeros((batch_size, input_shape[0], input_shape[1], 3))
            batch_masks = np.zeros((batch_size, output_shape[0], output_shape[1], 1))
            for j in range(batch_size):
                batch_imgs[j] = preprocess_input(cv2.imread(batch_files[j]), input_shape)
                tmp_mask = cv2.resize(imageio.mimread(batch_filesmask[j])[0], output_shape)
                batch_masks[j] = np.expand_dims(tmp_mask, axis=2) // 255

            if aug:
                for j in range(batch_size):
                    seed = np.random.randint(1e6)
                    batch_imgs[j] = aug_gen.random_transform(batch_imgs[j], seed=seed)
                    batch_masks[j] = aug_gen.random_transform(batch_masks[j], seed=seed)

            yield (batch_imgs, batch_masks)
示例#29
0
def augment_set(x_samples, y_samples, num_runs_to_add=1, class_label=1):

    if x_samples.shape[0] != y_samples.shape[0]:
        print(
            'number of samples doesnt equal number of labels, something is wrong!\n'
        )

    num_total_samples_before = x_samples.shape[0]

    x_myclass = x_samples[y_samples[:, 0] == class_label, ...]
    #y_myclass = y_samples[y_samples[:,0] == class_label,...]

    #count the number of samples to augment, to reserve the appropriate space
    num_positive_samples = x_myclass.shape[0]

    #reserve space for the new samples (shape is the same as before, exept with a new number of samples)
    x_augmented = np.zeros(
        (((x_samples.shape[0] + num_positive_samples * num_runs_to_add), ) +
         x_samples.shape[1:]),
        dtype='uint8')
    y_augmented = np.zeros(
        (((y_samples.shape[0] + num_positive_samples * num_runs_to_add), ) +
         y_samples.shape[1:]),
        dtype='uint8')

    #the first part of the set is the old set
    x_augmented[0:x_samples.shape[0], ...] = x_samples
    y_augmented[0:y_samples.shape[0], ...] = y_samples

    datagen = ImageDataGenerator(
        rotation_range=90,
        zoom_range=[1, 1.4],  #[lower, upper] = [1-zoom_range, 1+zoom_range]
        horizontal_flip=True,
        vertical_flip=True,
        fill_mode='reflect',
        preprocessing_function=None)  # maybe include the strech histogram here

    #using seed=42 makes the augmentations be the same each time this function is called (at least i think so)
    #so its like we are using a saved, larger set
    datagen.fit(x_myclass, augment=True, seed=42)

    #now iterate trough all samples of class_label num_runs_to_add times
    counter = num_total_samples_before
    for i in range(num_runs_to_add):
        for current_sample in x_myclass:
            #print(current_sample.shape)
            x_augmented[counter,
                        ...] = datagen.random_transform(current_sample)
            counter += 1

    return x_augmented, y_augmented
示例#30
0
 def _augment_data(self, tensor, data_aug_param, img_num, add_self=True):
     """Augment data set and add noises."""
     data_generator = ImageDataGenerator(**data_aug_param)
     if add_self:
         new_x_tensors = copy(tensor)
     else:
         new_x_tensors = []
     while True:
         for i in range(len(tensor)):
             if len(new_x_tensors) >= img_num:
                 return np.array(new_x_tensors, dtype=self.data_type)
             augmented = data_generator.random_transform(tensor[i].astype(
                 np.float32))
             new_x_tensors.append(augmented)
示例#31
0
def keras_generator_test():
    from keras.preprocessing.image import ImageDataGenerator
    image = skimage.io.imread(
        "F:/lung_project2/lidc_cubes_64_overbound_roughspacing/LIDC-IDRI-1012_1_2.0_[2.       0.722656 0.722656]_oa_s3.png"
    )
    #image = np.random.rand(50, 50, 3)
    plt.imshow(image)
    plt.savefig('temp.png')
    generator = ImageDataGenerator(shear_range=1.,
                                   data_format='channels_last',
                                   fill_mode='constant')
    image_transformed = generator.random_transform(image)
    plt.imshow(image_transformed)
    plt.show()