微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我们如何使用 tensorflow

如何解决我们如何使用 tensorflow

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

我在我的 tensorflow 代码中使用相册进行数据增强,但我面临着形状尺寸的粗鲁问题。

我在 train_dataset 管道的输出中得到了 (32,1,256,3) 而不是 (32,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,3))
    img_output.set_shape((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,Tout=[tf.float32])
    aug_img_output = tf.reshape(aug_img_output,3])
    return aug_img_input,aug_img_output

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?