├── README.md ├── assets ├── brain_x.png ├── brain_y.png ├── brain_z.png └── vgg16.png ├── mri_convolutional_neuralnet.py ├── mri_convolutional_neuralnet2.py ├── mri_convolutional_neuralnet3.py ├── mri_convolutional_neuralnet4.py ├── mri_convolutional_neuralnet_3dim_vgg.py ├── mri_neuralnet.py ├── utils.py ├── vgg_2dim.py └── vgg_3dim.py /README.md: -------------------------------------------------------------------------------- 1 | # Age Prediction Of MRI Image 2 | 3 | To predict ages based on 3D MRI Images, VGG network architecture is expanded to three dimensions. 4 | 5 | projection_x 6 | projection_z 7 | projection_y 8 | 9 | 10 | ## Model 11 | 12 | ### 3 Dimensional VGG in Tensorflow 13 | 14 | Tensorflow implementation of [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556) by [Visual Geometry Group](http://www.robots.ox.ac.uk/~vgg/) 15 | 16 | vgg16 17 | 18 | ## Prerequisites 19 | 20 | - Python 3.3+ 21 | - [Tensorflow](https://www.tensorflow.org/) 22 | - [SciPy](http://www.scipy.org/install.html) 23 | - [Nibabel](http://nipy.org/nibabel/) 24 | - (Optional) [IXI-MRA.zip](http://brain-development.org/ixi-dataset/) : Large-scale MRA Dataset 25 | 26 | 27 | ## Usage 28 | 29 | To train a model with downloaded dataset: 30 | 31 | $ python mri_convolutional_neuralnet.py 32 | 33 | 34 | ## Author 35 | 36 | Joey 37 | -------------------------------------------------------------------------------- /assets/brain_x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/j52y/mri-age-classifier/4f8e6561997795cf665525ef3364342a871931f5/assets/brain_x.png -------------------------------------------------------------------------------- /assets/brain_y.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/j52y/mri-age-classifier/4f8e6561997795cf665525ef3364342a871931f5/assets/brain_y.png -------------------------------------------------------------------------------- /assets/brain_z.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/j52y/mri-age-classifier/4f8e6561997795cf665525ef3364342a871931f5/assets/brain_z.png -------------------------------------------------------------------------------- /assets/vgg16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/j52y/mri-age-classifier/4f8e6561997795cf665525ef3364342a871931f5/assets/vgg16.png -------------------------------------------------------------------------------- /mri_convolutional_neuralnet.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import tensorflow as tf 3 | from utils import * 4 | from scipy.ndimage.interpolation import zoom 5 | 6 | # r_range = 0.1 7 | i_max = 1480 8 | train_x, train_y = load_train_data() 9 | 10 | min_age, max_age = min(train_y), max(train_y) 11 | 12 | original_row, original_col = 360, 512 13 | original_size = original_row * original_col 14 | n_row, n_col = 28, 28 15 | n_input = n_row * n_col 16 | n_output = 10 # len(set(train_y)) 17 | 18 | sess = tf.InteractiveSession() 19 | x = tf.placeholder(tf.float32, shape=[None, n_input]) 20 | y_ = tf.placeholder(tf.float32, shape=[None, n_output]) 21 | 22 | 23 | def weight_variable(shape): 24 | initial = tf.truncated_normal(shape, stddev=0.1) 25 | return tf.Variable(initial) 26 | 27 | 28 | def bias_variable(shape): 29 | initial = tf.constant(0.1, shape=shape) 30 | return tf.Variable(initial) 31 | 32 | 33 | def conv2d(x, W): 34 | return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 35 | 36 | 37 | def max_pool_2x2(x): 38 | return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], 39 | strides=[1, 2, 2, 1], padding='SAME') 40 | 41 | W_conv1 = weight_variable([5, 5, 1, 32]) 42 | b_conv1 = bias_variable([32]) 43 | 44 | x_image = tf.reshape(x, [-1,n_row,n_col,1]) 45 | 46 | h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 47 | h_pool1 = max_pool_2x2(h_conv1) 48 | 49 | W_conv2 = weight_variable([5, 5, 32, 64]) 50 | b_conv2 = bias_variable([64]) 51 | 52 | h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 53 | h_pool2 = max_pool_2x2(h_conv2) 54 | 55 | W_fc1 = weight_variable([7 * 7 * 64, 1024]) 56 | b_fc1 = bias_variable([1024]) 57 | 58 | h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) 59 | h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 60 | 61 | keep_prob = tf.placeholder(tf.float32) 62 | h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 63 | 64 | W_fc2 = weight_variable([1024, 10]) 65 | b_fc2 = bias_variable([10]) 66 | 67 | y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 68 | 69 | cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1])) 70 | train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) 71 | correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1)) 72 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 73 | sess.run(tf.initialize_all_variables()) 74 | for i in range(20000): 75 | accu = 0.0 76 | for j in range(len(train_x)): 77 | if train_x[j].shape[0] * train_x[j].shape[1] != original_size: 78 | continue 79 | batch_x = np.max(train_x[j].get_data(), axis=2) 80 | batch_x = batch_x[40:320, 116:396] 81 | batch_x = zoom(batch_x, 0.1) 82 | batch_x = batch_x.reshape(1, n_input) / i_max 83 | # batch_y = np.array([[train_y[j]/(max_age - min_age)]]) 84 | # batch_y = (np.arange(10)/10).reshape(-1,10) 85 | batch_y = np.zeros(10) 86 | batch_y[int(train_y[j]/10)] = 1 87 | print(train_y[j], batch_y) 88 | batch_y = batch_y.reshape(-1,10) 89 | train_step.run(feed_dict={x: batch_x, y_: batch_y, keep_prob: 0.5}) 90 | accu = y_conv.eval(feed_dict={x: batch_x, y_: batch_y, keep_prob: 0.5}) 91 | print(i, j, accu) 92 | -------------------------------------------------------------------------------- /mri_convolutional_neuralnet2.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import tensorflow as tf 3 | from utils import * 4 | from scipy.ndimage.interpolation import zoom 5 | 6 | # r_range = 0.1 7 | i_max = 1480 8 | train_x, train_y = load_train_data() 9 | 10 | #train_y = 48~97, label = (y - 48)/5 11 | 12 | min_age, max_age = min(train_y), max(train_y) 13 | 14 | original_row, original_col = 360, 512 15 | original_size = original_row * original_col 16 | n_row, n_col = 280, 280 17 | n_input = n_row * n_col 18 | n_output = 10 # len(set(train_y)) 19 | 20 | sess = tf.InteractiveSession() 21 | x = tf.placeholder(tf.float32, shape=[None, n_input]) 22 | y_ = tf.placeholder(tf.float32, shape=[None, n_output]) 23 | 24 | def one_hot_y(y_val): 25 | zeros = np.zeros(10) 26 | i = int((y_val - 48)/5) 27 | zeros[i] = 1 28 | zeros = zeros.reshape(-1,10) 29 | return zeros 30 | 31 | def weight_variable(shape): 32 | initial = tf.truncated_normal(shape, stddev=0.1) 33 | return tf.Variable(initial) 34 | 35 | 36 | def bias_variable(shape): 37 | initial = tf.constant(0.1, shape=shape) 38 | return tf.Variable(initial) 39 | 40 | 41 | def conv2d(x, W): 42 | return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 43 | 44 | 45 | def max_pool_2x2(x): 46 | return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], 47 | strides=[1, 2, 2, 1], padding='SAME') 48 | 49 | def max_pool_20x20(x): 50 | return tf.nn.max_pool(x, ksize=[1, 20, 20, 1], 51 | strides=[1, 20, 20, 1], padding='SAME') 52 | 53 | def max_pool_5x5(x): 54 | return tf.nn.max_pool(x, ksize=[1, 5, 5, 1], 55 | strides=[1, 5, 5, 1], padding='SAME') 56 | 57 | 58 | W_conv1 = weight_variable([5, 5, 1, 32]) 59 | b_conv1 = bias_variable([32]) 60 | 61 | x_image = tf.reshape(x, [-1,n_row,n_col,1]) 62 | 63 | h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 64 | h_pool1 = max_pool_2x2(h_conv1) 65 | 66 | W_conv2 = weight_variable([5, 5, 32, 64]) 67 | b_conv2 = bias_variable([64]) 68 | 69 | h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 70 | h_pool2 = max_pool_2x2(h_conv2) 71 | 72 | W_conv3 = weight_variable([5, 5, 64, 128]) 73 | b_conv3 = bias_variable([128]) 74 | 75 | h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3) 76 | h_pool3 = max_pool_5x5(h_conv3) 77 | 78 | W_fc1 = weight_variable([14 * 14 * 128, 1024]) 79 | b_fc1 = bias_variable([1024]) 80 | 81 | h_pool2_flat = tf.reshape(h_pool3, [-1, 14*14*128]) 82 | h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 83 | 84 | keep_prob = tf.placeholder(tf.float32) 85 | h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 86 | 87 | W_fc2 = weight_variable([1024, 10]) 88 | b_fc2 = bias_variable([10]) 89 | 90 | y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 91 | 92 | cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1])) 93 | train_step = tf.train.AdamOptimizer(1e-5).minimize(cross_entropy) 94 | correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1)) 95 | accuracy = tf.reduce_sum(tf.cast(correct_prediction, tf.float32)) 96 | sess.run(tf.initialize_all_variables()) 97 | for i in range(20000): 98 | entropy = 0.0 99 | accu = 0.0 100 | for j in range(len(train_x)): 101 | #14 102 | if train_x[j].shape[0] * train_x[j].shape[1] != original_size: 103 | continue 104 | batch_x = np.max(train_x[j].get_data(), axis=2) 105 | batch_x = batch_x[40:320, 116:396] 106 | batch_x = (batch_x.reshape(1, n_input) - 53) / i_max 107 | batch_y = one_hot_y(train_y[j]) 108 | fetches = [train_step, cross_entropy, accuracy, y_conv] 109 | t = sess.run(fetches, feed_dict={x: batch_x, y_: batch_y, keep_prob: 0.1}) 110 | entropy += t[1] 111 | accu += t[2] 112 | print(t[3].shape) 113 | print(np.argmax(t[3]), (train_y[j]-48)/5) 114 | print(i, j, entropy, accu) 115 | -------------------------------------------------------------------------------- /mri_convolutional_neuralnet3.py: -------------------------------------------------------------------------------- 1 | import math 2 | import numpy as np 3 | import tensorflow as tf 4 | from utils import * 5 | from scipy.ndimage.interpolation import zoom 6 | 7 | i_max = 1480 8 | train_x, train_y = load_train_data() 9 | 10 | #train_y = 48~97, label = (y - 48)/5 11 | 12 | min_age, max_age = min(train_y), max(train_y) 13 | 14 | dn = 4 15 | d0, d1, d2 = 360, 512, 216 16 | d0, d1, d2 = int(d0/dn), int(d1/dn), int(d2/dn) 17 | d = d0 * d1 * d2 18 | 19 | n_output = 1 20 | 21 | sess = tf.InteractiveSession() 22 | x = tf.placeholder(tf.float32, shape=[None, d]) 23 | y_ = tf.placeholder(tf.float32, shape=[None, n_output]) 24 | 25 | 26 | def weight_variable(shape): 27 | initial = tf.truncated_normal(shape, stddev=0.1) 28 | return tf.Variable(initial) 29 | 30 | 31 | def bias_variable(shape): 32 | initial = tf.constant(0.1, shape=shape) 33 | return tf.Variable(initial) 34 | 35 | 36 | def conv2d(x, W): 37 | return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 38 | 39 | 40 | def conv3d(x, W): 41 | return tf.nn.conv3d(x, W, strides=[1, 1, 1, 1, 1], padding='SAME') 42 | 43 | 44 | def max_pool_2x2(x): 45 | return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], 46 | strides=[1, 2, 2, 1], padding='SAME') 47 | 48 | def max_pool_2x2x2(x): 49 | return tf.nn.max_pool3d(x, ksize=[1, 2, 2, 2, 1], 50 | strides=[1, 2, 2, 2, 1], padding='SAME') 51 | 52 | 53 | W_conv1 = weight_variable([5, 5, 5, 1, 32]) 54 | b_conv1 = bias_variable([32]) 55 | 56 | x_image = tf.reshape(x, [-1, d0, d1, d2, 1]) 57 | 58 | h_conv1 = tf.nn.relu(conv3d(x_image, W_conv1) + b_conv1) 59 | h_pool1 = max_pool_2x2x2(h_conv1) 60 | 61 | W_conv2 = weight_variable([5, 5, 5, 32, 64]) 62 | b_conv2 = bias_variable([64]) 63 | 64 | h_conv2 = tf.nn.relu(conv3d(h_pool1, W_conv2) + b_conv2) 65 | h_pool2 = max_pool_2x2x2(h_conv2) 66 | 67 | # W_conv3 = weight_variable([5, 5, 64, 128]) 68 | # b_conv3 = bias_variable([128]) 69 | # 70 | # h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3) 71 | # h_pool3 = max_pool_5x5(h_conv3) 72 | 73 | # input = (1, 90, 128, 54) 74 | # h1_poo1.shape = (1, 45, 64, 27, 32) 75 | # h2_poo2.shape = (1, 23, 32, 14, 64) 76 | 77 | n_pool, n_stride = 2, 2 78 | dt = n_pool * n_stride 79 | drow = math.ceil(d0/dt) * math.ceil(d1/dt) * math.ceil(d2/dt) * 64 # drow = 659456, vulnerable 80 | h_pool2_flat = tf.reshape(h_pool2, [-1, drow]) 81 | 82 | W_fc1 = weight_variable([drow, 1024]) 83 | b_fc1 = bias_variable([1024]) 84 | h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 85 | 86 | keep_prob = tf.placeholder(tf.float32) 87 | h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 88 | 89 | W_fc2 = weight_variable([1024, 10]) 90 | b_fc2 = bias_variable([10]) 91 | h_fc2 = tf.nn.relu(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 92 | h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob) 93 | 94 | W_fc3 = weight_variable([10, 1]) 95 | b_fc3 = bias_variable([1]) 96 | y_conv= tf.matmul(h_fc2_drop, W_fc3) + b_fc3 97 | 98 | error = tf.sqrt(tf.reduce_mean(tf.square(tf.sub(y_, y_conv)))) 99 | train_step = tf.train.AdamOptimizer(1e-3).minimize(error) 100 | sess.run(tf.initialize_all_variables()) 101 | for i in range(20000): 102 | err = 0.0 103 | accu = 0.0 104 | for j in range(len(train_x)): 105 | if train_x[j].shape[0] * train_x[j].shape[1] * train_x[j].shape[2] != d * dn**3: 106 | continue #14 107 | batch_x = train_x[j].get_data() 108 | batch_x = zoom(batch_x, 1/dn) 109 | print(batch_x.shape) 110 | batch_x = (batch_x.reshape(1, d) - 53) / i_max 111 | batch_y = np.array([[train_y[j]]]) 112 | fetches = [train_step, error, y_conv] 113 | t = sess.run(fetches, feed_dict={x: batch_x, y_: batch_y, keep_prob: 0.1}) 114 | err += t[1] 115 | pred = t[2] 116 | print(i, j, err, pred) 117 | -------------------------------------------------------------------------------- /mri_convolutional_neuralnet4.py: -------------------------------------------------------------------------------- 1 | import os 2 | import math 3 | import numpy as np 4 | import tensorflow as tf 5 | from utils import * 6 | from scipy.ndimage.interpolation import zoom 7 | 8 | input_max = 1480 9 | train_x, train_y = load_train_data() 10 | 11 | min_age, max_age = min(train_y), max(train_y) 12 | 13 | dn = 4 14 | o0, o1, o2 = 300, 300, 200 #360, 512, 216 15 | d0, d1, d2 = round(o0/dn), round(o1/dn), round(o2/dn) 16 | d = d0 * d1 * d2 17 | 18 | n_output = 1 19 | p = 3 # stride size in pooling layer 20 | 21 | 22 | def weight_variable(shape): 23 | initial = tf.truncated_normal(shape, stddev=0.1) 24 | return tf.Variable(initial) 25 | 26 | 27 | def bias_variable(shape): 28 | initial = tf.constant(0.1, shape=shape) 29 | return tf.Variable(initial) 30 | 31 | 32 | def conv2d(x, W): 33 | return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 34 | 35 | 36 | def conv3d(x, W): 37 | return tf.nn.conv3d(x, W, strides=[1, 1, 1, 1, 1], padding='SAME') 38 | 39 | 40 | def max_pool_2x2(x): 41 | return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], 42 | strides=[1, 2, 2, 1], padding='SAME') 43 | 44 | 45 | def max_pool_2x2x2(x): 46 | return tf.nn.max_pool3d(x, ksize=[1, p, p, p, 1], 47 | strides=[1, p, p, p, 1], padding='SAME') 48 | 49 | 50 | def restore(saver, sess, name=''): 51 | fname = "./tmp/model_" + name + ".ckpt" 52 | if os.path.isfile(fname): 53 | saver.restore(sess, fname) 54 | 55 | 56 | with tf.device('/cpu:0'): 57 | x = tf.placeholder(tf.float32, shape=[None, d]) 58 | y_ = tf.placeholder(tf.float32, shape=[None, n_output]) 59 | W_conv1 = weight_variable([3, 3, 3, 1, 32]) 60 | b_conv1 = bias_variable([32]) 61 | x_image = tf.reshape(x, [-1, d0, d1, d2, 1]) 62 | h_conv1 = tf.nn.relu(conv3d(x_image, W_conv1) + b_conv1) 63 | h_pool1 = max_pool_2x2x2(h_conv1) 64 | 65 | W_conv2 = weight_variable([3, 3, 3, 32, 64]) 66 | b_conv2 = bias_variable([64]) 67 | h_conv2 = tf.nn.relu(conv3d(h_pool1, W_conv2) + b_conv2) 68 | h_pool2 = max_pool_2x2x2(h_conv2) 69 | 70 | W_conv3 = weight_variable([3, 3, 3, 64, 128]) 71 | b_conv3 = bias_variable([128]) 72 | h_conv3 = tf.nn.relu(conv3d(h_pool2, W_conv3) + b_conv3) 73 | h_pool3 = max_pool_2x2x2(h_conv3) 74 | 75 | n_pool = 3 76 | dt = p ** n_pool 77 | drow = math.ceil(d0/dt) * math.ceil(d1/dt) * math.ceil(d2/dt) * 128 # vulnerable 78 | h_pool3_flat = tf.reshape(h_pool3, [-1, drow]) 79 | 80 | keep_prob = tf.placeholder(tf.float32) 81 | 82 | W_fc0 = weight_variable([drow, 8192]) 83 | b_fc0 = bias_variable([8192]) 84 | h_fc0 = tf.nn.relu(tf.matmul(h_pool3_flat, W_fc0) + b_fc0) 85 | h_fc0_drop = tf.nn.dropout(h_fc0, keep_prob) 86 | 87 | W_fc1 = weight_variable([8192, 1024]) 88 | b_fc1 = bias_variable([1024]) 89 | h_fc1 = tf.nn.relu(tf.matmul(h_fc0_drop, W_fc1) + b_fc1) 90 | h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 91 | 92 | W_fc2 = weight_variable([1024, 10]) 93 | b_fc2 = bias_variable([10]) 94 | h_fc2 = tf.nn.relu(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 95 | h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob) 96 | 97 | W_fc3 = weight_variable([10, 1]) 98 | b_fc3 = bias_variable([1]) 99 | y_conv= tf.matmul(h_fc2_drop, W_fc3) + b_fc3 100 | 101 | error = tf.sqrt(tf.reduce_mean(tf.square(tf.sub(y_, y_conv)))) 102 | 103 | with tf.device('/gpu:0'): 104 | train_step = tf.train.GradientDescentOptimizer(1e-5).minimize(error) 105 | 106 | sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) 107 | sess.run(tf.initialize_all_variables()) 108 | 109 | saver_conv1 = tf.train.Saver({"w": W_conv1, "b": b_conv1}) 110 | saver_conv2 = tf.train.Saver({"w": W_conv2, "b": b_conv2}) 111 | saver_conv3 = tf.train.Saver({"w": W_conv3, "b": b_conv3}) 112 | saver_fc = tf.train.Saver({"w0": W_fc0, "b0": b_fc0, "w1": W_fc1, "b1": b_fc1, "w2": W_fc2, "b2": b_fc2, "w3": W_fc3, "b3": b_fc3}) 113 | 114 | restore(saver_conv1, sess, name='conv1') 115 | restore(saver_conv2, sess, name='conv2') 116 | restore(saver_conv3, sess, name='conv3') 117 | restore(saver_fc, sess, name='fc') 118 | 119 | 120 | for i in range(20000): 121 | accu = 0.0 122 | for j in range(len(train_x)): 123 | err = 0.0 124 | shape = train_x[j].shape 125 | batch_x = image_crop(train_x[j].get_data(), [o0, o1, o2]) 126 | batch_x = zoom(batch_x, 1/dn) 127 | batch_x = (batch_x.reshape(1, d) - 53) / input_max 128 | batch_y = np.array([[train_y[j]]]) 129 | fetches = [train_step, error, y_conv] 130 | t = sess.run(fetches, feed_dict={x: batch_x, y_: batch_y, keep_prob: 0.3}) 131 | err += t[1] 132 | pred = t[2] 133 | print(i, j, err, pred) 134 | 135 | saver_conv1.save(sess, "./tmp/model_conv1.ckpt") 136 | saver_conv2.save(sess, "./tmp/model_conv2.ckpt") 137 | saver_conv3.save(sess, "./tmp/model_conv3.ckpt") 138 | saver_fc.save(sess, "./tmp/model_fc.ckpt") 139 | -------------------------------------------------------------------------------- /mri_convolutional_neuralnet_3dim_vgg.py: -------------------------------------------------------------------------------- 1 | import os 2 | import math 3 | import numpy as np 4 | import tensorflow as tf 5 | from utils import * 6 | from scipy.ndimage.interpolation import zoom 7 | 8 | train_x, train_y = load_train_data() 9 | min_age, max_age = min(train_y), max(train_y) 10 | 11 | dn = 2 12 | o0, o1, o2 = 300, 300, 200 #360, 512, 216 13 | d0, d1, d2 = round(o0/dn), round(o1/dn), round(o2/dn) 14 | d = d0 * d1 * d2 15 | 16 | n_output = 1 17 | p = 2 # stride size in pooling layer 18 | 19 | 20 | def weight_variable(shape): 21 | initial = tf.truncated_normal(shape, stddev=0.1) 22 | return tf.Variable(initial) 23 | 24 | 25 | def bias_variable(shape): 26 | initial = tf.constant(0.1, shape=shape) 27 | return tf.Variable(initial) 28 | 29 | 30 | def conv2d(x, W): 31 | return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 32 | 33 | 34 | def conv3d(x, W): 35 | return tf.nn.conv3d(x, W, strides=[1, 1, 1, 1, 1], padding='SAME') 36 | 37 | 38 | def max_pool_2x2(x): 39 | return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], 40 | strides=[1, 2, 2, 1], padding='SAME') 41 | 42 | 43 | def max_pool_2x2x2(x): 44 | return tf.nn.max_pool3d(x, ksize=[1, p, p, p, 1], 45 | strides=[1, p, p, p, 1], padding='SAME') 46 | 47 | 48 | def restore(saver, sess, name=''): 49 | fname = "./tmp/model_" + name + ".ckpt" 50 | if os.path.isfile(fname): 51 | saver.restore(sess, fname) 52 | 53 | 54 | with tf.device('/cpu:0'): 55 | x = tf.placeholder(tf.float32, shape=[None, d]) 56 | y_ = tf.placeholder(tf.float32, shape=[None, n_output]) 57 | 58 | x_image = tf.reshape(x, [-1, d0, d1, d2, 1]) 59 | 60 | W_conv1a = weight_variable([3, 3, 3, 1, 64]) 61 | b_conv1a = bias_variable([64]) 62 | h_conv1a = tf.nn.relu(conv3d(x_image, W_conv1a) + b_conv1a) 63 | 64 | W_conv1b = weight_variable([3, 3, 3, 64, 64]) 65 | b_conv1b = bias_variable([64]) 66 | x_image = tf.reshape(x, [-1, d0, d1, d2, 1]) 67 | h_conv1b = tf.nn.relu(conv3d(h_conv1a, W_conv1b) + b_conv1b) 68 | 69 | h_pool1 = max_pool_2x2x2(h_conv1b) 70 | 71 | W_conv2a = weight_variable([3, 3, 3, 64, 128]) 72 | b_conv2a = bias_variable([128]) 73 | h_conv2a = tf.nn.relu(conv3d(h_pool1, W_conv2a) + b_conv2a) 74 | 75 | W_conv2b = weight_variable([3, 3, 3, 128, 128]) 76 | b_conv2b = bias_variable([128]) 77 | h_conv2b = tf.nn.relu(conv3d(h_conv2a, W_conv2b) + b_conv2b) 78 | 79 | h_pool2 = max_pool_2x2x2(h_conv2b) 80 | 81 | W_conv3a = weight_variable([3, 3, 3, 128, 256]) 82 | b_conv3a = bias_variable([256]) 83 | h_conv3a = tf.nn.relu(conv3d(h_pool2, W_conv3a) + b_conv3a) 84 | 85 | W_conv3b = weight_variable([3, 3, 3, 256, 256]) 86 | b_conv3b = bias_variable([256]) 87 | h_conv3b = tf.nn.relu(conv3d(h_conv3a, W_conv3b) + b_conv3b) 88 | 89 | h_pool3 = max_pool_2x2x2(h_conv3b) 90 | 91 | W_conv4a = weight_variable([3, 3, 3, 256, 512]) 92 | b_conv4a = bias_variable([512]) 93 | h_conv4a = tf.nn.relu(conv3d(h_pool3, W_conv4a) + b_conv4a) 94 | 95 | W_conv4b = weight_variable([3, 3, 3, 512, 512]) 96 | b_conv4b = bias_variable([512]) 97 | h_conv4b = tf.nn.relu(conv3d(h_conv4a, W_conv4b) + b_conv4b) 98 | 99 | W_conv4c = weight_variable([3, 3, 3, 512, 512]) 100 | b_conv4c = bias_variable([512]) 101 | h_conv4c = tf.nn.relu(conv3d(h_conv4b, W_conv4c) + b_conv4c) 102 | 103 | h_pool4 = max_pool_2x2x2(h_conv4c) 104 | 105 | W_conv5a = weight_variable([3, 3, 3, 512, 512]) 106 | b_conv5a = bias_variable([512]) 107 | h_conv5a = tf.nn.relu(conv3d(h_pool4, W_conv5a) + b_conv5a) 108 | 109 | W_conv5b = weight_variable([3, 3, 3, 512, 512]) 110 | b_conv5b = bias_variable([512]) 111 | h_conv5b = tf.nn.relu(conv3d(h_conv5a, W_conv5b) + b_conv5b) 112 | 113 | W_conv5c = weight_variable([3, 3, 3, 512, 512]) 114 | b_conv5c = bias_variable([512]) 115 | h_conv5c = tf.nn.relu(conv3d(h_conv5b, W_conv5c) + b_conv5c) 116 | 117 | h_pool5 = max_pool_2x2x2(h_conv5c) 118 | 119 | n_pool = 5 120 | dt = 2 ** n_pool 121 | drow = math.ceil(d0/dt) * math.ceil(d1/dt) * math.ceil(d2/dt) * 512 # vulnerable 122 | h_pool5_flat = tf.reshape(h_pool5, [-1, drow]) 123 | 124 | keep_prob = tf.placeholder(tf.float32) 125 | 126 | with tf.device('/gpu:0'): 127 | W_fc0 = weight_variable([drow, 8192]) 128 | b_fc0 = bias_variable([8192]) 129 | h_fc0 = tf.nn.relu(tf.matmul(h_pool5_flat, W_fc0) + b_fc0) 130 | h_fc0_drop = tf.nn.dropout(h_fc0, keep_prob) 131 | 132 | W_fc1 = weight_variable([8192, 1024]) 133 | b_fc1 = bias_variable([1024]) 134 | h_fc1 = tf.nn.relu(tf.matmul(h_fc0_drop, W_fc1) + b_fc1) 135 | h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 136 | 137 | W_fc2 = weight_variable([1024, 10]) 138 | b_fc2 = bias_variable([10]) 139 | h_fc2 = tf.nn.relu(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 140 | h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob) 141 | 142 | W_fc3 = weight_variable([10, 1]) 143 | b_fc3 = bias_variable([1]) 144 | y_conv= tf.matmul(h_fc2_drop, W_fc3) + b_fc3 145 | 146 | #error = tf.sqrt(tf.reduce_mean(tf.square(tf.sub(y_, y_conv)))) 147 | error = tf.abs(tf.sub(y_, y_conv)) 148 | 149 | train_step = tf.train.GradientDescentOptimizer(2e-5).minimize(error) 150 | 151 | sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) 152 | sess.run(tf.initialize_all_variables()) 153 | saver = tf.train.Saver() 154 | restore(saver, sess, name='3dim_vgg') 155 | 156 | 157 | for i in range(20000): 158 | error_sum = 0.0 159 | for j in range(len(train_x)): 160 | err = 0.0 161 | shape = train_x[j].shape 162 | batch_x = normalize_image(crop_image(train_x[j].get_data(), [o0, o1, o2])) 163 | r0, r1, r2 = np.random.choice(o0, d0), np.random.choice(o1, d1), np.random.choice(o2, d2) 164 | batch_x = batch_x[r0,:,:][:,r1,:][:,:,r2].reshape(1, d) 165 | batch_y = np.array([[train_y[j]]]) 166 | fetches = [train_step, error, y_conv] 167 | t = sess.run(fetches, feed_dict={x: batch_x, y_: batch_y, keep_prob: 0.3}) 168 | err = t[1] 169 | error_sum += err[0][0] 170 | pred = t[2] 171 | print(i, j, train_y[j], pred[0][0], err, error_sum) 172 | if j%2 == 0: 173 | msg = '{} {} {} {} {}'.format(i, j, pred[0][0], err[0][0], error_sum) 174 | os.system("curl \"https://api.telegram.org/bot236245101:AAFZ12aHX2emHeZuU99R11TdWMk9fZfl1j0/sendMessage?chat_id=237652977&text=" + msg + "\"") 175 | print('') 176 | saver.save(sess, "./tmp/model_3dim_vgg.ckpt") 177 | -------------------------------------------------------------------------------- /mri_neuralnet.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import tensorflow as tf 3 | from utils import * 4 | 5 | r_range = 0.1 6 | i_max = 1480 7 | train_x, train_y = load_train_data() 8 | 9 | min_age, max_age = min(train_y), max(train_y) 10 | input_size = 360 * 512 11 | output_size = 1 # len(set(train_y)) 12 | 13 | sess = tf.InteractiveSession() 14 | 15 | X = tf.placeholder(tf.float32, shape=[None, input_size]) 16 | y_ = tf.placeholder(tf.float32, shape=[None, output_size]) 17 | 18 | W0 = tf.Variable(tf.random_uniform((input_size, 100), -r_range, r_range), name='W') 19 | b0 = tf.Variable(tf.random_uniform((100,), -r_range, r_range), name='b') 20 | 21 | W1 = tf.Variable(tf.random_uniform((100, output_size), -r_range, r_range), name='W') 22 | b1 = tf.Variable(tf.random_uniform((output_size,), -r_range, r_range), name='b') 23 | 24 | sess.run(tf.initialize_all_variables()) 25 | 26 | h0 = tf.matmul(X, W0) + b0 27 | y0 = tf.nn.sigmoid(h0) 28 | 29 | h1 = tf.matmul(y0, W1) + b1 30 | y1 = tf.nn.sigmoid(h1) 31 | 32 | cost = tf.reduce_sum(tf.abs(tf.sub(y1, y_))) 33 | 34 | optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(cost) 35 | #optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) 36 | 37 | for i in range(1000): 38 | for j in range(len(train_x)): 39 | print(i, j) 40 | if train_x[j].shape[0] * train_x[j].shape[1] != input_size: 41 | continue 42 | 43 | batch_x = np.max(train_x[j].get_data(), axis=2).reshape(1, input_size) / i_max 44 | batch_y = np.array([[train_y[j]/(max_age - min_age)]]) 45 | optimizer.run(feed_dict={X: batch_x, y_: batch_y}) 46 | 47 | s = 0 48 | for j in range(len(train_x)): 49 | if train_x[j].shape[0] * train_x[j].shape[1] != input_size: 50 | continue 51 | 52 | batch_x = np.max(train_x[j].get_data(), axis=2).reshape(1, input_size) / i_max 53 | batch_y = np.array([[(train_y[j] - min_age) / (max_age - min_age)]]) 54 | s += sess.run(cost, feed_dict={X: batch_x, y_: batch_y}) 55 | 56 | print(s) 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import csv 3 | import numpy as np 4 | import nibabel as nib 5 | import matplotlib.pyplot as plt 6 | from scipy.ndimage.interpolation import zoom 7 | 8 | data_num = 111 9 | meta_file = './data/meta.csv' 10 | img_dir = './data/img/' 11 | pre_dir = './data/pre/' 12 | ixi_dir = './data/IXI-MRA/' 13 | ixi_meta_file = './data/ixi_meta.csv' 14 | 15 | incorrect_indices = [0, 11, 27, 47] 16 | validation_indices = [31, 21, 2, 65, 100, 3, 30, 84] # ages [48, 55, 58, 63, 66, 71, 77, 40] 17 | train_indices = [ i for i in range(data_num) 18 | if i not in incorrect_indices and i not in validation_indices] 19 | 20 | invalid_ixi_key = [35,44,116,145,172,230,231,232,234,238,290,291,292,293,294,303,305,306,307,310,314,315,322,331,332,371,372,373,378,382,388,395,423,424,425,426,427,430,433,434,442,462,463,464,469,470,473,474,475,476,477,478,479,480,505,510,517,532,534,541,542,543,547,548,553,561,563,571,573,574,580,588,593,595,596,597] 21 | #580 shape (1024,1024,92) 22 | #116 doesn't exist 23 | 24 | def save_norm_image(): 25 | imgs, ages = load_ixi_data() 26 | for k in imgs: 27 | np.save('./data/ixi_mra_mip/crop/' + str(k) + '.npy', normalize_image(np.max(crop_image(imgs[k].get_data(), [300, 450, 100]), axis=2))) 28 | 29 | 30 | def load_ixi_data(): 31 | f = open(ixi_meta_file, encoding='utf-8') 32 | reader = csv.reader(f) 33 | ages = {} 34 | for i, row in enumerate(reader): 35 | if not row[1] == '' and int(row[0]) not in invalid_ixi_key: 36 | ages[int(row[0])] = float(row[1]) 37 | 38 | imgs = {} 39 | files = os.listdir(ixi_dir) 40 | for f in files: 41 | index = int(f[3:6]) 42 | if index in ages: 43 | imgs[index] = nib.load(ixi_dir + f) 44 | 45 | redundants = ages.keys() - imgs.keys() 46 | for k in redundants: 47 | ages.pop(k, None) 48 | return imgs, ages 49 | 50 | 51 | def load_train_data(): 52 | return load_data(train_indices) 53 | 54 | 55 | def load_validation_data(): 56 | return load_data(validation_indices) 57 | 58 | 59 | def load_data(indices): 60 | return data_imgs(indices), data_ages(indices) 61 | 62 | 63 | def data_imgs(indices): 64 | return [__data_img(i) for i in indices] 65 | # return [__preprocessed_img(i) for i in indices] 66 | 67 | 68 | # return iso.nii and voxel.nii file 69 | def __data_img(i): 70 | num = '%03d' % (i+1) 71 | path = img_dir + num + '/' 72 | files = os.listdir(path) 73 | files = [f for f in files if f.endswith('voxel.nii')] 74 | return nib.load(path + files[0]) 75 | 76 | 77 | def __preprocessed_img(i): 78 | path = pre_dir + str(i) + '.nii.gz' 79 | return nib.load(path) 80 | 81 | 82 | def data_ages(indices): 83 | f = open(meta_file, encoding='utf-8') 84 | reader = csv.reader(f) 85 | header = next(reader, None) 86 | age_index = header.index('age') 87 | ages = [int(row[age_index]) for i, row in enumerate(reader) if i in indices] 88 | return ages 89 | 90 | 91 | def crop_image(x, shape=[]): 92 | s = x.shape 93 | if len(s) == 2: 94 | c0, c1 = shape[0], shape[1] 95 | l0, l1 = round((s[0]-c0)/2), round((s[1]-c1)/2) 96 | return x[l0:l0+c0, l1:l1+c1] 97 | c0, c1, c2 = shape[0], shape[1], shape[2] 98 | l0, l1, l2 = round((s[0]-c0)/2), round((s[1]-c1)/2), round((s[2]-c2)/2) 99 | return x[l0:l0+c0, l1:l1+c1, l2:l2+c2] 100 | 101 | 102 | def normalize_image(x): 103 | std = np.std(x) 104 | avg = np.mean(x) 105 | return (x - avg) / (1e-6 + std) 106 | 107 | 108 | def save_sample(indices, preprocess=False, crop=[], z=1): 109 | for d in indices: 110 | prefix = '' 111 | img = __data_img(d).get_data() 112 | 113 | if preprocess: 114 | img = preprocess(img) 115 | prefix += 'p' 116 | 117 | if len(crop) == 3: 118 | img = image_crop(img, crop) 119 | prefix += 'c' 120 | 121 | if zoom != 1: 122 | img = zoom(img, 1/z) 123 | prefix += 'z' 124 | 125 | for i in range(3): 126 | x = np.max(img, axis=i) 127 | plt.imsave('data/sample/' + prefix + d + '_' + str(i), x) 128 | 129 | 130 | def save_preprocessed(): 131 | indices = train_indices + validation_indices 132 | indices = sorted(indices)[80:] 133 | print(indices) 134 | for i in indices: 135 | print(i) 136 | img = __data_img(i) 137 | p = nib.Nifti1Image(preprocess(img.get_data()), np.eye(4)) 138 | nib.save(p, './data/pre/' + str(i) + '.nii.gz') 139 | 140 | 141 | def preprocess(img): 142 | p = np.max(img, axis=0) 143 | avg = np.average(p[100:130, :30]) 144 | avg2= np.average(p[360:420, :60]) 145 | avg = max(avg, avg2) 146 | avg = avg * 1.15 147 | img[img older else [0,1,0] for k in keys] 95 | return fetch_x, fetch_y 96 | 97 | 98 | def divide_set(keys): 99 | trains = [k for i, k in enumerate(keys) if i % 9 != 0] 100 | valids = [k for i, k in enumerate(keys) if i % 9 == 0] 101 | return trains, valids 102 | 103 | 104 | imgs, ages = load_ixi_data() 105 | del imgs 106 | 107 | youngs = [k for k, v in ages.items() if v <= younger] 108 | middles = [k for k, v in ages.items() if v >= younger + padding and v < older - padding] 109 | olds = [k for k, v in ages.items() if v > older] 110 | 111 | print("The Number of Samples per Class: ", len(youngs), len(middles), len(olds)) 112 | 113 | young_train, young_valid = divide_set(youngs) 114 | middle_train, middle_valid = divide_set(middles) 115 | old_train, old_valid = divide_set(olds) 116 | 117 | x_train = np.vstack((young_train, middle_train, old_train)).reshape((-1,),order='F') #interweave 118 | x_valid = np.array(young_valid + middle_valid + old_valid) 119 | 120 | o0, o1, o2 = 300, 450, 100 #512, 512, 100 121 | d0, d1, d2 = o1 * o2, o0 * o2, o0 * o1 122 | 123 | n_output = 3 124 | 125 | with tf.device('/gpu:0'): 126 | x = tf.placeholder(tf.float32, shape=[None, d2]) 127 | y_ = tf.placeholder(tf.float32, shape=[None, n_output]) 128 | drop = tf.placeholder(tf.float32) 129 | 130 | h_vgg, r2 = vgg2d(x, o0, o1) 131 | h_fc0 = fc2d(h_vgg, r2, 4096, drop) 132 | h_fc1 = fc2d(h_fc0, 4096, 1024, drop) 133 | h_fc2 = fc2d(h_fc1, 1024, 128, drop) 134 | h_fc3 = fc2d(h_fc2, 128, 10, drop) 135 | 136 | W_fc3 = weight_variable([10, n_output]) 137 | b_fc3 = bias_variable([n_output]) 138 | y_conv= tf.nn.softmax(tf.matmul(h_fc3, W_fc3) + b_fc3) 139 | # error = tf.reduce_mean(tf.abs(y_ - y_conv)) 140 | error = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1])) 141 | 142 | with tf.device('/gpu:0'): 143 | train_step = tf.train.GradientDescentOptimizer(0.005).minimize(error) 144 | 145 | sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) 146 | # sess.run(tf.initialize_all_variables()) 147 | saver = tf.train.Saver() 148 | restore(saver, sess, '2dim_vgg_3class') 149 | 150 | batch_size = 20 151 | 152 | for i in range(200000): 153 | batch_indices = np.random.choice(len(x_train), batch_size, replace=False) 154 | batch_x, batch_y = fetch(x_train[batch_indices]) 155 | fetches = [train_step, error, y_conv] 156 | t = sess.run(fetches, feed_dict={x: batch_x, y_: batch_y, drop: 0.5}) 157 | 158 | miss = np.count_nonzero(np.argmax(t[2], axis=1) - np.argmax(batch_y, axis=1)) 159 | print(i, t[1], miss) 160 | 161 | if i % 100 == 0: 162 | batch_x, batch_y = fetch(x_valid) 163 | fetches = [error, y_conv] 164 | t = sess.run(fetches, feed_dict={x: batch_x, y_: batch_y, drop: 0.5}) 165 | miss = np.count_nonzero(np.argmax(t[1], axis=1) - np.argmax(batch_y, axis=1)) 166 | print('VALIDATION: ', t[0], miss) 167 | 168 | 169 | # msg = '{} {}'.format(i, err) 170 | # os.system("curl \"https://api.telegram.org/bot236245101:AAFZ12aHX2emHeZuU99R11TdWMk9fZfl1j0/sendMessage?chat_id=237652977&text=" + msg + "\"") 171 | #print('') 172 | if i % 1000 == 0: 173 | saver.save(sess, "./tmp/model_2dim_vgg_3class.ckpt") 174 | -------------------------------------------------------------------------------- /vgg_3dim.py: -------------------------------------------------------------------------------- 1 | import os 2 | import math 3 | import itertools 4 | import numpy as np 5 | import tensorflow as tf 6 | from utils import * 7 | from scipy.ndimage.interpolation import zoom 8 | 9 | 10 | def weight_variable(shape): 11 | initial = tf.truncated_normal(shape, stddev=0.025) 12 | return tf.Variable(initial) 13 | 14 | 15 | def bias_variable(shape): 16 | initial = tf.constant(0.1, shape=shape) 17 | return tf.Variable(initial) 18 | 19 | 20 | def fc2d(x, d0, d1, drop): 21 | W_fc = weight_variable([d0, d1]) 22 | b_fc = bias_variable([d1]) 23 | h_fc = tf.nn.relu(tf.matmul(x, W_fc) + b_fc) 24 | h_fc_drop = tf.nn.dropout(h_fc, drop) 25 | return h_fc_drop 26 | 27 | 28 | def conv3d(x, W): 29 | return tf.nn.conv3d(x, W, strides=[1, 1, 1, 1, 1], padding='SAME') 30 | 31 | 32 | def max_pool_2x2x2(x): 33 | return tf.nn.max_pool3d(x, ksize=[1, 2, 2, 2, 1], 34 | strides=[1, 2, 2, 2, 1], padding='SAME') 35 | 36 | 37 | def convl(l, r1, r2): 38 | W_conv = weight_variable([3, 3, 3, r1, r2]) 39 | b_conv = bias_variable([r2]) 40 | h_conv = tf.nn.relu(conv3d(l, W_conv) + b_conv) 41 | return h_conv 42 | 43 | 44 | def vgg3d(x, d0, d1, d2): 45 | d = d0 * d1 * d2 46 | x_image = tf.reshape(x, [-1, d0, d1, d2, 1]) 47 | 48 | n_pool = 6 49 | dt = 2 ** n_pool 50 | drow = math.ceil(d0/dt) * math.ceil(d1/dt) * math.ceil(d2/dt) * 2048 51 | 52 | with tf.device('/gpu:0'): 53 | conv1a = convl(x_image, 1, 64) 54 | conv1b = convl(conv1a, 64, 64) 55 | h_pool1 = max_pool_2x2x2(conv1b) 56 | 57 | conv2a = convl(h_pool1, 64, 128) 58 | conv2b = convl(conv2a, 128, 128) 59 | h_pool2 = max_pool_2x2x2(conv2b) 60 | 61 | conv3a = convl(h_pool2, 128, 256) 62 | conv3b = convl(conv3a, 256, 256) 63 | h_pool3 = max_pool_2x2x2(conv3b) 64 | 65 | conv4a = convl(h_pool3, 256, 512) 66 | conv4b = convl(conv4a, 512, 512) 67 | # conv4c = convl(conv4b, 512, 512) 68 | h_pool4 = max_pool_2x2x2(conv4b) 69 | 70 | conv5a = convl(h_pool4, 512, 1024) 71 | conv5b = convl(conv5a, 1024, 1024) 72 | # conv5c = convl(conv5b, 1024, 1024) 73 | h_pool5 = max_pool_2x2x2(conv5b) 74 | 75 | conv6a = convl(h_pool5, 1024, 2048) 76 | conv6b = convl(conv6a, 2048, 2048) 77 | # conv6c = convl(conv6b, 2048, 2048) 78 | h_pool6 = max_pool_2x2x2(conv6b) 79 | 80 | h_pool6_flat = tf.reshape(h_pool6, [-1, drow]) 81 | # conv7a = convl(h_pool6, 2048, 4096) 82 | # conv7b = convl(conv7a, 4096, 4096) 83 | # conv7c = convl(conv7b, 4096, 4096) 84 | # h_pool7 = max_pool_2x2x2(conv7c) 85 | 86 | return h_pool6_flat, drow 87 | 88 | ckpt_name = '3d_vgg_3cls' 89 | def restore(saver, sess, name=ckpt_name): 90 | fname = "./tmp/model_" + name + ".ckpt" 91 | if os.path.isfile(fname): 92 | saver.restore(sess, fname) 93 | 94 | younger = 25 95 | older = 72.14 96 | padding = 20.38 97 | 98 | d0, d1, d2 = 192, 192, 100 #512, 512, 100 99 | d = d0 * d1 * d2 100 | 101 | n_output = 3 102 | 103 | def fetch(keys): 104 | fetch_x = [normalize_image(crop_image(imgs[k].get_data(), [d0, d1, d2])).reshape(-1) for k in keys] 105 | fetch_y = [[1,0,0] if ages[k] <= younger else [0,0,1] if ages[k] > older else [0,1,0] for k in keys] 106 | return fetch_x, fetch_y 107 | 108 | 109 | def divide_set(keys): 110 | trains = [k for i, k in enumerate(keys) if i % 9 != 0] 111 | valids = [k for i, k in enumerate(keys) if i % 9 == 0] 112 | return trains, valids 113 | 114 | 115 | imgs, ages = load_ixi_data() 116 | 117 | youngs = [k for k, v in ages.items() if v <= younger] 118 | middles = [k for k, v in ages.items() if v >= younger + padding and v < older - padding] 119 | olds = [k for k, v in ages.items() if v > older] 120 | 121 | print("The Number of Samples per Class: ", len(youngs), len(middles), len(olds)) 122 | 123 | 124 | young_train, young_valid = divide_set(youngs) 125 | middle_train, middle_valid = divide_set(middles) 126 | old_train, old_valid = divide_set(olds) 127 | 128 | 129 | x_train = np.vstack((young_train, middle_train, old_train)).reshape((-1,),order='F') #interweave 130 | x_valid = np.array(young_valid + middle_valid + old_valid) 131 | 132 | # def fit_group_size(group, size): 133 | # l = len(group) 134 | # if l >= size: 135 | # return group[:size] 136 | # return fit_group_size(group + group, size) 137 | # 138 | # 139 | # def age_group(ages): 140 | # g20 = [k for k,v in ages.items() if v < 30] 141 | # g30 = [k for k,v in ages.items() if 30 <= v < 40] 142 | # g40 = [k for k,v in ages.items() if 40 <= v < 50] 143 | # g50 = [k for k,v in ages.items() if 50 <= v < 60] 144 | # g60 = [k for k,v in ages.items() if 60 <= v < 70] 145 | # g70 = [k for k,v in ages.items() if 70 <= v < 80] 146 | # g80 = [k for k,v in ages.items() if 80 <= v < 90] 147 | # return list(itertools.chain.from_iterable(zip(fit_group_size(g20, 100),fit_group_size(g30, 100),fit_group_size(g40, 100),fit_group_size(g50, 100),fit_group_size(g60, 100),fit_group_size(g70, 100),fit_group_size(g80, 100)))) 148 | # 149 | 150 | x = tf.placeholder(tf.float32, shape=[None, d]) 151 | y_ = tf.placeholder(tf.float32, shape=[None, n_output]) 152 | drop = tf.placeholder(tf.float32) 153 | h_vgg, r2 = vgg3d(x, d0, d1, d2) 154 | 155 | # with tf.device('/cpu:0'): 156 | 157 | with tf.device('/cpu:0'): 158 | h_fc0 = fc2d(h_vgg, r2, 4096, drop) 159 | 160 | with tf.device('/gpu:0'): 161 | h_fc1 = fc2d(h_fc0, 4096, 1024, drop) 162 | h_fc2 = fc2d(h_fc1, 1024, 128, drop) 163 | h_fc3 = fc2d(h_fc2, 128, 10, drop) 164 | 165 | W_fc4 = weight_variable([10, n_output]) 166 | b_fc4 = bias_variable([n_output]) 167 | y_conv= tf.nn.softmax(tf.matmul(h_fc3, W_fc4) + b_fc4) 168 | 169 | error = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1])) 170 | train_step = tf.train.GradientDescentOptimizer(1e-4).minimize(error) 171 | 172 | sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) 173 | sess.run(tf.initialize_all_variables()) 174 | saver = tf.train.Saver() 175 | restore(saver, sess) 176 | 177 | error_sum = 0.0 178 | for i in range(2000000): 179 | j = i%len(x_train) 180 | batch_x, batch_y = fetch([x_train[j]]) 181 | fetches = [train_step, error, y_conv] 182 | t = sess.run(fetches, feed_dict={x: batch_x, y_: batch_y, drop: 0.5}) 183 | err = t[1] 184 | error_sum += err 185 | pred = t[2] 186 | print(i, j, batch_y[0], pred[0], err, error_sum) 187 | 188 | if j == len(x_train)-1: 189 | saver.save(sess, "./tmp/model_" + ckpt_name + ".ckpt") 190 | error_sum = 0.0 191 | 192 | for k in range(len(x_valid)): 193 | batch_x, batch_y = fetch([x_valid[k]]) 194 | fetches = [error, y_conv] 195 | t = sess.run(fetches, feed_dict={x: batch_x, y_: batch_y, drop: 0.5}) 196 | err = t[0] 197 | pred = t[1] 198 | print('VALIDATION', k, batch_y[0], pred[0], err) 199 | 200 | msg = '{} {} {} {}'.format(k, err, np.array(batch_y[0]).argmax(), np.array(pred[0]).argmax()) 201 | os.system("curl \"https://api.telegram.org/bot236245101:AAFZ12aHX2emHeZuU99R11TdWMk9fZfl1j0/sendMessage?chat_id=237652977&text=" + msg + "\"") 202 | print('') 203 | --------------------------------------------------------------------------------