コード例 #1
0
def aligned_dataset(opt):
    opt = opt
    root = opt.dataroot

    ### input A (label maps)
    dir_A = '_A' if opt.label_nc == 0 else '_label'
    dir_A = os.path.join(opt.dataroot, opt.phase + dir_A)
    A_paths = sorted(make_dataset(dir_A))

    ### input B (real images)
    if opt.isTrain or opt.use_encoded_image:
        dir_B = '_B' if opt.label_nc == 0 else '_img'
        dir_B = os.path.join(opt.dataroot, opt.phase + dir_B)
        B_paths = sorted(make_dataset(dir_B))

    assert len(A_paths) == len(B_paths)
    A_dataset = tl.disk_image_batch_dataset(
        A_paths,
        opt.batchSize,
        drop_remainder=not opt.no_drop_remainder,
        map_fn=preprocess_image_A,
        shuffle=not opt.no_shuffle,
        repeat=opt.repeat_num)

    B_dataset = tl.disk_image_batch_dataset(
        B_paths,
        opt.batchSize,
        drop_remainder=not opt.no_drop_remainder,
        map_fn=preprocess_image_B,
        shuffle=not opt.no_shuffle,
        repeat=opt.repeat_num)
    A_B_dataset = tf.data.Dataset.zip((A_dataset, B_dataset))
    len_dataset = len(A_paths) // opt.batchSize
    return A_B_dataset, len_dataset
コード例 #2
0
def make_dataset(img_paths, batch_size, load_size, crop_size, training, drop_remainder=True, shuffle=True, repeat=1):
    if training:
        @tf.function
        def _map_fn(img):  # preprocessing
            img = tf.image.random_flip_left_right(img)
            img = tf.image.random_flip_up_down(img)
            # img = tf.image.resize(img, [load_size, load_size])
            img = tf.image.random_crop(img, [crop_size, crop_size, tf.shape(img)[-1]])
            img = tf.clip_by_value(tf.cast(img, tf.float32), 0, 255) / 255.0  # or img = tl.minmax_norm(img)
            img = img * 2 - 1
            return img
    else:
        @tf.function
        def _map_fn(img):  # preprocessing
            # img = tf.image.resize(img, [crop_size, crop_size]) # or
            img = tf.image.resize(img, [load_size, load_size]);
            img = tl.center_crop(img, load_size)
            img = tf.clip_by_value(tf.cast(img, tf.float32), 0, 255) / 255.0  # or img = tl.minmax_norm(img)
            img = img * 2 - 1
            return img

    return tl.disk_image_batch_dataset(img_paths,
                                       batch_size,
                                       drop_remainder=drop_remainder,
                                       map_fn=_map_fn,
                                       shuffle=shuffle,
                                       repeat=repeat)
コード例 #3
0
def make_custom_datset(img_paths, batch_size, resize=64, drop_remainder=True, shuffle=True, repeat=1):
    @tf.function
    def _map_fn(img):
        # ======================================
        # =               custom               =
        # ======================================
        img = ...  # custom preprocessings, should output img in [0.0, 255.0]
        # ======================================
        # =               custom               =
        # ======================================
        img = tf.image.resize(img, [resize, resize])
        img = tf.clip_by_value(img, 0, 255)
        img = img / 127.5 - 1
        return img

    dataset = tl.disk_image_batch_dataset(img_paths,
                                          batch_size,
                                          drop_remainder=drop_remainder,
                                          map_fn=_map_fn,
                                          shuffle=shuffle,
                                          repeat=repeat)
    img_shape = (resize, resize, 3)
    len_dataset = len(img_paths) // batch_size

    return dataset, img_shape, len_dataset
