OlafenwaMoses / ImageAI

A python library built to empower developers to build applications and systems with self-contained Computer Vision capabilities
https://www.genxr.co/#products
MIT License
8.5k stars 2.19k forks source link

ValueError: Error when checking target: expected activation_49 to have shape (3,) but got array with shape (5,) #141

Closed jborden13 closed 5 years ago

jborden13 commented 5 years ago

Context:

I'm trying to train a model and every time it gets to the last one it throws the error below.

Directory Structure:

idenprof > test > chef idenprof > test > firefighter idenprof > test > police idenprof > train > chef idenprof > train > firefighter idenprof > train > police

Code:

from imageai.Prediction.Custom import ModelTraining

model_trainer = ModelTraining()
model_trainer.setModelTypeAsResNet()
model_trainer.setDataDirectory("idenprof")
model_trainer.trainModel(num_objects=3, num_experiments=100, enhance_data=True, batch_size=32, show_network_summary=True)

Error:

67/68 [============================>.] - ETA: 11s - loss: 1.6255 - acc: 0.5870Traceback (most recent call last): File "hello.py", line 6, in model_trainer.trainModel(num_objects=3, num_experiments=100, enhance_data=True, batch_size=32, show_network_summary=True) File "/usr/lib/python3.6/site-packages/imageai/Prediction/Custom/init.py", line 249, in trainModel validation_steps=int(num_test / batch_size), callbacks=[checkpoint, lr_scheduler]) File "/usr/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 2177, in fit_generator initial_epoch=initial_epoch) File "/usr/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_generator.py", line 197, in fit_generator max_queue_size=max_queue_size) File "/usr/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_generator.py", line 310, in evaluate_generator outs = model.test_on_batch(x, y, sample_weight=sample_weight) File "/usr/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1987, in test_on_batch x, y, sample_weight=sample_weight) File "/usr/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 992, in _standardize_user_data class_weight, batch_size) File "/usr/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1154, in _standardize_weights exception_prefix='target') File "/usr/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 332, in standardize_input_data ' but got array with shape ' + str(data_shape)) ValueError: Error when checking target: expected activation_49 to have shape (3,) but got array with shape (5,)

LittleOrangeC commented 5 years ago

I'm seeing a similar error:


__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 224, 224, 3)  0                                            
__________________________________________________________________________________________________
conv2d (Conv2D)                 (None, 111, 111, 64) 1792        input_1[0][0]                    
__________________________________________________________________________________________________
activation (Activation)         (None, 111, 111, 64) 0           conv2d[0][0]                     
__________________________________________________________________________________________________
max_pooling2d (MaxPooling2D)    (None, 55, 55, 64)   0           activation[0][0]                 
__________________________________________________________________________________________________
conv2d_1 (Conv2D)               (None, 55, 55, 16)   1040        max_pooling2d[0][0]              
__________________________________________________________________________________________________
activation_1 (Activation)       (None, 55, 55, 16)   0           conv2d_1[0][0]                   
__________________________________________________________________________________________________
conv2d_2 (Conv2D)               (None, 55, 55, 64)   1088        activation_1[0][0]               
__________________________________________________________________________________________________
conv2d_3 (Conv2D)               (None, 55, 55, 64)   9280        activation_1[0][0]               
__________________________________________________________________________________________________
activation_2 (Activation)       (None, 55, 55, 64)   0           conv2d_2[0][0]                   
__________________________________________________________________________________________________
activation_3 (Activation)       (None, 55, 55, 64)   0           conv2d_3[0][0]                   
__________________________________________________________________________________________________
concatenate (Concatenate)       (None, 55, 55, 128)  0           activation_2[0][0]               
                                                                 activation_3[0][0]               
__________________________________________________________________________________________________
conv2d_4 (Conv2D)               (None, 55, 55, 16)   2064        concatenate[0][0]                
__________________________________________________________________________________________________
activation_4 (Activation)       (None, 55, 55, 16)   0           conv2d_4[0][0]                   
__________________________________________________________________________________________________
conv2d_5 (Conv2D)               (None, 55, 55, 64)   1088        activation_4[0][0]               
__________________________________________________________________________________________________
conv2d_6 (Conv2D)               (None, 55, 55, 64)   9280        activation_4[0][0]               
__________________________________________________________________________________________________
activation_5 (Activation)       (None, 55, 55, 64)   0           conv2d_5[0][0]                   
__________________________________________________________________________________________________
activation_6 (Activation)       (None, 55, 55, 64)   0           conv2d_6[0][0]                   
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 55, 55, 128)  0           activation_5[0][0]               
                                                                 activation_6[0][0]               
