amineHorseman / facial-expression-recognition-using-cnn

Deep facial expressions recognition using Opencv and Tensorflow. Recognizing facial expressions from images or camera stream
GNU General Public License v3.0
445 stars 141 forks source link

[Solved] ValueError: Cannot feed value of shape #1

Closed das-palash-appsbee closed 5 years ago

das-palash-appsbee commented 6 years ago

When using python train.py --train=yes --evaluate=yes it gives the following error: Traceback (most recent call last): File "train.py", line 118, in <module> train() File "train.py", line 57, in train n_epoch=TRAINING.epochs) File "/anaconda3/lib/python3.6/site-packages/tflearn/models/dnn.py", line 216, in fit callbacks=callbacks) File "/anaconda3/lib/python3.6/site-packages/tflearn/helpers/trainer.py", line 339, in fit show_metric) File "/anaconda3/lib/python3.6/site-packages/tflearn/helpers/trainer.py", line 818, in _train feed_batch) File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 900, in run run_metadata_ptr) File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1111, in _run str(subfeed_t.get_shape()))) ValueError: Cannot feed value of shape (48,) for Tensor 'output/Y:0', which has shape '(?, 5)'

How to overcome this error?

amineHorseman commented 6 years ago

can you give me more information about the input and output sizes? especially did you change values in the parameters.py file?

das-palash-appsbee commented 6 years ago

I didn't change anything except the save_model_path. Here it is:

class Dataset:
    name = 'Fer2013'
    train_folder = '../fer2013/fer2013_30000x5/train_set'
    validation_folder = '../fer2013/fer2013_30000x5/validation_set'
    test_folder = '../fer2013/fer2013_30000x5/test_set'
    shape_predictor_path='../fer2013/shape_predictor_68_face_landmarks.dat'
    trunc_trainset_to = 500
    trunc_validationset_to = 500
    trunc_testset_to = -1

class Network:
    input_size = 48
    output_size = 5
    activation = 'relu'
    loss = 'categorical_crossentropy'
    use_landmarks = True
    use_hog_and_landmarks = False
    use_batchnorm_after_conv_layers = True
    use_batchnorm_after_fully_connected_layers = False

class Hyperparams:
    keep_prob = 0.928
    learning_rate = 0.001
    learning_rate_decay = 0.96
    decay_step = 50
    optimizer = 'momentum'  # {'momentum', 'adam'}
    optimizer_param = 0.9   # momentum value for Momentum optimizer, or beta1 value for Adam

class Training:
    batch_size = 128
    epochs = 1
    snapshot_step = 10000
    vizualize = True
    logs_dir = "logs"
    checkpoint_dir = "checkpoints/"
    best_checkpoint_path = "checkpoints/best/"
    max_checkpoints = 1
    checkpoint_frequency = 1.0 # in hours
    save_model = True
    save_model_path = "saved_model.bin"

class VideoPredictor:
    emotions = ["Angry", "Happy", "Sad", "Surprise", "Neutral"]
    print_emotions = False
    send_by_osc_socket = False
    send_by_socket = False
    ip = "127.0.0.1"    # destination address for sockets and OSC sockets 
    port = 9003
    camera_source = 0
    face_detection_classifier = "lbpcascade_frontalface.xml"
    show_confidence = False
    time_to_wait_between_predictions = 0.5
    time_to_wait_to_send_by_socket = 5

def make_dir(folder):
    if not os.path.exists(folder):
        os.makedirs(folder)

DATASET = Dataset()
NETWORK = Network()
TRAINING = Training()
HYPERPARAMS = Hyperparams()
VIDEO_PREDICTOR = VideoPredictor()

make_dir(TRAINING.logs_dir)
make_dir(TRAINING.checkpoint_dir)

I used your https://github.com/amineHorseman/facial-expression-recognition-svm to generate the train_set, validation_test & test_set files.

amineHorseman commented 6 years ago

