Do I understand batch_size correctly in Keras?

时间:2019-03-17 22:27:17

标签: python tensorflow machine-learning keras conv-neural-network

I'm using Keras' built-in inception_resnet_v2 to train a CNN to recognize images. When training the model, I have a numpy array of data as inputs, with input shape (1000, 299, 299, 3),

 model.fit(x=X, y=Y, batch_size=16, ...) # Output shape `Y` is (1000, 6), for 6 classes

At first, When trying to predict, I passed in a single image of shape (299, 299, 3), but got the error

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (299, 299, 3)

I reshaped my input with:

x = np.reshape(x, ((1, 299, 299, 3)))

Now, when I predict,

y = model.predict(x, batch_size=1, verbose=0)

I don't get an error.

I want to make sure I understand batch_size correctly in both training and predicting. My assumptions are:

1) With model.fit, Keras takes batch_size elements from the input array (in this case, it works through my 1000 examples 16 samples at a time)

2) With model.predict, I should reshape my input to be a single 3D array, and I should explicitly set batch_size to 1.

Are these correct assumptions?

Also, would it be better (possible even) to provide training data to the model so that this sort of reshape before prediction was not necessary? Thank you for helping me learn this.

1 个答案:

答案 0 :(得分:2)

No, you got the idea wrong. batch_size specifies how many data examples are "forwarded" through the network at once (using GPU usually).

By default, this value is set to 32 inside model.predict method, but you may specify otherwise (as you did with batch_size=1). Because of this default value you got an error:

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (299, 299, 3)

You should not reshape your input this way, rather you would provide it with the correct batch size.

Say, for the default case you would pass an array of shape (32, 299, 299, 3), analogous for different batch_size, e.g. with batch_size=64 this function requires you to pass an input of shape (64, 299, 299, 3.

EDIT:

It seems you need to reshape your single sample into a batch. I would advise you to use np.expand_dims for improved readability and portability of your code, like this:

y = model.predict(np.expand_dims(x, axis=0), batch_size=1)