Exemplo n.º 1
0
	def get_nn(self, input_placer, gold_placer, keep_prob,
				learn_rate_placer, wd):
		self.x = input_placer
		self.y_ = gold_placer
		self.l_rate = learn_rate_placer
		self.keep_prob = keep_prob
		self.para_dict = FbUpBN2D(is_training = self.train_flag).generate_flow(input_placer,
												keep_prob,
												wd = wd)
		self.y_conv = self.para_dict['y_conv']
		self.y_res = self.para_dict['y_res']
		if self.train_flag:
			self.y_conv_all = self.y_conv
			self.y_conv_2x = dilation3D(self.y_conv_all)
			self.y_res_all = self.y_res
			self.shape = tf.shape(self.y_res)
			width = self.shape[1]
			self.y_conv = self.y_conv[:,2,2,2,:]
			self.y_res = self.y_res[:,2,2,2]

			cross_entropy_mean = -tf.reduce_mean(self.y_ * tf.log(self.y_conv))
			tf.add_to_collection('losses', cross_entropy_mean)
			cross_entropy = tf.add_n(tf.get_collection('losses'), name='total_loss')
			self.train_step = tf.train.AdamOptimizer(self.l_rate).minimize(cross_entropy)
			correct_prediction = tf.equal(tf.argmax(self.y_conv,1), tf.argmax(self.y_,1))
			self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
		else:
			self.y_conv_all = self.y_conv
			self.y_conv_2x = dilation3D(self.y_conv_all,tf.shape(input_placer))
			self.y_res_all = tf.expand_dims(self.y_res,4)
			self.y_res_2x = tf.squeeze(dilation3D(self.y_res_all,tf.shape(input_placer)),[4])
Exemplo n.º 2
0
    def get_nn(self, input_placer, gold_placer, keep_prob, learn_rate_placer,
               wd):
        self.x = input_placer
        self.y_ = gold_placer
        self.l_rate = learn_rate_placer
        self.keep_prob = keep_prob
        self.para_dict = dict()
        para_dict = FbUp().generate_flow(input_placer, keep_prob, wd=wd)
        self.y_conv = para_dict['y_conv']
        self.y_res = para_dict['y_res']
        if self.train_flag:
            self.y_conv_all = self.y_conv
            self.y_conv_2x = dilation3D(self.y_conv_all)
            self.y_res_all = self.y_res
            self.shape = tf.shape(self.y_res)
            width = self.shape[1]
            self.y_conv = self.y_conv[:, 3, 3, 3, :]
            self.y_res = self.y_res[:, 3, 3, 3]

            cross_entropy_mean = -tf.reduce_mean(self.y_ * tf.log(self.y_conv))
            tf.add_to_collection('losses', cross_entropy_mean)
            cross_entropy = tf.add_n(tf.get_collection('losses'),
                                     name='total_loss')
            self.train_step = tf.train.AdamOptimizer(
                self.l_rate).minimize(cross_entropy)
            correct_prediction = tf.equal(tf.argmax(self.y_conv, 1),
                                          tf.argmax(self.y_, 1))
            self.accuracy = tf.reduce_mean(tf.cast(correct_prediction,
                                                   "float"))
        else:
            self.y_conv_all = self.y_conv
            self.y_conv_2x = dilation3D(self.y_conv_all)
            self.y_res_all = self.y_res
            self.y_res_2x = dilation3D(self.y_res_all)
            self.para_dict['y_conv'] = self.y_conv_2x
            self.para_dict['y_res'] = self.y_res_2x
            self.para_dict['h_conv4'] = para_dict['h_conv4']
            self.para_dict['h_pool2'] = para_dict['h_pool2']
            self.para_dict['x'] = para_dict['x']
            self.para_dict['h_conv3'] = para_dict['h_conv3']
            self.para_dict['h_conv2'] = para_dict['h_conv2']
            self.para_dict['h_conv_out'] = para_dict['h_conv_out']