コード例 #4
0
ファイル: data.py プロジェクト: gawain93/tfan
def make_celeba_dataset(img_paths,
                        batch_size,
                        resize=64,
                        drop_remainder=True,
                        shuffle=True,
                        repeat=1):
    @tf.function
    def _map_fn(img):
        crop_size = 108
        img = tf.image.crop_to_bounding_box(img, (218 - crop_size) // 2,
                                            (178 - crop_size) // 2, crop_size,
                                            crop_size)
        img = tf.image.resize(img, [resize, resize])
        img = tf.clip_by_value(img, 0, 255)
        img = img / 127.5 - 1
        return img

    dataset = tl.disk_image_batch_dataset(img_paths,
                                          batch_size,
                                          drop_remainder=drop_remainder,
                                          map_fn=_map_fn,
                                          shuffle=shuffle,
                                          repeat=repeat)
    img_shape = (resize, resize, 3)
    len_dataset = len(img_paths) // batch_size

    return dataset, img_shape, len_dataset
コード例 #5
0
def make_dataset(img_paths,
                 batch_size,
                 load_size,
                 crop_size,
                 training,
                 drop_remainder=True,
                 shuffle=True,
                 repeat=1):
    _map_fn = img_preprocessing_fn(load_size, crop_size, training)
    return tl.disk_image_batch_dataset(img_paths,
                                       batch_size,
                                       drop_remainder=drop_remainder,
                                       map_fn=_map_fn,
                                       shuffle=shuffle,
                                       repeat=repeat)
コード例 #6
0
def make_PETCT_dataset(img_paths, batch_size, resize=128, drop_remainder=True, shuffle=True, repeat=1):
    @tf.function
    def _map_fn(img):
        img = tf.image.resize(img, [resize, resize])
        img = tf.clip_by_value(img, 0, 255)
        img = img / 127.5 - 1
        return img

    dataset = tl.disk_image_batch_dataset(img_paths,
                                          batch_size,
                                          drop_remainder=drop_remainder,
                                          map_fn=_map_fn,
                                          shuffle=shuffle,
                                          repeat=repeat)
    img_shape = (resize, resize, 3)
    len_dataset = len(img_paths) // batch_size

    return dataset, img_shape, len_dataset