That's probably because the input array is waiting for an one hot encoding array.

Make sure you set the option --onehot to yes when converting the dataset file:

python convert_fer2013_to_images_and_landmarks.py --landmarks=yes --hog=no --jpg=no --onehot=yes --expressions=1,3,4,5,6

palash89 commented 6 years ago

It worked.

husnejahan commented 5 years ago

File "/Desktop/facial-expression-recognition-using-cnn-master/data_loader.py", line 30, in load_data data_dict['Y'] = data_dict['Y'][0:DATASET.trunc_trainset_to, :] IndexError: too many indices for array

I am getting error when run, python train.py --train=yes I used python convert_fer2013_to_images_and_landmarks.py to get train, test and validation set(training,publictest,privatetest)

amineHorseman commented 5 years ago

@husnejahan did you set the argument --onehot to yes? Try this:

python convert_fer2013_to_images_and_landmarks.py --onehot=yes

husnejahan commented 5 years ago

Thanks I did and solved. But after training the model I got saved_model.bin.index, how can I get the model in .pbtxt or .bin? need to run ---python predict-from-video.py using saved_model.bin

amineHorseman commented 5 years ago

It appears that there's an error in predict-from-video.py and predict.py The current version do not load HOG features from the frame, so the prediction works only if the saved model was trained without the HOG features. I will try to correct the bug these following days

amineHorseman commented 5 years ago

The bug was solved in the following commit: https://github.com/amineHorseman/facial-expression-recognition-using-cnn/commit/dbec49f186b28ba86ab9de79ab52739cf34521d1

Please note that sometimes the model is saved as .bin.meta, if this is the case for you, you have to remove the .meta extension, or add it to saved_model_path variable in parameters.py

husnejahan commented 5 years ago

Hi Amine, Thank you. Yes l got.bin.meta, l don't how l get only.bin or only .pbtxt

On Thursday, August 9, 2018, Amine notifications@github.com wrote:

The bug was solved in the following commit: dbec49f https://github.com/amineHorseman/facial-expression-recognition-using-cnn/commit/dbec49f186b28ba86ab9de79ab52739cf34521d1

Please note that sometimes the model is saved as .bin.meta, if this is the case for you, you have to remove the .meta extension, or add it to saved_model_path variable in parameters.py

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/amineHorseman/facial-expression-recognition-using-cnn/issues/1#issuecomment-411705935, or mute the thread https://github.com/notifications/unsubscribe-auth/AWINOn274X2oCWpFkrzLeVQvhch_4gT9ks5uPAi4gaJpZM4UTO2b .

-- Best Regards Most Husne Jahan mosthusne.jahan@ryerson.ca

amineHorseman commented 5 years ago

The last commit https://github.com/amineHorseman/facial-expression-recognition-using-cnn/commit/071bfc19ac2025389fc369c50d4ec34a8da4e545 should solve the problem. Now after training you got the file saved_model.bin

Please note that according to tensorflow's implementation: the save function will create 3 files, each one of them contain different information, and thus if one of the files is deleted the saved_model can not been loaded

amineHorseman commented 5 years ago

I will close the issue for now, thank you you help Feel free to reopen the issue if the problem persists

ghost commented 5 years ago

Hey, when I type " python train.py --train=yes " I get the following error. I haven't changed anything in parameters.py