__________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D)  (None, 27, 27, 128)  0           concatenate_1[0][0]              
__________________________________________________________________________________________________
conv2d_7 (Conv2D)               (None, 27, 27, 32)   4128        max_pooling2d_1[0][0]            
__________________________________________________________________________________________________
activation_7 (Activation)       (None, 27, 27, 32)   0           conv2d_7[0][0]                   
__________________________________________________________________________________________________
conv2d_8 (Conv2D)               (None, 27, 27, 128)  4224        activation_7[0][0]               
__________________________________________________________________________________________________
conv2d_9 (Conv2D)               (None, 27, 27, 128)  36992       activation_7[0][0]               
__________________________________________________________________________________________________
activation_8 (Activation)       (None, 27, 27, 128)  0           conv2d_8[0][0]                   
__________________________________________________________________________________________________
activation_9 (Activation)       (None, 27, 27, 128)  0           conv2d_9[0][0]                   
__________________________________________________________________________________________________
concatenate_2 (Concatenate)     (None, 27, 27, 256)  0           activation_8[0][0]               
                                                                 activation_9[0][0]               
__________________________________________________________________________________________________
conv2d_10 (Conv2D)              (None, 27, 27, 32)   8224        concatenate_2[0][0]              
__________________________________________________________________________________________________
activation_10 (Activation)      (None, 27, 27, 32)   0           conv2d_10[0][0]                  
__________________________________________________________________________________________________
conv2d_11 (Conv2D)              (None, 27, 27, 128)  4224        activation_10[0][0]              
__________________________________________________________________________________________________
conv2d_12 (Conv2D)              (None, 27, 27, 128)  36992       activation_10[0][0]              
__________________________________________________________________________________________________
activation_11 (Activation)      (None, 27, 27, 128)  0           conv2d_11[0][0]                  
__________________________________________________________________________________________________
activation_12 (Activation)      (None, 27, 27, 128)  0           conv2d_12[0][0]                  
__________________________________________________________________________________________________
concatenate_3 (Concatenate)     (None, 27, 27, 256)  0           activation_11[0][0]              
                                                                 activation_12[0][0]              
__________________________________________________________________________________________________
max_pooling2d_2 (MaxPooling2D)  (None, 13, 13, 256)  0           concatenate_3[0][0]              
__________________________________________________________________________________________________
conv2d_13 (Conv2D)              (None, 13, 13, 48)   12336       max_pooling2d_2[0][0]            
__________________________________________________________________________________________________
activation_13 (Activation)      (None, 13, 13, 48)   0           conv2d_13[0][0]                  
__________________________________________________________________________________________________
conv2d_14 (Conv2D)              (None, 13, 13, 192)  9408        activation_13[0][0]              
__________________________________________________________________________________________________
conv2d_15 (Conv2D)              (None, 13, 13, 192)  83136       activation_13[0][0]              
__________________________________________________________________________________________________
activation_14 (Activation)      (None, 13, 13, 192)  0           conv2d_14[0][0]                  
__________________________________________________________________________________________________
activation_15 (Activation)      (None, 13, 13, 192)  0           conv2d_15[0][0]                  
__________________________________________________________________________________________________
concatenate_4 (Concatenate)     (None, 13, 13, 384)  0           activation_14[0][0]              
                                                                 activation_15[0][0]              
__________________________________________________________________________________________________
conv2d_16 (Conv2D)              (None, 13, 13, 48)   18480       concatenate_4[0][0]              
__________________________________________________________________________________________________
activation_16 (Activation)      (None, 13, 13, 48)   0           conv2d_16[0][0]                  
__________________________________________________________________________________________________
conv2d_17 (Conv2D)              (None, 13, 13, 192)  9408        activation_16[0][0]              
__________________________________________________________________________________________________
conv2d_18 (Conv2D)              (None, 13, 13, 192)  83136       activation_16[0][0]              
__________________________________________________________________________________________________
activation_17 (Activation)      (None, 13, 13, 192)  0           conv2d_17[0][0]                  
__________________________________________________________________________________________________
activation_18 (Activation)      (None, 13, 13, 192)  0           conv2d_18[0][0]                  
__________________________________________________________________________________________________
concatenate_5 (Concatenate)     (None, 13, 13, 384)  0           activation_17[0][0]              
                                                                 activation_18[0][0]              
