首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tf.reshape尺寸与tf.decode_raw的输出不匹配

tf.reshape尺寸与tf.decode_raw的输出不匹配
EN

Stack Overflow用户
提问于 2019-11-12 09:39:28
回答 1查看 168关注 0票数 0
代码语言:javascript
复制
import cv2
img = cv2.imread(cat_in_snow)
height, width, channels = img.shape
print (height, width, channels)

上述代码片段的

输出为213 320 3

代码语言:javascript
复制
raw_image_dataset = tf.data.TFRecordDataset('images.tfrecords')
# Create a dictionary describing the features.
image_feature_description = {
    'height': tf.io.FixedLenFeature([], tf.int64),
    'width': tf.io.FixedLenFeature([], tf.int64),
    'depth': tf.io.FixedLenFeature([], tf.int64),
    'label': tf.io.FixedLenFeature([], tf.int64),
    'image_raw': tf.io.FixedLenFeature([], tf.string),
}

def _parse_image_function(example_proto):
  # Parse the input tf.Example proto using the dictionary above.
  return tf.io.parse_single_example(example_proto, image_feature_description)

parsed_image_dataset = raw_image_dataset.map(_parse_image_function)

for image_features in parsed_image_dataset:  
  print(image_features['image_raw'])
  image_raw = image_features['image_raw'].numpy()
  dec_img = tf.io.decode_raw(image_features['image_raw'], tf.uint8)
  img = tf.reshape(dec_img,[213 ,320, 3])

InvalidArgumentError: Input to reshape是一个张量,值为17858,但所请求的形状有204480 Op:Reshape

上面的文件包含与opencv中使用的相同的图像,但是decode_raw函数提供了不同的输出。有人能帮我解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2020-03-04 14:19:04

我也有同样的问题。

我发现,如果将图像字符串保存到tfrecords中,可以使用`tf.io.decode_raw‘方法。

代码语言:javascript
复制
image_data = matplotlib.image.imread(img_path)
# Convert image to string data
image_str = image_data.tostring()

但是对我来说,我用字节()读取和存储图像数据(),所以我使用'tf.image.decode_image‘方法。

代码语言:javascript
复制
with tf.compat.v1.gfile.FastGFile(img_path, 'rb') as fid:
                    image_data = fid.read()

希望这个答案对你有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58815825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档