<p>The reason why this error occured is that model always expects the <strong>batch</strong> of examples - not a <strong>single</strong> example. This diverge from a common understanding of models as mathematical functions of their inputs. The reasons why model expects batches are:</p>

<ol>
<li>Models are computationaly designed to work faster on batches in order to speed up training.</li>
<li>There are algorithms which takes into account the batch nature of input (e.g. <a href="https://arxiv.org/abs/1502.03167" rel="nofollow noreferrer"><strong>Batch Normalization</strong></a> or <strong>GAN</strong> training tricks).</li>
</ol>

<p>So four dimensions comes from a first dimension which is a <strong>sample / batch</strong> dimension and then - the next 3 dimensions are image dims.</p>
TensorFlow 1.2.1 and InceptionV3 to classify an image
<p>I'm trying to create an example using the Keras built in the latest version of TensorFlow from Google. This example should be able to classify a classic image of an elephant. The code looks like this:</p>

<pre><code># Import a few libraries for use later
from PIL import Image as IMG

from tensorflow.contrib.keras.python.keras.preprocessing import image
from tensorflow.contrib.keras.python.keras.applications.inception_v3 import InceptionV3
from tensorflow.contrib.keras.python.keras.applications.inception_v3 import preprocess_input, decode_predictions


# Get a copy of the Inception model
print('Loading Inception V3...\n')
model = InceptionV3(weights='imagenet', include_top=True)
print ('Inception V3 loaded\n')

# Read the elephant JPG
elephant_img = IMG.open('elephant.jpg')

# Convert the elephant to an array
elephant = image.img_to_array(elephant_img)
elephant = preprocess_input(elephant)

elephant_preds = model.predict(elephant)

print ('Predictions: ', decode_predictions(elephant_preds))
</code></pre>

<p>Unfortunately I'm getting an error when trying to evaluate the model with model.predict:</p>

<pre><code>ValueError: Error when checking : expected input_1 to have 4 dimensions, but got array with shape (299, 299, 3)
</code></pre>

<p>This code is taken from and based on <a href="https://github.com/vml-ffleschner/coremltools-keras-inception-test" rel="nofollow noreferrer">the excellent example coremltools-keras-inception</a> and will be expanded more when it is figured out.</p>