Esempio n. 1
0
def batch_ood_generator(
        list_samples,
        ood_paths,
        batch_size=32,
        pre_processing_function=None,
        resize_size=(128, 128),
        augment=False,
        n_label=102,
        base_path="",
):
    seq = get_seq()
    pre_processing_function = (pre_processing_function
                               if pre_processing_function is not None else
                               preprocess_input)
    while True:
        shuffle(list_samples)
        shuffle(ood_paths)
        for batch_samples, ood_samples in zip(
                chunks(list_samples, size=batch_size // 2),
                chunks(ood_paths, size=batch_size // 2),
        ):
            images = [
                read_img_from_path(base_path + sample.path)
                for sample in batch_samples
            ]

            ood_images = [
                read_img_from_path(sample.path) for sample in ood_samples
            ]

            if augment:
                images = seq.augment_images(images=images)
                ood_images = seq.augment_images(images=ood_images)

            images = [
                resize_img(x, h=resize_size[0], w=resize_size[1])
                for x in images
            ]
            ood_images = [
                resize_img(x, h=resize_size[0], w=resize_size[1])
                for x in ood_images
            ]

            images = [pre_processing_function(a) for a in images]
            ood_images = [pre_processing_function(a) for a in ood_images]

            targets = [sample.label for sample in batch_samples]

            X = np.array(images + ood_images)
            Y1 = np.array(targets) - 1
            Y1 = np.eye(n_label)[Y1, :].tolist()
            Y2 = [[1 / n_label for _ in range(n_label)]] * len(ood_samples)
            Y = np.array(Y1 + Y2)
            yield X, Y
def batch_generator(
        list_samples,
        batch_size=32,
        pre_processing_function=None,
        resize_size=(128, 128),
        augment=False,
):
    seq = get_seq()
    pre_processing_function = (pre_processing_function
                               if pre_processing_function is not None else
                               preprocess_input)
    while True:
        shuffle(list_samples)
        for batch_samples in chunks(list_samples, size=batch_size):
            images = [
                read_img_from_path(sample.path) for sample in batch_samples
            ]

            if augment:
                images = seq.augment_images(images=images)

            images = [
                resize_img(x, h=resize_size[0], w=resize_size[1])
                for x in images
            ]

            images = [pre_processing_function(a) for a in images]
            targets = [sample.target_vector for sample in batch_samples]
            X = np.array(images)
            Y = np.array(targets)

            yield X, Y
 def predict_from_path(self, path):
     """
     if platform.system() == "Windows":
         dash = "\\"
     else:
         dash = "/"
     # Establish main path for files # <-- Final GUI change
     path = dash.join(os.path.realpath(__file__).split(dash)[:-1]) + dash
     """
     arr = read_img_from_path(path)
     return self.predict_from_array(arr)
def batch_generator(
    list_samples,
    batch_size=32,
    pre_processing_function=None,
    resize_size=(128, 128),
    augment=False,
    max_value_labels=19982,
    embedding_size=100,
    base_path="",
):
    seq = get_seq()
    pre_processing_function = (pre_processing_function
                               if pre_processing_function is not None else
                               preprocess_input)
    while True:
        shuffle(list_samples)
        for batch_samples in chunks(list_samples, size=batch_size):
            images = [
                read_img_from_path(base_path + sample.path)
                for sample in batch_samples
            ]

            if augment:
                images = seq.augment_images(images=images)

            images = [
                resize_img(x, h=resize_size[0], w=resize_size[1])
                for x in images
            ]

            images = [pre_processing_function(a) for a in images]
            targets_positive = [
                choice(sample.labels) for sample in batch_samples
            ]
            targets_negative = targets_positive[::-1]
            targets_negative = [
                x if x not in sample.labels and np.random.uniform(0, 1) < 0.9
                else np.random.randint(0, max_value_labels + 1)
                for x, sample in zip(targets_negative, batch_samples)
            ]

            X = np.array(images)
            Y1 = np.array(targets_positive)[..., np.newaxis]
            Y2 = np.array(targets_negative)[..., np.newaxis]

            yield [X, Y1, Y2], np.zeros(
                (len(batch_samples), 3 * embedding_size))
Esempio n. 5
0
    resize_img,
)

if __name__ == "__main__":

    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--fold",
        help="fold",
        default="train_3",
    )
    args = parser.parse_args()

    with open("../example/training_config.yaml", "r") as f:
        training_config = yaml.load(f, yaml.SafeLoader)

    fold = args.fold
    folder = training_config["data_path"] + fold + "_small"
    shutil.rmtree(folder, ignore_errors=True)
    os.makedirs(folder, exist_ok=True)

    images = glob(training_config["data_path"] + fold + "/*.jpg")

    for img_path in tqdm(images):
        img = read_img_from_path(img_path)
        img = resize_img(img, h=224, w=224)
        img = img.astype(np.uint8)
        imwrite(img_path.replace(fold, fold + "_small"), img)
Esempio n. 6
0
 def predict_from_path(self, path):
     arr = read_img_from_path(path)
     return self.predict_from_array(arr)