ValueError:检查目标时出错:预期density_8的形状为(10,),但数组的形状为(95,)

时间:2019-04-25 11:31:48

标签: python spyder

我正在尝试解决这个问题。我正在使用CNN算法处理图像,并识别图像,然后遇到诸如此类的错误.....

  

ValueError:检查目标时出错:预期density_8具有形状   (10),但得到形状为(95,)的数组

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_6 (Conv2D)            (None, 126, 126, 32)      896       
_________________________________________________________________
batch_normalization_5 (Batch (None, 126, 126, 32)      128       
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 63, 63, 32)        0         
_________________________________________________________________
dropout_5 (Dropout)          (None, 63, 63, 32)        0         
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 61, 61, 64)        18496     
_________________________________________________________________
batch_normalization_6 (Batch (None, 61, 61, 64)        256       
_________________________________________________________________
max_pooling2d_7 (MaxPooling2 (None, 30, 30, 64)        0         
_________________________________________________________________
dropout_6 (Dropout)          (None, 30, 30, 64)        0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 28, 28, 128)       73856     
_________________________________________________________________
batch_normalization_7 (Batch (None, 28, 28, 128)       512       
_________________________________________________________________
max_pooling2d_8 (MaxPooling2 (None, 14, 14, 128)       0         
_________________________________________________________________
dropout_7 (Dropout)          (None, 14, 14, 128)       0         
_________________________________________________________________
flatten_3 (Flatten)          (None, 25088)             0         
_________________________________________________________________
dense_7 (Dense)              (None, 512)               12845568  
_________________________________________________________________
batch_normalization_8 (Batch (None, 512)               2048      
_________________________________________________________________
dropout_8 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_8 (Dense)              (None, 10)                5130      
=================================================================
Total params: 12,946,890
Trainable params: 12,945,418
Non-trainable params: 1,472
_________________________________________________________________
Found 48905 images belonging to 95 classes.
Found 16421 images belonging to 95 classes.
{'Apple Braeburn': 0, 'Apple Golden 1': 1, 'Apple Golden 2': 2, 'Apple Golden 3': 3, 'Apple Granny Smith': 4, 'Apple Red 1': 5, 'Apple Red 2': 6, 'Apple Red 3': 7, 'Apple Red Delicious': 8, 'Apple Red Yellow 1': 9, 'Apple Red Yellow 2': 10, 'Apricot': 11, 'Avocado': 12, 'Avocado ripe': 13, 'Banana': 14, 'Banana Lady Finger': 15, 'Banana Red': 16, 'Cactus fruit': 17, 'Cantaloupe 1': 18, 'Cantaloupe 2': 19, 'Carambula': 20, 'Cherry 1': 21, 'Cherry 2': 22, 'Cherry Rainier': 23, 'Cherry Wax Black': 24, 'Cherry Wax Red': 25, 'Cherry Wax Yellow': 26, 'Chestnut': 27, 'Clementine': 28, 'Cocos': 29, 'Dates': 30, 'Granadilla': 31, 'Grape Blue': 32, 'Grape Pink': 33, 'Grape White': 34, 'Grape White 2': 35, 'Grape White 3': 36, 'Grape White 4': 37, 'Grapefruit Pink': 38, 'Grapefruit White': 39, 'Guava': 40, 'Hazelnut': 41, 'Huckleberry': 42, 'Kaki': 43, 'Kiwi': 44, 'Kumquats': 45, 'Lemon': 46, 'Lemon Meyer': 47, 'Limes': 48, 'Lychee': 49, 'Mandarine': 50, 'Mango': 51, 'Mangostan': 52, 'Maracuja': 53, 'Melon Piel de Sapo': 54, 'Mulberry': 55, 'Nectarine': 56, 'Orange': 57, 'Papaya': 58, 'Passion Fruit': 59, 'Peach': 60, 'Peach 2': 61, 'Peach Flat': 62, 'Pear': 63, 'Pear Abate': 64, 'Pear Kaiser': 65, 'Pear Monster': 66, 'Pear Williams': 67, 'Pepino': 68, 'Physalis': 69, 'Physalis with Husk': 70, 'Pineapple': 71, 'Pineapple Mini': 72, 'Pitahaya Red': 73, 'Plum': 74, 'Plum 2': 75, 'Plum 3': 76, 'Pomegranate': 77, 'Pomelo Sweetie': 78, 'Quince': 79, 'Rambutan': 80, 'Raspberry': 81, 'Redcurrant': 82, 'Salak': 83, 'Strawberry': 84, 'Strawberry Wedge': 85, 'Tamarillo': 86, 'Tangelo': 87, 'Tomato 1': 88, 'Tomato 2': 89, 'Tomato 3': 90, 'Tomato 4': 91, 'Tomato Cherry Red': 92, 'Tomato Maroon': 93, 'Walnut': 94}
Epoch 1/10
Traceback (most recent call last):
ValueError: Error when checking target: expected dense_8 to have shape (10,)
but got array with shape (95,)

1 个答案:

答案 0 :(得分:1)

模型的最后一层应包含等于数据中类数的节点。您有95个类,因此最后一层应该有95个节点

将模型的最后一层更改为

model.add(Dense(95, activation='softmax'))