代码如下:
#darknet19细节
import tensorflow as tf
from tensorflow.keras.layers import Dense,Flatten,Conv2D,MaxPooling2D,ZeroPadding2D,UpSampling2D
from tensorflow.keras.layers import Input,AveragePooling2D,Activation
from tensorflow.keras import Model
inputs=Input([256,256,3])
"""
#前面两层卷积的结构函数
"""
def conv2d_1(filters,inputs):
x=Conv2D(filters,(3,3),strides=1,padding='same',activation='relu')(inputs)
x=MaxPooling2D()(x)
return x
def conv2d_2(filters,inputs):
filter1,filter2=filters
x=Conv2D(filter1,(3,3),padding='same',activation='relu')(inputs)
x=Conv2D(filter2,1,padding='same',activation='relu')(x)
x=Conv2D(filter1,3,padding='same',activation='relu')(x)
x=MaxPooling2D()(x)
return x
def conv2d_3(filters,inputs):
filter1,filter2=filters
x=Conv2D(filter1,(3,3),padding='same',activation='relu')(inputs)
x=Conv2D(filter2,(1,1),padding='same',activation='relu')(x)
x=Conv2D(filter1,(3,3),padding='same',activation='relu')(x)
x=Conv2D(filter2,(1,1),padding='same',activation='relu')(x)
x=Conv2D(filter1,(3,3),padding='same',activation='relu')(x)
return x
x=conv2d_1(32,inputs)
x=conv2d_1(64,x)
x=conv2d_2([128,64],x)
x=conv2d_2([256,128],x)
x=conv2d_3([512,256],x)
x=MaxPooling2D()(x)
x=conv2d_3([1024,512],x)
x=Conv2D(1000,1,padding='same',activation='relu')(x)
x=AveragePooling2D(pool_size=(8,8))(x)
x=Flatten()(x)
x=Activation('softmax')(x)
model=Model(inputs,x)
model.summary()
可以通过model.summary()查看网络结构
#darknet19结构图
"""
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 256, 256, 3)] 0
_________________________________________________________________
conv2d (Conv2D) (None, 256, 256, 32) 896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 128, 128, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 128, 128, 64) 18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 64, 64, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 64, 64, 128) 73856
_________________________________________________________________
conv2d_3 (Conv2D) (None, 64, 64, 64) 8256
_________________________________________________________________
conv2d_4 (Conv2D) (None, 64, 64, 128) 73856
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 32, 32, 128) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 32, 32, 256) 295168
_________________________________________________________________
conv2d_6 (Conv2D) (None, 32, 32, 128) 32896
_________________________________________________________________
conv2d_7 (Conv2D) (None, 32, 32, 256) 295168
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 16, 16, 256) 0
_________________________________________________________________
conv2d_8 (Conv2D) (None, 16, 16, 512) 1180160
_________________________________________________________________
conv2d_9 (Conv2D) (None, 16, 16, 256) 131328
_________________________________________________________________
conv2d_10 (Conv2D) (None, 16, 16, 512) 1180160
_________________________________________________________________
conv2d_11 (Conv2D) (None, 16, 16, 256) 131328
_________________________________________________________________
conv2d_12 (Conv2D) (None, 16, 16, 512) 1180160
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 8, 8, 512) 0
_________________________________________________________________
conv2d_13 (Conv2D) (None, 8, 8, 1024) 4719616
_________________________________________________________________
conv2d_14 (Conv2D) (None, 8, 8, 512) 524800
_________________________________________________________________
conv2d_15 (Conv2D) (None, 8, 8, 1024) 4719616
_________________________________________________________________
conv2d_16 (Conv2D) (None, 8, 8, 512) 524800
_________________________________________________________________
conv2d_17 (Conv2D) (None, 8, 8, 1024) 4719616
_________________________________________________________________
conv2d_18 (Conv2D) (None, 8, 8, 1000) 1025000
_________________________________________________________________
average_pooling2d (AveragePo (None, 1, 1, 1000) 0
_________________________________________________________________
flatten (Flatten) (None, 1000) 0
_________________________________________________________________
activation (Activation) (None, 1000) 0
=================================================================
Total params: 20,835,176
Trainable params: 20,835,176
Non-trainable params: 0
"""