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.58k stars 2.19k forks source link

Custom Model Training Tutorial not training #15

Closed david-schweitzer closed 6 years ago

david-schweitzer commented 6 years ago

Hi. I created a dataset folder just as you described. I have one class, "vehicles," and so I have the train -> vehicle -> images and test -> vehicle -> images setup.

When I run

from imageai.Prediction.Custom import ModelTraining model_trainer = ModelTraining() model_trainer.setModelTypeAsResNet() model_trainer.setDataDirectory("vehicles") model_trainer.trainModel(num_objects=1, num_experiments=100, enhance_data=True, batch_size=32, show_network_summary=True)

I get the following error after Epoch 1 begins

Epoch 1/100 Traceback (most recent call last): File "trainer.py", line 14, in model_trainer.trainModel(num_objects=1, num_experiments=100, enhance_data=False, batch_size=32, show_network_summary=False) File "C:\Users\RDITLDTS\anaconda3\lib\site-packages\imageai\Prediction\Custom__init__.py", line 240, in trainModel validation_steps=int(num_test / batch_size), callbacks=[checkpoint, lr_scheduler]) File "C:\Users\RDITLDTS\anaconda3\lib\site-packages\tensorflow\python\keras_impl\keras\engine\training.py", line 2172, in fit_generator x, y, sample_weight=sample_weight, class_weight=class_weight) File "C:\Users\RDITLDTS\anaconda3\lib\site-packages\tensorflow\python\keras_impl\keras\engine\training.py", line 1855, in train_on_batch check_batch_axis=True) File "C:\Users\RDITLDTS\anaconda3\lib\site-packages\tensorflow\python\keras_impl\keras\engine\training.py", line 1449, in _standardize_user_data self._feed_output_shapes) File "C:\Users\RDITLDTS\anaconda3\lib\site-packages\tensorflow\python\keras_impl\keras\engine\training.py", line 283, in _check_loss_and_target_compatibility y.shape) + ' while using as loss categorical_crossentropy. ' ValueError: You are passing a target array of shape (32, 1) while using as loss categorical_crossentropy. categorical_crossentropy expects targets to be binary matrices (1s and 0s) of shape (samples, classes). If your targets are integer classes, you can convert them to the expected format via:

from keras.utils import to_categorical
y_binary = to_categorical(y_int)

Alternatively, you can use the loss function sparse_categorical_crossentropy instead, which does expect integer targets.:

How do I fix this?

OlafenwaMoses commented 6 years ago

Hello,

ImageAI supports training models for 2 or more classes. Instead of having only vehicles, I will advice you structure your dataset as below:

train-> car ->images train-> bus ->images (you can add more)

test-> car ->images test-> bus ->images (you can add more)

david-schweitzer commented 6 years ago

Works perfectly, thanks!

OlafenwaMoses commented 6 years ago

You are welcome.

mmargegaj commented 5 years ago

Hi, I have the following error. What might be the problem? screenshot from 2018-10-25 15-34-52