Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Updated to the Keras 2.0 API.
'''This script goes along the blog post
"Building powerful image classification models using very little data"
It uses data that can be downloaded at:
In our setup, we:
- created a data/ folder
- created train/ and validation/ subfolders inside data/
- created cats/ and dogs/ subfolders inside train/ and validation/
- put the cat pictures index 0-999 in data/train/cats
- put the cat pictures index 1000-1400 in data/validation/cats
- put the dogs pictures index 12500-13499 in data/train/dogs
- put the dog pictures index 13500-13900 in data/validation/dogs
So that we have 1000 training examples for each class, and 400 validation examples for each class.
In summary, this is our directory structure:
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
# dimensions of our images.
img_width, img_height = 150, 150
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 50
batch_size = 16
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
input_shape = (img_width, img_height, 3)
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1. / 255,
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
target_size=(img_width, img_height),
validation_generator = test_datagen.flow_from_directory(
target_size=(img_width, img_height),
steps_per_epoch=nb_train_samples // batch_size,
validation_steps=nb_validation_samples // batch_size)
Copy link

Graystripe17 commented Sep 7, 2018

I'm on Google Colab and I'm finding it extremely difficult to replicate the folder structure. I'm using kaggle api and unzipping them, but the file names are not very conducive to the folder structure. What is everyone else doing?

Copy link

zeelmehta389 commented Sep 14, 2018

How to test this model. Actually I am a new learner of this thing.
My task is to identify the face of mine and my friends that is there are two classes in my code also(me and my friend). My code have to identify my or my friend's face at runtime. I understood the code but I am facing problem in implementation. please help!

Copy link

hrishpep commented Sep 28, 2018

I carefully followed the given procedure given both at blog and here. Unfortunately, I am getting the following error which I couldn't find a way to solve:
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/", line 540, in runfile
execfile(filename, namespace)
File "/home/santez/Desktop/DL Examples/KERAS/Object Recognition/Classifier From Little Data KeraBlog/", line 80, in
File "/usr/local/lib/python2.7/dist-packages/keras/", line 308, in add
output_tensor = layer(self.outputs[0])
File "/usr/local/lib/python2.7/dist-packages/keras/engine/", line 487, in call[0])
File "/usr/local/lib/python2.7/dist-packages/keras/layers/", line 695, in build
File "/usr/local/lib/python2.7/dist-packages/keras/", line 59, in glorot_uniform
return uniform(shape, s, name=name)
File "/usr/local/lib/python2.7/dist-packages/keras/", line 32, in uniform
return K.random_uniform_variable(shape, -scale, scale, name=name)
File "/usr/local/lib/python2.7/dist-packages/keras/backend/", line 140, in random_uniform_variable
return variable(np.random.uniform(low=low, high=high, size=shape),
File "mtrand.pyx", line 1177, in mtrand.RandomState.uniform (numpy/random/mtrand/mtrand.c:9148)
File "mtrand.pyx", line 203, in mtrand.cont2_array_sc (numpy/random/mtrand/mtrand.c:2371)
ValueError: negative dimensions are not allowed

your kernel size is larger than the size of the image - either at input or even as you move through the CNN.

Copy link

krtrth commented Oct 14, 2018

how to use it to train digits I have 10 folders under train and validation code ran successfully but in result I got "[array([[1]], dtype=int32), array([[1.]], dtype=float32)]"

Copy link

dynamo214 commented Nov 11, 2018

i am using this fit_generator method --

steps_per_epoch = 250,
epochs = 25,
validation_data = test_set,
validation_steps = 63)

and getting this error --

File "", line 5, in
validation_steps = 63)

File "C:\Users\dell\Anaconda3\lib\site-packages\keras\legacy\", line 91, in wrapper
return func(*args, **kwargs)

File "C:\Users\dell\Anaconda3\lib\site-packages\keras\engine\", line 1418, in fit_generator

File "C:\Users\dell\Anaconda3\lib\site-packages\keras\engine\", line 40, in fit_generator

File "C:\Users\dell\Anaconda3\lib\site-packages\keras\engine\", line 519, in _make_train_function

File "C:\Users\dell\Anaconda3\lib\site-packages\keras\backend\", line 2744, in function
return Function(inputs, outputs, updates=updates, **kwargs)

File "C:\Users\dell\Anaconda3\lib\site-packages\keras\backend\", line 2575, in init
'time: %s', session_kwargs.keys())

ValueError: ('Some keys in session_kwargs are not supported at this time: %s', dict_keys(['matris']))

can anybody help?

Copy link

Bavitharan commented Jan 25, 2019

How to check model accuracy?

Copy link

sunnyFish071 commented Feb 20, 2019

@marco-zorzi thank you!

Copy link

AlaaSaeed300 commented Mar 14, 2019

Hi,@fchollet and anyone
i am new to deep learning and keras.
thanks for your tutorial!
i have successfully run the code.
Now , How can i test it ?
i am using the tensorflow backend.
If anyone can help me with code , it will be helpful.

Copy link

anandpawara commented Mar 30, 2019

thanks for great tutorial! I think the last line of code should be model_save_weights('first_try.h5').

its model.save_weights('first_try.h5').

Copy link

Yupeii commented Apr 1, 2019

Good learning example.
I am greenhorn with Keras, figuring my way through.

How does one add :
a) K-fold cross validation
b) checkpoint
with the generators
c) change the example to more than 2 classes
I tried
model.add(Dense(3)) # 3 classes
model.add(Activation('softmax')) not sure if this is the only change. Will the class_mode change ?

I am still confused how does one add : a) K-fold cross validation

Copy link

sharoseali commented Apr 22, 2019

@fchollet hope u are doing well....
reference to your powerful image classifier your keras code i was doing binery classification on mac and other laptops. My training set contains 1000 examples of mac and other laptops (total 2000 training examples). i trained it for 50 epochs. when training ends the accuracy was 0.93 written on screen but when i test the model on my test set it give naughty results,,, can u please help me what should be done in this situation as i need 95% above accuracy to classify between Mac and other brand laptops....
i test my model with this code:

preds = laptop_model.predict_classes(x)
prob = laptop_model.predict_proba(x)  
print(preds, prob)

if(prob <= 0.5): # My first folder in train directory was Mac
        label = "MacBook " + str(prob)
        color = (0, 0, 255)
        label = "Laptop" + str(prob)
        color = (0, 255, 0)


Kindly check the code.......
here is the result of what my naughty model had learned yet.. ?
model result

Copy link

aezco commented May 21, 2019

I would like go implement a hierarchical resnet architecture. However, I could not find any solution for this. For example, my data structure is like:

class A
Subclass 1
Subclass 2
class B
subclass 6
So i would like to train and predict the main class and then the subclass of the chosen/predicted mainclass. Let say, we predict first the age group of a person (40,60), then the classifiers knows that the age is something between 40-60 years old and it will predict the apparent age 53. I cannot get the idea behind the implementation for this. Can someone provide a simple example how to do this with generators?

I know how to compile the model and do fit_generator, but do not get the intuition behind the model creation.

base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(224, 224, 3), pooling="avg") # ResNet50 for layer in base_model.layers: if isinstance(layer, Conv2D) or isinstance(layer, Dense): layer.add_loss(l2(0.0005)(layer.kernel)) if hasattr(layer, 'bias_regularizer') and layer.use_bias: layer.add_loss(l2(0.0005)(layer.bias)) prediction = Dense(units=100, kernel_initializer="he_normal", use_bias=False, activation="softmax", name="pred_age", kernel_regularizer=l2(0.0005))(base_model.output) model = Model(inputs=base_model.input, outputs=prediction)

Copy link

daitranthanhhoa commented May 24, 2019

What is validation data? Is it the same with train data?

Copy link

aezco commented May 24, 2019

What is validation data? Is it the same with train data?

My validation data is the same as train data. I use ImageDataGenerator for splitting.

train_datagen = ImageDataGenerator(validation_split=val_split,rescale=1./255) # set validation split

train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_size, img_size), batch_size=batch_size, color_mode='rgb', shuffle=True, subset='training') # set as training data

validation_generator = train_datagen.flow_from_directory( train_data_dir, # same directory as training data target_size=(img_size, img_size), color_mode='rgb', shuffle=False, subset='validation') # set as validation data

I was thinking to train first a model on N age groups (0-10,11-18,...) and then train N models for each age group (model 1 (0-10), model 2 (11-18), model 3 (..)) and then combine those 5 models into 1 model, however I do not know how to combine those N models as 1 model. Because if 1 model has 10 prediction layers and another model 15 etc.. The final model must have 25 prediction output, so the predictions outputs are also combined into 1 large output (100 prediction size output for the combination of all the model).

Copy link

daitranthanhhoa commented May 30, 2019

I see some samples , not use validation data.
It only use train data and train label.
Is it necessary?

Copy link

daitranthanhhoa commented May 30, 2019

My validation data is the same as train data. I use ImageDataGenerator for splitting.

I see number image of valid data is smaller than train data.

Copy link

zneha commented Aug 28, 2019

Has anyone written small prediction script for testing the data under test folder (if given one?)

Copy link

keve11 commented Mar 7, 2020

Hello everyone,
I run the same codes and got this error:
ValueError: Error when checking input: expected conv2d_11_input to have shape (28, 28, 2) but got array with shape (150, 150, 3)
any help?

Copy link

MasterHansCoding commented Mar 31, 2020


How do you use .load_weights('example.h5') do you have an example ?

Thanks !

Copy link

ConnorKevin commented May 20, 2020


I wonder if the file type must be .h5 in the sentence "model.save_weights('first_try.h5')" ?
and I have the same question like her answer @MasterWas

Thanks !!

Copy link

jitendersaini commented May 22, 2020

Hi, I generated the confusion matrix on prediction result and that is [[2500 0] [2500 0] ]. I think it's not up to the mark. I'm training dogs and cats , training images are 20000 and validation images are 5000.

Copy link

ypirkani commented Jun 3, 2020

Hi, I generated the confusion matrix on prediction result and that is [[2500 0] [2500 0] ]. I think it's not up to the mark. I'm training dogs and cats , training images are 20000 and validation images are 5000.

Can you tell me how you generated the confusion matrix?

Copy link

alif2499 commented Jul 2, 2020

Actually I got not more than 70% when I increased number of dataset. However, It looks not stable.
Any new with you? How can you use one-hot encoding based on the example?

Hello.did you implement the one hot encoding in your code?if so then will you please help me out and is it a must to use one hot encoding in multi class image classification??

Thank you

Copy link

alif2499 commented Jul 2, 2020

I wonder, don't we need the 'label' ? There is no such vector "y_train" being used in the code?

I have this same question.can anyone please clarify?


Copy link

xxwtiancai commented Dec 14, 2020

Accuracy not rising over 0.5000

I met the same problem! Did you solve it.

Copy link

Tech-49 commented Dec 29, 2020

Hi, I am doing Python and classification both the first time. Can anyone tell me is it necessary to keep the index as part of the image name or as long as I have a unique image name it will work?

Copy link

rvencu commented Apr 26, 2021

I wonder, don't we need the 'label' ? There is no such vector "y_train" being used in the code?

I have this same question.can anyone please clarify?


This line of code takes care of the label (i.e. the name of the subdirectory is the label)


Copy link

yousrakateb commented May 17, 2021

how can i test this code on new images ?
thank you

Copy link

diouck commented Jun 2, 2021

Hello excellent tutorial, While trying the code I came across this error. Do you have an idea. It's the same code.
Thank you

Found 21 images belonging to 2 classes. /home/abdou/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/ UserWarning: Model.predict_generatoris deprecated and will be removed in a future version. Please useModel.predict, which supports generators. warnings.warn('Model.predict_generatoris deprecated and ' WARNING:tensorflow:Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at leaststeps_per_epoch * epochs` batches (in this case, 125.0 batches). You may need to use the repeat() function when building your dataset.

TypeError Traceback (most recent call last)
in ()
---> 26 save_bottlebeck_features()
27 train_top_model()

in save_bottlebeck_features()
26 generator, nb_train_samples / batch_size)
27'bottleneck_features_train.npy', 'w'),
---> 28 bottleneck_features_train)
30 generator = datagen.flow_from_directory(

<array_function internals> in save(*args, **kwargs)

/home/abdou/.local/lib/python3.6/site-packages/numpy/lib/ in save(file, arr, allow_pickle, fix_imports)
527 arr = np.asanyarray(arr)
528 format.write_array(fid, arr, allow_pickle=allow_pickle,
--> 529 pickle_kwargs=dict(fix_imports=fix_imports))

/home/abdou/.local/lib/python3.6/site-packages/numpy/lib/ in write_array(fp, array, version, allow_pickle, pickle_kwargs)
646 """
647 _check_version(version)
--> 648 _write_array_header(fp, header_data_from_array_1_0(array), version)
650 if array.itemsize == 0:

/home/abdou/.local/lib/python3.6/site-packages/numpy/lib/ in _write_array_header(fp, d, version)
426 else:
427 header = _wrap_header(header, version)
--> 428 fp.write(header)
430 def write_array_header_1_0(fp, d):

TypeError: write() argument must be str, not bytes`

Copy link

fellipeassuncao commented Jun 3, 2021

@diouck I think that your dataset is very small to use this CNN. Try to classify with more samples and update your packages!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment