So, I am trying to make a emotion classifier using the 7 face expressions. I know that in order to use integer labels instead of 0 and 1 one needs to use the sparse_categorical_crossentropy and need to put the out layer activation as softmax but it isn't working out as expected.
I am using the data set from here https://www.kaggle.com/ashishpatel26/facial-expression-recognitionferchallenge
import pandas as pd
import numpy as np
from PIL import Image
import random
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.optimizers import RMSprop
from keras.layers import Conv1D, MaxPooling1D
from keras.layers import Activation, Dropout, Flatten, Dense
emotion = {0 : 'Angry', 1 : 'Disgust',2 : 'Fear',3 : 'Happy',
4 : 'Sad',5 : 'Surprise',6 : 'Neutral'}
df=pd.read_csv('fer.csv')
faces=df.values[0:500,1]
faces=faces.tolist()
emos=df.values[0:500,0]
for i in range(len(faces)):
faces[i]=[int(x) for x in faces[i].split()]
emos[i]=int(emos[i])
faces=np.array(faces)
faces=np.expand_dims(faces, axis=2)
model = Sequential()
model.add(Conv1D(16, 3, padding='same', input_shape=(2304,1), activation='relu'))
model.add(Conv1D(16, 3, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(32, 3, padding='same', activation='relu'))
model.add(Conv1D(32, 3, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(64, 3, padding='same', activation='relu'))
model.add(Conv1D(64, 3, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(128, 3, padding='same', activation='relu'))
model.add(Conv1D(256, 3, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(faces,emos,epochs=10,batch_size=8)
model.save_weights('model.h5')
W tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at sparse_xent_op.cc:90 : Invalid argument: Received a label value of 6 which is outside the valid range of [0, 1). Label values: 6 0 2 4 6 0 0 3
Traceback (most recent call last):
File "FEClassifier.py", line 56, in <module>
model.fit(faces,emos,epochs=10,batch_size=8)
File "C:\Users\nrj10\Anaconda3\lib\site-packages\keras\engine\training.py", line 1039, in fit
validation_steps=validation_steps)
File "C:\Users\nrj10\Anaconda3\lib\site-packages\keras\engine\training_arrays.py", line 199, in fit_loop
outs = f(ins_batch)
File "C:\Users\nrj10\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2715, in __call__
return self._call(inputs)
File "C:\Users\nrj10\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2675, in _call
fetched = self._callable_fn(*array_vals)
File "C:\Users\nrj10\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1439, in __call__
run_metadata_ptr)
File "C:\Users\nrj10\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 528, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Received a label value of 6 which is outside the valid range of [0, 1). Label values: 6 0 2 4 6 0 0 3
[[{{node loss/dense_3_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]]
If you have N classes (N > 2), the last layer needs to have N neurons no matter you are using sparse labels or not:
model.add(Dense(7, activation='softmax'))
Don't forget that sparse labels are just for convenience and your model still needs to produce a score for each of the classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With