预期density_1的形状为(8,),但数组的形状为(1,)

时间:2018-07-07 16:15:53

标签: python-2.7 tensorflow keras

我已经使用keras库编写了代码,并使用Inception Resnet v2将tensorflow作为后端。它给出以下错误。当我使用fit函数而不是fit_generator时。该代码似乎有效。

预期density_1的形状为(8,),但数组的形状为(1,)

from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
import numpy as np
import keras
import tensorflow as tf
from numpy import genfromtxt
from keras import backend as K



batch_size = 4

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)

# this is a generator that will read pictures found in
# subfolers of 'data/train', and indefinitely generate
# batches of augmented image data
train_generator = train_datagen.flow_from_directory(
        'Data/train',  # this is the target directory
        target_size=(299, 299),  # all images will be resized to 299*299
        class_mode='sparse')  

# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
        'Data/validation',
        target_size=(299, 299),
        batch_size=batch_size,
        class_mode='sparse')


# create the base pre-trained model
base_model = InceptionResNetV2(weights='imagenet')

# add a global spatial average pooling layer
x = base_model.output
predictions = Dense(8, activation='softmax')(x)

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


# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

# train the model on the new data for a few epochs
model.fit_generator(
        train_generator,
        steps_per_epoch=16 // batch_size,
        epochs=10,
        validation_data=validation_generator,
        validation_steps=16 // batch_size)

model.save_weights('first_try.h5')  # always save your weights after training or during training

0 个答案:

没有答案