import tensorflow as tf
import numpy as np
reuse = False
init = tf.initializers.variance_scaling()
X = tf.placeholder(
dtype=tf.float32,
shape=[None] + [86]*3 + [1])
with tf.variable_scope("level1"):
net1 = tf.layers.conv3d(X, 16, 3, activation='elu',padding='valid',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='conv', kernel_initializer=init )
with tf.variable_scope("level2"):
net2_in = tf.layers.conv3d(net1, 32, 2, strides=2, activation='elu',padding='valid',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='ds', kernel_initializer=init )
net2 = tf.layers.conv3d(net2_in, 32, 3, activation='elu',padding='valid',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='conv', kernel_initializer=init )
with tf.variable_scope("level3"):
net3_in = tf.layers.conv3d(net2, 64, 2, strides=2, activation='elu',padding='valid',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='ds', kernel_initializer=init )
net3 = tf.layers.conv3d(net3_in, 64, 3, activation='elu',padding='valid',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='conv', kernel_initializer=init )
with tf.variable_scope("level4"):
net4_in = tf.layers.conv3d(net3, 128, 2, strides=2, activation='elu',padding='valid',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='ds', kernel_initializer=init )
net4 = tf.layers.conv3d(net4_in, 128, 3, activation='elu',padding='valid',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='conv', kernel_initializer=init)
with tf.variable_scope("level5"):
net5 = tf.layers.conv3d_transpose(net4, 64, 2, strides=2, activation='elu',padding='same',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='trans', kernel_initializer=init)
net5 = tf.concat([net5, tf.slice(net3,[0]+[(18-14)//2]*3+[0],[-1]+[14]*3+[64])], axis=4) #tf.slice(net8, begin, tf.shape(net18))
net5 = tf.layers.conv3d(net5, 128, 3, activation='elu',padding='valid',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='conv', kernel_initializer=init)
with tf.variable_scope("level6"):
net6 = tf.layers.conv3d_transpose(net5, 32, 2, strides=2, activation='elu',padding='same',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='trans', kernel_initializer=init)
net6 = tf.concat([net6, tf.slice(net2,[0]+[(40-24)//2]*3+[0],[-1]+[24]*3+[32])], axis=4) #tf.slice(net5, begin, tf.shape(net18))
net6 = tf.layers.conv3d(net6, 64, 3, activation='elu',padding='valid',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='conv', kernel_initializer=init)
with tf.variable_scope("level7"):
net7 = tf.layers.conv3d_transpose(net6, 16, 2, strides=2, activation='elu',padding='same',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='trans', kernel_initializer=init)
net7 = tf.concat([net7, tf.slice(net1,[0]+[(84-44)//2]*3+[0],[-1]+[44]*3+[16])], axis=4) #tf.slice(net5, begin, tf.shape(net18))
net7 = tf.layers.conv3d(net7, 32, 3, activation='elu',padding='valid',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='conv', kernel_initializer=init)
with tf.variable_scope("out"):
net_out = tf.layers.conv3d(net7, 2, 1, activation='elu',padding='valid',kernel_regularizer=tf.contrib.layers.l2_regularizer(0.0001),reuse=reuse,name='conv', kernel_initializer=init)
model = net_out
sess = tf.Session()
sess.run(tf.local_variables_initializer())
sess.run(tf.global_variables_initializer())
batch_size = 34
full_batch = np.random.rand(154,86,86,86,1)
num_batches = (len(full_batch)//batch_size)
if len(full_batch) % batch_size: num_batches += 1
y_out_list = []
print("Batch size of 34")
for i in range(0, num_batches):
batch = full_batch[i*batch_size:min(len(full_batch), (i+1)*batch_size)]
batch1 = full_batch[2*i*batch_size//2:min(len(full_batch), (2*i+1)*batch_size//2)]
batch2 = full_batch[(2*i+1)*batch_size//2:min(len(full_batch), (2*i+2)*batch_size//2)]
y_out = sess.run(model, feed_dict={X:batch})
y_out1 = sess.run(model, feed_dict={X:batch1})
y_out2 = sess.run(model, feed_dict={X:batch2})
print("Should be true if independent of batch size: ", np.all(y_out==np.concatenate([y_out1,y_out2])), " Shape: ", y_out.shape)
batch_size = 20
full_batch = np.random.rand(154,86,86,86,1)
num_batches = (len(full_batch)//batch_size)
if len(full_batch) % batch_size: num_batches += 1
y_out_list = []
print("Batch size of 20")
for i in range(0, num_batches):
batch = full_batch[i*batch_size:min(len(full_batch), (i+1)*batch_size)]
batch1 = full_batch[2*i*batch_size//2:min(len(full_batch), (2*i+1)*batch_size//2)]
batch2 = full_batch[(2*i+1)*batch_size//2:min(len(full_batch), (2*i+2)*batch_size//2)]
y_out = sess.run(model, feed_dict={X:batch})
y_out1 = sess.run(model, feed_dict={X:batch1})
y_out2 = sess.run(model, feed_dict={X:batch2})
print("Should be true if independent of batch size: ", np.all(y_out==np.concatenate([y_out1,y_out2])), " Shape: ", y_out.shape)
batch_size = 42
full_batch = np.random.rand(154,86,86,86,1)
num_batches = (len(full_batch)//batch_size)
if len(full_batch) % batch_size: num_batches += 1
y_out_list = []
print("Batch size of 42")
for i in range(0, num_batches):
batch = full_batch[i*batch_size:min(len(full_batch), (i+1)*batch_size)]
batch1 = full_batch[2*i*batch_size//2:min(len(full_batch), (2*i+1)*batch_size//2)]
batch2 = full_batch[(2*i+1)*batch_size//2:min(len(full_batch), (2*i+2)*batch_size//2)]
y_out = sess.run(model, feed_dict={X:batch})
y_out1 = sess.run(model, feed_dict={X:batch1})
y_out2 = sess.run(model, feed_dict={X:batch2})
print("Should be true if independent of batch size: ", np.all(y_out==np.concatenate([y_out1,y_out2])), " Shape: ", y_out.shape)
对于给定的输入张量尺寸和我定义的模型,我得到的结果取决于批次大小(34和42似乎都出错),但这肯定是独立的。我是否缺少某些东西,或者可能与此bug类似?