python - 我们如何使用 tensorflow 的alphentation 获得正确的形状尺寸
问题描述
我正在编写一个卷积自动编码器。
我在我的 tensorflow 代码中使用albumentations 进行数据增强,但我在形状尺寸方面遇到了一个粗鲁的问题。
我在 train_dataset 管道的输出中得到了 (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))
我认为这个问题来自 process_aug 函数,并在数据集管道中注释了 map 函数,但我没有找到 process_aug 函数中的问题所在。
解决方案
我通过添加 tf.reshape() 编辑了函数,但我希望你们中的一个可以解释如何避免 tf.numpy_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
推荐阅读
- pdf - 将 LaTex 表格中的每个元素居中
- amazon-web-services - AWS Insights 嵌套查询?
- c# - .Net Core Console 应用程序未在 Docker 容器中运行
- ios - 动画UILabel从后台返回时消失
- twitter-bootstrap - 在加载时停用文本字段,以及
- angular - 无法绑定到“countUp”,因为它不是“h1”的已知属性。countup.js-angular2
- faunadb - 具有可搜索嵌套对象数组的 CreateIndex
- internationalization - 如何在模板中添加短语以在 prestashop 1.7.6 中翻译?
- json - 将一些 JSON 提取到一个变量中,现在想要从该变量中提取 JSON 到另一个变量中
- c# - 如何在数据表中分配格式化的列值