keras实现Lenet网络,图像分类

欢迎大家加入京东淘宝捡垃圾群:698433653  

话不多说,上模型。
# LeNet-5
def Lenet(inputShape,weight_decay=0.0005,classNum=0):
# 初始化模型 输入尺寸32 32
model = Sequential()
inputShape =(height, width, depth)
# 1
model.add(Conv2D(20, (5, 5), padding="same", input_shape=inputShape))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# 2
model.add(Conv2D(50, (5, 5), padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# 4
model.add(Flatten())
model.add(Dense(500))
model.add(Activation("relu"))
# model.add(Dropout(0.5))
# 5
model.add(Dense(2))
model.add(Activation("softmax"))

# 返回构造的模型
return model

adam = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)#参数设置
model.compile(loss="binary_crossentropy", optimizer=adam, metrics=["accuracy"])
#二进制交叉熵 binary crossentropy    分类交叉熵categorical_crossentropy

# train the network
print("[INFO] training network...")
model_checkpoint = ModelCheckpoint('best.hdf5', monitor='val_acc', verbose=1, save_best_only=True)
tb_cb = keras.callbacks.TensorBoard(log_dir="./log", write_images=1, histogram_freq=0)
# 设置log的存储位置,将网络权值以图片格式保持在tensorboard中显示,设置每一个周期计算一次网络的
H = model.fit_generator(train_generator,steps_per_epoch=len(trainList)//BS,epochs=EPOCHS,
                        validation_data=validation_generator,validation_steps=len(valList)//BS,callbacks=[model_checkpoint, tb_cb, reduce_lr, lr_scheduler])
min_delta=0.0001, cooldown=0, min_lr=0)

# save the model to disk
print("[INFO] serializing network...")
model.save(".//lasted.hdf5")

##############到此结束

 

你可能感兴趣的:(深度学习模型)