コード例 #1
0
def test_network():
    if not ct.TEST_PERCENTAGE:
        loadImageAndConvertToTFRecord(test_percentage=100,
                                      validation_percentage=0,
                                      inputDataDir=ct.TEST_DATASET_PATH,
                                      infoSavePath=ct.TEST_INFOMATION_PATH,
                                      tfrecordPath=ct.TEST_TFRECORD_DIR)
        dataSetSizeList = readInfoFromFile(ct.TEST_INFOMATION_PATH)
    else:
        dataSetSizeList = readInfoFromFile(ct.INFORMATION_PATH)
    test_image_num = int(dataSetSizeList['testing'])
    image_inputs = tf.placeholder(
        tf.float32, (1, ct.INPUT_SIZE, ct.INPUT_SIZE, ct.IMAGE_CHANNEL * 2),
        'testing_inputs')
    label_inputs = tf.placeholder(tf.float32, (1, ct.CLASS_NUM),
                                  'testing_outputs')

    nn_output = foward_propagation(image_inputs, is_training=False)
    correct_prediction = tf.equal(tf.argmax(nn_output, 1),
                                  tf.argmax(label_inputs, 1))
    #     accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

    image_tensor, label_tensor = readImageFromTFRecord(
        ct.CATELOGS[1], tfrecord_dir=ct.TEST_TFRECORD_DIR)
    saver = tf.train.Saver()
    with tf.Session() as sess:

        tf.local_variables_initializer().run()
        tf.global_variables_initializer().run()

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        correct_prediction_list = []
        ckpt = tf.train.get_checkpoint_state(ct.MODEL_SAVE_PATH)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                '-')[-1]
            for _ in range(test_image_num):
                #             for _ in range(800):
                test_image, test_label = sess.run([image_tensor, label_tensor])
                per_correct_prediction = sess.run(correct_prediction,
                                                  feed_dict={
                                                      image_inputs:
                                                      [test_image],
                                                      label_inputs:
                                                      [test_label]
                                                  })
                correct_prediction_list.append(per_correct_prediction[0])
            correct_num = 0
            for rst in correct_prediction_list:
                correct_num += rst
            accuracy_score = correct_num / len(correct_prediction_list)
            print('after %s iteration, the testing accuracy is %g' %
                  (global_step, accuracy_score))
        else:
            print('no model')
        coord.request_stop()
        coord.join(threads)