loading dataset Fer2013... building model... WARNING:tensorflow:From /home/dipesh/anaconda3/lib/python3.6/site-packages/tflearn/initializations.py:119: UniformUnitScaling.init (from tensorflow.python.ops.init_ops) is deprecated and will be removed in a future version. Instructions for updating: Use tf.initializers.variance_scaling instead with distribution=uniform to get equivalent behavior. WARNING:tensorflow:From /home/dipesh/anaconda3/lib/python3.6/site-packages/tflearn/objectives.py:66: calling reduce_sum (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version. Instructions for updating: keep_dims is deprecated, use keepdims instead 2018-09-04 15:16:06.575559: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2018-09-04 15:16:06.640654: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:897] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2018-09-04 15:16:06.640979: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1405] Found device 0 with properties: name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335 pciBusID: 0000:01:00.0 totalMemory: 7.93GiB freeMemory: 7.56GiB 2018-09-04 15:16:06.640993: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1484] Adding visible gpu devices: 0 2018-09-04 15:16:06.803537: I tensorflow/core/common_runtime/gpu/gpu_device.cc:965] Device interconnect StreamExecutor with strength 1 edge matrix: 2018-09-04 15:16:06.803581: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0 2018-09-04 15:16:06.803587: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] 0: N 2018-09-04 15:16:06.803736: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1097] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 7296 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1) 2018-09-04 15:16:07.744556: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1484] Adding visible gpu devices: 0 2018-09-04 15:16:07.744589: I tensorflow/core/common_runtime/gpu/gpu_device.cc:965] Device interconnect StreamExecutor with strength 1 edge matrix: 2018-09-04 15:16:07.744595: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0 2018-09-04 15:16:07.744599: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] 0: N 2018-09-04 15:16:07.744691: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1097] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 7296 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1) start training...

amineHorseman commented 5 years ago

Hello @diycamindia,

Just put output_size = 7 in line 19 on parameters.py

It was fixed to 5 because I used only 5 expressions during my tests, but since you use the whole dataset with 7 expressions, you have to set the output_size parameter to 7.

N.B: I see that you're using python 3.6, I only tested the code in python 2.7, so let me know if it's working fine for python 3.6 please

ghost commented 5 years ago

Thank you so much !

sruthisripathi commented 5 years ago

Hi. I didn't have any issue while training but when I try to predict the emotion from a picture doing python predict.py --image me.jpg it's giving the following error

Traceback (most recent call last):
  File "predict.py", line 93, in <module>
    emotion, confidence = predict(image, model, shape_predictor)
  File "predict.py", line 68, in predict
    predicted_label = model.predict([tensor_image, features.reshape((1, -1))])
  File "C:\Users\ssripat\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\tflearn\models\dnn.py", line 257, in predict
    return self.predictor.predict(feed_dict)
  File "C:\Users\ssripat\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\tflearn\helpers\evaluator.py", line 69, in predict
    return self.session.run(self.tensors[0], feed_dict=feed_dict)
  File "C:\Users\ssripat\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py", line 877, in run
    run_metadata_ptr)
  File "C:\Users\ssripat\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py", line 1076, in _run
    str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (1, 4744) for Tensor 'input2/X:0', which has shape '(?, 2728)'

I am using python 3.5.6. predict_from_video.py ran without errors though.

amineHorseman commented 4 years ago

Can you provide more details about the parameters you are using?

tanzeel291994 commented 4 years ago

Hi. I didn't have any issue while training but when I try to predict the emotion from a picture doing python predict.py --image me.jpg it's giving the following error

Traceback (most recent call last):
  File "predict.py", line 93, in <module>
    emotion, confidence = predict(image, model, shape_predictor)
  File "predict.py", line 68, in predict
    predicted_label = model.predict([tensor_image, features.reshape((1, -1))])
  File "C:\Users\ssripat\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\tflearn\models\dnn.py", line 257, in predict
    return self.predictor.predict(feed_dict)
  File "C:\Users\ssripat\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\tflearn\helpers\evaluator.py", line 69, in predict
    return self.session.run(self.tensors[0], feed_dict=feed_dict)
  File "C:\Users\ssripat\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py", line 877, in run
    run_metadata_ptr)
  File "C:\Users\ssripat\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py", line 1076, in _run
    str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (1, 4744) for Tensor 'input2/X:0', which has shape '(?, 2728)'

I am using python 3.5.6. predict_from_video.py ran without errors though.

got the same