コード例 #7
0
ファイル: data.py プロジェクト: serre-lab/tripletcyclegan
def make_dataset(img_paths,
                 batch_size,
                 load_size,
                 crop_size,
                 training,
                 drop_remainder=True,
                 grayscale=False,
                 shuffle=False,
                 repeat=1):
    # if training:

    #     @tf.function
    #     def _map_fn(img):  # preprocessing
    #         #toss = np.random.uniform(0,1)
    #         if grayscale:
    #             img = tf.image.rgb_to_grayscale(img)
    #             img = tf.image.grayscale_to_rgb(img)
    #         img = tf.image.random_flip_left_right(img)

    #         img = tf.image.resize_with_pad(img, load_size, load_size, antialias = True)
    #         img = tf.image.random_crop(img, [crop_size, crop_size, tf.shape(img)[-1]])
    #         img = tf.clip_by_value(img, 0, 255) / 255.0  # or img = tl.minmax_norm(img)
    #         img = img * 2 - 1
    #         return img
    # else:
    #     @tf.function
    #     def _map_fn(img):  # preprocessing
    #         img = tf.image.resize_with_pad(img,crop_size, crop_size, antialias = True)  # or img = tf.image.resize(img, [load_size, load_size]); img = tl.center_crop(img, crop_size)
    #         if grayscale:
    #             img = tf.image.rgb_to_grayscale(img)
    #             img = tf.image.grayscale_to_rgb(img)
    #         img = tf.clip_by_value(img, 0, 255) / 255.0  # or img = tl.minmax_norm(img)
    #         img = img * 2 - 1
    #         return img
    if training:

        @tf.function
        def _map_fn(img):  # preprocessing

            #toss = np.random.uniform(0,1)
            if grayscale:
                img = tf.image.rgb_to_grayscale(img)
                img = tf.image.grayscale_to_rgb(img)
            img = tf.image.random_flip_left_right(img)
            maxside = tf.math.maximum(tf.shape(img)[0], tf.shape(img)[1])
            minside = tf.math.minimum(tf.shape(img)[0], tf.shape(img)[1])
            new_img = img

            if tf.math.divide(maxside, minside) > 1.2:

                repeating = tf.math.floor(tf.math.divide(maxside, minside))
                new_img = img
                if tf.math.equal(tf.shape(img)[1], minside):
                    for i in range(int(repeating)):
                        new_img = tf.concat((new_img, img), axis=1)

                if tf.math.equal(tf.shape(img)[0], minside):
                    for i in range(int(repeating)):
                        new_img = tf.concat((new_img, img), axis=0)
                    new_img = tf.image.rot90(new_img)

            else:
                new_img = img

            img = tf.image.resize(new_img, [crop_size, crop_size])
            #im.imwrite(img.numpy(),'test.jpg')
            #img = tf.image.central_crop(img, [crop_size, crop_size, tf.shape(img)[-1]])
            img = tf.clip_by_value(img, 0,
                                   255) / 255.0  # or img = tl.minmax_norm(img)
            img = img * 2 - 1
            return img
    else:

        @tf.function
        def _map_fn(img):  # preprocessing
            maxside = tf.math.maximum(tf.shape(img)[0], tf.shape(img)[1])
            minside = tf.math.minimum(tf.shape(img)[0], tf.shape(img)[1])
            new_img = img

            if tf.math.divide(maxside, minside) > 1.3:

                repeating = tf.math.floor(tf.math.divide(maxside, minside))
                new_img = img
                if tf.math.equal(tf.shape(img)[1], minside):
                    for i in range(int(repeating)):
                        new_img = tf.concat((new_img, img), axis=1)

                if tf.math.equal(tf.shape(img)[0], minside):
                    for i in range(int(repeating)):
                        new_img = tf.concat((new_img, img), axis=0)
                    new_img = tf.image.rot90(new_img)
            else:
                new_img = img
            img = tf.image.resize(new_img, [crop_size, crop_size])
            #padx = load_size - tf.shape(img)[0]
            #pady = load_size -tf.shape(img)[1]
            #paddings = [[padx/2,padx/2],[pady/2,pady/2],[0, 0]]
            #img = tf.pad(img,paddings,'SYMMETRIC')
            #img = tf.image.resize_with_pad(img,crop_size, crop_size, antialias = True)  # or img = tf.image.resize(img, [load_size, load_size]); img = tl.center_crop(img, crop_size)
            if grayscale:
                img = tf.image.rgb_to_grayscale(img)
                img = tf.image.grayscale_to_rgb(img)
            #img = tf.image.random_crop(img, [crop_size, crop_size, tf.shape(img)[-1]])
            img = tf.clip_by_value(img, 0,
                                   255) / 255.0  # or img = tl.minmax_norm(img)
            img = img * 2 - 1
            return img

    return tl.disk_image_batch_dataset(img_paths,
                                       batch_size,
                                       drop_remainder=drop_remainder,
                                       map_fn=_map_fn,
                                       shuffle=shuffle,
                                       repeat=repeat)
