keras —Training model using VGG16 produces ValueError.

Asked 1 years ago, Updated 1 years ago, 111 views

Based on images prepared with confidence using this ,
I tried to implement a learning model with two classes, but I got a ValueError.

Implementation Environment
·Anaconda 4.3.14
·Python 3.5.3
·Tensorflow 1.2.0rc2
·keras 2.0.4
·Numpy 1.12.1

I think there is a problem with predict_generator(), but I am posting it because it cannot be solved even after I look into it.

Here's the details.

Implemented Code

#-*-coding:utf-8-*-
from__future_import print_function
importos
import sys
import h5py
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense
from keras.applications import VGG16


img_w=150
img_h = 150

top_model_weights_path='bottleneck_fc_model.h5'
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'

# classes = ['landscape', 'portrait' ]
# classes = ['landscape', 'portraits' ]
# nb_classes=len(classes)

nb_train_samples = 180
nb_validation_samples=20
# nb_samples_per_class=90
epochs = 10
batch_size=16


# Preserve the VGG16 model pre-FC layer feature
def save_bottleneck_features():
    datagen= ImageDataGenerator(
        rotation_range=40,
        width_shift_range = 0.2,
        height_shift_range = 0.2,
        shear_range = 0.2,
        zoom_range = 0.2,
        horizontal_flip = True,
        fill_mode='nearest')

    # Create a VGG16 Network
    model=VGG16(include_top=False, weights='imagenet')

    generator = datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_w,img_h),
        batch_size = batch_size,
        shuffle=False,
        class_mode=None)
        # classes=classes)
    botleneck_features_train=model.predict_generator(
        generator, nb_train_samples//batch_size)
    np.save(open('bottleneck_features_train.npy', 'wb'),
            botleneck_features_train)

    generator = datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_w,img_h),
        batch_size = batch_size,
        shuffle=False,
        class_mode=None)
        # classes=classes)
    botleneck_features_validation=model.predict_generator(
        generator, nb_validation_samples//batch_size)
    np.save(open('bottleneck_features_validation.npy', 'wb'),
            botleneck_features_validation)


# Create and learn an FC layer based on the feature amount stored in save_bottleneck_features
deftrain_top_model():
    train_data=np.load(open('bottleneck_features_train.npy', 'rb')))
    train_labels = np.array([0]*int(nb_train_samples/2)+[1]*int(nb_train_samples/2))

    validation_data=np.load(open('bottleneck_features_validation.npy', 'rb')))
    validation_labels=np.array([0]*int(nb_validation_samples/2)+[1]*int(nb_validation_samples/2))

    # (180, 4, 4, 512) Expected Value
    print(train_data.shape)
    # (20,4,4,512) Expected value
    print(validation_data.shape)

    # Create FC Network
    top_model=Sequential()
    top_model.add(Flatten(input_shape=train_data.shape[1:]))
    top_model.add(Dense(256, activation='relu'))
    top_model.add(Dropout(0.5))
    top_model.add(Dense(1,activation='sigmoid'))

    top_model.compile(
        optimizer='rmsprop',
        loss='binary_crossentropy',
        metrics=['accuracy'])

    # visualize model
    top_model.summary()
    from keras.utils import plot_model
    plot_model(top_model, to_file="model.png", show_shapes=True)


    # learning
    top_model.fit(
        train_data,
        train_labels,
        epochs = epochs,
        batch_size = batch_size,
        validation_data=(validation_data, validation_labels))
        # validation_split=0.05)

    # Save learned weights
    top_model.save_weights(top_model_weights_path)


save_bottleneck_features()
train_top_model()

Below are the details of the error.

Found 180 images belonging to 2 classes.
Found 20 images belonging to 2 classes.
(176,4,4,512)# print(train_data.shape) value
(16,4,4,512)# print(validation_data.shape) value
Traceback (most recent call last):
  File "train.py", line 115, in<module>
    train_top_model()
  File "train.py", line 107, intrain_top_model
    validation_data=(validation_data, validation_labels))
  File "C:\Users\username\Anaconda3\envs\py35\lib\site-packages\keras\models.py", line 856, infit
    initial_epoch = initial_epoch)
  File "C:\Users\username\Anaconda3\envs\py35\lib\site-packages\keras\engine\training.py", line 1429, infit
    batch_size=batch_size)
  File "C:\Users\username\Anaconda3\envs\py35\lib\site-packages\keras\engine\training.py", line 1317, in_standardize_user_data
    _check_array_lengths(x,y,sample_weights)
  File "C:\Users\username\Anaconda3\envs\py35\lib\site-packages\keras\engine\training.py", line 235, in_check_array_lengths
    'and'+str(list(set_y)[0])+'target samples.')
ValueError: Input arrays should have the same number of samples as target arrays. Found 176 input samples and 180 target samples.

python3 tensorflow deep-learning keras

2022-09-30 19:52

1 Answers

As you mentioned in the log below,

 (176,4,4,512)# print(train_data.shape) value

The number of inputs (train_data read from botleneck_features_train.npy) is 176, while the number of labels (train_labels) seems to be expected to be 180.
Please match the number of inputs to the number of labels.


2022-09-30 19:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.