Exemplo n.º 3
0
    def get_nn(self,
               input_placer,
               gold_placer,
               learn_rate_placer,
               keep_prob,
               window_size,
               conv_1_neuron_num=256,
               conv_2_neuron_num=128,
               conv_3_neuron_num=64,
               conv_4_neuron_num=64,
               conv_5_neuron_num=64,
               conv_1=1,
               conv_2=3,
               conv_3=3,
               conv_4=3,
               conv_5=1,
               wd=1e-9,
               wdo=1e-9):
        #create sub classes
        self.x = input_placer
        self.y_ = gold_placer
        self.l_rate = learn_rate_placer
        self.keep_prob = keep_prob
        fg_dict = dict()
        y_conv_combined = []
        y_res_combined = []
        for angle_xy in range(30, 181, 30):
            for angle_xz in range(30, 181, 30):
                print("Create FG NN %d %d" % (angle_xy, angle_xz))
                fg_dict[(angle_xy,angle_xz)] = \
                 FbUpTrain(self.sess,input_placer,gold_placer,\
                  keep_prob,learn_rate_placer,\
                   angle_xy = angle_xy, \
                   angle_xz = angle_xz,
                   train_flag = False,
                   wd = 0e-9)
                fg_variables = tf.get_collection(
                    tf.GraphKeys.TRAINABLE_VARIABLES,
                    scope="fg_" + str(angle_xy) + '_' + str(angle_xz))
                fg_dict[(angle_xy,angle_xz)].saver = \
                 tf.train.Saver(fg_variables)
                #fg_dict[(angle_xy,angle_xz)].saver.restore(self.sess,\
                #		model_path + '/model_foreground_'+str(angle_xy)+'_'+str(angle_xz)+'.ckpt')
                y_conv_combined.append(
                    tf.expand_dims(
                        fg_dict[(angle_xy, angle_xz)].para_dict['h_conv4'], 5))
                #y_res_combined.append(
                #	tf.expand_dims(fg_dict[(angle_xy,angle_xz)].y_res,4))
        FEAT_NUM = 32
        self.fg_dict = fg_dict
        y_conv_combined = tf.concat(5, y_conv_combined)
        shape = tf.shape(y_conv_combined)
        dir_out = tf.reshape(
            y_conv_combined,
            [shape[0], shape[1], shape[2], shape[3], 36 * FEAT_NUM])
        #dir_out = tf.reshape(y_conv_combined,[-1,window_size,window_size,window_size,36 * FEAT_NUM])
        self.dir_out = dir_out
        with tf.variable_scope("transfer"):
            #h_pool2 = self.pool_3x3(h_conv2)
            with tf.variable_scope("transfer_layer_1"):
                W_conv1 = self.nn.weight_variable(
                    [conv_1, conv_1, conv_1, 36 * FEAT_NUM, conv_1_neuron_num],
                    wd, 1**3 * 36 * FEAT_NUM, conv_1**3 * conv_1_neuron_num)
                b_conv1 = self.nn.bias_variable([conv_1_neuron_num])
                h_conv1 = tf.nn.relu(
                    tf.nn.bias_add(self.nn.conv2d(dir_out, W_conv1), b_conv1))

            with tf.variable_scope("transfer_layer_2"):
                W_conv2 = self.nn.weight_variable([
                    conv_2, conv_2, conv_2, conv_1_neuron_num,
                    conv_2_neuron_num
                ], wd, conv_1**3 * conv_1_neuron_num, conv_2**3 *
                                                  conv_2_neuron_num)
                b_conv2 = self.nn.bias_variable([conv_2_neuron_num])
                h_conv2 = tf.nn.relu(
                    tf.nn.bias_add(self.nn.conv2d(h_conv1, W_conv2), b_conv2))

            with tf.variable_scope("transfer_layer_3"):
                W_conv3 = self.nn.weight_variable([
                    conv_3, conv_3, conv_3, conv_2_neuron_num,
                    conv_3_neuron_num
                ], wd, conv_2**3 * conv_2_neuron_num, conv_3**3 *
                                                  conv_3_neuron_num)
                b_conv3 = self.nn.bias_variable([conv_3_neuron_num])
                h_conv3 = tf.nn.relu(
                    tf.nn.bias_add(self.nn.conv2d(h_conv2, W_conv3), b_conv3))

            with tf.variable_scope("transfer_layer_4"):
                W_conv4 = self.nn.weight_variable([
                    conv_4, conv_4, conv_4, conv_3_neuron_num,
                    conv_4_neuron_num
                ], wd, conv_3**3 * conv_3_neuron_num, conv_4**3 *
                                                  conv_4_neuron_num)
                b_conv4 = self.nn.bias_variable([conv_4_neuron_num])
                h_conv4 = tf.nn.relu(
                    tf.nn.bias_add(self.nn.conv2d(h_conv3, W_conv4), b_conv4))

            with tf.variable_scope("transfer_layer_5"):
                W_conv5 = self.nn.weight_variable([
                    conv_5, conv_5, conv_5, conv_4_neuron_num,
                    conv_5_neuron_num
                ], wd, conv_4**3 * conv_4_neuron_num, conv_5**3 *
                                                  conv_5_neuron_num)
                b_conv5 = self.nn.bias_variable([conv_5_neuron_num])
                h_conv5 = tf.nn.relu(
                    tf.nn.bias_add(self.nn.conv2d(h_conv4, W_conv5), b_conv5))

            weight_decay = tf.mul(tf.nn.l2_loss(h_conv5),
                                  wd,
                                  name='weight_loss')
            tf.add_to_collection('losses', weight_decay)

            with tf.variable_scope("transfer_final"):
                W_fc1 = self.nn.weight_variable(
                    [1, 1, 1, conv_5_neuron_num, 2], wd=wdo)
                b_fc1 = self.nn.bias_variable([2])
                h_fc1 = tf.nn.relu(
                    tf.nn.bias_add(self.nn.conv2d(h_conv5, W_fc1), b_fc1))

        if self.predict_flag:
            final_shape = tf.shape(h_fc1)
            self.y_conv = tf.nn.softmax(h_fc1[:, (final_shape[1] - 1) / 2,
                                              (final_shape[2] - 1) / 2,
                                              (final_shape[3] - 1) / 2, :])
            #self.y_conv = tf.nn.softmax(h_fc1[:,(window_size-1)//2,(window_size-1)//2,(window_size-1)//2,:])
            self.y_res = tf.argmax(self.y_conv, 1)
        else:
            curr_shape = tf.shape(h_fc1)
            reshaped_conv = tf.reshape(h_fc1, [-1, 2])
            softmaxed_reshaped_conv = tf.nn.softmax(reshaped_conv)
            self.y_conv = tf.reshape(softmaxed_reshaped_conv, curr_shape)
            self.y_res = tf.argmax(self.y_conv, 4)
            self.y_conv_2x = dilation3D(self.y_conv)
            tmp = tf.expand_dims(self.y_res, 4)
            self.y_res_2x = tf.squeeze(dilation3D(tmp))

        if self.valid_flag:
            cross_entropy_mean = -tf.reduce_mean(self.y_ * tf.log(self.y_conv))
            tf.add_to_collection('losses', cross_entropy_mean)
            cross_entropy = tf.add_n(tf.get_collection('losses'),
                                     name='total_loss')

            transfer_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                              "transfer")

            self.train_step = tf.train.AdamOptimizer(self.l_rate).minimize(
                cross_entropy, var_list=transfer_vars)
            correct_prediction = tf.equal(tf.argmax(self.y_conv, 1),
                                          tf.argmax(self.y_, 1))
            self.accuracy = tf.reduce_mean(tf.cast(correct_prediction,
                                                   "float"))
        else:
            print("Transfer Not For Training Singly")
