ValueError:参数必须是一个密集的张量:得到形状[8,299,299,3],但想要[8]

时间:2018-02-15 16:18:13

标签: python-3.x tensorflow

我在使用Windows操作系统的系统上使用Tensorflow 1.5(GPU版本)。 我想使用tf.train.batch()函数将批量图像传递给CNN模型。 这是代码:

Images = [] #TO STORE THE RESIZED IMAGES IN THE FORM OF LIST TO PASS IT TO load_batch()
images = glob.glob(images_file_path)
i = 0
for my_img in images:
    image = mpimg.imread(my_img)[:, :, :3]
    image = np.asarray(image, dtype = np.float32)
    Images.append(image)
    i = i + 1
    if i == 8:
        break
#I am testing the code with 8 images, each of size 299*299*3

#convert into onehot encoding, shape is (8, 120)
targets = convert_to_onehot(labels_dir, no_of_features = num_classes)

#Load batches of input data
def load_batch(batch_size, image = Images, label = targets, height = image_size, width = image_size): 
    images, labels = tf.train.batch([image, label], batch_size = batch_size, num_threads = 1, capacity = 2 * batch_size)
    return images, labels

培训代码:

        loss = tf.losses.softmax_cross_entropy(onehot_labels = Y_label, logits = my_layer_logits)  

        # Specify the optimizer and create the train op:
        optimizer = tf.train.AdamOptimizer(learning_rate=0.0001)
        train_op = slim.learning.create_train_op(loss, optimizer) 

        #Generating batch
        images, labels = load_batch(8)
        # Run the training inside a session.
        final_loss = slim.learning.train(train_op,logdir = new_ckpt_dir, number_of_steps = iterations, save_summaries_secs=5,log_every_n_steps=50)(feed_dict = {X_input:images , Y_label: labels})

回溯是:

Traceback (most recent call last):
File "dog_classify.py", line 108, in <module>
images, labels = load_batch(8)
File "dog_classify.py", line 56, in load_batch
images, labels = tf.train.batch([image, label], batch_size = batch_size, num_threads = 1, capacity = 2 * batch_size)

请帮助我解决错误,并指出任何类型的错误或建议更好的方法来完成所需的任务?

0 个答案:

没有答案