这个错误是什么意思,我哪里做错了? 'targets/Y:0' TensorFlow:形状错误。

What is the meaning of this error, where am I doing wrong? 'targets/Y:0' TensorFlow: Shape error.

我正在用我的数据集构建训练分类器。我用 TensorFlow 编写了一个热门标签。在训练数据中附加numpy数组图像数据和一个热标签数据,然后作为测试数据。但是我遇到了张量流的形状错误。作为新手,我曾尝试搜索此问题并尝试自己解决但失败了。

代码

from sklearn.preprocessing import OneHotEncoder
import tensorflow as tf
import numpy as np
import scipy.io as cio
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpg
from random import shuffle
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import cv2

a = cio.loadmat("D:/compCarsThesisData/data/misc/make_model_name.mat")

images = "D:/compCarsThesisData/data/image/"
IMG_SIZE = 64
MODEL_NAME = 'Classification'
LR = 1e-3

b = a['make_names']
# c = b.reshape(163,)
d = []

for i in range(b.size):
  d.append(b[i][0][0])
  print(d)

labels_dic = {v: k for v, k in enumerate(d)}
print(labels_dic)
indices = np.arange(163)
depth = 163

y = tf.one_hot(indices,depth)
# print(y)

sess = tf.Session()

result = sess.run(y)
print(result)
# labels = []

# labels.append((result,labels_dic))
# print(labels)


 for root, _, files in os.walk(images):
   cdp = os.path.abspath(root)
   for f in files:
     name,ext = os.path.splitext(f)
     if ext == ".jpg":
       cip = os.path.join(cdp,f)
       ci = mpg.imread(cip)
       image = cv2.cv2.resize(ci,(IMG_SIZE,IMG_SIZE))
       image = np.array(image)
       print(image)

 training_data = []
 training_data.append([np.array(image),result])
 print("TrainingData",training_data)
 shuffle(training_data)
 np.save('training_data_with_One_Hot', training_data)
 testing_data = []
 testing_data.append([np.array(image),result])
 print("TestingDATA",testing_data)
 np.save('testing_data_with_One_Hot',testing_data)
 shuffle(testing_data)

#If the data already created First Time
#training_data = np.load('training_data_with_One_Hot.npy')
#testing_data = np.load('testing_data_with_One_Hot.npy')

train = training_data
test = testing_data[-50000:]

X_train = np.array([i[0] for i in train]).reshape(-1, IMG_SIZE, IMG_SIZE, 3)

y_train = [i[1] for i in train]

X_test = np.array([i[0] for i in test]).reshape(-1, IMG_SIZE, IMG_SIZE, 3)
y_test = [i[1] for i in test]
print("YTEST",y_test)



tf.reset_default_graph()
convnet = input_data(shape=[None,IMG_SIZE,IMG_SIZE,3],name='input')
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')
model = tflearn.DNN(convnet, tensorboard_dir='log', tensorboard_verbose=0)
model.fit({'input': X_train}, {'targets': y_train}, n_epoch=10,
          validation_set=({'input': X_test}, {'targets': y_test}),
          snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

我不断收到的错误如下。请帮忙。

Run id: Classification
Log directory: log/
---------------------------------
Training samples: 1
Validation samples: 1
--
Traceback (most recent call last):
  File "d:/ThesisWork/seriouswork/classifier_with_onehot.py", line 109, in <module>>
    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)                       16, in fit
  File "C:\Users\zeele\Miniconda3\lib\site-packages\tflearn\models\dnn.py", line 216, in fit                                                                        ine 339, in fit
    callbacks=callbacks)
  File "C:\Users\zeele\Miniconda3\lib\site-packages\tflearn\helpers\trainer.py", line 818, in _trainine 339, in fit
    show_metric)                                                                  on.py", line 929, in run
  File "C:\Users\zeele\Miniconda3\lib\site-packages\tflearn\helpers\trainer.py", line 818, in _train                                                                on.py", line 1128, in _run
    feed_batch)
  File "C:\Users\zeele\Miniconda3\lib\site-packages\tensorflow\python\client\sessich has shape '(?, 2)'on.py", line 929, in run
    run_metadata_ptr)
  File "C:\Users\zeele\Miniconda3\lib\site-packages\tensorflow\python\client\session.py", line 1128, in _run
    str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (1, 163, 163) for Tensor 'targets/Y:0', which has shape '(?, 2)'

这个错误意味着你的神经网络的输出形状是 (None, 2),但是这里 y_train 你有一些形状是 (1, 163, 163)。

仔细检查您创建 y_trainy_test 的方式。我将从观察它们的形状开始。

此处指定 result,它在 training_data 中使用,其形状为 (163, 163):

indices = np.arange(163)
depth = 163
y = tf.one_hot(indices,depth)
result = sess.run(y)

虽然您的回归的输出维度为 2。但我不确定您创建 163 个单热向量的目的是什么——您是否试图将某些东西分类为 163 个维度?无论哪种方式,单热向量维度和回归的输出必须具有匹配的维度。

这是我能提供的最佳建议,因为我不确定您打算如何为数据生成标签。