コード例 #8
0
ファイル: data.py プロジェクト: serre-lab/tripletcyclegan
def make_dataset2(img_paths,
                  labels,
                  batch_size,
                  load_size,
                  crop_size,
                  training,
                  drop_remainder=True,
                  grayscale=False,
                  shuffle=False,
                  repeat=1):
    if training:

        @tf.function
        def _map_fn(img, label):  # preprocessing

            #toss = np.random.uniform(0,1)
            if grayscale:
                img = tf.image.rgb_to_grayscale(img)
                img = tf.image.grayscale_to_rgb(img)
            img = tf.image.random_flip_left_right(img)
            maxside = tf.math.maximum(tf.shape(img)[0], tf.shape(img)[1])
            while tf.math.square(tf.shape(img)[0] - tf.shape(img)[1]) > 100:
                padx = tf.math.minimum(
                    maxside - tf.shape(img)[0],
                    tf.math.minimum(tf.shape(img)[0],
                                    tf.shape(img)[1]))
                pady = tf.math.minimum(
                    maxside - tf.shape(img)[1],
                    tf.math.minimum(tf.shape(img)[0],
                                    tf.shape(img)[1]))
                paddings = [[padx / 2, padx / 2], [pady / 2, pady / 2], [0, 0]]
                img = tf.pad(
                    img, paddings, 'SYMMETRIC'
                )  #tf.image.resize_with_pad(img, load_size, load_size, antialias = True)
            img = tf.image.resize(img, [load_size * +10, load_size + 10],
                                  preserve_aspect_ratio=True)
            img = tf.image.random_crop(
                img,
                [crop_size, crop_size, tf.shape(img)[-1]])
            img = tf.clip_by_value(img, 0,
                                   255) / 255.0  # or img = tl.minmax_norm(img)
            img = img * 2 - 1
            return [img, label]
    else:

        @tf.function
        def _map_fn(img, label):  # preprocessing
            img = _aspect_preserving_resize(
                img,
                load_size + 4)  # tf.image.resize(img, [load_size,load_size])
            #padx = load_size - tf.shape(img)[0]
            #pady = load_size -tf.shape(img)[1]
            #paddings = [[padx/2,padx/2],[pady/2,pady/2],[0, 0]]
            #img = tf.pad(img,paddings,'SYMMETRIC')
            #img = tf.image.resize_with_pad(img,crop_size, crop_size, antialias = True)  # or img = tf.image.resize(img, [load_size, load_size]); img = tl.center_crop(img, crop_size)
            if grayscale:
                img = tf.image.rgb_to_grayscale(img)
                img = tf.image.grayscale_to_rgb(img)
            img = tf.image.random_crop(
                img,
                [crop_size, crop_size, tf.shape(img)[-1]])
            img = tf.clip_by_value(img, 0,
                                   255) / 255.0  # or img = tl.minmax_norm(img)
            img = img * 2 - 1
            return [img, label]

    return tl.disk_image_batch_dataset(img_paths,
                                       batch_size,
                                       labels=labels,
                                       drop_remainder=drop_remainder,
                                       map_fn=_map_fn,
                                       shuffle=shuffle,
                                       repeat=repeat)
コード例 #9
0
ファイル: data.py プロジェクト: iszotic/CycleGAN-Tensorflow-2
def make_dataset(img_paths,
                 batch_size,
                 load_size,
                 crop_size,
                 training,
                 drop_remainder=True,
                 shuffle=True,
                 repeat=1,
                 inference=False,
                 augmentation_preset='Normal'):
    #if training:
    if not inference:

        @tf.function
        def _map_fn(img):  # preprocessing
            img = tf.image.random_flip_left_right(img)
            if augmentation_preset == 'Normal':
                shape_y = load_size
                shape_x = load_size
            elif augmentation_preset == 'Ratio':
                scale = tf.random.uniform([], minval=1.0001, maxval=1.0625)
                shape = tf.shape(img)
                min_shape = tf.math.minimum(shape[0], shape[1])
                ratio = tf.cast(load_size / min_shape, tf.float32)
                shape_y = tf.cast(shape[0], tf.float32)
                shape_x = tf.cast(shape[1], tf.float32)
                shape_y = tf.cast(shape_y * scale * ratio, tf.int32)
                shape_x = tf.cast(shape_x * scale * ratio, tf.int32)

            img = tf.image.resize(img, [shape_y, shape_x])
            img = (img - tf.reduce_min(img)) / (
                tf.reduce_max(img) - tf.reduce_min(img)
            )  # or img = tf.clip_by_value(img, 0, 255) / 255.0  # or img = tl.minmax_norm(img)
            img = tf.image.random_crop(
                img,
                [crop_size, crop_size, tf.shape(img)[-1]])
            img = img * 2 - 1
            return img

    elif inference:

        @tf.function
        def _map_fn(img):  # preprocessing
            img = tf.clip_by_value(img, 0,
                                   255) / 255.0  # or img = tl.minmax_norm(img)
            img = img * 2 - 1
            return img
    else:

        @tf.function
        def _map_fn(img):  # preprocessing
            img = tf.image.resize(
                img, [crop_size, crop_size]
            )  # or img = tf.image.resize(img, [load_size, load_size]); img = tl.center_crop(img, crop_size)
            img = img * 2 - 1
            return img

    return tl.disk_image_batch_dataset(img_paths,
                                       batch_size,
                                       drop_remainder=drop_remainder,
                                       map_fn=_map_fn,
                                       shuffle=shuffle,
                                       repeat=repeat)