0

I have trained this vgg-19 model and now need to predict single image. I have tried something like this: Cannot predict the label for a single image with VGG19 in Keras

I add it at the end of model, but it doesn´t work.

base_model = VGG19(weights=None, include_top=False, pooling='avg', input_shape=(LEFT, RIGHT, 3))

    # add a global spatial average pooling layer
    x = base_model.output
    x = Dense(1024, activation='relu')(x)

    # and a logistic layer -- let's say we have 2 classes
    predictions = Dense(2, activation='softmax')(x)

    # this is the model we will train
    model = Model(inputs=base_model.input, outputs=predictions)

    # Print the layers
    for i, layer in enumerate(model.layers):
        print(i, layer.name, layer.output_shape) 
    plot_model(model, show_shapes=True, to_file=MODELDIR + IDENTNAME + '_model.png')

    # we chose to train the top  inception blocks, i.e. we will freeze
    # the first 5 layers and unfreeze the rest:
    for layer in model.layers[:10]:
        layer.trainable = True
    for layer in model.layers[10:]:
        layer.trainable = True

    # we need to recompile the model for these modifications to take effect

    from keras.optimizers import Adam

    optimizer = Adam(lr=0.00008, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=True)

    model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])

    history = model.fit_generator(generator(BATCHSIZE, DATADIR), steps_per_epoch=DATASTEPS,
                                  validation_data=generator(NUMVALIDATIONFILES, VALIDATIONDIR), validation_steps=1,
                                  epochs=EPOCHS, verbose=1, class_weight={0: 1, 1: 1})

    # Save model and weights....
    # serialize model to YAML
    model_yaml = model.to_yaml()
    with open(MODELDIR + IDENTNAME + '_model.yaml', "w") as yaml_file:
        yaml_file.write(model_yaml)
    # serialize weights to HDF5
    model.save_weights(MODELDIR + IDENTNAME + '_weights.h5')
    print("Saved model to disk")

#######predict one image#####
   from keras.preprocessing.image import load_img
    image = load_img('picture.png', target_size=(64, 64))
    from keras.preprocessing.image import img_to_array
    image = img_to_array(image)
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    from keras.applications.vgg19 import preprocess_input
    image = preprocess_input(image)
    yhat = model.predict(image)

    # create a list containing the class labels
    class_labels = ['class1', 'class2']

    # find the index of the class with maximum score
    pred = np.argmax(class_labels, axis=-1)

    # print the label of the class with maximum score
    print(class_labels[pred[0]])

The last line makes error: invalid index to scalar variable. How to correct this error? Should be the problem in dimensions of picture? It has actually 4 dimensions: r,g,b and transparency? When I prepare the pictures (before model), I make this step:

batch_features[i, :, :, :] = imageio.imread(t)[:, :, :3]

Is it the thing I have to do even with the single image?

Edit

Now the code of import single image looks like this:

from keras.preprocessing.image import load_img
    image = load_img('picture.png', target_size=(64, 64, 3))
    np.expand_dims(image, axis=0)
    from keras.preprocessing.image import img_to_array
    image = img_to_array(image)
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))

    from keras.applications.vgg19 import preprocess_input
    image = preprocess_input(image)
    yhat = model.predict(image)
    # create a list containing the class labels
    class_labels = ['class1', 'class2']
    # find the index of the class with maximum score
    pred = np.argmax(class_labels, axis=-1)
    # print the label of the class with maximum score
    print(class_labels[pred[0]])

Shouldn´t be a problem in line:

    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))

Edit(1):

This is how looks like input data into model:

        for i, t in enumerate(target_b):
            batch_features[i, :, :, :] = imageio.imread(t)[:, :, :3]  

            batch_labels[i, :] = np.array([1, 0]) if "_avalanche_" in t else np.array([0, 1])

I think I have to change format of single image to [1,0] array?

Community
  • 1
  • 1
td23
  • 1
  • 2
  • For one, yes, you need to be consistent with your input data, even when predicting a single image – WiseDev Jun 18 '19 at 09:32
  • @BlueRine S So I removed transparency from single image, but still have the same error. How should I change my code to correct it, please? – td23 Jun 18 '19 at 10:24
  • What is the shape of your input data now? – WiseDev Jun 18 '19 at 10:38
  • Input images to model are 64x64, rgb + transparency, transparency edited with this: imageio.imread(t)[:, :, :3] input single image is only 64x64 rgb, transparency deleted – td23 Jun 18 '19 at 10:42
  • In general, the input should be (batch_size, *resolution, num_channels). So for example if your image x is of shape (64, 64, 3), you need to convert it to (1, 64, 64, 3) for a single image. You can do that with np.expand_dims(x, axis=0) – WiseDev Jun 18 '19 at 10:47
  • I actually add np.expand_dims, but still the same error. Script looks like this: from keras.preprocessing.image import load_img image = load_img('pic.png', target_size=(64, 64, 3)) np.expand_dims(image, axis=0) from keras.preprocessing.image import img_to_array image = img_to_array(image) image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) from keras.applications.vgg19 import preprocess_input image = preprocess_input(image) yhat = model.predict(image) class_labels = ['class1', 'class2'] pred = np.argmax(class_labels, axis=-1) print(class_labels[pred[0]]) – td23 Jun 18 '19 at 11:44
  • Edit this code into your original post please. – WiseDev Jun 18 '19 at 11:46
  • np.expand_dims is not a mutable operation as far as I know. You need to do image = np.expand_dims(image, axis=0) – WiseDev Jun 18 '19 at 12:58
  • Thank you for your help, but it says it is unsupported image size [1,64,64,3]. I think the problem is in input data to the model, because I noticed it makes some [1,0] arrays. I edit the code in original post again. – td23 Jun 18 '19 at 15:43

0 Answers0