ValueError:无法为Tensor u'InputData / X:0'提供形状值(64,32,32),其形状为'(?,32,32,1)'

时间:2017-05-14 14:53:06

标签: python-2.7 tensorflow tflearn

我正在尝试使用tflearn和我自己的数据训练模型。

我有19748个灰度图像,我想用我的模型训练。我使用tflearn的Image_Preloader方法输入图像。并且所有图像都转换为32 * 32大小。但是当我开始训练过程时,我收到此错误“ValueError:无法为Tensor u'InputData / X:0'提供形状值(64,32,32),其形状为'(?,32,32,1) '“

我已经尝试了所有知识但我无法解决它并且stackoverflow中存在类似类型的问题,但它们对我不起作用。

这是我的代码。

from __future__ import division, print_function, absolute_import


import tflearn
import pickle
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression
from time import gmtime, strftime
from tflearn.data_utils import image_preloader
import numpy as np


dataset_file = 'noww.txt'



X = np.zeros((19748,32,32,1))
Y = np.zeros((19748,10))

X, Y = image_preloader(dataset_file, image_shape=(32, 32),   mode='file', categorical_labels=True,   normalize=True)


network = input_data(shape=[None, 32, 32, 1])


network = conv_2d(network, 64, 3, activation='relu')
network = conv_2d(network, 64, 3, activation='relu')
network = max_pool_2d(network, 2, strides=2)

network = conv_2d(network, 128, 3, activation='relu')
network = conv_2d(network, 128, 3, activation='relu')
network = max_pool_2d(network, 2, strides=2)

network = conv_2d(network, 256, 3, activation='relu')
network = conv_2d(network, 256, 3, activation='relu')
network = conv_2d(network, 256, 3, activation='relu')
network = max_pool_2d(network, 2, strides=2)

network = fully_connected(network, 1024, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 1024, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 10, activation='softmax')
network = regression(network, optimizer='rmsprop',
                     loss='categorical_crossentropy',
                     learning_rate=0.0001)


model = tflearn.DNN(network, checkpoint_path='model_1',
                    max_checkpoints=1, tensorboard_verbose=0)
model.fit(X, Y, n_epoch=200, shuffle=True,
          show_metric=True, batch_size=64, snapshot_step=200,
          snapshot_epoch=False, run_id='model_1')

请帮忙。

1 个答案:

答案 0 :(得分:3)

错误说Tensorflow不能将具有形状[64,32,32]的Tensor放入另一个形状为[?,32,32,1]的Tensor中,此处?表示批量大小。

您的模型无法将批量数据反馈到X变量,因为它们的形状不同,您应该更改X形状。

更改此行X, Y = image_preloader(dataset_file, image_shape=(32, 32), mode='file', categorical_labels=True, normalize=True)

X, Y = image_preloader(dataset_file, image_shape=(None, 32, 32, 1), mode='file', categorical_labels=True, normalize=True)

希望这很有用。