例外:检查模型目标时出错:期望dense_3具有形状(无,1000)但是具有形状的数组(32,2)

时间:2016-09-05 17:41:09

标签: keras

如何为我的数据创建VGG-16序列?

数据如下:

model = Sequential() 
model.add(ZeroPadding2D((1, 1), input_shape=(3, img_width, img_height))) model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1')) model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1))) 
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1')) model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1))) 
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1')) model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
model.add(ZeroPadding2D((1, 1))) 
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3')) model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1))) 
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1')) model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
model.add(ZeroPadding2D((1, 1))) 
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3')) model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1))) 
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1')) model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
model.add(ZeroPadding2D((1, 1))) 
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3')) model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(Flatten()) 
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5)) 
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5)) 
model.add(Dense(1000, activation='softmax'))

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy')

train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=32)

validation_generator = test_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_width, img_height),
        batch_size=32)

model.fit_generator(
        train_generator,
        samples_per_epoch=2000,
        nb_epoch=1,
        verbose=1,
        validation_data=validation_generator,
        nb_val_samples=800)

json_string = model.to_json()  
open('my_model_architecture.json','w').write(json_string) 
model.save_weights('Second_try.h5')

我收到了一个错误:

  

异常:检查模型目标时出错:期望dense_3有   形状(无,32)但是有形状的阵列(32,2)

如何更改Dense以使其正常工作?

2 个答案:

答案 0 :(得分:6)

我有10种,
我已经解决了这个问题 改变:

model.add(Dense(1000, activation='softmax'))

到:

model.add(Dense(10, activation='softmax'))

然后它有效。

答案 1 :(得分:1)

这里代替1000,你应该拥有类的总数,因为它是输出层。

model.add(Dense(1000, activation='softmax')) 

标签的形状(或Y_train / Y_test)也应该是(类的总数,记录的总数)。

这有助于我解决类似的错误。