keras搭建VGG、ResNet GoogleNet InceptionV3实现图像的多分类任务

  

基于keras实现分类任务

基于keras利用VGG, ResNet GoogleNet   InceptionV3实现图像的分类任务,下面会给出完整代码,但为了熟悉不同整个网络的特点,建议大家自己搭建一下每个分类网络,毕竟利用keras搭建网络还是比较简单的。

# - * -编码:utf - 8 - * - - - - - -

import操作系统从keras

。跑龙套进口plot_model keras.applications

。resnet50进口resnet50 keras.applications

。vgg19进口vgg19 keras.applications

。inception_v3进口InceptionV3 keras

。进口致密层,压平,GlobalAveragePooling2D keras

。模型导入模型,load_model keras

。优化进口SGD keras.preprocessing

。图像导入ImageDataGenerator

进口matplotlib。pyplot plt

类PowerTransferMode:

#数据准备

def DataGen(自我、dir_path img_row、img_col batch_size, is_train):

如果is_train:

DataGen=ImageDataGenerator(重新调节=1。/255,

zoom_range=0.25, rotation_range=15。

channel_shift_range=25。width_shift_range=0.02,   height_shift_range=0.02

horizontal_flip=True, fill_mode=俺J?

:

datagen=ImageDataGenerator(重新调节=1./255)

发电机=datagen。flow_from_directory (

dir_path, target_size=(img_row img_col)

batch_size=batch_size

# class_mode=岸啤?

class_mode=胺掷唷?

洗牌=is_train)

返回发电机

# ResNet模型

def ResNet50_model(自我,lr=0.005,=1 e-6衰减,动量=0.9,nb_classes=2,   img_rows=197, img_cols=197, RGB=True, is_plot_model=False):

=3颜色如果RGB其他1

base_model=ResNet50(权重=imagenet, include_top=False,池=没有   input_shape=(img_rows img_cols,颜色),

类=nb_classes)

#冻结base_model所有层,这样就可以正确获得瓶颈特征

base_model层。层:

层。可训练的=False

x=base_model。输出

#添加自己的全链接分类层

x=平()(x)

# x=GlobalAveragePooling2D () (x)

# x=密集(1024年,激活=relu) (x)=

预测致密(nb_classes激活=softmax) (x)

#训练模型

=模型(输入=base_model模型。输入,输出=预测)

sgd=sgd (lr=lr,衰变=衰变,动量=动量,nesterov=True)

model.compile (=癱ategorical_crossentropy”损失,优化器=sgd,   指标=[“准确性”])

#绘制模型

如果is_plot_model:

plot_model(模型、to_file=' resnet50_model.png show_shapes=True)

回归模型# VGG模型

def VGG19_model(自我,lr=0.005,=1 e-6衰减,动量=0.9,nb_classes=18,   img_rows=197, img_cols=197, RGB=True, is_plot_model=False):

=3颜色如果RGB其他1

base_model=VGG19(权重=imagenet, include_top=False,池=没有   input_shape=(img_rows img_cols,颜色),

类=nb_classes)

#冻结base_model所有层,这样就可以正确获得瓶颈特征

base_model层。层:

层。可训练的=False

x=base_model。输出

#添加自己的全链接分类层

x=GlobalAveragePooling2D () (x)

x=密集(1024年,激活=relu) (x)

x=密集(1024年,激活=relu) (x)=

预测致密(nb_classes激活=softmax) (x)

#训练模型

=模型(输入=base_model模型。输入,输出=预测)

sgd=sgd (lr=lr,衰变=衰变,动量=动量,nesterov=True)

model.compile (=癱ategorical_crossentropy”损失,优化器=sgd,   指标=[“准确性”])

#绘图

如果is_plot_model:

plot_model(模型、to_file=' vgg19_model.png show_shapes=True)

回归模型# InceptionV3模型

def InceptionV3_model(自我,lr=0.005,=1 e-6衰减,动量=0.9,   img_rows nb_classes=18日=197,img_cols=197, RGB=True,

is_plot_model=False):

颜色=3如果RGB其他1

base_model=InceptionV3(权重=imagenet, include_top=False,   池=,

input_shape=(img_rows img_cols,颜色),

类=nb_classes)

#冻结base_model所有层,这样就可以正确获得瓶颈特征

base_model层。层:

层。可训练的=False

x=base_model。输出

#添加自己的全链接分类层

x=GlobalAveragePooling2D () (x)

x=密集(1024年,激活=relu) (x)

# x=GlobalAveragePooling2D () (x)

x=密集(1024年,激活=relu) (x)=

预测致密(nb_classes激活=softmax) (x)

#训练模型

=模型(输入=base_model模型。输入,输出=预测)

sgd=sgd (lr=lr,衰变=衰变,动量=动量,nesterov=True)

model.compile (=癱ategorical_crossentropy”损失,优化器=sgd,   指标=[“准确性”])

#绘图

如果is_plot_model:

plot_model(模型,to_file=' inception_v3_model。png, show_shapes=True)

keras搭建VGG、ResNet GoogleNet InceptionV3实现图像的多分类任务