如何训练标签也是图像的代码?

2024-06-16 10:40:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个图像去噪代码,所以我的图像是有噪图像,我的标签是干净的图像。我已经把所有的图像放进了一个.tf记录文件。现在当我训练的时候,我得到一个错误:

Traceback (most recent call last):
  File "/home/tian/tensorflow/example/Denoising/Try_Myself/train.py", line 59, in <module>
    tf.app.run()
  File "/home/tian/.local/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "/home/tian/tensorflow/example/Denoising/Try_Myself/train.py", line 54, in main
    img_noised,img_gray=get_batch(image1, image2, batch_size)
  File "/home/tian/tensorflow/example/Denoising/Try_Myself/ops.py", line 64, in get_batch
    num_threads=16, capacity=5000, min_after_dequeue=1000,)
  File "/home/tian/.local/lib/python3.5/site-packages/tensorflow/python/training/input.py", line 1220, in shuffle_batch
    name=name)
  File "/home/tian/.local/lib/python3.5/site-packages/tensorflow/python/training/input.py", line 776, in _shuffle_batch
    dtypes=types, shapes=shapes, shared_name=shared_name)
  File "/home/tian/.local/lib/python3.5/site-packages/tensorflow/python/ops/data_flow_ops.py", line 629, in __init__
    shapes = _as_shape_list(shapes, dtypes)
  File "/home/tian/.local/lib/python3.5/site-packages/tensorflow/python/ops/data_flow_ops.py", line 76, in _as_shape_list
    raise ValueError("All shapes must be fully defined: %s" % shapes)
ValueError: All shapes must be fully defined: [TensorShape([Dimension(None)]), TensorShape([Dimension(None)])]

它说这里有点不对劲:

def get_batch(image, label, batch_size):
    images, label_batch = tf.train.shuffle_batch([image, label], batch_size=batch_size,
                                                 num_threads=16, capacity=5000, min_after_dequeue=1000,)

    #return images, tf.reshape(label_batch, [batch_size])
    return images, label_batch

返回图像为带噪图像,标签批为干净图像。 请,有人告诉我如何修复它,我可以训练这个代码属于那些命令:

‘’‘’‘’
for i in range(Train_step):
     image_noised,image_clean=sess.run([train_X,train_Y])
     print('Shape',image_gray.shape)
     _,loss_value,step=sess.run([train_op,loss,global_step],feed_dict= {X:image_noised, X_:image_gray})
‘’‘’‘’

Ps:图像形状为[180180,1],批量大小为8。如果你需要什么信息,请告诉我。你知道吗

读取.tfrecords文件的代码是:

def read_and_decode(filename):
    filename_queue = tf.train.string_input_producer([filename])  

    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)  
    features = tf.parse_single_example(serialized_example,
                                       features={
                                           'image_raw_gray': tf.FixedLenFeature([], tf.string),
                                           'image_raw_noised': tf.FixedLenFeature([], tf.string),
                                       })

    img_g = tf.decode_raw(features['image_raw_gray'], tf.uint8)
    img_n = tf.decode_raw(features['image_raw_noised'], tf.uint8)

    image_g = tf.cast(img_g, tf.float32) * (1. / 255) - 0.5
    image_n = tf.cast(img_n, tf.float32) * (1. / 255) - 0.5

    return image_g, image_n

我创建.tfrecords文件的代码如下:

''''''
''''''
img_gray=cv2.imread(gray_img)
img_noised=cv2.imread(noised_img)

img_raw_gray=img_gray.tobytes()
img_raw_noised=img_noised.tobytes()

example = tf.train.Example(features=tf.train.Features(feature={
    'image_raw_gray': _bytes_feature(img_raw_gray),
    'image_raw_noised': _bytes_feature(img_raw_noised),
    'height': _int64_feature(img_gray.shape[0]),
    'width': _int64_feature(img_gray.shape[1]),
}))
'''''''
'''''''

Tags: inpy图像imagehomeimgrawtf
1条回答
网友
1楼 · 发布于 2024-06-16 10:40:23

关于你的错误

Shape(?,)表示有一个长度未指定的一维向量。 由于将预解码图像存储在TFRecord中,tf.decode_raw无法知道图像最初的形状。 你需要在你的decode_raw调用之后加一个img_g = tf.reshape(img_g, (180, 180, 1))来解决这个问题。你知道吗

关于您的TF记录

构建TFRecord文件的方式有点奇怪。首先,由于您存储的是每个图像的原始解码字节,因此示例相当繁重。你知道吗

其次,您依赖于来自opencv的解码数据与Tensorflow直接兼容(如,数据的相同表示)。这种情况通常是这样的,不过如果使用3通道图像,可能会遇到问题。你知道吗

要同时解决这两个问题,应改为在tf.train.Example中写入图像文件中包含的字节,即编码的图像,然后用tensorflow进行解码:

# in your file to create tfrecords
with open(gray_img, 'rb') as fp:
  img_gray_encoded = fp.read()
with open(noised_img, 'rb') as fp:
  img_noised_encoded = fp.read()

example = tf.train.Example(features=tf.train.Features(feature={
    'image_encoded_gray': _bytes_feature(img_gray_encoded),
    'image_encoded_noised': _bytes_feature(img_noised_encoded)
}))

注意,我正在删除宽度和高度特性,因为这已经在编码图像中隐式定义了。你知道吗

# in the input pipeline
img_g = tf.image.decode_image(features['image_gray_encoded'], channels=1)
tf.set_shape(img_g, (180, 180, 1))

img_n = tf.image.decode_image(features['image_noised_encoded'], channels=1)
tf.set_shape(img_n, (180, 180, 1))

注意,在本例中,我使用的不是reshape,而是set_shape。 这是因为,与您的示例不同,这里decode_image知道(在运行时)图像的形状。为了在图形构建时使该形状可用,我们通知tensorflow,图像实际上始终具有形状(180, 180, 1)。你知道吗

相关问题 更多 >