[英]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.