Search code examples
pythontensorflowdeep-learningtflearnkaggle

Cannot feed value of shape (64, 7) for Tensor 'targets/Y:0', which has shape '(?,)'


I'm working on Kaggle's fer2013 dataset. Here's a link to the dataset.

I'm using TFLearn framework, I convert the Labels(7 class labels) to hot_shot and everything works fine until I run it in the networks and I get the error: Cannot feed value of shape (64, 7) for Tensor 'targets/Y:0', which has shape '(?,)'

I read previous similar questions and I understand that I'm trying to feed the network a tensor of shape which is different than what it expects, my problem here is I don't know how to reshape what it expects, or at least the shape of what it expects so I can reshape my tensor to.

Here's my code.

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt 
import matplotlib.image as mpimg


#Read csv file
data = pd.read_csv('fer2013.csv')

#Number of samples
n_samples = len(data)
n_samples_train = 28709
n_samples_test = 3589
n_samples_validation = 3589
IMG_SIZE = 48

#Pixel width and height
w = 48
h = 48

#Separating labels and features respectively
y = data['emotion']
X = np.zeros((n_samples, w, h,1))
for i in range(n_samples):
    X[i] = np.fromstring(data['pixels'][i], dtype=int, sep=' ').reshape(w, h,1)

#Training set   
X_train = X[:n_samples_train]
y_train = y[:n_samples_train]

X_val = X[n_samples_train : (n_samples_train + n_samples_test)]  
y_val = y[n_samples_train : (n_samples_train + n_samples_test)]

n_values = np.max(y_train) + 1
y_hot_shot_train = np.eye(n_values)[y_train]

n_values_val = np.max(y_val) + 1
y_hot_shot_val = np.eye(n_values_val)[y_val]

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
from tflearn.data_augmentation import ImageAugmentation


LR = 0.001

imgaug = ImageAugmentation()
imgaug.add_random_flip_leftright()
imgaug.add_random_rotation(max_angle=25.)

convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1],data_augmentation=imgaug, 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, 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, 7, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets', to_one_hot = True, n_classes=7)

model = tflearn.DNN(convnet, tensorboard_dir='log')

MODEL_NAME = 'SentimentAnalysis-{}-{}.model'.format(LR, '6conv-basic')

model.fit({'input': X_train}, {'targets': y_hot_shot_train}, n_epoch=6,batch_size=64, validation_set=({'input': X_val}, {'targets': y_hot_shot_val}), 
    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

Solution

  • When you set to_one_hot to True in the regression function, it already converts your target to one-hot labels. So, it expects a value with shape (?,) and you should just provide the original data, y_train and y_val, to the fit function.

    model.fit({'input': X_train}, {'targets': y_train}, n_epoch=6, batch_size=64, 
        validation_set=({'input': X_val}, {'targets': y_val}),
        snapshot_step=500, show_metric=True, run_id=MODEL_NAME)