7.9学习记录 今天走了一遍训练流程,中途遇到了各种bug,比如tensorflow和keras的版本不兼容的问题,最终安装了tensorflow1.14和keras2.2.5本,成功运行。
训练的是口罩识别系统
这是搭建的卷积神经网络
def build_model(inputshape,shape_count):
img_in = Input(shape=inputshape, name='img_in')
X = img_in
X = Convolution2D(8, 3, padding='same', activation='relu', name='conv1')(X)
X = MaxPooling2D(pool_size=2, strides=2, padding='same', name='maxpool1')(X)
X = Convolution2D(16, 3, padding='same', activation='relu', name='conv2')(X)
X = MaxPooling2D(pool_size=2, strides=2, padding='same', name='maxpool2')(X)
X_SHORT = X
X = Convolution2D(16, 3, padding='same', activation='relu', name='conv3')(X)
X = layers.add([X, X_SHORT])
X = Convolution2D(32, 3, padding='same', activation='relu', name='conv4')(X)
X = MaxPooling2D(pool_size=2, strides=2, padding='same', name='maxpool3')(X)
X = Convolution2D(64, 3, padding='same', activation='relu', name='conv5')(X)
X = MaxPooling2D(pool_size=2, strides=2, padding='same', name='maxpool4')(X)
X = Flatten(name='flattened')(X)
X = Dense(128, activation='relu', name='dense1')(X)
X = Dense(128, activation='relu', name='dense2')(X)
X = Dropout(0.25)(X)
classify = Dense(shape_count, activation='softmax', name='dense3')(X)
model = Model(inputs=[img_in], outputs=[classify])
return model
主函数
if __name__ == '__main__':
model = build_model((64, 64, 3),2)
adam = Adam(lr=0.0005)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])
datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=10,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
brightness_range = (0.4,1.4),
fill_mode='nearest')
datagen_vail = ImageDataGenerator(rescale=1./255)
gener=datagen.flow_from_directory('.\kzdataset',
batch_size=40,
target_size = (64,64),
shuffle=True)
gener_vail = datagen_vail.flow_from_directory('.\kzdataset',
batch_size=200,
target_size = (64,64),
shuffle=True)
model.fit_generator(gener,steps_per_epoch=100,epochs=80, verbose=1,validation_data=gener_vail)
model.save("cell.h5")
print("训练完毕")
开始运行… 放着慢慢跑吧 玩了一会儿游戏…它还在跑 吃了个饭…它还在跑… 睡了会儿觉…它跑完了。
我舍友说,就在我醒的前几分钟跑完的… 那么我六点多开始跑的,跑到了十点多…WTF
辛苦我的cpu了,果然cpu的效率贼低,还是用cuda效率高,几分钟就跑完了,可惜我的电脑显卡不支持(掀桌)。
总结: 第一次体验训练人工智能的过程,心里有了个底。不过代码是老师给的,之后的任务就是研究研究这段python代码。老师说通过简单的修改,不光能成为口罩识别,还能进行眼镜识别。继续学习ing…
|