Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input to reshape is a tensor with 37632 values, but the requested shape has 150528

I have the same question:Input to reshape is a tensor with 37632 values, but the requested shape has 150528.

 writer = tf.python_io.TFRecordWriter("/home/henson/Desktop/vgg/test.tfrecords")  # 要生成的文件

for index, name in enumerate(classes):
    class_path = cwd + name +'/'
    for img_name in os.listdir(class_path):
        img_path = class_path + img_name  # 每一个图片的地址
    img = Image.open(img_path)
    img = img.resize((224, 224))
    img_raw = img.tobytes()  # 将图片转化为二进制格式
    example = tf.train.Example(features=tf.train.Features(feature={
        "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
        'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
    }))  # example对象对label和image数据进行封装
    writer.write(example.SerializeToString())  # 序列化为字符串

writer.close()


def read_and_decode(filename):  # 读入dog_train.tfrecords
    filename_queue = tf.train.string_input_producer([filename])  # 生成一个queue队列

reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)  # 返回文件名和文件
features = tf.parse_single_example(serialized_example,
                                   features={
                                       'label': tf.FixedLenFeature([], tf.int64),
                                       'img_raw': tf.FixedLenFeature([], tf.string),
                                   })  # 将image数据和label取出来

img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [224, 224, 3])  # reshape为128*128的3通道图片
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5  # 在流中抛出img张量
label = tf.cast(features['label'], tf.int32)  # 在流中抛出label张量
print(img,label)
return img, label

images, labels = read_and_decode("/home/henson/Desktop/vgg/TFrecord.tfrecords")
print(images,labels)
images, labels = tf.train.shuffle_batch([images, labels], batch_size=20, capacity=16*20, min_after_dequeue=8*20)

I thonght I have resize img to 224*224,and reshape to [224,224,3],but it doesn't work. How could I make it?

like image 701
Henson Wells Avatar asked Sep 28 '17 09:09

Henson Wells


People also ask

How does TF reshape work?

The tf. reshape does not change the order of or the total number of elements in the tensor, and so it can reuse the underlying data buffer. This makes it a fast operation independent of how big of a tensor it is operating on. To instead reorder the data to rearrange the dimensions of a tensor, see tf.


2 Answers

The problem is basically related to shape of Architecture of CNN.Let say I defined architecture shown in pictureCNN Architecture int coding we defined weights and biases in following wayenter image description here If we see (weights) Lets start with

wc1 in this layer I defined 32 filters of 3x3 size will be applied

wc2 in this layer I defined 64 filters of 3x3 size will be applied

wc3 in this layer I defined 128 filters of 3x3 size will be applied

wd1 38*38*128 is interesting (Where it comes from).

And in Architecture we also defined maxpooling concept. See Architecture pic in every step 1.Lets Explain it Let say your input image is 300 x 300 x 1 (in picture it is 28x28x1) 2. (If strides defined is set to 1)Each filter will have an 300x300x1 picture so After applying 32 filter of 3x3 the we will have 32 pictures of 300x300 thus collected images will be 300x300x32

3.After Maxpooling if (Strides=2 depends what you defined usually it is 2) image size will change from 300 x 300 x 32 to 150 x 150 x 32

  1. (If strides defined is set to 1)Now Each filter will have an 150x150x32 picture so After applying 64 filter of 3x3 the we will have 64 pictures of 300x300 thus collected images will be 150x150x(32x64)

5.After Maxpooling if (Strides=2 depends what you defined usually it is 2) image size will change from 150x150x(32x64) to 75 x 75 x (32x64)

  1. (If strides defined is set to 1)Now Each filter will have an 75 x 75 x (32x64) picture so After applying 64 filter of 3x3 the we will have 128 pictures of 75 x 75 x (32x64) thus collected images will be 75 x 75 x (32x64x128)

7.After Maxpooling since dimension of image is 75x75(odd dimension make it even) so it is needed to pad first (if padding defined ='Same') then it will change to 76x76(even) ** if (Strides=2 depends what you defined usually it is 2) image size will change from 76x76x(32x64x128) to **38 x 38 x (32x64x128)

Now See 'wd1' in coding picture here comes 38*38*128

like image 189
hafiz ahmad hassan Avatar answered Oct 21 '22 05:10

hafiz ahmad hassan


I had the same error , so changed my code from this:

image = tf.decode_raw(image_raw, tf.float32)
image = tf.reshape(image, [img_width, img_height, 3])

to this:

image = tf.decode_raw(image_raw, tf.uint8)
image = tf.reshape(image, [img_width, img_height, 3])


# The type is now uint8 but we need it to be float.
image = tf.cast(image, tf.float32)

It is because somehow there's a mismatch in my generate_tf_record data format. I serialized it to string instead of bytelist. I notice the difference you and me, you change your image to byte . Here's how I write my image to tfrecord.

            file_path, label = sample
            image = Image.open(file_path)
            image = image.resize((224, 224))
            image_raw = np.array(image).tostring()

            features = {
                'label': _int64_feature(class_map[label]),
                'text_label': _bytes_feature(bytes(label, encoding = 'utf-8')),
                'image': _bytes_feature(image_raw)
            }

            example = tf.train.Example(features=tf.train.Features(feature=features))
            writer.write(example.SerializeToString())  

hope it will help.

like image 37
marstebi Avatar answered Oct 21 '22 06:10

marstebi