__________________________________________________________________________________________________
conv2d_19 (Conv2D)              (None, 13, 13, 64)   24640       concatenate_5[0][0]              
__________________________________________________________________________________________________
activation_19 (Activation)      (None, 13, 13, 64)   0           conv2d_19[0][0]                  
__________________________________________________________________________________________________
conv2d_20 (Conv2D)              (None, 13, 13, 256)  16640       activation_19[0][0]              
__________________________________________________________________________________________________
conv2d_21 (Conv2D)              (None, 13, 13, 256)  147712      activation_19[0][0]              
__________________________________________________________________________________________________
activation_20 (Activation)      (None, 13, 13, 256)  0           conv2d_20[0][0]                  
__________________________________________________________________________________________________
activation_21 (Activation)      (None, 13, 13, 256)  0           conv2d_21[0][0]                  
__________________________________________________________________________________________________
concatenate_6 (Concatenate)     (None, 13, 13, 512)  0           activation_20[0][0]              
                                                                 activation_21[0][0]              
__________________________________________________________________________________________________
conv2d_22 (Conv2D)              (None, 13, 13, 64)   32832       concatenate_6[0][0]              
__________________________________________________________________________________________________
activation_22 (Activation)      (None, 13, 13, 64)   0           conv2d_22[0][0]                  
__________________________________________________________________________________________________
conv2d_23 (Conv2D)              (None, 13, 13, 256)  16640       activation_22[0][0]              
__________________________________________________________________________________________________
conv2d_24 (Conv2D)              (None, 13, 13, 256)  147712      activation_22[0][0]              
__________________________________________________________________________________________________
activation_23 (Activation)      (None, 13, 13, 256)  0           conv2d_23[0][0]                  
__________________________________________________________________________________________________
activation_24 (Activation)      (None, 13, 13, 256)  0           conv2d_24[0][0]                  
__________________________________________________________________________________________________
concatenate_7 (Concatenate)     (None, 13, 13, 512)  0           activation_23[0][0]              
                                                                 activation_24[0][0]              
__________________________________________________________________________________________________
dropout (Dropout)               (None, 13, 13, 512)  0           concatenate_7[0][0]              
__________________________________________________________________________________________________
last_conv (Conv2D)              (None, 13, 13, 1)    513         dropout[0][0]                    
__________________________________________________________________________________________________
activation_25 (Activation)      (None, 13, 13, 1)    0           last_conv[0][0]                  
__________________________________________________________________________________________________
global_average_pooling2d (Globa (None, 1)            0           activation_25[0][0]              
__________________________________________________________________________________________________
activation_26 (Activation)      (None, 1)            0           global_average_pooling2d[0][0]   
==================================================================================================
Total params: 723,009
Trainable params: 723,009
Non-trainable params: 0
__________________________________________________________________________________________________
Using Enhanced Data Generation
Found 2890 images belonging to 5 classes.
Found 343 images belonging to 5 classes.
JSON Mapping for the model classes saved to  /Users/matt/json/model_class.json
Number of experiments (Epochs) :  100
2019-01-29 17:58:18.906145: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Epoch 1/100
Traceback (most recent call last):
  File "model-training.py", line 13, in <module>
    model_trainer.trainModel(num_objects=1, num_experiments=100, enhance_data=True, batch_size=32, show_network_summary=True)
  File "/usr/local/lib/python3.6/site-packages/imageai/Prediction/Custom/__init__.py", line 249, in trainModel
    validation_steps=int(num_test / batch_size), callbacks=[checkpoint, lr_scheduler])
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 2177, in fit_generator
    initial_epoch=initial_epoch)
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_generator.py", line 176, in fit_generator
    x, y, sample_weight=sample_weight, class_weight=class_weight)
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1928, in train_on_batch
    x, y, sample_weight=sample_weight, class_weight=class_weight)
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 992, in _standardize_user_data
    class_weight, batch_size)
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1154, in _standardize_weights
    exception_prefix='target')
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 332, in standardize_input_data
    ' but got array with shape ' + str(data_shape))
ValueError: Error when checking target: expected activation_26 to have shape (1,) but got array with shape (5,)```
LittleOrangeC commented 5 years ago

I resolved my own issue. I had num_objects=1 when it should have been set to num_objects=5.

jborden13 commented 5 years ago

I trained more images and it worked fine. I believe my sample was too small.