训练网络张量流后的图像分割预测

时间:2019-02-04 22:53:09

标签: tensorflow prediction image-segmentation

我正在努力从视网膜图像中进行血管分割。我在最后一层使用具有softmax激活功能的自动编码器网络。 这是我的以下用于训练和预测的代码:

** training code
//loss function
losses =tf.nn.softmax_cross_entropy_with_logits(labels=net_output, logits=network)

cost = tf.reduce_mean(losses)
tf.summary.scalar("cross_entropy", cost)
//Adam optimizer
opt = tf.train.AdamOptimizer(args.learning_rate).minimize(cost, var_list=[var for var in tf.trainable_variables()])

init = tf.global_variables_initializer()

correct_prediction = tf.equal(tf.argmax(network, 1), tf.argmax(net_output, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


for epoch in range(0, args.num_epochs):     

    for i in range(0, len(train_input_names)):

       input_black_ring_image = preprocessing(train_input_names[i])
       input_image, segmented_image = data_augmentation(input_black_ring_image, train_segmented_names[i])
           for k in range(0, len(input_image)):

                for j in range(0, args.batch_size):

                    index= k*args.batch_size + j
                        with tf.device('/gpu:0'):
                          input_slice = np.float32(input_image[index]) / 255.0

                          output_slice = np.float32(helpers.one_hot_it(label=segmented_image[index], label_values=label_values))


                                    input_image_batch.append(np.expand_dims(input_slice, axis=0))
                                    segmented_image_batch.append(np.expand_dims(output_slice, axis=0))                        
                if args.batch_size == 1:
                    input_img = input_image_batch[0]


                     output_img = segmented_image_batch[0]

                else:
                     input_img = np.squeeze(np.stack(input_image_batch, axis=1))
                     output_img = np.squeeze(np.stack(segmented_image_batch, axis=1))  
//training      
                _,current=sess.run([opt, cost],feed_dict={net_input:input_img, net_output:output_img})
                print("loss of img "+str(i)   +" slice   " +str(k)+"     ="+str(current))


**prediction code

                //image for prediction
                 input_slice_test = np.float32(input_image_test[15]) / 255.0 


                input_image_test = np.expand_dims(input_slice_test, axis=0)
                print("input     ", input_image_test)
             //prediction    
                output_image = sess.run(network, feed_dict={net_input:input_image_test})
                print("output", output_image)`

精度看起来逻辑的值在每个时期之后都会收敛,但是当我在训练所有时期之后尝试预测图像时,预测数组输出的值如下所示:

//result of prediction
            [[[[4.4425573e-02 8.8295966e-01] 
                   [5.4390044e-03 9.7242403e-01]
                   [1.9392670e-03 9.8569876e-01]

,图像为全白。 请帮忙!

0 个答案:

没有答案