本篇文章主要是为了让自己熟悉tf.nn的一些常用函数,所以基于tf.nn自己搭建了一个inception v3的网络,以下是完整代码,如果存在错误还请大家指正
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
import numpy as np
#第一个模块
class block1():
def __init__(self) -> None:
pass
#构建模型的中Block1中的子模块1
def block1_model1(self,x):
n_in=x.get_shape()[-1]
with tf.name_scope('B1-M1'):
with tf.name_scope('S1'):
with tf.name_scope('conv1_1'):
kernel1_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,64]))
bias1_1=tf.Variable(initial_value=tf.constant(0.0,shape=[64]))
conv1_1=tf.nn.conv2d(x,kernel1_1,strides=[1,1,1,1], padding='SAME')
biases1_1=tf.nn.bias_add(conv1_1,bias1_1)
mean1_1,var1_1=tf.nn.moments(biases1_1,axes=[0,1,2])
bn1_1= tf.nn.batch_normalization(biases1_1,mean1_1,var1_1,offset=None,scale=None,variance_epsilon=1e-8)
x1_1=tf.nn.relu(bn1_1,name='conv1_1')
x1_out=x1_1
with tf.name_scope('S2'):
with tf.name_scope('conv2_1'):
kernel2_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,48]))
bias2_1=tf.Variable(initial_value=tf.constant(0.0,shape=[48]))
conv2_1=tf.nn.conv2d(x,kernel2_1,strides=[1,1,1,1],padding='SAME')
biases2_1=tf.nn.bias_add(conv2_1,bias2_1)
mean2_1,var2_1=tf.nn.moments(biases2_1,axes=[0,1,2])
bn2_1= tf.nn.batch_normalization(biases2_1,mean2_1,var2_1,offset=None,scale=None,variance_epsilon=1e-8)
x2_1=tf.nn.relu(bn2_1,name='conv2_1')
with tf.name_scope('conv2_2'):
kernel2_2=tf.Variable(initial_value=tf.random.normal([5,5,48,64]))
bias2_2=tf.Variable(initial_value=tf.constant(0.0,shape=[64]))
conv2_2=tf.nn.conv2d(x2_1,kernel2_2,strides=[1,1,1,1],padding='SAME')
biases2_2=tf.nn.bias_add(conv2_2,bias2_2)
mean2_2,var2_2=tf.nn.moments(biases2_2,axes=[0,1,2])
bn2_2= tf.nn.batch_normalization(biases2_2,mean2_2,var2_2,offset=None,scale=None,variance_epsilon=1e-8)
x2_2=tf.nn.relu(bn2_2,name='conv2_2')
x2_out=x2_2
with tf.name_scope('S3'):
with tf.name_scope('conv3_1'):
kernel3_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,64]))
bias3_1=tf.Variable(initial_value=tf.constant(0.0,shape=[64]))
conv3_1=tf.nn.conv2d(x,kernel3_1,strides=[1,1,1,1],padding='SAME')
biases3_1=tf.nn.bias_add(conv3_1,bias3_1)
mean3_1,var3_1=tf.nn.moments(biases3_1,axes=[0,1,2])
bn3_1= tf.nn.batch_normalization(biases3_1,mean3_1,var3_1,offset=None,scale=None,variance_epsilon=1e-8)
x3_1=tf.nn.relu(bn3_1,name='conv3_1')
with tf.name_scope('conv3_2'):
kernel3_2=tf.Variable(initial_value=tf.random.normal([3,3,64,96]))
bias3_2=tf.Variable(initial_value=tf.constant(0.0,shape=[96]))
conv3_2=tf.nn.conv2d(x3_1,kernel3_2,strides=[1,1,1,1],padding='SAME')
biases3_2=tf.nn.bias_add(conv3_2,bias3_2)
mean3_2,var3_2=tf.nn.moments(biases3_2,axes=[0,1,2])
bn3_2= tf.nn.batch_normalization(biases3_2,mean3_2,var3_2,offset=None,scale=None,variance_epsilon=1e-8)
x3_2=tf.nn.relu(bn3_2,name='conv3_2')
with tf.name_scope('conv3_3'):
kernel3_3=tf.Variable(initial_value=tf.random.normal([3,3,96,96]))
bias3_3=tf.Variable(initial_value=tf.constant(0.0,shape=[96]))
conv3_3=tf.nn.conv2d(x3_2,kernel3_3,strides=[1,1,1,1],padding='SAME')
biases3_3=tf.nn.bias_add(conv3_3,bias3_3)
mean3_3,var3_3=tf.nn.moments(biases3_3,axes=[0,1,2])
bn3_3= tf.nn.batch_normalization(biases3_3,mean3_3,var3_3,offset=None,scale=None,variance_epsilon=1e-8)
x3_3=tf.nn.relu(bn3_3,name='conv3_3')
x3_out=x3_3
with tf.name_scope('S4'):
with tf.name_scope('avpool4_1'):
x4_1=tf.nn.avg_pool2d(x,ksize=[1,3,3,1],strides=[1,1,1,1],padding='SAME',name='avpool4_1')#确定池化的padding
with tf.name_scope('conv4_2'):
kernel4_2=tf.Variable(initial_value=tf.random.normal([1,1,n_in,32]))
bias4_2=tf.Variable(initial_value=tf.constant(0.0,shape=[32]))
conv4_2=tf.nn.conv2d(x4_1,kernel4_2,strides=[1,1,1,1],padding='SAME')
biases4_2=tf.nn.bias_add(conv4_2,bias4_2)
mean4_2,var4_2=tf.nn.moments(biases4_2,axes=[0,1,2])
bn4_2= tf.nn.batch_normalization(biases4_2,mean4_2,var4_2,offset=None,scale=None,variance_epsilon=1e-8)
x4_2=tf.nn.relu(bn4_2,name='conv4_2')
x4_out=x4_2
#将所有的输出进行连接
x_out=tf.concat([x1_out,x2_out,x3_out,x4_out],axis=-1)
return x_out
#构建模型的中Block1中的子模块2
def block1_model2(self,x):
n_in=x.get_shape()[-1]
with tf.name_scope('B1-M2'):
with tf.name_scope('S1'):
with tf.name_scope('conv1_1'):
kernel1_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,64]))
bias1_1=tf.Variable(initial_value=tf.constant(0.0,shape=[64]))
conv1_1=tf.nn.conv2d(x,kernel1_1,strides=[1,1,1,1], padding='SAME')
biases1_1=tf.nn.bias_add(conv1_1,bias1_1)
mean1_1,var1_1=tf.nn.moments(biases1_1,axes=[0,1,2])
bn1_1= tf.nn.batch_normalization(biases1_1,mean1_1,var1_1,offset=None,scale=None,variance_epsilon=1e-8)
x1_1=tf.nn.relu(bn1_1,name='conv1_1')
x1_out=x1_1
with tf.name_scope('S2'):
with tf.name_scope('conv2_1'):
kernel2_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,48]))
bias2_1=tf.Variable(initial_value=tf.constant(0.0,shape=[48]))
conv2_1=tf.nn.conv2d(x,kernel2_1,strides=[1,1,1,1],padding='SAME')
biases2_1=tf.nn.bias_add(conv2_1,bias2_1)
mean2_1,var2_1=tf.nn.moments(biases2_1,axes=[0,1,2])
bn2_1= tf.nn.batch_normalization(biases2_1,mean2_1,var2_1,offset=None,scale=None,variance_epsilon=1e-8)
x2_1=tf.nn.relu(bn2_1,name='conv2_1')
with tf.name_scope('conv2_2'):
kernel2_2=tf.Variable(initial_value=tf.random.normal([5,5,48,64]))
bias2_2=tf.Variable(initial_value=tf.constant(0.0,shape=[64]))
conv2_2=tf.nn.conv2d(x2_1,kernel2_2,strides=[1,1,1,1],padding='SAME')
biases2_2=tf.nn.bias_add(conv2_2,bias2_2)
mean2_2,var2_2=tf.nn.moments(biases2_2,axes=[0,1,2])
bn2_2= tf.nn.batch_normalization(biases2_2,mean2_2,var2_2,offset=None,scale=None,variance_epsilon=1e-8)
x2_2=tf.nn.relu(bn2_2,name='conv2_2')
x2_out=x2_2
with tf.name_scope('S3'):
with tf.name_scope('conv3_1'):
kernel3_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,64]))
bias3_1=tf.Variable(initial_value=tf.constant(0.0,shape=[64]))
conv3_1=tf.nn.conv2d(x,kernel3_1,strides=[1,1,1,1],padding='SAME')
biases3_1=tf.nn.bias_add(conv3_1,bias3_1)
mean3_1,var3_1=tf.nn.moments(biases3_1,axes=[0,1,2])
bn3_1= tf.nn.batch_normalization(biases3_1,mean3_1,var3_1,offset=None,scale=None,variance_epsilon=1e-8)
x3_1=tf.nn.relu(bn3_1,name='conv3_1')
with tf.name_scope('conv3_2'):
kernel3_2=tf.Variable(initial_value=tf.random.normal([3,3,64,96]))
bias3_2=tf.Variable(initial_value=tf.constant(0.0,shape=[96]))
conv3_2=tf.nn.conv2d(x3_1,kernel3_2,strides=[1,1,1,1],padding='SAME')
biases3_2=tf.nn.bias_add(conv3_2,bias3_2)
mean3_2,var3_2=tf.nn.moments(biases3_2,axes=[0,1,2])
bn3_2= tf.nn.batch_normalization(biases3_2,mean3_2,var3_2,offset=None,scale=None,variance_epsilon=1e-8)
x3_2=tf.nn.relu(bn3_2,name='conv3_2')
with tf.name_scope('conv3_3'):
kernel3_3=tf.Variable(initial_value=tf.random.normal([3,3,96,96]))
bias3_3=tf.Variable(initial_value=tf.constant(0.0,shape=[96]))
conv3_3=tf.nn.conv2d(x3_2,kernel3_3,strides=[1,1,1,1],padding='SAME')
biases3_3=tf.nn.bias_add(conv3_3,bias3_3)
mean3_3,var3_3=tf.nn.moments(biases3_3,axes=[0,1,2])
bn3_3= tf.nn.batch_normalization(biases3_3,mean3_3,var3_3,offset=None,scale=None,variance_epsilon=1e-8)
x3_3=tf.nn.relu(bn3_3,name='conv3_3')
x3_out=x3_3
with tf.name_scope('S4'):
with tf.name_scope('avpool4_1'):
x4_1=tf.nn.avg_pool2d(x,ksize=[1,3,3,1],strides=[1,1,1,1],padding='SAME',name='avpool4_1')#确定池化的padding
with tf.name_scope('conv4_2'):
kernel4_2=tf.Variable(initial_value=tf.random.normal([1,1,n_in,64]))
bias4_2=tf.Variable(initial_value=tf.constant(0.0,shape=[64]))
conv4_2=tf.nn.conv2d(x4_1,kernel4_2,strides=[1,1,1,1],padding='SAME')
biases4_2=tf.nn.bias_add(conv4_2,bias4_2)
mean4_2,var4_2=tf.nn.moments(biases4_2,axes=[0,1,2])
bn4_2= tf.nn.batch_normalization(biases4_2,mean4_2,var4_2,offset=None,scale=None,variance_epsilon=1e-8)
x4_2=tf.nn.relu(bn4_2,name='conv4_2')
x4_out=x4_2
#将所有的输出进行连接
x_out=tf.concat([x1_out,x2_out,x3_out,x4_out],axis=-1)
return x_out
#构建模型的中Block1中的子模块3
def block1_model3(self,x):
n_in=x.get_shape()[-1]
with tf.name_scope('B1-M3'):
with tf.name_scope('S1'):
with tf.name_scope('conv1_1'):
kernel1_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,64]))
bias1_1=tf.Variable(initial_value=tf.constant(0.0,shape=[64]))
conv1_1=tf.nn.conv2d(x,kernel1_1,strides=[1,1,1,1], padding='SAME')
biases1_1=tf.nn.bias_add(conv1_1,bias1_1)
mean1_1,var1_1=tf.nn.moments(biases1_1,axes=[0,1,2])
bn1_1= tf.nn.batch_normalization(biases1_1,mean1_1,var1_1,offset=None,scale=None,variance_epsilon=1e-8)
x1_1=tf.nn.relu(bn1_1,name='conv1_1')
x1_out=x1_1
with tf.name_scope('S2'):
with tf.name_scope('conv2_1'):
kernel2_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,48]))
bias2_1=tf.Variable(initial_value=tf.constant(0.0,shape=[48]))
conv2_1=tf.nn.conv2d(x,kernel2_1,strides=[1,1,1,1],padding='SAME')
biases2_1=tf.nn.bias_add(conv2_1,bias2_1)
mean2_1,var2_1=tf.nn.moments(biases2_1,axes=[0,1,2])
bn2_1= tf.nn.batch_normalization(biases2_1,mean2_1,var2_1,offset=None,scale=None,variance_epsilon=1e-8)
x2_1=tf.nn.relu(bn2_1,name='conv2_1')
with tf.name_scope('conv2_2'):
kernel2_2=tf.Variable(initial_value=tf.random.normal([5,5,48,64]))
bias2_2=tf.Variable(initial_value=tf.constant(0.0,shape=[64]))
conv2_2=tf.nn.conv2d(x2_1,kernel2_2,strides=[1,1,1,1],padding='SAME')
biases2_2=tf.nn.bias_add(conv2_2,bias2_2)
mean2_2,var2_2=tf.nn.moments(biases2_2,axes=[0,1,2])
bn2_2= tf.nn.batch_normalization(biases2_2,mean2_2,var2_2,offset=None,scale=None,variance_epsilon=1e-8)
x2_2=tf.nn.relu(bn2_2,name='conv2_2')
x2_out=x2_2
with tf.name_scope('S3'):
with tf.name_scope('conv3_1'):
kernel3_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,64]))
bias3_1=tf.Variable(initial_value=tf.constant(0.0,shape=[64]))
conv3_1=tf.nn.conv2d(x,kernel3_1,strides=[1,1,1,1],padding='SAME')
biases3_1=tf.nn.bias_add(conv3_1,bias3_1)
mean3_1,var3_1=tf.nn.moments(biases3_1,axes=[0,1,2])
bn3_1= tf.nn.batch_normalization(biases3_1,mean3_1,var3_1,offset=None,scale=None,variance_epsilon=1e-8)
x3_1=tf.nn.relu(bn3_1,name='conv3_1')
with tf.name_scope('conv3_2'):
kernel3_2=tf.Variable(initial_value=tf.random.normal([3,3,64,96]))
bias3_2=tf.Variable(initial_value=tf.constant(0.0,shape=[96]))
conv3_2=tf.nn.conv2d(x3_1,kernel3_2,strides=[1,1,1,1],padding='SAME')
biases3_2=tf.nn.bias_add(conv3_2,bias3_2)
mean3_2,var3_2=tf.nn.moments(biases3_2,axes=[0,1,2])
bn3_2= tf.nn.batch_normalization(biases3_2,mean3_2,var3_2,offset=None,scale=None,variance_epsilon=1e-8)
x3_2=tf.nn.relu(bn3_2,name='conv3_2')
with tf.name_scope('conv3_3'):
kernel3_3=tf.Variable(initial_value=tf.random.normal([3,3,96,96]))
bias3_3=tf.Variable(initial_value=tf.constant(0.0,shape=[96]))
conv3_3=tf.nn.conv2d(x3_2,kernel3_3,strides=[1,1,1,1],padding='SAME')
biases3_3=tf.nn.bias_add(conv3_3,bias3_3)
mean3_3,var3_3=tf.nn.moments(biases3_3,axes=[0,1,2])
bn3_3= tf.nn.batch_normalization(biases3_3,mean3_3,var3_3,offset=None,scale=None,variance_epsilon=1e-8)
x3_3=tf.nn.relu(bn3_3,name='conv3_3')
x3_out=x3_3
with tf.name_scope('S4'):
with tf.name_scope('avpool4_1'):
x4_1=tf.nn.avg_pool2d(x,ksize=[1,3,3,1],strides=[1,1,1,1],padding='SAME',name='avpool4_1')#确定池化的padding
with tf.name_scope('conv4_2'):
kernel4_2=tf.Variable(initial_value=tf.random.normal([1,1,n_in,64]))
bias4_2=tf.Variable(initial_value=tf.constant(0.0,shape=[64]))
conv4_2=tf.nn.conv2d(x4_1,kernel4_2,strides=[1,1,1,1],padding='SAME')
biases4_2=tf.nn.bias_add(conv4_2,bias4_2)
mean4_2,var4_2=tf.nn.moments(biases4_2,axes=[0,1,2])
bn4_2= tf.nn.batch_normalization(biases4_2,mean4_2,var4_2,offset=None,scale=None,variance_epsilon=1e-8)
x4_2=tf.nn.relu(bn4_2,name='conv4_2')
x4_out=x4_2
#将所有的输出进行连接
x_out=tf.concat([x1_out,x2_out,x3_out,x4_out],axis=-1)
return x_out
#第二个模块
class block2():
def __init__(self) -> None:
pass
#第二个模块的子模块1
def block2_model1(self, x):
n_in=x.get_shape()[-1]
with tf.name_scope('B2-M1'):
with tf.name_scope('S1'):
with tf.name_scope('conv1_1'):
kernel1_1=tf.Variable(initial_value=tf.random.normal([3,3,n_in,384]))
bias1_1=tf.Variable(initial_value=tf.constant(0.0,shape=[384]))
conv1_1=tf.nn.conv2d(x,kernel1_1,strides=[1,2,2,1], padding='VALID')
biases1_1=tf.nn.bias_add(conv1_1,bias1_1)
mean1_1,var1_1=tf.nn.moments(biases1_1,axes=[0,1,2])
bn1_1= tf.nn.batch_normalization(biases1_1,mean1_1,var1_1,offset=None,scale=None,variance_epsilon=1e-8)
x1_1=tf.nn.relu(bn1_1,name='conv1_1')
x1_out=x1_1
with tf.name_scope('S2'):
with tf.name_scope('conv2_1'):
kernel2_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,64]))
bias2_1=tf.Variable(initial_value=tf.constant(0.0,shape=[64]))
conv2_1=tf.nn.conv2d(x,kernel2_1,strides=[1,1,1,1],padding='SAME')
biases2_1=tf.nn.bias_add(conv2_1,bias2_1)
mean2_1,var2_1=tf.nn.moments(biases2_1,axes=[0,1,2])
bn2_1= tf.nn.batch_normalization(biases2_1,mean2_1,var2_1,offset=None,scale=None,variance_epsilon=1e-8)
x2_1=tf.nn.relu(bn2_1,name='conv2_1')
with tf.name_scope('conv2_2'):
kernel2_2=tf.Variable(initial_value=tf.random.normal([3,3,64,96]))
bias2_2=tf.Variable(initial_value=tf.constant(0.0,shape=[96]))
conv2_2=tf.nn.conv2d(x2_1,kernel2_2,strides=[1,1,1,1],padding='SAME')
biases2_2=tf.nn.bias_add(conv2_2,bias2_2)
mean2_2,var2_2=tf.nn.moments(biases2_2,axes=[0,1,2])
bn2_2= tf.nn.batch_normalization(biases2_2,mean2_2,var2_2,offset=None,scale=None,variance_epsilon=1e-8)
x2_2=tf.nn.relu(bn2_2,name='conv2_2')
with tf.name_scope('conv2_3'):
kernel2_3=tf.Variable(initial_value=tf.random.normal([3,3,96,96]))
bias2_3=tf.Variable(initial_value=tf.constant(0.0,shape=[96]))
conv2_3=tf.nn.conv2d(x2_2,kernel2_3,strides=[1,2,2,1],padding='VALID')
biases2_3=tf.nn.bias_add(conv2_3,bias2_3)
mean2_3,var2_3=tf.nn.moments(biases2_3,axes=[0,1,2])
bn2_3= tf.nn.batch_normalization(biases2_3,mean2_3,var2_3,offset=None,scale=None,variance_epsilon=1e-8)
x2_3=tf.nn.relu(bn2_3,name='conv2_3')
x2_out=x2_3
with tf.name_scope('S3'):
with tf.name_scope('avpool3_1'):
x3_1=tf.nn.max_pool2d(x,ksize=[1,3,3,1],strides=[1,2,2,1],padding='VALID',name='avpool3_1')#确定池化的padding
x3_out=x3_1
x_out=tf.concat([x1_out,x2_out,x3_out],axis=-1)
return x_out
#第二个模块的子模块2
def block2_model2(self, x):
n_in=x.get_shape()[-1]
with tf.name_scope('B2-M2'):
with tf.name_scope('S1'):
with tf.name_scope('conv1_1'):
kernel1_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,192]))
bias1_1=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv1_1=tf.nn.conv2d(x,kernel1_1,strides=[1,1,1,1], padding='SAME')
biases1_1=tf.nn.bias_add(conv1_1,bias1_1)
mean1_1,var1_1=tf.nn.moments(biases1_1,axes=[0,1,2])
bn1_1= tf.nn.batch_normalization(biases1_1,mean1_1,var1_1,offset=None,scale=None,variance_epsilon=1e-8)
x1_1=tf.nn.relu(bn1_1,name='conv1_1')
x1_out=x1_1
with tf.name_scope('S2'):
with tf.name_scope('conv2_1'):
kernel2_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,128]))
bias2_1=tf.Variable(initial_value=tf.constant(0.0,shape=[128]))
conv2_1=tf.nn.conv2d(x,kernel2_1,strides=[1,1,1,1],padding='SAME')
biases2_1=tf.nn.bias_add(conv2_1,bias2_1)
mean2_1,var2_1=tf.nn.moments(biases2_1,axes=[0,1,2])
bn2_1= tf.nn.batch_normalization(biases2_1,mean2_1,var2_1,offset=None,scale=None,variance_epsilon=1e-8)
x2_1=tf.nn.relu(bn2_1,name='conv2_1')
with tf.name_scope('conv2_2'):
kernel2_2=tf.Variable(initial_value=tf.random.normal([1,7,128,128]))
bias2_2=tf.Variable(initial_value=tf.constant(0.0,shape=[128]))
conv2_2=tf.nn.conv2d(x2_1,kernel2_2,strides=[1,1,1,1],padding='SAME')
biases2_2=tf.nn.bias_add(conv2_2,bias2_2)
mean2_2,var2_2=tf.nn.moments(biases2_2,axes=[0,1,2])
bn2_2= tf.nn.batch_normalization(biases2_2,mean2_2,var2_2,offset=None,scale=None,variance_epsilon=1e-8)
x2_2=tf.nn.relu(bn2_2,name='conv2_2')
with tf.name_scope('conv2_3'):
kernel2_3=tf.Variable(initial_value=tf.random.normal([7,1,128,192]))
bias2_3=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv2_3=tf.nn.conv2d(x2_2,kernel2_3,strides=[1,1,1,1],padding='SAME')
biases2_3=tf.nn.bias_add(conv2_3,bias2_3)
mean2_3,var2_3=tf.nn.moments(biases2_3,axes=[0,1,2])
bn2_3= tf.nn.batch_normalization(biases2_3,mean2_3,var2_3,offset=None,scale=None,variance_epsilon=1e-8)
x2_3=tf.nn.relu(bn2_3,name='conv2_3')
x2_out=x2_3
with tf.name_scope('S3'):
with tf.name_scope('conv3_1'):
kernel3_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,128]))
bias3_1=tf.Variable(initial_value=tf.constant(0.0,shape=[128]))
conv3_1=tf.nn.conv2d(x,kernel3_1,strides=[1,1,1,1],padding='SAME')
biases3_1=tf.nn.bias_add(conv3_1,bias3_1)
mean3_1,var3_1=tf.nn.moments(biases3_1,axes=[0,1,2])
bn3_1= tf.nn.batch_normalization(biases3_1,mean3_1,var3_1,offset=None,scale=None,variance_epsilon=1e-8)
x3_1=tf.nn.relu(bn3_1,name='conv3_1')
with tf.name_scope('conv3_2'):
kernel3_2=tf.Variable(initial_value=tf.random.normal([7,1,128,128]))
bias3_2=tf.Variable(initial_value=tf.constant(0.0,shape=[128]))
conv3_2=tf.nn.conv2d(x3_1,kernel3_2,strides=[1,1,1,1],padding='SAME')
biases3_2=tf.nn.bias_add(conv3_2,bias3_2)
mean3_2,var3_2=tf.nn.moments(biases3_2,axes=[0,1,2])
bn3_2= tf.nn.batch_normalization(biases3_2,mean3_2,var3_2,offset=None,scale=None,variance_epsilon=1e-8)
x3_2=tf.nn.relu(bn3_2,name='conv3_2')
with tf.name_scope('conv3_3'):
kernel3_3=tf.Variable(initial_value=tf.random.normal([1,7,128,128]))
bias3_3=tf.Variable(initial_value=tf.constant(0.0,shape=[128]))
conv3_3=tf.nn.conv2d(x3_2,kernel3_3,strides=[1,1,1,1],padding='SAME')
biases3_3=tf.nn.bias_add(conv3_3,bias3_3)
mean3_3,var3_3=tf.nn.moments(biases3_3,axes=[0,1,2])
bn3_3= tf.nn.batch_normalization(biases3_3,mean3_3,var3_3,offset=None,scale=None,variance_epsilon=1e-8)
x3_3=tf.nn.relu(bn3_3,name='conv3_3')
with tf.name_scope('conv3_4'):
kernel3_4=tf.Variable(initial_value=tf.random.normal([7,1,128,128]))
bias3_4=tf.Variable(initial_value=tf.constant(0.0,shape=[128]))
conv3_4=tf.nn.conv2d(x3_3,kernel3_4,strides=[1,1,1,1],padding='SAME')
biases3_4=tf.nn.bias_add(conv3_4,bias3_4)
mean3_4,var3_4=tf.nn.moments(biases3_4,axes=[0,1,2])
bn3_4= tf.nn.batch_normalization(biases3_4,mean3_4,var3_4,offset=None,scale=None,variance_epsilon=1e-8)
x3_4=tf.nn.relu(bn3_4,name='conv3_4')
with tf.name_scope('conv3_5'):
kernel3_5=tf.Variable(initial_value=tf.random.normal([1,7,128,192]))
bias3_5=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv3_5=tf.nn.conv2d(x3_4,kernel3_5,strides=[1,1,1,1],padding='SAME')
biases3_5=tf.nn.bias_add(conv3_5,bias3_5)
mean3_5,var3_5=tf.nn.moments(biases3_5,axes=[0,1,2])
bn3_5= tf.nn.batch_normalization(biases3_5,mean3_5,var3_5,offset=None,scale=None,variance_epsilon=1e-8)
x3_5=tf.nn.relu(bn3_5,name='conv3_5')
x3_out=x3_5
with tf.name_scope('S4'):
with tf.name_scope('avpool4_1'):
x4_1=tf.nn.avg_pool2d(x,ksize=[1,3,3,1],strides=[1,1,1,1],padding='SAME',name='avpool4_1')#确定池化的padding
with tf.name_scope('conv4_2'):
kernel4_2=tf.Variable(initial_value=tf.random.normal([1,1,n_in,192]))
bias4_2=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv4_2=tf.nn.conv2d(x4_1,kernel4_2,strides=[1,1,1,1],padding='SAME')
biases4_2=tf.nn.bias_add(conv4_2,bias4_2)
mean4_2,var4_2=tf.nn.moments(biases4_2,axes=[0,1,2])
bn4_2= tf.nn.batch_normalization(biases4_2,mean4_2,var4_2,offset=None,scale=None,variance_epsilon=1e-8)
x4_2=tf.nn.relu(bn4_2,name='conv4_2')
x4_out=x4_2
#将所有的输出进行连接
x_out=tf.concat([x1_out,x2_out,x3_out,x4_out],axis=-1)
return x_out
#第二个模块的子模块3和4(一样,只需要执行两次就行)
def block2_model34(self, x):
n_in=x.get_shape()[-1]
with tf.name_scope('B2-M34'):
with tf.name_scope('S1'):
with tf.name_scope('conv1_1'):
kernel1_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,192]))
bias1_1=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv1_1=tf.nn.conv2d(x,kernel1_1,strides=[1,1,1,1], padding='SAME')
biases1_1=tf.nn.bias_add(conv1_1,bias1_1)
mean1_1,var1_1=tf.nn.moments(biases1_1,axes=[0,1,2])
bn1_1= tf.nn.batch_normalization(biases1_1,mean1_1,var1_1,offset=None,scale=None,variance_epsilon=1e-8)
x1_1=tf.nn.relu(bn1_1,name='conv1_1')
x1_out=x1_1
with tf.name_scope('S2'):
with tf.name_scope('conv2_1'):
kernel2_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,160]))
bias2_1=tf.Variable(initial_value=tf.constant(0.0,shape=[160]))
conv2_1=tf.nn.conv2d(x,kernel2_1,strides=[1,1,1,1],padding='SAME')
biases2_1=tf.nn.bias_add(conv2_1,bias2_1)
mean2_1,var2_1=tf.nn.moments(biases2_1,axes=[0,1,2])
bn2_1= tf.nn.batch_normalization(biases2_1,mean2_1,var2_1,offset=None,scale=None,variance_epsilon=1e-8)
x2_1=tf.nn.relu(bn2_1,name='conv2_1')
with tf.name_scope('conv2_2'):
kernel2_2=tf.Variable(initial_value=tf.random.normal([1,7,160,160]))
bias2_2=tf.Variable(initial_value=tf.constant(0.0,shape=[160]))
conv2_2=tf.nn.conv2d(x2_1,kernel2_2,strides=[1,1,1,1],padding='SAME')
biases2_2=tf.nn.bias_add(conv2_2,bias2_2)
mean2_2,var2_2=tf.nn.moments(biases2_2,axes=[0,1,2])
bn2_2= tf.nn.batch_normalization(biases2_2,mean2_2,var2_2,offset=None,scale=None,variance_epsilon=1e-8)
x2_2=tf.nn.relu(bn2_2,name='conv2_2')
with tf.name_scope('conv2_3'):
kernel2_3=tf.Variable(initial_value=tf.random.normal([7,1,160,192]))
bias2_3=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv2_3=tf.nn.conv2d(x2_2,kernel2_3,strides=[1,1,1,1],padding='SAME')
biases2_3=tf.nn.bias_add(conv2_3,bias2_3)
mean2_3,var2_3=tf.nn.moments(biases2_3,axes=[0,1,2])
bn2_3= tf.nn.batch_normalization(biases2_3,mean2_3,var2_3,offset=None,scale=None,variance_epsilon=1e-8)
x2_3=tf.nn.relu(bn2_3,name='conv2_3')
x2_out=x2_3
with tf.name_scope('S3'):
with tf.name_scope('conv3_1'):
kernel3_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,160]))
bias3_1=tf.Variable(initial_value=tf.constant(0.0,shape=[160]))
conv3_1=tf.nn.conv2d(x,kernel3_1,strides=[1,1,1,1],padding='SAME')
biases3_1=tf.nn.bias_add(conv3_1,bias3_1)
mean3_1,var3_1=tf.nn.moments(biases3_1,axes=[0,1,2])
bn3_1= tf.nn.batch_normalization(biases3_1,mean3_1,var3_1,offset=None,scale=None,variance_epsilon=1e-8)
x3_1=tf.nn.relu(bn3_1,name='conv3_1')
with tf.name_scope('conv3_2'):
kernel3_2=tf.Variable(initial_value=tf.random.normal([7,1,160,160]))
bias3_2=tf.Variable(initial_value=tf.constant(0.0,shape=[160]))
conv3_2=tf.nn.conv2d(x3_1,kernel3_2,strides=[1,1,1,1],padding='SAME')
biases3_2=tf.nn.bias_add(conv3_2,bias3_2)
mean3_2,var3_2=tf.nn.moments(biases3_2,axes=[0,1,2])
bn3_2= tf.nn.batch_normalization(biases3_2,mean3_2,var3_2,offset=None,scale=None,variance_epsilon=1e-8)
x3_2=tf.nn.relu(bn3_2,name='conv3_2')
with tf.name_scope('conv3_3'):
kernel3_3=tf.Variable(initial_value=tf.random.normal([1,7,160,160]))
bias3_3=tf.Variable(initial_value=tf.constant(0.0,shape=[160]))
conv3_3=tf.nn.conv2d(x3_2,kernel3_3,strides=[1,1,1,1],padding='SAME')
biases3_3=tf.nn.bias_add(conv3_3,bias3_3)
mean3_3,var3_3=tf.nn.moments(biases3_3,axes=[0,1,2])
bn3_3= tf.nn.batch_normalization(biases3_3,mean3_3,var3_3,offset=None,scale=None,variance_epsilon=1e-8)
x3_3=tf.nn.relu(bn3_3,name='conv3_3')
with tf.name_scope('conv3_4'):
kernel3_4=tf.Variable(initial_value=tf.random.normal([7,1,160,160]))
bias3_4=tf.Variable(initial_value=tf.constant(0.0,shape=[160]))
conv3_4=tf.nn.conv2d(x3_3,kernel3_4,strides=[1,1,1,1],padding='SAME')
biases3_4=tf.nn.bias_add(conv3_4,bias3_4)
mean3_4,var3_4=tf.nn.moments(biases3_4,axes=[0,1,2])
bn3_4= tf.nn.batch_normalization(biases3_4,mean3_4,var3_4,offset=None,scale=None,variance_epsilon=1e-8)
x3_4=tf.nn.relu(bn3_4,name='conv3_4')
with tf.name_scope('conv3_5'):
kernel3_5=tf.Variable(initial_value=tf.random.normal([1,7,160,192]))
bias3_5=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv3_5=tf.nn.conv2d(x3_4,kernel3_5,strides=[1,1,1,1],padding='SAME')
biases3_5=tf.nn.bias_add(conv3_5,bias3_5)
mean3_5,var3_5=tf.nn.moments(biases3_5,axes=[0,1,2])
bn3_5= tf.nn.batch_normalization(biases3_5,mean3_5,var3_5,offset=None,scale=None,variance_epsilon=1e-8)
x3_5=tf.nn.relu(bn3_5,name='conv3_5')
x3_out=x3_5
with tf.name_scope('S4'):
with tf.name_scope('avpool4_1'):
x4_1=tf.nn.avg_pool2d(x,ksize=[1,3,3,1],strides=[1,1,1,1],padding='SAME',name='avpool4_1')#确定池化的padding
with tf.name_scope('conv4_2'):
kernel4_2=tf.Variable(initial_value=tf.random.normal([1,1,n_in,192]))
bias4_2=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv4_2=tf.nn.conv2d(x4_1,kernel4_2,strides=[1,1,1,1],padding='SAME')
biases4_2=tf.nn.bias_add(conv4_2,bias4_2)
mean4_2,var4_2=tf.nn.moments(biases4_2,axes=[0,1,2])
bn4_2= tf.nn.batch_normalization(biases4_2,mean4_2,var4_2,offset=None,scale=None,variance_epsilon=1e-8)
x4_2=tf.nn.relu(bn4_2,name='conv4_2')
x4_out=x4_2
#将所有的输出进行连接
x_out=tf.concat([x1_out,x2_out,x3_out,x4_out],axis=-1)
return x_out
#第二个模块的子模块5
def block2_model5(self, x):
n_in=x.get_shape()[-1]
with tf.name_scope('B2-M5'):
with tf.name_scope('S1'):
with tf.name_scope('conv1_1'):
kernel1_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,192]))
bias1_1=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv1_1=tf.nn.conv2d(x,kernel1_1,strides=[1,1,1,1], padding='SAME')
biases1_1=tf.nn.bias_add(conv1_1,bias1_1)
mean1_1,var1_1=tf.nn.moments(biases1_1,axes=[0,1,2])
bn1_1= tf.nn.batch_normalization(biases1_1,mean1_1,var1_1,offset=None,scale=None,variance_epsilon=1e-8)
x1_1=tf.nn.relu(bn1_1,name='conv1_1')
x1_out=x1_1
with tf.name_scope('S2'):
with tf.name_scope('conv2_1'):
kernel2_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,192]))
bias2_1=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv2_1=tf.nn.conv2d(x,kernel2_1,strides=[1,1,1,1],padding='SAME')
biases2_1=tf.nn.bias_add(conv2_1,bias2_1)
mean2_1,var2_1=tf.nn.moments(biases2_1,axes=[0,1,2])
bn2_1= tf.nn.batch_normalization(biases2_1,mean2_1,var2_1,offset=None,scale=None,variance_epsilon=1e-8)
x2_1=tf.nn.relu(bn2_1,name='conv2_1')
with tf.name_scope('conv2_2'):
kernel2_2=tf.Variable(initial_value=tf.random.normal([1,7,192,192]))
bias2_2=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv2_2=tf.nn.conv2d(x2_1,kernel2_2,strides=[1,1,1,1],padding='SAME')
biases2_2=tf.nn.bias_add(conv2_2,bias2_2)
mean2_2,var2_2=tf.nn.moments(biases2_2,axes=[0,1,2])
bn2_2= tf.nn.batch_normalization(biases2_2,mean2_2,var2_2,offset=None,scale=None,variance_epsilon=1e-8)
x2_2=tf.nn.relu(bn2_2,name='conv2_2')
with tf.name_scope('conv2_3'):
kernel2_3=tf.Variable(initial_value=tf.random.normal([7,1,192,192]))
bias2_3=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv2_3=tf.nn.conv2d(x2_2,kernel2_3,strides=[1,1,1,1],padding='SAME')
biases2_3=tf.nn.bias_add(conv2_3,bias2_3)
mean2_3,var2_3=tf.nn.moments(biases2_3,axes=[0,1,2])
bn2_3= tf.nn.batch_normalization(biases2_3,mean2_3,var2_3,offset=None,scale=None,variance_epsilon=1e-8)
x2_3=tf.nn.relu(bn2_3,name='conv2_3')
x2_out=x2_3
with tf.name_scope('S3'):
with tf.name_scope('conv3_1'):
kernel3_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,192]))
bias3_1=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv3_1=tf.nn.conv2d(x,kernel3_1,strides=[1,1,1,1],padding='SAME')
biases3_1=tf.nn.bias_add(conv3_1,bias3_1)
mean3_1,var3_1=tf.nn.moments(biases3_1,axes=[0,1,2])
bn3_1= tf.nn.batch_normalization(biases3_1,mean3_1,var3_1,offset=None,scale=None,variance_epsilon=1e-8)
x3_1=tf.nn.relu(bn3_1,name='conv3_1')
with tf.name_scope('conv3_2'):
kernel3_2=tf.Variable(initial_value=tf.random.normal([7,1,192,192]))
bias3_2=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv3_2=tf.nn.conv2d(x3_1,kernel3_2,strides=[1,1,1,1],padding='SAME')
biases3_2=tf.nn.bias_add(conv3_2,bias3_2)
mean3_2,var3_2=tf.nn.moments(biases3_2,axes=[0,1,2])
bn3_2= tf.nn.batch_normalization(biases3_2,mean3_2,var3_2,offset=None,scale=None,variance_epsilon=1e-8)
x3_2=tf.nn.relu(bn3_2,name='conv3_2')
with tf.name_scope('conv3_3'):
kernel3_3=tf.Variable(initial_value=tf.random.normal([1,7,192,192]))
bias3_3=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv3_3=tf.nn.conv2d(x3_2,kernel3_3,strides=[1,1,1,1],padding='SAME')
biases3_3=tf.nn.bias_add(conv3_3,bias3_3)
mean3_3,var3_3=tf.nn.moments(biases3_3,axes=[0,1,2])
bn3_3= tf.nn.batch_normalization(biases3_3,mean3_3,var3_3,offset=None,scale=None,variance_epsilon=1e-8)
x3_3=tf.nn.relu(bn3_3,name='conv3_3')
with tf.name_scope('conv3_4'):
kernel3_4=tf.Variable(initial_value=tf.random.normal([7,1,192,192]))
bias3_4=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv3_4=tf.nn.conv2d(x3_3,kernel3_4,strides=[1,1,1,1],padding='SAME')
biases3_4=tf.nn.bias_add(conv3_4,bias3_4)
mean3_4,var3_4=tf.nn.moments(biases3_4,axes=[0,1,2])
bn3_4= tf.nn.batch_normalization(biases3_4,mean3_4,var3_4,offset=None,scale=None,variance_epsilon=1e-8)
x3_4=tf.nn.relu(bn3_4,name='conv3_4')
with tf.name_scope('conv3_5'):
kernel3_5=tf.Variable(initial_value=tf.random.normal([1,7,192,192]))
bias3_5=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv3_5=tf.nn.conv2d(x3_4,kernel3_5,strides=[1,1,1,1],padding='SAME')
biases3_5=tf.nn.bias_add(conv3_5,bias3_5)
mean3_5,var3_5=tf.nn.moments(biases3_5,axes=[0,1,2])
bn3_5= tf.nn.batch_normalization(biases3_5,mean3_5,var3_5,offset=None,scale=None,variance_epsilon=1e-8)
x3_5=tf.nn.relu(bn3_5,name='conv3_5')
x3_out=x3_5
with tf.name_scope('S4'):
with tf.name_scope('avpool4_1'):
x4_1=tf.nn.avg_pool2d(x,ksize=[1,3,3,1],strides=[1,1,1,1],padding='SAME',name='avpool4_1')#确定池化的padding
with tf.name_scope('conv4_2'):
kernel4_2=tf.Variable(initial_value=tf.random.normal([1,1,n_in,192]))
bias4_2=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv4_2=tf.nn.conv2d(x4_1,kernel4_2,strides=[1,1,1,1],padding='VALID')
biases4_2=tf.nn.bias_add(conv4_2,bias4_2)
mean4_2,var4_2=tf.nn.moments(biases4_2,axes=[0,1,2])
bn4_2= tf.nn.batch_normalization(biases4_2,mean4_2,var4_2,offset=None,scale=None,variance_epsilon=1e-8)
x4_2=tf.nn.relu(bn4_2,name='conv4_2')
x4_out=x4_2
#将所有的输出进行连接
x_out=tf.concat([x1_out,x2_out,x3_out,x4_out],axis=-1)
return x_out
#第三个模块
class block3():
def __init__(self) -> None:
pass
#第三个模块的子模块1
def block3_model1(self, x):
n_in=x.get_shape()[-1]
with tf.name_scope('B3-M1'):
with tf.name_scope('S1'):
with tf.name_scope('conv1_1'):
kernel1_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,192]))
bias1_1=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv1_1=tf.nn.conv2d(x,kernel1_1,strides=[1,1,1,1], padding='SAME')
biases1_1=tf.nn.bias_add(conv1_1,bias1_1)
mean1_1,var1_1=tf.nn.moments(biases1_1,axes=[0,1,2])
bn1_1= tf.nn.batch_normalization(biases1_1,mean1_1,var1_1,offset=None,scale=None,variance_epsilon=1e-8)
x1_1=tf.nn.relu(bn1_1,name='conv1_1')
with tf.name_scope('conv1_2'):
kernel1_2=tf.Variable(initial_value=tf.random.normal([3,3,192,320]))
bias1_2=tf.Variable(initial_value=tf.constant(0.0,shape=[320]))
conv1_2=tf.nn.conv2d(x1_1,kernel1_2,strides=[1,2,2,1], padding='VALID')
biases1_2=tf.nn.bias_add(conv1_2,bias1_2)
mean1_2,var1_2=tf.nn.moments(biases1_2,axes=[0,1,2])
bn1_2= tf.nn.batch_normalization(biases1_2,mean1_2,var1_2,offset=None,scale=None,variance_epsilon=1e-8)
x1_2=tf.nn.relu(bn1_2,name='conv1_2')
x1_out=x1_2
with tf.name_scope('S2'):
with tf.name_scope('conv2_1'):
kernel2_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,192]))
bias2_1=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv2_1=tf.nn.conv2d(x,kernel2_1,strides=[1,1,1,1],padding='SAME')
biases2_1=tf.nn.bias_add(conv2_1,bias2_1)
mean2_1,var2_1=tf.nn.moments(biases2_1,axes=[0,1,2])
bn2_1= tf.nn.batch_normalization(biases2_1,mean2_1,var2_1,offset=None,scale=None,variance_epsilon=1e-8)
x2_1=tf.nn.relu(bn2_1,name='conv2_1')
with tf.name_scope('conv2_2'):
kernel2_2=tf.Variable(initial_value=tf.random.normal([1,7,192,192]))
bias2_2=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv2_2=tf.nn.conv2d(x2_1,kernel2_2,strides=[1,1,1,1],padding='SAME')
biases2_2=tf.nn.bias_add(conv2_2,bias2_2)
mean2_2,var2_2=tf.nn.moments(biases2_2,axes=[0,1,2])
bn2_2= tf.nn.batch_normalization(biases2_2,mean2_2,var2_2,offset=None,scale=None,variance_epsilon=1e-8)
x2_2=tf.nn.relu(bn2_2,name='conv2_2')
with tf.name_scope('conv2_3'):
kernel2_3=tf.Variable(initial_value=tf.random.normal([7,1,192,192]))
bias2_3=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv2_3=tf.nn.conv2d(x2_2,kernel2_3,strides=[1,1,1,1],padding='SAME')
biases2_3=tf.nn.bias_add(conv2_3,bias2_3)
mean2_3,var2_3=tf.nn.moments(biases2_3,axes=[0,1,2])
bn2_3= tf.nn.batch_normalization(biases2_3,mean2_3,var2_3,offset=None,scale=None,variance_epsilon=1e-8)
x2_3=tf.nn.relu(bn2_3,name='conv2_3')
with tf.name_scope('conv2_4'):
kernel2_4=tf.Variable(initial_value=tf.random.normal([3,3,192,192]))
bias2_4=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv2_4=tf.nn.conv2d(x2_3,kernel2_4,strides=[1,2,2,1],padding='VALID')
biases2_4=tf.nn.bias_add(conv2_4,bias2_4)
mean2_4,var2_4=tf.nn.moments(biases2_4,axes=[0,1,2])
bn2_4= tf.nn.batch_normalization(biases2_4,mean2_4,var2_4,offset=None,scale=None,variance_epsilon=1e-8)
x2_4=tf.nn.relu(bn2_4,name='conv2_4')
x2_out=x2_4
with tf.name_scope('S3'):
with tf.name_scope('avpool3_1'):
x3_1=tf.nn.max_pool2d(x,ksize=[1,3,3,1],strides=[1,2,2,1],padding='VALID',name='avpool3_1')#确定池化的padding
x3_out=x3_1
x_out=tf.concat([x1_out,x2_out,x3_out],axis=-1)
return x_out
#定义第三个模块的子模块2
def block3_model2(self, x):
n_in=x.get_shape()[-1]
with tf.name_scope('B3-M2'):
with tf.name_scope('S1'):
with tf.name_scope('conv1_1'):
kernel1_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,320]))
bias1_1=tf.Variable(initial_value=tf.constant(0.0,shape=[320]))
conv1_1=tf.nn.conv2d(x,kernel1_1,strides=[1,1,1,1], padding='SAME')
biases1_1=tf.nn.bias_add(conv1_1,bias1_1)
mean1_1,var1_1=tf.nn.moments(biases1_1,axes=[0,1,2])
bn1_1= tf.nn.batch_normalization(biases1_1,mean1_1,var1_1,offset=None,scale=None,variance_epsilon=1e-8)
x1_1=tf.nn.relu(bn1_1,name='conv1_1')
x1_out=x1_1
with tf.name_scope('S2'):
with tf.name_scope('conv2_1'):
kernel2_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,384]))
bias2_1=tf.Variable(initial_value=tf.constant(0.0,shape=[384]))
conv2_1=tf.nn.conv2d(x,kernel2_1,strides=[1,1,1,1], padding='SAME')
biases2_1=tf.nn.bias_add(conv2_1,bias2_1)
mean2_1,var2_1=tf.nn.moments(biases2_1,axes=[0,1,2])
bn2_1= tf.nn.batch_normalization(biases2_1,mean2_1,var2_1,offset=None,scale=None,variance_epsilon=1e-8)
x2_1=tf.nn.relu(bn2_1,name='conv2_1')
with tf.name_scope('conv2_2_branch1'):
kernel2_2_1=tf.Variable(initial_value=tf.random.normal([1,3,384,384]))
bias2_2_1=tf.Variable(initial_value=tf.constant(0.0,shape=[384]))
conv2_2_1=tf.nn.conv2d(x2_1,kernel2_2_1,strides=[1,1,1,1], padding='SAME')
biases2_2_1=tf.nn.bias_add(conv2_2_1,bias2_2_1)
mean2_2_1,var2_2_1=tf.nn.moments(biases2_2_1,axes=[0,1,2])
bn2_2_1= tf.nn.batch_normalization(biases2_2_1,mean2_2_1,var2_2_1,offset=None,scale=None,variance_epsilon=1e-8)
x2_2_1=tf.nn.relu(bn2_2_1,name='conv2_2_branch1')
with tf.name_scope('conv2_2_branch2'):
kernel2_2_2=tf.Variable(initial_value=tf.random.normal([3,1,384,384]))
bias2_2_2=tf.Variable(initial_value=tf.constant(0.0,shape=[384]))
conv2_2_2=tf.nn.conv2d(x2_1,kernel2_2_2,strides=[1,1,1,1], padding='SAME')
biases2_2_2=tf.nn.bias_add(conv2_2_2,bias2_2_2)
mean2_2_2,var2_2_2=tf.nn.moments(biases2_2_2,axes=[0,1,2])
bn2_2_2= tf.nn.batch_normalization(biases2_2_2,mean2_2_2,var2_2_2,offset=None,scale=None,variance_epsilon=1e-8)
x2_2_2=tf.nn.relu(bn2_2_2,name='conv2_2_branch2')
x2_out=tf.concat([x2_2_1,x2_2_2],axis=-1)
with tf.name_scope('S3'):
with tf.name_scope('conv3_1'):
kernel3_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,448]))
bias3_1=tf.Variable(initial_value=tf.constant(0.0,shape=[448]))
conv3_1=tf.nn.conv2d(x,kernel3_1,strides=[1,1,1,1], padding='SAME')
biases3_1=tf.nn.bias_add(conv3_1,bias3_1)
mean3_1,var3_1=tf.nn.moments(biases3_1,axes=[0,1,2])
bn3_1= tf.nn.batch_normalization(biases3_1,mean3_1,var3_1,offset=None,scale=None,variance_epsilon=1e-8)
x3_1=tf.nn.relu(bn3_1,name='conv3_1')
with tf.name_scope('conv3_2'):
kernel3_2=tf.Variable(initial_value=tf.random.normal([3,3,448,384]))
bias3_2=tf.Variable(initial_value=tf.constant(0.0,shape=[384]))
conv3_2=tf.nn.conv2d(x3_1,kernel3_2,strides=[1,1,1,1], padding='SAME')
biases3_2=tf.nn.bias_add(conv3_2,bias3_2)
mean3_2,var3_2=tf.nn.moments(biases3_2,axes=[0,1,2])
bn3_2= tf.nn.batch_normalization(biases3_2,mean3_2,var3_2,offset=None,scale=None,variance_epsilon=1e-8)
x3_2=tf.nn.relu(bn3_2,name='conv3_2')
with tf.name_scope('conv3_3_branch1'):
kernel3_3_1=tf.Variable(initial_value=tf.random.normal([1,3,384,384]))
bias3_3_1=tf.Variable(initial_value=tf.constant(0.0,shape=[384]))
conv3_3_1=tf.nn.conv2d(x3_2,kernel3_3_1,strides=[1,1,1,1], padding='SAME')
biases3_3_1=tf.nn.bias_add(conv3_3_1,bias3_3_1)
mean3_3_1,var3_3_1=tf.nn.moments(biases3_3_1,axes=[0,1,2])
bn3_3_1= tf.nn.batch_normalization(biases3_3_1,mean3_3_1,var3_3_1,offset=None,scale=None,variance_epsilon=1e-8)
x3_3_1=tf.nn.relu(bn3_3_1,name='conv3_3_branch1')
with tf.name_scope('conv3_3_branch2'):
kernel3_3_2=tf.Variable(initial_value=tf.random.normal([3,1,384,384]))
bias3_3_2=tf.Variable(initial_value=tf.constant(0.0,shape=[384]))
conv3_3_2=tf.nn.conv2d(x3_2,kernel3_3_2,strides=[1,1,1,1], padding='SAME')
biases3_3_2=tf.nn.bias_add(conv3_3_2,bias3_3_2)
mean3_3_2,var3_3_2=tf.nn.moments(biases3_3_2,axes=[0,1,2])
bn3_3_2= tf.nn.batch_normalization(biases3_3_2,mean3_3_2,var3_3_2,offset=None,scale=None,variance_epsilon=1e-8)
x3_3_2=tf.nn.relu(bn3_3_2,name='conv3_3_branch2')
x3_out=tf.concat([x3_3_1,x3_3_2],axis=-1)
with tf.name_scope('S4'):
with tf.name_scope('avpool4_1'):
x4_1=tf.nn.avg_pool2d(x,ksize=[1,3,3,1],strides=[1,1,1,1],padding='SAME',name='avpool4_1')#确定池化的padding
with tf.name_scope('conv4_2'):
kernel4_2=tf.Variable(initial_value=tf.random.normal([1,1,n_in,192]))
bias4_2=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv4_2=tf.nn.conv2d(x4_1,kernel4_2,strides=[1,1,1,1],padding='SAME')
biases4_2=tf.nn.bias_add(conv4_2,bias4_2)
mean4_2,var4_2=tf.nn.moments(biases4_2,axes=[0,1,2])
bn4_2= tf.nn.batch_normalization(biases4_2,mean4_2,var4_2,offset=None,scale=None,variance_epsilon=1e-8)
x4_2=tf.nn.relu(bn4_2,name='conv4_2')
x4_out=x4_2
x_out=tf.concat([x1_out,x2_out,x3_out,x4_out],axis=-1)
return x_out
#定义第三个模块的子模块3
def block3_model3(self, x):
n_in=x.get_shape()[-1]
with tf.name_scope('B3-M3'):
with tf.name_scope('S1'):
with tf.name_scope('conv1_1'):
kernel1_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,320]))
bias1_1=tf.Variable(initial_value=tf.constant(0.0,shape=[320]))
conv1_1=tf.nn.conv2d(x,kernel1_1,strides=[1,1,1,1], padding='SAME')
biases1_1=tf.nn.bias_add(conv1_1,bias1_1)
mean1_1,var1_1=tf.nn.moments(biases1_1,axes=[0,1,2])
bn1_1= tf.nn.batch_normalization(biases1_1,mean1_1,var1_1,offset=None,scale=None,variance_epsilon=1e-8)
x1_1=tf.nn.relu(bn1_1,name='conv1_1')
x1_out=x1_1
with tf.name_scope('S2'):
with tf.name_scope('conv2_1'):
kernel2_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,384]))
bias2_1=tf.Variable(initial_value=tf.constant(0.0,shape=[384]))
conv2_1=tf.nn.conv2d(x,kernel2_1,strides=[1,1,1,1], padding='SAME')
biases2_1=tf.nn.bias_add(conv2_1,bias2_1)
mean2_1,var2_1=tf.nn.moments(biases2_1,axes=[0,1,2])
bn2_1= tf.nn.batch_normalization(biases2_1,mean2_1,var2_1,offset=None,scale=None,variance_epsilon=1e-8)
x2_1=tf.nn.relu(bn2_1,name='conv2_1')
with tf.name_scope('conv2_2_branch1'):
kernel2_2_1=tf.Variable(initial_value=tf.random.normal([1,3,384,384]))
bias2_2_1=tf.Variable(initial_value=tf.constant(0.0,shape=[384]))
conv2_2_1=tf.nn.conv2d(x2_1,kernel2_2_1,strides=[1,1,1,1], padding='SAME')
biases2_2_1=tf.nn.bias_add(conv2_2_1,bias2_2_1)
mean2_2_1,var2_2_1=tf.nn.moments(biases2_2_1,axes=[0,1,2])
bn2_2_1= tf.nn.batch_normalization(biases2_2_1,mean2_2_1,var2_2_1,offset=None,scale=None,variance_epsilon=1e-8)
x2_2_1=tf.nn.relu(bn2_2_1,name='conv2_2_branch1')
with tf.name_scope('conv2_2_branch2'):
kernel2_2_2=tf.Variable(initial_value=tf.random.normal([3,1,384,384]))
bias2_2_2=tf.Variable(initial_value=tf.constant(0.0,shape=[384]))
conv2_2_2=tf.nn.conv2d(x2_1,kernel2_2_2,strides=[1,1,1,1], padding='SAME')
biases2_2_2=tf.nn.bias_add(conv2_2_2,bias2_2_2)
mean2_2_2,var2_2_2=tf.nn.moments(biases2_2_2,axes=[0,1,2])
bn2_2_2= tf.nn.batch_normalization(biases2_2_2,mean2_2_2,var2_2_2,offset=None,scale=None,variance_epsilon=1e-8)
x2_2_2=tf.nn.relu(bn2_2_2,name='conv2_2_branch2')
x2_out=tf.concat([x2_2_1,x2_2_2],axis=-1)
with tf.name_scope('S3'):
with tf.name_scope('conv3_1'):
kernel3_1=tf.Variable(initial_value=tf.random.normal([1,1,n_in,448]))
bias3_1=tf.Variable(initial_value=tf.constant(0.0,shape=[448]))
conv3_1=tf.nn.conv2d(x,kernel3_1,strides=[1,1,1,1], padding='SAME')
biases3_1=tf.nn.bias_add(conv3_1,bias3_1)
mean3_1,var3_1=tf.nn.moments(biases3_1,axes=[0,1,2])
bn3_1= tf.nn.batch_normalization(biases3_1,mean3_1,var3_1,offset=None,scale=None,variance_epsilon=1e-8)
x3_1=tf.nn.relu(bn3_1,name='conv3_1')
with tf.name_scope('conv3_2'):
kernel3_2=tf.Variable(initial_value=tf.random.normal([3,3,448,384]))
bias3_2=tf.Variable(initial_value=tf.constant(0.0,shape=[384]))
conv3_2=tf.nn.conv2d(x3_1,kernel3_2,strides=[1,1,1,1], padding='SAME')
biases3_2=tf.nn.bias_add(conv3_2,bias3_2)
mean3_2,var3_2=tf.nn.moments(biases3_2,axes=[0,1,2])
bn3_2= tf.nn.batch_normalization(biases3_2,mean3_2,var3_2,offset=None,scale=None,variance_epsilon=1e-8)
x3_2=tf.nn.relu(bn3_2,name='conv3_2')
with tf.name_scope('conv3_3_branch1'):
kernel3_3_1=tf.Variable(initial_value=tf.random.normal([1,3,384,384]))
bias3_3_1=tf.Variable(initial_value=tf.constant(0.0,shape=[384]))
conv3_3_1=tf.nn.conv2d(x3_2,kernel3_3_1,strides=[1,1,1,1], padding='SAME')
biases3_3_1=tf.nn.bias_add(conv3_3_1,bias3_3_1)
mean3_3_1,var3_3_1=tf.nn.moments(biases3_3_1,axes=[0,1,2])
bn3_3_1= tf.nn.batch_normalization(biases3_3_1,mean3_3_1,var3_3_1,offset=None,scale=None,variance_epsilon=1e-8)
x3_3_1=tf.nn.relu(bn3_3_1,name='conv3_3_branch1')
with tf.name_scope('conv3_3_branch2'):
kernel3_3_2=tf.Variable(initial_value=tf.random.normal([3,1,384,384]))
bias3_3_2=tf.Variable(initial_value=tf.constant(0.0,shape=[384]))
conv3_3_2=tf.nn.conv2d(x3_2,kernel3_3_2,strides=[1,1,1,1], padding='SAME')
biases3_3_2=tf.nn.bias_add(conv3_3_2,bias3_3_2)
mean3_3_2,var3_3_2=tf.nn.moments(biases3_3_2,axes=[0,1,2])
bn3_3_2= tf.nn.batch_normalization(biases3_3_2,mean3_3_2,var3_3_2,offset=None,scale=None,variance_epsilon=1e-8)
x3_3_2=tf.nn.relu(bn3_3_2,name='conv3_3_branch2')
x3_out=tf.concat([x3_3_1,x3_3_2],axis=-1)
with tf.name_scope('S4'):
with tf.name_scope('avpool4_1'):
x4_1=tf.nn.avg_pool2d(x,ksize=[1,3,3,1],strides=[1,1,1,1],padding='SAME',name='avpool4_1')#确定池化的padding
with tf.name_scope('conv4_2'):
kernel4_2=tf.Variable(initial_value=tf.random.normal([1,1,n_in,192]))
bias4_2=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv4_2=tf.nn.conv2d(x4_1,kernel4_2,strides=[1,1,1,1],padding='SAME')
biases4_2=tf.nn.bias_add(conv4_2,bias4_2)
mean4_2,var4_2=tf.nn.moments(biases4_2,axes=[0,1,2])
bn4_2= tf.nn.batch_normalization(biases4_2,mean4_2,var4_2,offset=None,scale=None,variance_epsilon=1e-8)
x4_2=tf.nn.relu(bn4_2,name='conv4_2')
x4_out=x4_2
x_out=tf.concat([x1_out,x2_out,x3_out,x4_out],axis=-1)
return x_out
#构建最终的网络结构
def inceptionV3(x):
n_in=x.get_shape()[-1]
with tf.name_scope('conv1'):
kernel1=tf.Variable(initial_value=tf.random.normal([3,3,n_in,32]))
bias1=tf.Variable(initial_value=tf.constant(0.0,shape=[32]))
conv1=tf.nn.conv2d(x,kernel1,strides=[1,2,2,1], padding='VALID')
biases1=tf.nn.bias_add(conv1,bias1)
mean1,var1=tf.nn.moments(biases1,axes=[0,1,2])
bn1= tf.nn.batch_normalization(biases1,mean1,var1,offset=None,scale=None,variance_epsilon=1e-8)
x1=tf.nn.relu(bn1,name='conv1')#[batch,149,149,32]
with tf.name_scope('conv2'):
kernel2=tf.Variable(initial_value=tf.random.normal([3,3,32,32]))
bias2=tf.Variable(initial_value=tf.constant(0.0,shape=[32]))
conv2=tf.nn.conv2d(x1,kernel2,strides=[1,1,1,1], padding='VALID')
biases2=tf.nn.bias_add(conv2,bias2)
mean2,var2=tf.nn.moments(biases2,axes=[0,1,2])
bn2= tf.nn.batch_normalization(biases2,mean2,var2,offset=None,scale=None,variance_epsilon=1e-8)
x2=tf.nn.relu(bn2,name='conv2')
with tf.name_scope('conv3'):
kernel3=tf.Variable(initial_value=tf.random.normal([3,3,32,64]))
bias3=tf.Variable(initial_value=tf.constant(0.0,shape=[64]))
conv3=tf.nn.conv2d(x2,kernel3,strides=[1,1,1,1], padding='SAME')
biases3=tf.nn.bias_add(conv3,bias3)
mean3,var3=tf.nn.moments(biases3,axes=[0,1,2])
bn3= tf.nn.batch_normalization(biases3,mean3,var3,offset=None,scale=None,variance_epsilon=1e-8)
x3=tf.nn.relu(bn3,name='conv3')
with tf.name_scope('mpool4'):
x4=tf.nn.max_pool2d(x3,ksize=[1,3,3,1],strides=[1,2,2,1],padding='VALID',name='mpool4')#确定池化的padding
with tf.name_scope('conv5'):
kernel5=tf.Variable(initial_value=tf.random.normal([1,1,64,80]))
bias5=tf.Variable(initial_value=tf.constant(0.0,shape=[80]))
conv5=tf.nn.conv2d(x4,kernel5,strides=[1,1,1,1], padding='VALID')
biases5=tf.nn.bias_add(conv5,bias5)
mean5,var5=tf.nn.moments(biases5,axes=[0,1,2])
bn5= tf.nn.batch_normalization(biases5,mean5,var5,offset=None,scale=None,variance_epsilon=1e-8)
x5=tf.nn.relu(bn5,name='conv5')
with tf.name_scope('conv6'):
kernel6=tf.Variable(initial_value=tf.random.normal([3,3,80,192]))
bias6=tf.Variable(initial_value=tf.constant(0.0,shape=[192]))
conv6=tf.nn.conv2d(x5,kernel6,strides=[1,1,1,1], padding='VALID')
biases6=tf.nn.bias_add(conv6,bias6)
mean6,var6=tf.nn.moments(biases6,axes=[0,1,2])
bn6= tf.nn.batch_normalization(biases6,mean6,var6,offset=None,scale=None,variance_epsilon=1e-8)
x6=tf.nn.relu(bn6,name='conv6')
with tf.name_scope('mpool7'):
x7=tf.nn.max_pool2d(x6,ksize=[1,3,3,1],strides=[1,2,2,1],padding='VALID',name='mpool7')#确定池化的padding
with tf.name_scope('block1'):
block11=block1()
x8=block11.block1_model1(x7)
x9=block11.block1_model2(x8)
x10=block11.block1_model3(x9)
with tf.name_scope('block2'):
block22=block2()
x11=block22.block2_model1(x10)
x12=block22.block2_model2(x11)
x13=block22.block2_model34(x12)
x14=block22.block2_model34(x13)
x15=block22.block2_model5(x14)
with tf.name_scope('block3'):
block33=block3()
x16=block33.block3_model1(x15)
x17=block33.block3_model2(x16)
x18=block33.block3_model3(x17)
with tf.name_scope('mpool19'):
x19=tf.nn.max_pool2d(x18,ksize=[1,8,8,1],strides=[1,1,1,1],padding='VALID',name='mpool7')#确定池化的padding
with tf.name_scope('dropout20'):
x20=tf.nn.dropout(x19,rate=0.5,seed=1)
with tf.name_scope('conv21'):
kernel21=tf.Variable(initial_value=tf.random.normal([1,1,2048,1000]))
bias21=tf.Variable(initial_value=tf.constant(0.0,shape=[1000]))
conv21=tf.nn.conv2d(x20,kernel21,strides=[1,1,1,1], padding='VALID')
biases21=tf.nn.bias_add(conv21,bias21)
mean21,var21=tf.nn.moments(biases21,axes=[0,1,2])
bn21= tf.nn.batch_normalization(biases21,mean21,var21,offset=None,scale=None,variance_epsilon=1e-8)
x21=tf.nn.relu(bn21,name='conv21')
return x21
#验证
x=np.random.randint(255,size=(2,299,299,3))
x=x.astype(np.float32)
x_in=tf.convert_to_tensor(x)
x_out=inceptionV3(x_in)
本篇主要参考资料如下:
- tensorflow 2的使用文档说明:
链接: 使用文档说明. - Google inception v3 net的网络结构说明:
链接: inception v3网络结构说明.