Mobilenetv1 network structure training model (image classification)

Keep_ Trying_ Go2022-05-14 19:34:21

1.MobileNetV1 article

https://mydreamambitious.blog.csdn.net/article/details/124560414


2. Rebuild MobileNetV1 Model structure ( Easy to train )

notes : As long as Github In the code downloaded from, the network structure is changed to the following MobileNetV1 Structure is enough ( If the network structure is designed in an object-oriented way , When training data, you need to make some adjustments ) And then modify some names in the code .
The names to be modified are as follows :
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
Rebuilt structure


def DepthWiseConv(inputs,filter,stride=(1,1)):
"""
:param filter: In deep separable convolution 1x1 The number of convolutions
:param stride: Step size of depth convolution
:return:
"""
depthwiseconv = layers.DepthwiseConv2D(kernel_size=[3, 3], strides=stride, padding='same')(inputs)
depthbatch = layers.BatchNormalization()(depthwiseconv)
depthrelu = layers.Activation('relu')(depthbatch)
conv11 = layers.Conv2D(filter, kernel_size=[1, 1], strides=[1, 1], padding='same')(depthrelu)
conv11batch = layers.BatchNormalization()(conv11)
conv11relu = layers.Activation('relu')(conv11batch)
return conv11relu
def MobileNetV1(input_shape=(224,224,3),num_classes=5):
input=keras.Input(input_shape)
conv1 = layers.Conv2D(32, kernel_size=[3, 3], strides=[2, 2], padding='same')(input)
conv1batch = layers.BatchNormalization()(conv1)
conv1relu = layers.Activation('relu')(conv1batch)
x=DepthWiseConv(conv1relu,filter=64, stride=(1, 1))
x=DepthWiseConv(x,filter=128, stride=(2, 2))
x=DepthWiseConv(x,filter=128, stride=(1, 1))
x=DepthWiseConv(x,filter=256, stride=(2, 2))
x=DepthWiseConv(x,filter=256, stride=(1, 1))
x=DepthWiseConv(x,filter=256, stride=(2, 2))
for i in range(5):
x=DepthWiseConv(x,filter=512, stride=(1, 1))
x=DepthWiseConv(x,filter=512, stride=(2, 2))
x=DepthWiseConv(x,filter=1024, stride=(1, 1))
avgpool = layers.GlobalAveragePooling2D()(x)
dense = layers.Dense(num_classes)(avgpool)
dropout = layers.Dropout(rate=0.8)(dense)
softmax = layers.Activation('softmax')(dropout)
model=Model(inputs=input,outputs=softmax)
return model
model_mobilenetv1=MobileNetV1(input_shape=(224,224,3),num_classes=5)
# model_mobilenetv1.summary()

3. The result of the training

 Insert picture description here
 Insert picture description here
 Insert picture description here

Because there are not many data sets , Only near 1000 A picture , And it contains two categories ( cat -cat, Dog -dot) So the final effect of training is general , Readers can collect pictures for training according to their own needs .
 Insert picture description here


4. Image classification test

Only need to flask Just replace the weight file loaded in with your own training (flask This part of the code is from Github In the downloaded file ).
 Insert picture description here

flask.py file :
 Insert picture description here
 Insert picture description here
Front end files :index.html
 Insert picture description here
 Insert picture description here
 Insert picture description here
From the prediction results, the effect is similar to that of model training , Readers can train themselves for several more generations (epoch).



thank
Similar articles