gpt4 book ai didi

python - 使用 alexnet 和 flow from 目录来训练灰度数据集

转载 作者:行者123 更新时间:2023-11-30 08:35:39 25 4
gpt4 key购买 nike

这是我的引用: flow from directory example alexnet architecture

我尝试使用 alexnet 架构训练 3 个类别。数据集是灰度图像。我将第一个链接修改为分类类模式,然后将 CNN 模型从第二个链接修改为 alexnet。我收到 2 条错误消息:

  1. ValueError:由于输入形状为:[?,1,1,384]、[3,3,384,384]的“conv2d_83/卷积”(操作:“Conv2D”)从 1 中减去 3 导致的负维度大小。

  2. 如果我更改 img_width,img_height = 224,224TypeError: 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")

最佳答案

  1. AlexNet旨在与 input_size 一起使用227x227。论文中提到了 224x224,但这是一个拼写错误。这并不是说您不能使用其他尺寸,但是这样架构就没有什么意义了。当输入大小太小时,即您的情况,会出现更明显的问题。 strides=2 的卷积和最大池化操作降低了后续层的维度。您只是用完了尺寸,这由 ValueError: Negative dimension size caused by subtracting 3 from 1 for 'conv2d_83/convolution' 表示。 。对输入图像进行上采样或更改架构。

  2. 错误源于 model.add(Dense(4096, input_shape)) 。如果您检查keras Dense 的文档层,您会注意到第二个参数是 activation 。如果有的话,你应该使用 model.add(Dense(4096, input_shape=your_input_shape))

关于python - 使用 alexnet 和 flow from 目录来训练灰度数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59518514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com