Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Model Training Tutorial not training #15

Closed
david-schweitzer opened this issue Jun 29, 2018 · 4 comments
Closed

Custom Model Training Tutorial not training #15

david-schweitzer opened this issue Jun 29, 2018 · 4 comments

Comments

@david-schweitzer
Copy link

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
Copy link
Owner

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
Copy link
Author

Works perfectly, thanks!

@OlafenwaMoses
Copy link
Owner

You are welcome.

@mmargegaj
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants