读取TFRecord图像数据时Reshape报错,请求技术协助
Let’s break down your problem clearly: that error InvalidArgumentError: Input to reshape is a tensor with 11566 values, but the requested shape has 196608 tells us exactly what’s wrong. 196608 is the total pixel count for a 256×256×3 image, but the data you’re reading only has 11566 bytes—way smaller. This mismatch stems from two critical issues in your code:
Why This Happens
- Hardcoded image dimensions: In
create_tf_example, you’re settingheight=256,width=256,depth=3without verifying if your source images actually match these values. If your images are a different size or format, this breaks the reshape step later. - Incorrect decoding logic: You’re writing compressed image data (directly reading the file with
f.read(), which gives JPEG/PNG bytes) but trying to decode it withtf.decode_raw—a function designed for raw uncompressed pixel data. Compressed images have far fewer bytes than a full pixel array, hence the mismatch.
Step-by-Step Fixes
1. Write TFRecords with Real Image Metadata
Stop hardcoding dimensions—extract the actual size from the image and save it, along with the image format (JPEG/PNG) for proper decoding later. Here’s the updated create_tf_example:
import tensorflow as tf from PIL import Image def create_tf_example(example): # Clean up the file path filename = example['Title'].encode() path = example['path'].replace('.json', '') example['path'] = path # Read compressed image data with open(path, 'rb') as f: encoded_image_data = f.read() # Get real image dimensions using PIL with Image.open(path) as img: width, height = img.size depth = len(img.getbands()) # 3 for RGB, 1 for grayscale # Build the TFExample with all necessary features tf_example = tf.train.Example(features=tf.train.Features(feature={ 'image/height': tf.train.Feature(int64_list=tf.train.Int64List(value=[height])), 'image/width': tf.train.Feature(int64_list=tf.train.Int64List(value=[width])), 'image/depth': tf.train.Feature(int64_list=tf.train.Int64List(value=[depth])), 'image/encoded': tf.train.Feature(bytes_list=tf.train.BytesList(value=[encoded_image_data])), 'image/format': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b'jpeg'])) # Update to b'png' if needed })) return tf_example
2. Read TFRecords with Proper Image Decoding
Instead of tf.decode_raw, use TensorFlow’s built-in image decoding functions that handle compressed formats. This automatically converts compressed bytes into the correct pixel tensor:
def read_and_decode(filename_queue): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, features={ 'image/height': tf.FixedLenFeature([], tf.int64), 'image/width': tf.FixedLenFeature([], tf.int64), 'image/depth': tf.FixedLenFeature([], tf.int64), 'image/encoded': tf.FixedLenFeature([], tf.string), 'image/format': tf.FixedLenFeature([], tf.string), }) # Decode based on the saved image format image_format = tf.compat.as_str(features['image/format'].numpy()) if image_format == 'jpeg': image = tf.image.decode_jpeg(features['image/encoded'], channels=tf.cast(features['image/depth'], tf.int32)) elif image_format == 'png': image = tf.image.decode_png(features['image/encoded'], channels=tf.cast(features['image/depth'], tf.int32)) else: raise ValueError(f"Unsupported image format: {image_format}") # Optional: Resize to 256x256 if you need a fixed output size image = tf.image.resize(image, [256, 256]) return image
3. Alternative: Write Raw Uncompressed Pixel Data
If you specifically need to save raw pixel data (not compressed), modify the writing code to decode, resize, and save raw bytes:
# Inside create_tf_example, replace the encoded_image_data section: import numpy as np with Image.open(path) as img: # Resize to your target 256x256 size resized_img = img.resize((256, 256)) # Convert to numpy array and get raw bytes image_array = np.array(resized_img) encoded_image_data = image_array.tobytes() # Update dimensions to match the resized image height, width, depth = image_array.shape
In this case, your original read_and_decode code using tf.decode_raw will work, since the bytes now exactly match the 256×256×3 shape.
Final Notes
The core issue was mixing compressed image data with raw pixel decoding. By either saving compressed data and using the right decoding function, or saving raw pixels and matching the shape, you’ll fix that reshape error. Always avoid hardcoding dimensions—pull them from the actual image to prevent mismatches.
内容的提问来源于stack exchange,提问作者rohith_92