コード例 #2
0
def validate_network():
    dataSetSizeList = readInfoFromFile(ct.INFORMATION_PATH)
    validation_image_num = int(dataSetSizeList['validation'])
    image_inputs = tf.placeholder(
        tf.float32, (1, ct.INPUT_SIZE, ct.INPUT_SIZE, ct.IMAGE_CHANNEL * 2),
        'validation_inputs')
    label_inputs = tf.placeholder(tf.float32, (1, ct.CLASS_NUM),
                                  'validation_outputs')

    nn_output = foward_propagation(image_inputs)
    correct_prediction = tf.equal(tf.argmax(nn_output, 1),
                                  tf.argmax(label_inputs, 1))
    #     accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

    image_tensor, label_tensor = readImageFromTFRecord(ct.CATELOGS[2])
    saver = tf.train.Saver()
    with tf.Session() as sess:

        tf.local_variables_initializer().run()
        tf.global_variables_initializer().run()

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        while (True):
            correct_prediction_list = []
            ckpt = tf.train.get_checkpoint_state(ct.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                for _ in range(validation_image_num):
                    test_image, test_label = sess.run(
                        [image_tensor, label_tensor])
                    per_correct_prediction = sess.run(correct_prediction,
                                                      feed_dict={
                                                          image_inputs:
                                                          [test_image],
                                                          label_inputs:
                                                          [test_label]
                                                      })
                    correct_prediction_list.append(per_correct_prediction[0])
                correct_num = 0
                for rst in correct_prediction_list:
                    correct_num += rst
                accuracy_score = correct_num / len(correct_prediction_list)
                print('after %s iteration, the validation accuracy is %g' %
                      (global_step, accuracy_score))
            else:
                print('no model')
            print('running..........')
            time.sleep(300)
        coord.request_stop()
        coord.join(threads)
def validate_network():
    
    if not ct.VALIDATION_PERCENTAGE:
        loadImageAndConvertToTFRecord(test_percentage=0,validation_percentage=100,inputDataDir=ct.TEST_DATASET_PATH,
                                      infoSavePath=ct.TEST_INFOMATION_PATH,tfrecordPath=ct.TEST_TFRECORD_DIR)
        dataSetSizeList = readInfoFromFile(ct.TEST_INFOMATION_PATH)
    else:
        dataSetSizeList = readInfoFromFile(ct.INFORMATION_PATH)
#     dataSetSizeList = readInfoFromFile(ct.INFORMATION_PATH)
    validation_image_num = int(dataSetSizeList['validation'])
    image_inputs=tf.placeholder(tf.float32, (1,None,None,ct.IMAGE_CHANNEL*2), 'validation_inputs')

   
#     image_inputs = tf.cond(tf.equal(input_shape_flag,0),lambda:image_inputs_256_256,
#                         lambda:tf.cond(tf.equal(input_shape_flag,1),lambda:image_inputs_181_362,lambda:image_inputs_362_181))      
#     image_inputs=tf.placeholder(tf.float32, (1,input_size[0],input_size[1],ct.IMAGE_CHANNEL*2), 'validation_inputs')
    label_inputs =tf.placeholder(tf.float32,(1,ct.CLASS_NUM), 'validation_outputs')

    nn_output,layer_outputs_tensor = forward_propagation(image_inputs,is_training=False)
    label_value_tensor = tf.argmax(label_inputs,1)
    pred_value_tensor = tf.argmax(nn_output,1)
#     correct_prediction = tf.equal(tf.argmax(nn_output,1), tf.argmax(label_inputs,1))
#     accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
   
    image_tensor,label_tensor,proportion_tensor= readImageFromTFRecord(ct.CATELOGS[2],tfrecord_dir= ct.TEST_TFRECORD_DIR,num_epochs=None)
    saver = tf.train.Saver()
    with tf.Session() as sess :
         
        tf.local_variables_initializer().run()
        tf.global_variables_initializer().run()
        
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    
   
        while(True):
            TP = 0
            FN = 0
            FP = 0
            TN = 0
            ckpt = tf.train.get_checkpoint_state(ct.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess,ckpt.model_checkpoint_path)
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                if int(global_step)>ct.STEPS+1:
                    break 
                for i in range(validation_image_num):
                    test_image, test_label, proportion= sess.run([image_tensor,label_tensor,proportion_tensor])
                    
                    result=[0,0,0]
                    prediction = 0
                    input_size = ((360,180),(180,360),(256,256))
                    for j in range(3):
                        test_image = cv2.resize(test_image,input_size[j],interpolation=cv2.INTER_LINEAR)
                        pred,label,layer_outputs = sess.run([pred_value_tensor,label_value_tensor,layer_outputs_tensor], feed_dict= {image_inputs:[test_image],label_inputs:[test_label]})
                        result[j]=pred[0];
                    if sum(result)>=2:
                        prediction = 1    
                    
                    if label[0]:
                        if prediction:
                            TP+=1
                        else:
                            FN+=1
                    else:
                        if not prediction:
                            TN+=1
                        else:
                            FP+=1
                accuracy = (TP+TN)/(TP+FN+TN+FP)
                precision = TP/(TP+FP+1e-8)
                recall = TP/(TP+FN)
                f1 = 2*precision*recall/(precision+recall+1e-8)
                print('after %s iteration, the  accuracy is %g,precision is %g,recall is %g,F1 is %g'%(global_step,accuracy,precision,recall,f1))
                
            else:
                print('no model')
#             update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
#             print(sess.run(update_ops))
            print('running..........')
            time.sleep(100)
        coord.request_stop()
        coord.join(threads) 
コード例 #4
0
def validate_network():
    
    if not ct.VALIDATION_PERCENTAGE:
        loadImageAndConvertToTFRecord(test_percentage=0,validation_percentage=100,inputDataDir=ct.TEST_DATASET_PATH,
                                      infoSavePath=ct.TEST_INFOMATION_PATH,tfrecordPath=ct.TEST_TFRECORD_DIR)
        dataSetSizeList = readInfoFromFile(ct.TEST_INFOMATION_PATH)
    else:
        dataSetSizeList = readInfoFromFile(ct.INFORMATION_PATH)
#     dataSetSizeList = readInfoFromFile(ct.INFORMATION_PATH)
    validation_image_num = int(dataSetSizeList['validation'])
    image_inputs=tf.placeholder(tf.float32, (1,None,None,ct.IMAGE_CHANNEL*2), 'validation_inputs')

   
#     image_inputs = tf.cond(tf.equal(input_shape_flag,0),lambda:image_inputs_256_256,
#                         lambda:tf.cond(tf.equal(input_shape_flag,1),lambda:image_inputs_181_362,lambda:image_inputs_362_181))      
#     image_inputs=tf.placeholder(tf.float32, (1,input_size[0],input_size[1],ct.IMAGE_CHANNEL*2), 'validation_inputs')
    label_inputs =tf.placeholder(tf.float32,(1,ct.CLASS_NUM), 'validation_outputs')

    nn_output,layer_outputs_tensor = forward_propagation(image_inputs,is_training=False)
    label_value_tensor = tf.argmax(label_inputs,1)
    pred_value_tensor = tf.argmax(nn_output,1)
#     correct_prediction = tf.equal(tf.argmax(nn_output,1), tf.argmax(label_inputs,1))
#     accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
   
    image_tensor,label_tensor,proportion_tensor= readImageFromTFRecord(ct.CATELOGS[2],tfrecord_dir= ct.TEST_TFRECORD_DIR,num_epochs=None)
    saver = tf.train.Saver()
    with tf.Session() as sess :
         
        tf.local_variables_initializer().run()
        tf.global_variables_initializer().run()
        
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    
   
        while(True):
            TP = 0
            FN = 0
            FP = 0
            TN = 0
            ckpt = tf.train.get_checkpoint_state(ct.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess,ckpt.model_checkpoint_path)
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                if int(global_step)>ct.STEPS+1:
                    break 
                for i in range(validation_image_num):
                    test_image, test_label, proportion= sess.run([image_tensor,label_tensor,proportion_tensor])
                    if proportion<1/1.5:
                        input_size = (360,180)
                    elif proportion >1.5:
#                         input_shape_flag = 2
                        input_size = (180,360)
                    else:
#                         input_shape_flag = 0
                        input_size = (256,256)
                        
#                     feature_map_path=os.path.join(ct.FEATURE_MAP,str(i))
#                     isFileExist(feature_map_path)
                    test_image = cv2.resize(test_image,input_size,interpolation=cv2.INTER_LINEAR)
#                     a,b = cv2.split(test_image)
#                     cv2.imwrite(os.path.join(feature_map_path,'hist.jpg'),a*255)
#                     cv2.imwrite(os.path.join(feature_map_path,'curr.jpg'),b*255)
                    
#                     cv2.namedWindow('1',0)   
#                     cv2.namedWindow('2',0)
#                     cv2.imshow('1',a)
#                     cv2.imshow('2',b) 
#                     cv2.waitKey()

                    pred,label,layer_outputs = sess.run([pred_value_tensor,label_value_tensor,layer_outputs_tensor], feed_dict= {image_inputs:[test_image],label_inputs:[test_label]})
#                     
#                     feature_map = layer_outputs['resnet_v2_50/conv1']
#                     predict = layer_outputs['prediction']
#                     with open(os.path.join(feature_map_path,'predict'),'w') as f:
#                             f.write(str(predict))
#                             f.write('\n')
# 
#                     feature_map = np.squeeze(feature_map)
#                     feature_map = cv2.split(feature_map)
# #                     cv2.namedWindow('3',0)
#                     for i in range(len(feature_map)): 
#                             cv2.imwrite(os.path.join(feature_map_path,str(i)+'.jpg'),feature_map[i]*255)
# #                         cv2.imshow('3',feature_map[i])
# #                         cv2.waitKey()
                    
                    if label[0]:
                        if pred[0]:
                            TP+=1
                        else:
                            FN+=1
                    else:
                        if not pred[0]:
                            TN+=1
                        else:
                            FP+=1
                accuracy = (TP+TN)/(TP+FN+TN+FP)
                precision = TP/(TP+FP+1e-8)
                recall = TP/(TP+FN)
                f1 = 2*precision*recall/(precision+recall+1e-8)
                print('after %s iteration, the  accuracy is %g,precision is %g,recall is %g,F1 is %g'%(global_step,accuracy,precision,recall,f1))
                
            else:
                print('no model')
#             update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
#             print(sess.run(update_ops))
            print('running..........')
            time.sleep(200)
        coord.request_stop()
        coord.join(threads) 
コード例 #5
0
def validate_network():
    if not ct.VALIDATION_PERCENTAGE:
        loadImageAndConvertToTFRecord(test_percentage=0,
                                      validation_percentage=100,
                                      inputDataDir=ct.TEST_DATASET_PATH,
                                      infoSavePath=ct.TEST_INFOMATION_PATH,
                                      tfrecordPath=ct.TEST_TFRECORD_DIR)
        dataSetSizeList = readInfoFromFile(ct.TEST_INFOMATION_PATH)
    else:
        dataSetSizeList = readInfoFromFile(ct.INFORMATION_PATH)
#     dataSetSizeList = readInfoFromFile(ct.INFORMATION_PATH)
    validation_image_num = int(dataSetSizeList['validation'])
    image_inputs = tf.placeholder(
        tf.float32, (1, ct.INPUT_SIZE, ct.INPUT_SIZE, ct.IMAGE_CHANNEL * 2),
        'validation_inputs')
    label_inputs = tf.placeholder(tf.float32, (1, ct.CLASS_NUM),
                                  'validation_outputs')

    nn_output, _ = foward_propagation(image_inputs, is_training=False)
    label_value_tensor = tf.argmax(label_inputs, 1)
    pred_value_tensor = tf.argmax(nn_output, 1)
    #     correct_prediction = tf.equal(tf.argmax(nn_output,1), tf.argmax(label_inputs,1))
    #     accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

    image_tensor, label_tensor = readImageFromTFRecord(
        ct.CATELOGS[2], tfrecord_dir=ct.TEST_TFRECORD_DIR)
    saver = tf.train.Saver()
    with tf.Session() as sess:

        tf.local_variables_initializer().run()
        tf.global_variables_initializer().run()

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        while (True):
            TP = 0
            FN = 0
            FP = 0
            TN = 0
            #             sample_num = [0 for _ in range(7)]
            #             correct_num = [0 for _ in range(7)]
            ckpt = tf.train.get_checkpoint_state(ct.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                for _ in range(validation_image_num):
                    test_image, test_label = sess.run(
                        [image_tensor, label_tensor])
                    pred, label = sess.run(
                        [pred_value_tensor, label_value_tensor],
                        feed_dict={
                            image_inputs: [test_image],
                            label_inputs: [test_label]
                        })
                    if label[0]:
                        if pred[0]:
                            TP += 1
                        else:
                            FN += 1
                    else:
                        if not pred[0]:
                            TN += 1
                        else:
                            FP += 1
#                     index = label[0]
#                     sample_num[index]+=1
#                     if pred[0] == index:
#                         correct_num[index]+=1

#                 print(sample_num)
#                 print(positive_sample_num)
#                 print(negative_sample_num)
                accuracy = (TP + TN) / (TP + FN + TN + FP)
                precision = TP / (TP + FP + 1e-8)
                recall = TP / (TP + FN)
                f1 = 2 * precision * recall / (precision + recall + 1e-8)
                #                 correct_num = sum(correct_num)
                #                 accuracy_score = correct_num / sum(sample_num)
                #                 print('after %s iteration, the validation accuracy is %g'%(global_step,accuracy_score))
                print(
                    'after %s iteration, the  accuracy is %g,precision is %g,recall is %g,F1 is %g'
                    % (global_step, accuracy, precision, recall, f1))
            else:
                print('no model')


#             update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
#             print(sess.run(update_ops))
            if int(global_step) > ct.STEPS:
                break
            print('running..........')
            time.sleep(100)
        coord.request_stop()
        coord.join(threads)
コード例 #6
0
def validate_network():
    if not ct.VALIDATION_PERCENTAGE:
        loadImageAndConvertToTFRecord(test_percentage=0,
                                      validation_percentage=100,
                                      inputDataDir=ct.TEST_DATASET_PATH,
                                      infoSavePath=ct.TEST_INFOMATION_PATH,
                                      tfrecordPath=ct.TEST_TFRECORD_DIR)
        dataSetSizeList = readInfoFromFile(ct.TEST_INFOMATION_PATH)
    else:
        dataSetSizeList = readInfoFromFile(ct.INFORMATION_PATH)

    validation_image_num = int(dataSetSizeList['validation'])
    image_inputs = tf.placeholder(
        tf.float32, (1, ct.INPUT_SIZE, ct.INPUT_SIZE, ct.IMAGE_CHANNEL * 2),
        'validation_inputs')
    label_inputs = tf.placeholder(tf.float32, (1, ct.CLASS_NUM),
                                  'validation_outputs')

    nn_output = foward_propagation(image_inputs, is_training=False)
    label_value_tensor = tf.argmax(label_inputs, 1)
    pred_value_tensor = tf.argmax(nn_output, 1)
    #     correct_prediction = tf.equal(tf.argmax(nn_output,1), tf.argmax(label_inputs,1))
    #     accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

    image_tensor, label_tensor = readImageFromTFRecord(
        ct.CATELOGS[2], tfrecord_dir=ct.TEST_TFRECORD_DIR, num_epochs=None)
    saver = tf.train.Saver()
    with tf.Session() as sess:

        tf.local_variables_initializer().run()
        tf.global_variables_initializer().run()

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        while (True):
            #             positive_sample_num=0
            #             negative_sample_num=0
            #             p2p = 0
            #             n2n=0
            sample_num = [0 for _ in range(ct.CLASS_NUM)]
            correct_num = [0 for _ in range(ct.CLASS_NUM)]
            proportion = [0 for _ in range(ct.CLASS_NUM)]
            ckpt = tf.train.get_checkpoint_state(ct.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                if int(global_step) > ct.STEPS + 1:
                    break
                for _ in range(validation_image_num):
                    test_image, test_label = sess.run(
                        [image_tensor, label_tensor])
                    pred, label = sess.run(
                        [pred_value_tensor, label_value_tensor],
                        feed_dict={
                            image_inputs: [test_image],
                            label_inputs: [test_label]
                        })
                    #                     if label[0]:
                    #                         positive_sample_num+=1
                    #                         if pred[0]:
                    #                             p2p+=1
                    #                     else:
                    #                         negative_sample_num+=1
                    #                         if not pred[0]:
                    #                             n2n+=1
                    index = label[0]
                    sample_num[index] += 1
                    if pred[0] == index:
                        correct_num[index] += 1

#                 print(sample_num)
#                 print(positive_sample_num)
#                 print(negative_sample_num)
#                 correct_num = p2p+ n2n
#                 accuracy_score = correct_num/(positive_sample_num+negative_sample_num)
#                 p2p_score = p2p/positive_sample_num
#                 n2n_score = n2n/negative_sample_num
                accuracy_score = sum(correct_num) / sum(sample_num)
                proportion = [
                    correct_num[i] / sample_num[i] for i in range(ct.CLASS_NUM)
                ]
                print('after %s iteration, the validation accuracy is %g' %
                      (global_step, accuracy_score))
                print(
                    'stain:%g, luminance:%g, rotation:%g, abnormal:%g, foreignBody:%g, character:%g'
                    % (proportion[0], proportion[1], proportion[2],
                       proportion[3], proportion[4], proportion[5]))
#                 print('after %s iteration, the validation accuracy is %g,p2p_score is %g,n2n_score is %g'%(global_step,accuracy_score,p2p_score,n2n_score))
            else:
                print('no model')


#             update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
#             print(sess.run(update_ops))
            print('running..........')
            time.sleep(100)
        coord.request_stop()
        coord.join(threads)
コード例 #7
0
def validate_network():
    if not ct.VALIDATION_PERCENTAGE:
        loadImageAndConvertToTFRecord(test_percentage=0,validation_percentage=100,inputDataDir=ct.TEST_DATASET_PATH,
                                      infoSavePath=ct.TEST_INFOMATION_PATH,tfrecordPath=ct.TEST_TFRECORD_DIR)
        dataSetSizeList = readInfoFromFile(ct.TEST_INFOMATION_PATH)
    else:
        dataSetSizeList = readInfoFromFile(ct.INFORMATION_PATH)
#     dataSetSizeList = readInfoFromFile(ct.INFORMATION_PATH)
    validation_image_num = int(dataSetSizeList['validation'])
#     image_inputs=tf.placeholder(tf.float32, (1,ct.INPUT_SIZE,ct.INPUT_SIZE,ct.IMAGE_CHANNEL*2), 'validation_inputs')
#     label_inputs =tf.placeholder(tf.float32,(1,ct.CLASS_NUM), 'validation_outputs')
    curr_image_inputs=tf.placeholder(tf.float32, (1,ct.INPUT_SIZE,ct.INPUT_SIZE,ct.IMAGE_CHANNEL), 'curr_inputs')
    hist_image_inputs=tf.placeholder(tf.float32, (1,ct.INPUT_SIZE,ct.INPUT_SIZE,ct.IMAGE_CHANNEL), 'hist_inputs')
    label_inputs =tf.placeholder(tf.float32,(1,ct.CLASS_NUM), 'outputs')
    
    nn_output,_ = forward_propagation(curr_image_inputs,hist_image_inputs,is_training=False)
    label_value_tensor = tf.argmax(label_inputs,1)
    pred_value_tensor = tf.argmax(nn_output,1)
#     correct_prediction = tf.equal(tf.argmax(nn_output,1), tf.argmax(label_inputs,1))
#     accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    
    curr_img_tensor,hist_img_tensor,label_tensor= readImageFromTFRecord(ct.CATELOGS[2],tfrecord_dir= ct.TEST_TFRECORD_DIR)
#     curr_img_tensor=tf.reshape(curr_img_tensor,[1,ct.INPUT_SIZE,ct.INPUT_SIZE,ct.IMAGE_CHANNEL])
#     hist_img_tensor = tf.reshape(hist_img_tensor,[1,ct.INPUT_SIZE,ct.INPUT_SIZE,ct.IMAGE_CHANNEL])
#     label_tensor = tf.reshape(label_tensor,[1,ct.CLASS_NUM])
    
    saver = tf.train.Saver()
    with tf.Session() as sess :
         
        tf.local_variables_initializer().run()
        tf.global_variables_initializer().run()
        
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        while(True):
            sample_num = [0 for _ in range(7)]
            correct_num = [0 for _ in range(7)]
            inference_time=0
            ckpt = tf.train.get_checkpoint_state(ct.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess,ckpt.model_checkpoint_path)
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                for _ in range(validation_image_num):
#                     start_time = time.time()
                    curr_img,hist_img,label = sess.run([curr_img_tensor,hist_img_tensor,label_tensor])
                    pred,label = sess.run([pred_value_tensor,label_value_tensor], feed_dict= {curr_image_inputs:[curr_img],hist_image_inputs:[hist_img],label_inputs:[label]})
#                     inference_time+=(time.time()-start_time)
                    index = label[0]
                    sample_num[index]+=1
                    if pred[0] == index:
                        correct_num[index]+=1

#                 print(sample_num)
#                 print(positive_sample_num)
#                 print(negative_sample_num)       
#                 accuracy = (TP+TN)/(TP+FN+TN+FP)
#                 precision = TP/(TP+FP+1e-8)
#                 recall = TP/(TP+FN)
#                 f1 = 2*precision*recall/(precision+recall+1e-8)
#                 print(inference_time/(TP+FN+TN+FP))
                accuracy_score = sum(correct_num) / sum(sample_num)
                proportion = [correct_num[i]/sample_num[i] for i in range(ct.CLASS_NUM)]
                print('after %s iteration, the validation accuracy is %g'%(global_step,accuracy_score))
                print('stain:%g, luminance:%g, rotation:%g, abnormal:%g, foreignBody:%g, character:%g'%(proportion[0],proportion[1],proportion[2],proportion[3],proportion[4],proportion[5]))               
#                 print('after %s iteration, the  accuracy is %g,precision is %g,recall is %g,F1 is %g'%(global_step,accuracy,precision,recall,f1))
            else:
                print('no model')
#             update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
#             print(sess.run(update_ops))
            if int(global_step)>ct.STEPS:
                break 
            print('running..........')
            time.sleep(100)
        coord.request_stop()
        coord.join(threads) 
コード例 #8
0
def validate_network():
    if not ct.VALIDATION_PERCENTAGE:
        loadImageAndConvertToTFRecord(test_percentage=0,
                                      validation_percentage=100,
                                      inputDataDir=ct.TEST_DATASET_PATH,
                                      infoSavePath=ct.TEST_INFOMATION_PATH,
                                      tfrecordPath=ct.TEST_TFRECORD_DIR)
        dataSetSizeList = readInfoFromFile(ct.TEST_INFOMATION_PATH)
    else:
        dataSetSizeList = readInfoFromFile(ct.INFORMATION_PATH)
#     dataSetSizeList = readInfoFromFile(ct.INFORMATION_PATH)
    validation_image_num = int(dataSetSizeList['validation'])
    image_inputs = tf.placeholder(
        tf.float32, (1, ct.INPUT_SIZE, ct.INPUT_SIZE, ct.IMAGE_CHANNEL * 2),
        'validation_inputs')
    label_inputs = tf.placeholder(tf.float32, (1, ct.CLASS_NUM),
                                  'validation_outputs')

    nn_output, layer_outputs_tensor = foward_propagation(image_inputs,
                                                         is_training=False)
    label_value_tensor = tf.argmax(label_inputs, 1)
    pred_value_tensor = tf.argmax(nn_output, 1)
    #     correct_prediction = tf.equal(tf.argmax(nn_output,1), tf.argmax(label_inputs,1))
    #     accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

    image_tensor, label_tensor = readImageFromTFRecord(
        ct.CATELOGS[2], tfrecord_dir=ct.TEST_TFRECORD_DIR)
    saver = tf.train.Saver()
    with tf.Session() as sess:

        tf.local_variables_initializer().run()
        tf.global_variables_initializer().run()

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        while (True):
            #             TP = 0
            #             FN = 0
            #             FP = 0
            #             TN = 0
            #             sample_num = [0 for _ in range(7)]
            #             correct_num = [0 for _ in range(7)]
            ckpt = tf.train.get_checkpoint_state(ct.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                for i in range(validation_image_num):
                    test_image, test_label = sess.run(
                        [image_tensor, label_tensor])
                    pred, label, layer_outputs = sess.run([
                        pred_value_tensor, label_value_tensor,
                        layer_outputs_tensor
                    ],
                                                          feed_dict={
                                                              image_inputs:
                                                              [test_image],
                                                              label_inputs:
                                                              [test_label]
                                                          })

                    feature_map_path = os.path.join(ct.FEATURE_MAP, str(i))
                    isFileExist(feature_map_path)
                    feature_map = layer_outputs[
                        'resnet_v2_50/block3/unit2/bottleneck_v2']
                    predict = layer_outputs['prediction']
                    with open(os.path.join(feature_map_path, 'predict'),
                              'w') as f:
                        f.write(str(predict))
                        f.write('\n')

                    feature_map = np.squeeze(feature_map)
                    feature_map = cv2.split(feature_map)
                    #                     cv2.namedWindow('3',0)
                    for i in range(len(feature_map)):
                        cv2.imwrite(
                            os.path.join(feature_map_path,
                                         str(i) + '.jpg'),
                            feature_map[i] * 255)
#                         cv2.imshow('3',feature_map[i])
#                         cv2.waitKey()
#                     pred,label = sess.run([pred_value_tensor,label_value_tensor], feed_dict= {image_inputs:[test_image],label_inputs:[test_label]})
#                     if label[0]:
#                         if pred[0]:
#                             TP+=1
#                         else:
#                             FN+=1
#                     else:
#                         if not pred[0]:
#                             TN+=1
#                         else:
#                             FP+=1
#                     index = label[0]
#                     sample_num[index]+=1
#                     if pred[0] == index:
#                         correct_num[index]+=1

#                 print(sample_num)
#                 print(positive_sample_num)
#                 print(negative_sample_num)
#                 accuracy = (TP+TN)/(TP+FN+TN+FP)
#                 precision = TP/(TP+FP+1e-8)
#                 recall = TP/(TP+FN)
#                 f1 = 2*precision*recall/(precision+recall+1e-8)
#                 correct_num = sum(correct_num)
#                 accuracy_score = correct_num / sum(sample_num)
#                 print('after %s iteration, the validation accuracy is %g'%(global_step,accuracy_score))
#                 print('after %s iteration, the  accuracy is %g,precision is %g,recall is %g,F1 is %g'%(global_step,accuracy,precision,recall,f1))
            else:
                print('no model')


#             update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
#             print(sess.run(update_ops))
            if int(global_step) > ct.STEPS:
                break
            print('running..........')
            time.sleep(100)
        coord.request_stop()
        coord.join(threads)