Exemplo n.º 4
0
    def get_nn(self,
               input_placer,
               gold_placer,
               learn_rate_placer,
               keep_prob,
               window_size,
               conv_1_neuron_num=256,
               conv_2_neuron_num=128,
               conv_3_neuron_num=64,
               conv_4_neuron_num=64,
               conv_5_neuron_num=64,
               conv_6_neuron_num=64,
               conv_7_neuron_num=64,
               up_conv_1_neuron_num=256,
               up_conv_1=3,
               conv_1=1,
               conv_2=3,
               conv_3=3,
               conv_4=3,
               conv_5=3,
               conv_6=3,
               conv_7=1,
               wd=1e-9,
               wdo=1e-9):
        if self.predict_flag:
            padding = 'VALID'
        else:
            padding = 'SAME'
        #create sub classes
        self.x = input_placer
        self.y_ = gold_placer
        self.l_rate = learn_rate_placer
        self.keep_prob = keep_prob
        fg_dict = dict()
        y_conv_combined = []
        y_res_combined = []
        for angle_xy in range(30, 181, 30):
            for angle_xz in range(30, 181, 30):
                print("Create FG NN %d %d" % (angle_xy, angle_xz))
                fg_dict[(angle_xy,angle_xz)] = \
                 FbUpTrainBN(self.sess,input_placer,gold_placer,\
                  keep_prob,learn_rate_placer,\
                   angle_xy = angle_xy, \
                   angle_xz = angle_xz,
                   train_flag = False,
                   wd = 0e-9)
                fg_variables = tf.get_collection(tf.GraphKeys.VARIABLES,
                                                 scope="fg_" + str(angle_xy) +
                                                 '_' + str(angle_xz))
                fg_dict[(angle_xy,angle_xz)].saver = \
                 tf.train.Saver(fg_variables)
                #fg_dict[(angle_xy,angle_xz)].saver.restore(self.sess,\
                #		model_path + '/model_foreground_'+str(angle_xy)+'_'+str(angle_xz)+'.ckpt')
                y_conv_combined.append(
                    fg_dict[(angle_xy, angle_xz)].para_dict['h_conv3'])
                #y_res_combined.append(
                #	tf.expand_dims(fg_dict[(angle_xy,angle_xz)].y_res,4))
        FEAT_NUM = 16
        self.fg_dict = fg_dict
        y_conv_combined = tf.concat(4, y_conv_combined)
        dir_out = y_conv_combined

        self.dir_out = dir_out
        with tf.variable_scope("transfer"):

            with tf.variable_scope("transfer_layer_1"):
                W_conv1 = self.nn.weight_variable(
                    [conv_1, conv_1, conv_1, 36 * FEAT_NUM, conv_1_neuron_num],
                    wd, 1**3 * 36 * FEAT_NUM, conv_1**3 * conv_1_neuron_num)
                tmp_1 = self.nn.conv2d(dir_out, W_conv1, padding=padding)
                h_conv1 = tf.nn.relu(
                    self.batch_norm_layer(tmp_1, self.predict_flag))

            with tf.variable_scope("transfer_layer_2"):
                W_conv2 = self.nn.weight_variable([
                    conv_2, conv_2, conv_2, conv_1_neuron_num,
                    conv_2_neuron_num
                ], wd, conv_1**3 * 2 * conv_1_neuron_num, conv_2**3 *
                                                  conv_2_neuron_num)
                tmp_2 = self.nn.conv2d(h_conv1, W_conv2, padding=padding)
                h_conv2 = tf.nn.relu(
                    self.batch_norm_layer(tmp_2, self.predict_flag))
            """
			with tf.variable_scope("transfer_layer_3"):
				W_conv3 = self.nn.weight_variable([conv_3,conv_3,conv_3,conv_2_neuron_num,conv_3_neuron_num],wd,conv_2 ** 3 * conv_2_neuron_num, conv_3 ** 3 * conv_3_neuron_num)
				b_conv3 = self.nn.bias_variable([conv_3_neuron_num])
				h_conv3 = tf.nn.relu(tf.nn.bias_add(self.nn.conv2d(h_conv2, W_conv3, padding = padding) , b_conv3))

			with tf.variable_scope("transfer_layer_4"):
				W_conv4 = self.nn.weight_variable([conv_4,conv_4,conv_4,conv_3_neuron_num,conv_4_neuron_num],wd,conv_3 ** 3 * conv_3_neuron_num,conv_4 ** 3 * conv_4_neuron_num)
				b_conv4 = self.nn.bias_variable([conv_4_neuron_num])
				if self.predict_flag:
					residual_4 = h_conv3[:,1:8,1:8,1:8,:]
				else:
					residual_4 = h_conv3
				h_conv4 = tf.nn.relu(tf.add(tf.nn.bias_add(self.nn.conv2d(h_conv3, W_conv4, padding = padding) , b_conv4),residual_4))
		
			with tf.variable_scope("transfer_layer_5"):
				W_conv5 = self.nn.weight_variable([conv_5,conv_5,conv_5,conv_4_neuron_num,conv_5_neuron_num],wd,conv_4 ** 3 * conv_4_neuron_num,conv_5 ** 3 * conv_5_neuron_num)
				b_conv5 = self.nn.bias_variable([conv_5_neuron_num])
				if self.predict_flag:
					residual_5 = h_conv4[:,1:6,1:6,1:6,:]
				else:
					residual_5 = h_conv4
				h_conv5 = tf.nn.relu(tf.add(tf.nn.bias_add(self.nn.conv2d(h_conv4, W_conv5, padding = padding) , b_conv5),residual_5))	

			with tf.variable_scope("transfer_layer_6"):
				W_conv6 = self.nn.weight_variable([conv_6,conv_6,conv_6,conv_5_neuron_num,conv_6_neuron_num],wd,conv_5 ** 3 * conv_5_neuron_num,conv_6 ** 3 * conv_6_neuron_num)
				b_conv6 = self.nn.bias_variable([conv_5_neuron_num])
				if self.predict_flag:
					residual_6 = h_conv5[:,1:4,1:4,1:4,:]
				else:
					residual_6 = h_conv5
				h_conv6 = tf.nn.relu(tf.add(tf.nn.bias_add(self.nn.conv2d(h_conv5, W_conv6, padding = padding) , b_conv6),residual_6))

			with tf.variable_scope("transfer_layer_7"):
				W_conv7 = self.nn.weight_variable([conv_7,conv_7,conv_7,conv_6_neuron_num,conv_7_neuron_num],wd,conv_6 ** 3 * conv_6_neuron_num,conv_7 ** 3 * conv_7_neuron_num)
				b_conv7 = self.nn.bias_variable([conv_7_neuron_num])
				h_conv7 = tf.nn.relu(tf.nn.bias_add(self.nn.conv2d(h_conv6, W_conv7, padding = padding) , b_conv7))	

			weight_decay = tf.mul(tf.nn.l2_loss(h_conv7), wd, name='weight_loss')
			tf.add_to_collection('losses', weight_decay)
			"""
            with tf.variable_scope("transfer_final"):
                W_fc1 = self.nn.weight_variable(
                    [1, 1, 1, conv_2_neuron_num, 2], wd=wdo)
                b_fc1 = self.nn.bias_variable([2])
                h_fc1 = (tf.nn.bias_add(
                    self.nn.conv2d(h_conv2, W_fc1, padding=padding), b_fc1))
            self.h_fc1 = h_fc1
        if self.predict_flag:
            final_shape = tf.shape(h_fc1)
            self.final_shape = final_shape
            #self.y_conv = tf.nn.softmax(h_fc1[:,(final_shape[1]-1)/2,(final_shape[2]-1)/2,(final_shape[3]-1)/2,:])
            self.y_conv = tf.nn.softmax(h_fc1[:, 2, 2, 2, :])
            self.y_res = tf.argmax(self.y_conv, 1)
            conv_center3x3 = h_fc1
            reshaped_conv_center3x3 = tf.reshape(conv_center3x3, [-1, 2])
            softmaxed_reshaped_conv_center3x3 = tf.nn.softmax(
                reshaped_conv_center3x3)
            self.y_conv_center3x3 = tf.reshape(
                softmaxed_reshaped_conv_center3x3, tf.shape(conv_center3x3))
        else:
            input_shape = tf.shape(input_placer)
            curr_shape = tf.shape(h_fc1)
            reshaped_conv = tf.reshape(h_fc1, [-1, 2])
            softmaxed_reshaped_conv = tf.nn.softmax(reshaped_conv)
            self.y_conv = tf.reshape(softmaxed_reshaped_conv, curr_shape)
            self.y_res = tf.argmax(self.y_conv, 4)
            self.y_conv_2x = dilation3D(self.y_conv, input_shape)
            tmp = tf.expand_dims(self.y_res, 4)
            self.y_res_2x = tf.squeeze(dilation3D(tmp, input_shape))

        if self.valid_flag:
            cross_entropy_mean = -tf.reduce_mean(self.y_ * tf.log(self.y_conv))
            #For Centerline
            center_sum = tf.reduce_sum(self.y_conv_center3x3, [1, 2, 3])
            #self.line_penalty = tf.mul(wdo,tf.reduce_sum(tf.mul(self.y_[:,1],tf.abs(tf.sub(center_sum[:,1],3)))))
            #self.ce = cross_entropy_mean
            #tf.add_to_collection('losses', self.line_penalty)

            tf.add_to_collection('losses', cross_entropy_mean)
            cross_entropy = tf.add_n(tf.get_collection('losses'),
                                     name='total_loss')

            transfer_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                              "transfer")

            self.train_step = tf.train.AdamOptimizer(self.l_rate).minimize(
                cross_entropy, var_list=transfer_vars)
            correct_prediction = tf.equal(tf.argmax(self.y_conv, 1),
                                          tf.argmax(self.y_, 1))
            self.accuracy = tf.reduce_mean(tf.cast(correct_prediction,
                                                   "float"))
        else:
            print("Transfer Not For Training Singly")