python - reshape 的输入是一个具有 37632 个值的张量,但请求的形状有 150528 个

标签 python tensorflow reshape

我有同样的问题:reshape 的输入是一个有 37632 个值的张量,但请求的形状有 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)

我想我已将 img 的大小调整为 224*224,并将其整形为 [224,224,3],但它不起作用。我怎样才能做到?

最佳答案

问题基本上与 CNN 架构的形状有关。假设我定义了如图所示的架构 CNN Architecture int 编码我们通过以下方式定义了权重和偏差 enter image description here 如果我们看到 (weights) 让我们从

开始

wc1 在这一层我定义了 32 个 3x3 大小的过滤器将被应用

wc2 在这一层我定义了 64 个 3x3 大小的过滤器将被应用

wc3 在这一层我定义了 128 个 3x3 大小的过滤器将被应用

wd1 38*38*128 很有趣(它来自哪里)。

并且在架构中我们还定义了最大池化概念。 在每一步中查看架构图片 1.让我们解释一下假设您的输入图像是 300 x 300 x 1(在图片中是 28x28x1) 2.(如果 strides defined 设置为 1)每个过滤器将有一个 300x300x1 图片,所以在应用 32 个 3x3 过滤器之后 我们将有 32 张 300x300 的图片,因此收集的图像将是 300x300x32

3.Maxpooling 后如果(Strides=2 取决于你定义的通常是 2)图像大小将从 300 x 300 x 32 变为 150 x 150 x 32

  1. (如果定义的步幅设置为 1)现在每个滤镜都有一张 150x150x32 的图片,所以在应用 64 个 3x3 滤镜后 我们将有 64 张 300x300 的图片,因此收集的图像将为 150x150x(32x64)

5.Maxpooling 后如果(Strides=2 取决于你定义的通常是 2)图像大小将从 150x150x(32x64) 改变 到 75 x 75 x (32x64)

  1. (如果定义的步幅设置为 1)现在每个过滤器将有一个 75 x 75 x (32x64) 的图片,所以在应用 3x3 的 64 过滤器之后 我们将有 128 张 75 x 75 x (32x64) 的图片,因此收集的图像将是 75 x 75 x (32x64x128)

7.Maxpooling后由于图像的尺寸是75x75(奇数尺寸使它均匀)所以需要先填充(如果填充定义='Same')然后它会变成76x76(偶数)**如果(Strides=2 取决于您定义的内容,通常为 2)图像大小将从 76x76x(32x64x128) 更改 到 **38 x 38 x (32x64x128)

现在在编码图片中看到'wd1'是38 * 38 * 128

关于python - reshape 的输入是一个具有 37632 个值的张量,但请求的形状有 150528 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46465925/

相关文章:

python - 在此程序中使用 try 和 except 在此 python 中

python - 当我执行 subprocess.Popen 时,Django 没有将数据插入数据库

java - 内存不足错误: Java heap space on Tensorflow tests execution

python - keras conv1d输入数据 reshape

python - 在keras中 reshape 数组

Python:在列表中嵌入的字典中搜索

python - 从 Python 中的字符串推断适当的数据库类型声明

python - ubuntu中gpu上的tensorflow安装

R如何将不同的数据框列合并为一

r - 将一列拆分为多个 R 并在为真时给出逻辑值