I am building a training classifier with my data set. I code the one hot labels with TensorFlow. Append the numpy array image data and one hot label data in training data and then in for testing data. But I am getting a shape error with tensorflow. As a newbie I have tried searching for this and tried to solve it out myself but failed.
CODE
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)
And the error I keep getting is below. Please help.
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)'
Here you specify that result
, which gets used in training_data
, has a shape of (163, 163)
:
indices = np.arange(163)
depth = 163
y = tf.one_hot(indices,depth)
result = sess.run(y)
While your regression has an output dimension of 2. I am not sure what your intent is by creating 163 one-hot vectors -- are you trying to classify something into 163 dimensions? Either way, the one-hot vector dimensions and output of your regression have to have matching dimensions.
This is the best advice I can give since I am not sure how you intend to generate the labels for your data.