繁体   English   中英

我们如何使用带有 tensorflow 的蛋白化获得正确的形状尺寸

[英]How do we get correct shape dimensions using albumentation with tensorflow

我正在编写一个卷积自动编码器。

我在我的 tensorflow 代码中使用albumentations 进行数据增强,但我在形状尺寸方面遇到了一个粗鲁的问题。

我在 train_dataset 管道的 output 中得到了 (32, 1, 256, 256, 3) 而不是 (32, 256, 256, 3) 的形状。

这是我的代码:

def process_path(image_input, image_output):
  # load the raw data from the file as a string
    img_input = tf.io.read_file(image_input)
    img_input = tf.image.decode_jpeg(img_input, channels=3)

    img_output = tf.io.read_file(image_output)
    img_output = tf.image.decode_jpeg(img_output, channels=3)

    return img_input, img_output

def aug_fn(image):
    data = {"image": image}
    aug_data = transforms(**data)
    aug_img = aug_data["image"]
    aug_img = tf.cast(aug_img/255.0, tf.float32)
    aug_img = tf.image.resize(aug_img, size=[256, 256])
    return aug_img

def process_aug(img_input, img_output):
    aug_img_input = tf.numpy_function(func=aug_fn, inp=[img_input], Tout=[tf.float32])
    aug_img_output = tf.numpy_function(func=aug_fn, inp=[img_output], Tout=[tf.float32])
    return aug_img_input, aug_img_output

def set_shapes(img_input, img_output):
    img_input.set_shape((256, 256, 3))
    img_output.set_shape((256, 256, 3))
    return img_input, img_output

transforms = OneOf([CLAHE(clip_limit=2), IAASharpen(), IAAEmboss(), RandomBrightnessContrast()], p=0.3)
train_dataset = (tf.data.Dataset.from_tensor_slices((DIR_TRAIN + filenames_train, 
                                                         DIR_TRAIN + filenames_train))
                        .map(process_path, num_parallel_calls=AUTO)
                        .map(process_aug, num_parallel_calls=AUTO)
                        .map(set_shapes, num_parallel_calls=AUTO)
                        .batch(parameters['BATCH_SIZE'])
                        .prefetch(AUTO)
                    )
next(iter(train_dataset))

I think this problem comes from the process_aug function with commenting the map function in the dataset pipeline but I didn't find where the problem is in the process_aug function exactly.

我编辑了 function 并添加了 tf.reshape() 但我希望你们中的一个可以解释如何避免 tf.numpy_function function 在此过程中添加维度?

def process_aug(img_input, img_output):
aug_img_input = tf.numpy_function(func=aug_fn, inp=[img_input, 256], Tout=[tf.float32])
    aug_img_input = tf.reshape(aug_img_input, [256, 256, 3])
    aug_img_output = tf.numpy_function(func=aug_fn, inp=[img_output, 256], Tout=[tf.float32])
    aug_img_output = tf.reshape(aug_img_output, [256, 256, 3])
    return aug_img_input, aug_img_output

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM