我正在使用我的数据集构建训练分类器。我用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)'
答案 0 :(得分:0)
此错误意味着您的神经网络的输出形状为(None,2),但是在y_train
中,您的形状为(1,163,163)。
再次检查如何创建y_train
和y_test
。我将从观察它们的形状开始。
答案 1 :(得分:0)
此处您指定result
中使用的training_data
的形状为(163, 163)
:
indices = np.arange(163)
depth = 163
y = tf.one_hot(indices,depth)
result = sess.run(y)
虽然您的回归的输出维数为2。我不确定通过创建163个一键向量来确定您的意图是什么?您是否正在尝试将某些东西分类为163个维?无论哪种方式,一键向量维和回归输出都必须具有匹配的维。
这是我能提供的最佳建议,因为我不确定您打算如何为数据生成标签。