- 1.efficientnetV1文章了解
- 2.MBConv详细结构(关键)
- 3.EfficientNetB0-B7的网络深度,宽度,分辨率,Dropout系数表
- 4.Swish激活函数
- (1)论文地址
- 5.网络的宽度和网络的深度计算
- 6.efficientnet代码实现
https://mydreamambitious.blog.csdn.net/article/details/125036194
2.MBConv详细结构(关键)
3.EfficientNetB0-B7的网络深度,宽度,分辨率,Dropout系数表
4.Swish激活函数
def Swish(inputs):
return inputs*tf.sigmoid(inputs)
(1)论文地址
https://arxiv.org/pdf/1710.05941.pdf
5.网络的宽度和网络的深度计算
#计算缩放之后的宽度
def rounds_width(self,filters):
filters*=self.width_coeff
#计算之后的宽度
new_filters=max(self.divisor,int(filters+self.divisor/2)//self.divisor*self.divisor)
if new_filters<0.9*filters:
new_filters+=self.divisor
return int(new_filters)
#计算深度
def rounds_depth(self,layers):
return int(math.ceil((self.depth_ceoff*layers)))
6.efficientnet代码实现
#Swish激活函数的实现
#网络的深度缩放
#网络的宽度缩放
import math
import os
import keras
import tensorflow as tf
from tensorflow.keras import layers
def Swish(inputs):
return inputs*tf.sigmoid(inputs)
class MBConvxBlock(tf.keras.Model):
def __init__(self,filter_inp,dropout_rate,kernel_size=(3,3),strides=(1,1)):
super(MBConvxBlock, self).__init__()
self.strides=strides
#逐点卷积(1x1卷积)归一化
self.conv11=layers.Conv2D(6*filter_inp,kernel_size=[1,1],strides=[1,1],padding='same')
self.BN1=layers.BatchNormalization()
#深度可分离卷积
self.depthwise=layers.DepthwiseConv2D(kernel_size=kernel_size,strides=strides,padding='same')
self.BN2=layers.BatchNormalization()
#SE操作
self.SE=keras.Sequential([
layers.AveragePooling2D(pool_size=[1,1]),
layers.Conv2D(6*filter_inp//16,kernel_size=[1,1],strides=[1,1],padding='same'),
layers.Conv2D(6*filter_inp,kernel_size=[1,1],strides=[1,1],padding='same')
])
self.conv22=layers.Conv2D(filter_inp,kernel_size=[1,1],strides=[1,1],padding='same')
self.BN3=layers.BatchNormalization()
#连接失活
self.dropout=layers.Dropout(dropout_rate)
#如果步长为1的话,残差连接
if strides==1:
self.shortcut=layers.Conv2D(filter_inp,kernel_size=[1,1],strides=[1,1],padding='same')
self.shortcutBN=layers.BatchNormalization()
def call(self,inputs,training=None):
x=self.conv11(inputs)
x=self.BN1(x)
x=Swish(x)
x=self.depthwise(x)
x=self.BN2(x)
x=Swish(x)
x=self.SE(x)
x=self.conv22(x)
x=self.BN3(x)
x=self.dropout(x)
if self.strides==1:
x1=self.shortcut(inputs)
x1=self.shortcutBN(x1)
x_out=tf.add(x,x1)
return x_out
return x
class EfficientNetV1(tf.keras.Model):
def __init__(self,width_ceoff,depth_ceoff,dropout=0.2):
super(EfficientNetV1, self).__init__()
self.width_coeff=width_ceoff
self.depth_ceoff=depth_ceoff
self.divisor=8
#输入的第一个3x3卷积操作
self.conv11=layers.Conv2D(32,kernel_size=[3,3],strides=[2,2],padding='same')
self.BN1=layers.BatchNormalization()
self.MBConvblock1 = self.MBConvx(filter_inp=self.rounds_width(16), layers=self.rounds_depth(1), kernel_size=(3,3), strides=(1,1), dropout_rate=0.2,i=1)
self.MBConvblock2 = self.MBConvx(filter_inp=self.rounds_width(24), layers=self.rounds_depth(2), kernel_size=(3, 3), strides=(2,2), dropout_rate=0.2,i=2)
self.MBConvblock3 = self.MBConvx(filter_inp=self.rounds_width(40), layers=self.rounds_depth(2), kernel_size=(5,5), strides=(2,2), dropout_rate=0.2,i=3)
self.MBConvblock4 = self.MBConvx(filter_inp=self.rounds_width(80), layers=self.rounds_depth(3), kernel_size=(3, 3), strides=(2,2), dropout_rate=0.2,i=4)
self.MBConvblock5 = self.MBConvx(filter_inp=self.rounds_width(112), layers=self.rounds_depth(3), kernel_size=(5,5), strides=(1,1), dropout_rate=0.2,i=5)
self.MBConvblock6 = self.MBConvx(filter_inp=self.rounds_width(192), layers=self.rounds_depth(4), kernel_size=(5,5), strides=(2,2), dropout_rate=0.2,i=6)
self.MBConvblock7 = self.MBConvx(filter_inp=self.rounds_width(320), layers=self.rounds_depth(1), kernel_size=(3, 3), strides=(1,1), dropout_rate=0.2,i=7)
self.conv22=layers.Conv2D(1280,kernel_size=[1,1],strides=[1,1],padding='same')
self.BN2=layers.BatchNormalization()
self.avgpooling=layers.GlobalAveragePooling2D()
self.dropout=layers.Dropout(dropout)
self.dense=layers.Dense(1000)
self.softmax=layers.Activation('softmax')
def MBConvx(self,filter_inp,layers,kernel_size,strides,dropout_rate,i):
mbconv=keras.Sequential([],name='MBConv'+str(i))
mbconv.add(
MBConvxBlock(filter_inp, dropout_rate, kernel_size, strides)
)
for i in range(1,layers):
mbconv.add(
MBConvxBlock(filter_inp,dropout_rate,kernel_size)
)
return mbconv
#计算缩放之后的宽度
def rounds_width(self,filters):
filters*=self.width_coeff
#计算之后的宽度
new_filters=max(self.divisor,int(filters+self.divisor/2)//self.divisor*self.divisor)
if new_filters<0.9*filters:
new_filters+=self.divisor
return int(new_filters)
#计算深度
def rounds_depth(self,layers):
return int(math.ceil((self.depth_ceoff*layers)))
def call(self,inputs,training=None):
x=self.conv11(inputs)
x=self.BN1(x)
x=Swish(x)
x = self.MBConvblock1(x)
x = self.MBConvblock2(x)
x = self.MBConvblock3(x)
x = self.MBConvblock4(x)
x = self.MBConvblock5(x)
x = self.MBConvblock6(x)
x = self.MBConvblock7(x)
x=self.conv22(x)
x=self.BN2(x)
x=Swish(x)
x=self.avgpooling(x)
x=self.dropout(x)
x=self.dense(x)
x_out=self.softmax(x)
return x_out
def efficientnet_Bx(width_ceoff=1.0,depth_ceoff=1.0,resolution=224,dropout=0.2):
efficientnet_B0 = EfficientNetV1(width_ceoff=width_ceoff, depth_ceoff=depth_ceoff, dropout=dropout)
efficientnet_B0.build(input_shape=(None,resolution,resolution, 3))
efficientnet_B0.summary()
if __name__ == '__main__':
print('Pycharm')
efficientnet_Bx(width_ceoff=1.0,depth_ceoff=1.0,resolution=224,dropout=0.2)
# efficientnet_Bx(width_ceoff=1.0, depth_ceoff=1.1,resolution=240, dropout=0.2)
# efficientnet_Bx(width_ceoff=1.1, depth_ceoff=1.2,resolution=260, dropout=0.3)
# efficientnet_Bx(width_ceoff=1.2, depth_ceoff=1.4,resolution=300, dropout=0.3)
# efficientnet_Bx(width_ceoff=1.4, depth_ceoff=1.8,resolution=380, dropout=0.4)
# efficientnet_Bx(width_ceoff=1.6, depth_ceoff=2.2,resolution=456, dropout=0.4)
# efficientnet_Bx(width_ceoff=1.8, depth_ceoff=2.6,resolution=528, dropout=0.5)
# efficientnet_Bx(width_ceoff=2.0, depth_ceoff=3.1,resolution=600, dropout=0.5)



