第一片代码model_CT.py
用于G和D的构造
"""
Created on Tue Jul 24 20:33:14 2018
E-mail: [email protected]
@author: DidiLv
"""
import tensorflow as tf
import numpy as np
def conv2d(x, W):
return tf.nn.conv2d(input = x, filter = W, strides = [1,1,1,1], padding = 'SAME')
def avg_pool_2x2(x):
return tf.nn.avg_pool(x, ksize = [1,2,2,1], strides = [1,2,2,1], padding = 'SAME')
def xavier_init(size):
in_dim = size[0]
xavier_stddev = 1. / tf.sqrt(in_dim / 2.)
return tf.random_normal(shape=size, stddev=xavier_stddev)
def sample_z(shape):
return np.random.uniform(-1., 1., size=shape)
def discriminator(x_image, reuse=False):
with tf.variable_scope('discriminator') as scope:
if (reuse):
tf.get_variable_scope().reuse_variables()
W_conv1 = tf.get_variable('d_wconv1', shape = [5, 5, 1, 8], initializer=tf.truncated_normal_initializer(stddev=0.02))
b_conv1 = tf.get_variable('d_bconv1', shape = [8], initializer=tf.constant_initializer(0))
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = avg_pool_2x2(h_conv1)
W_conv2 = tf.get_variable('d_wconv2', shape = [5, 5, 8, 16], initializer=tf.truncated_normal_initializer(stddev=0.02))
b_conv2 = tf.get_variable('d_bconv2', shape = [16], initializer=tf.constant_initializer(0))
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = avg_pool_2x2(h_conv2)
W_conv3 = tf.get_variable('d_wconv3', shape = [5, 5, 16, 32], initializer=tf.truncated_normal_initializer(stddev=0.02))
b_conv3 = tf.get_variable('d_bconv3', shape = [32], initializer=tf.constant_initializer(0))
h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3)
h_pool3 = avg_pool_2x2(h_conv3)
W_conv4 = tf.get_variable('d_wconv4', shape = [5, 5, 32, 64], initializer=tf.truncated_normal_initializer(stddev=0.02))
b_conv4 = tf.get_variable('d_bconv4', shape = [64], initializer=tf.constant_initializer(0))
h_conv4 = tf.nn.relu(conv2d(h_pool3, W_conv4) + b_conv4)
h_pool4 = avg_pool_2x2(h_conv4)
W_fc1 = tf.get_variable('d_wfc1', [14 * 12 * 64, 320], initializer=tf.truncated_normal_initializer(stddev=0.02))
b_fc1 = tf.get_variable('d_bfc1', [320], initializer=tf.constant_initializer(0))
h_pool4_flat = tf.reshape(h_pool4, [-1, 14 * 12 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool4_flat, W_fc1) + b_fc1)
W_fc2 = tf.get_variable('d_wfc2', [320, 80], initializer=tf.truncated_normal_initializer(stddev=0.02))
b_fc2 = tf.get_variable('d_bfc2', [80], initializer=tf.constant_initializer(0))
h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_fc2) + b_fc2)
W_fc3 = tf.get_variable('d_wfc3', [80, 1], initializer=tf.truncated_normal_initializer(stddev=0.02))
b_fc3 = tf.get_variable('d_bfc3', [1], initializer=tf.constant_initializer(0))
y_conv=(tf.matmul(h_fc2, W_fc3) + b_fc3)
return y_conv
def generator(z, batch_size, z_dim, reuse = False):
with tf.variable_scope('generator') as scope:
if (reuse):
tf.get_variable_scope().reuse_variables()
g_dim = 64
c_dim = 1
s_w = 221
s_h = 181
s_w2, s_w4, s_w8, s_w16, s_w32, s_w64 = int(s_w/2), int(s_w/4), int(s_w/8), int(s_w/16), int(s_w/32), int(s_w/64)
s_h2, s_h4, s_h8, s_h16, s_h32, s_h64 = int(s_h/2), int(s_h/4), int(s_h/8), int(s_h/16), int(s_h/32), int(s_h/64)
h0 = tf.reshape(z, [batch_size, s_w64+1, s_h64+1, g_dim])
h0 = tf.nn.relu(h0)
output1_shape = [batch_size, s_w32+1, s_h32+1, c_dim*256]
W_conv1 = tf.get_variable('g_wconv1', shape = [5,5,output1_shape[-1],int(h0.get_shape()[-1])],
initializer=tf.truncated_normal_initializer(stddev = 0.1)
)
b_conv1 = tf.get_variable('g_bconv1', shape = [output1_shape[-1]], initializer=tf.constant_initializer(.1))
H_conv1 = tf.nn.conv2d_transpose(h0, W_conv1, output_shape = output1_shape, strides = [1,2,2,1],
padding = 'SAME')
H_conv1 = tf.add(H_conv1, b_conv1)
H_conv1 = tf.contrib.layers.batch_norm(inputs = H_conv1, center=True, scale=True, is_training=True, scope="g_bn1")
H_conv1 = tf.nn.relu(H_conv1)
output2_shape = [batch_size, s_w16+1, s_h16+1, c_dim*128]
W_conv2 = tf.get_variable('g_wconv2', shape = [5,5,output2_shape[-1], int(H_conv1.get_shape()[-1])],
initializer=tf.truncated_normal_initializer(stddev = 0.1))
b_conv2 = tf.get_variable('g_bconv2', shape = [output2_shape[-1]], initializer=tf.truncated_normal_initializer(0.1))
H_conv2 = tf.nn.conv2d_transpose(H_conv1, W_conv2, output_shape = output2_shape, strides = [1,2,2,1],
padding = 'SAME')
H_conv2 = tf.add(H_conv2, b_conv2)
H_conv2 = tf.contrib.layers.batch_norm(inputs = H_conv2, center=True, scale=True, is_training=True, scope="g_bn2")
H_conv2 = tf.nn.relu(H_conv2)
output3_shape = [batch_size, s_w8+1, s_h8+1, c_dim*64]
W_conv3 = tf.get_variable('g_wconv3', [5, 5, output3_shape[-1], int(H_conv2.get_shape()[-1])],
initializer=tf.truncated_normal_initializer(stddev=0.1))
b_conv3 = tf.get_variable('g_bconv3', [output3_shape[-1]], initializer=tf.constant_initializer(.1))
H_conv3 = tf.nn.conv2d_transpose(H_conv2, W_conv3, output_shape=output3_shape, strides=[1, 2, 2, 1],
padding='SAME')
H_conv3 = tf.add(H_conv3, b_conv3)
H_conv3 = tf.contrib.layers.batch_norm(inputs = H_conv3, center=True, scale=True, is_training=True, scope="g_bn3")
H_conv3 = tf.nn.relu(H_conv3)
output4_shape = [batch_size, s_w4+1, s_h4+1, c_dim*32]
W_conv4 = tf.get_variable('g_wconv4', [5, 5, output4_shape[-1], int(H_conv3.get_shape()[-1])],
initializer=tf.truncated_normal_initializer(stddev=0.1))
b_conv4 = tf.get_variable('g_bconv4', [output4_shape[-1]], initializer=tf.constant_initializer(.1))
H_conv4 = tf.nn.conv2d_transpose(H_conv3, W_conv4, output_shape=output4_shape, strides=[1, 2, 2, 1],
padding='SAME')
H_conv4 = tf.add(H_conv4, b_conv4)
H_conv4 = tf.contrib.layers.batch_norm(inputs = H_conv4, center=True, scale=True, is_training=True, scope="g_bn4")
H_conv4 = tf.nn.relu(H_conv4)
output5_shape = [batch_size, s_w2+1, s_h2+1, c_dim*16]
W_conv5 = tf.get_variable('g_wconv5', [5, 5, output5_shape[-1], int(H_conv4.get_shape()[-1])],
initializer=tf.truncated_normal_initializer(stddev=0.1))
b_conv5 = tf.get_variable('g_bconv5', [output5_shape[-1]], initializer=tf.constant_initializer(.1))
H_conv5 = tf.nn.conv2d_transpose(H_conv4, W_conv5, output_shape=output5_shape, strides=[1, 2, 2, 1],
padding='SAME')
H_conv5 = tf.add(H_conv5, b_conv5)
H_conv5 = tf.contrib.layers.batch_norm(inputs = H_conv5, center=True, scale=True, is_training=True, scope="g_bn5")
H_conv5 = tf.nn.relu(H_conv5)
output6_shape = [batch_size, s_w, s_h, c_dim]
W_conv6 = tf.get_variable('g_wconv6', [5, 5, output6_shape[-1], int(H_conv5.get_shape()[-1])],
initializer=tf.truncated_normal_initializer(stddev=0.1))
b_conv6 = tf.get_variable('g_bconv6', [output6_shape[-1]], initializer=tf.constant_initializer(.1))
H_conv6 = tf.nn.conv2d_transpose(H_conv5, W_conv6, output_shape=output6_shape, strides=[1, 2, 2, 1],
padding='SAME')
H_conv6 = tf.add(H_conv6, b_conv6)
H_conv6 = tf.nn.tanh(H_conv6)
return H_conv6
第二片代码data_generate_CT.py
设计pipeline用于读取batch数据:
"""
Created on Thu Jul 19 15:40:11 2018
E-mail: [email protected]
@author: DidiLv
"""
import tensorflow as tf
import numpy as np
import os
import matplotlib.pyplot as plt
def get_files(file_dir):
lung_img = [];
label_lung_img = [];
for file in os.listdir(file_dir):
lung_img.append( file_dir + file)
label_lung_img.append(1)
image_list = np.hstack((lung_img))
label_list = np.hstack((label_lung_img))
temp = np.array([lung_img, label_lung_img]).T
np.random.shuffle(temp)
image_list = list(temp[:,0])
label_list = list(temp[:,1])
label_list = [int(i) for i in label_list]
return image_list, label_list
def get_batch(image,label,batch_size):
image_W, image_H = 221, 181
image=tf.cast(image,tf.string)
label=tf.cast(label,tf.int32)
epoch_num = 50
input_queue=tf.train.slice_input_producer([image,label], num_epochs=epoch_num)
label=input_queue[1]
image_contents=tf.read_file(input_queue[0])
image=tf.image.decode_png(image_contents,channels=1)
image=tf.image.resize_image_with_crop_or_pad(image,image_W,image_H)
image=tf.image.per_image_standardization(image)
min_after_dequeue=1000
capacity=min_after_dequeue+300*batch_size
image_batch,label_batch=tf.train.shuffle_batch([image,label],batch_size=batch_size,num_threads=1024,capacity=capacity,min_after_dequeue=min_after_dequeue)
image_batch = tf.reshape(image_batch,[batch_size,image_W,image_H,1])
image_batch=tf.cast(image_batch,np.float32)
return image_batch, label_batch
if __name__ == "__main__":
file_dir='D:\\CT_data\\Data_preprocessing\\'
image_list, label_list = get_files(file_dir)
image_batch, label_batch = get_batch(image_list, label_list, 28)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
j = 0
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
try:
while not coord.should_stop() and j<5:
img, label = sess.run([image_batch, label_batch])
plt.imshow(img[0,:,:,0])
plt.show()
j+=1
except tf.errors.OutOfRangeError:
print('done!')
finally:
coord.request_stop()
print('-----------')
coord.join(threads)
第三片代码train_CT.py
用于训练GAN
"""
Created on Fri Jul 27 14:57:23 2018
@author: DidiLv
"""
"""
Created on Wed Jul 25 09:42:35 2018
E-mail: [email protected]
@author: DidiLv
"""
import model_CT
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import data_generate_CT
file_dir='D:\\CT_data\\Data_preprocessing\\'
tf.reset_default_graph()
batch_size = 10
image_W = 221
image_H = 181
image_C = 1
z_dimensions = 4*3*64
image_list, label_list = data_generate_CT.get_files(file_dir)
image_batch, _ = data_generate_CT.get_batch(image_list, label_list, batch_size)
x_placeholder = image_batch
z_placeholder = tf.Variable(np.random.normal(-1, 1, size=[batch_size, z_dimensions]), dtype = tf.float32)
Dx = model_CT.discriminator(x_placeholder)
Gz = model_CT.generator(z_placeholder, batch_size, z_dimensions)
Dg = model_CT.discriminator(Gz, reuse=True)
g_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=Dg, labels=tf.ones_like(Dg)))
d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=Dx, labels = tf.ones_like(Dx)))
d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=Dg, labels = tf.zeros_like(Dx)))
d_loss = d_loss_real + d_loss_fake
tvars = tf.trainable_variables()
d_vars = [var for var in tvars if 'd_' in var.name]
g_vars = [var for var in tvars if 'g_' in var.name]
with tf.variable_scope(tf.get_variable_scope(), reuse = False):
trainerD = tf.train.AdadeltaOptimizer(learning_rate = 1e-3).minimize(d_loss, var_list = d_vars)
trainerG = tf.train.AdadeltaOptimizer(learning_rate = 1e-3).minimize(g_loss, var_list = g_vars)
iterations = 3000
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
i = 0
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
try:
while not coord.should_stop() and i