这是我的参考文献:
从目录示例流
AlexNet架构
我尝试使用AlexNet架构来训练3个类别的灰度图像数据集。我修改了第一个链接以成为分类模式,然后将CNN模型从第二个链接改为AlexNet。但是我收到了2个错误消息:
1. ValueError:由于从1中减去3而导致负维度大小,用于“conv2d_83 / convolution”(op:“ Conv2D”)的输入形状为[?,1,1,384],[3,3,384,384]。 2. 如果我更改img_width,img_height = 224,224 TypeError:
在CNN中是否存在不匹配的维度?谢谢。
这是代码:
1. ValueError:由于从1中减去3而导致负维度大小,用于“conv2d_83 / convolution”(op:“ Conv2D”)的输入形状为[?,1,1,384],[3,3,384,384]。 2. 如果我更改img_width,img_height = 224,224 TypeError:
Dense
只能接受1个位置参数('units'),但您传递了以下位置参数:[4096,(224,224,1)]在CNN中是否存在不匹配的维度?谢谢。
这是代码:
import json
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
#from tensorflow.keras.optimizers import RMSprop
# dimensions of our images.
img_width, img_height = 150,150
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = 200*3
nb_validation_samples = 50*3
epochs = 1
batch_size = 5
if K.image_data_format() == 'channels_first':
input_shape = (1, img_width, img_height)
else:
input_shape = (img_width, img_height, 1)
print(input_shape)
model = Sequential()
model.add(Conv2D(filters=96, input_shape=input_shape,data_format='channels_last', kernel_size=(11,11), strides=(4,4), padding='valid'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid'))
model.add(Conv2D(filters=256, kernel_size=(11,11), strides=(1,1), padding='valid'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid'))
model.add(Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding='valid'))
model.add(Activation('relu'))
#model.add(MaxPooling2D(pool_size=(2, 2)))
# 4th Convolutional Layer
model.add(Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding='valid'))
model.add(Activation('relu'))
# 5th Convolutional Layer
model.add(Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), padding='valid'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid'))
model.add(Flatten())
model.add(Dense(4096, input_shape))
model.add(Activation('relu'))
model.add(Dropout(0.4))
model.add(Dense(4096))
model.add(Activation('relu'))
model.add(Dropout(0.4))
model.add(Dense(1000))
model.add(Activation('relu'))
model.add(Dropout(0.4))
# Output Layer
model.add(Dense(3))
model.add(Activation('softmax'))
model.summary()
# Compile the model
model.compile(loss=keras.losses.categorical_crossentropy, optimizer='adam', metrics=['accuracy'])
#model.compile(loss='categorical_crossentropy',optimizer=RMSprop(lr=0.001),metrics=['accuracy'])
# 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)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
color_mode='grayscale',
batch_size=batch_size,
class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
color_mode='grayscale',
batch_size=batch_size,
class_mode='categorical')
model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)
model_json = model.to_json()
with open("model_in_json.json", "w") as json_file:
json.dump(model_json, json_file)
model.save_weights("model_weights.h5")