我在处理CK+数据集用于面部表情识别,我通过datagen.flow_from_directory
传递面部图像和标签来提取面部特征并映射到标签上。
标签作为分类值传递,范围从0到7。它们似乎是以独热编码的形式传递的。我的问题是我无法将标签值作为独热编码值进行广播。
我得到了以下错误:ValueError: could not broadcast input array from shape (32,8) into shape (32)
代码如下:
我得到了以下形状:
Found 209 images belonging to 8 classes.
Input batch shape: (32, 224, 224, 3)
Features batch shape: (32, 7, 7, 512)
Features shape: (209, 7, 7, 512)
Labels batch shape: (32, 8)
所以我很困惑为什么features_batch
可以被广播,而labels_batch
却不能。
我尝试了几种方法,其中一些包括:
1) 尝试将标签数组扁平化 – 这看起来不合理,但只是为了看看,我得到了行和列的完整元素计数32*8=259(如预期)。
2) 我尝试使用labels[i]=labels_batch
和labels=labels_batch
,这只返回了最后几个标签(17,从209-(6*32)=17剩下的)。
3) 我还尝试了来自这个问题的另一个解决方案。通过这样做:
for c in range(0,7):
labels[i * batch_size: (i + 1) * batch_size, [c]] = labels_batch
但反而得到了以下错误:
ValueError: Error when checking input: expected input_3 to have 4 dimensions, but got array with shape (32, 8)
我觉得我错过的可能是简单的,但我似乎无法解决它。有人有什么想法吗?
谢谢!
回答:
你的标签应该是labels = np.zeros(shape=(sample_count, num_classes))
的形状,而不是labels = np.zeros(shape=(sample_count))
,并且从生成器分配标签应该使用labels[i * batch_size: (i + 1) * batch_size,:] = labels_batch