Esempio n. 1
0
def test_resnet(device, num_classes, num_layers, dataset, normalization, checkpoint):
    """
    Computes accuracy for the test dataset
    Inputs: device - gpu device
            num_classes - number of output classes 
            num_layers - number of layers selected for ResNet 
            dataset - dataset selected, options are val and test
            normalization - normalization used options are standard z-score normalization or unet normalization
            checkpoint - file where graph model weights are stored
    Output: None
    """
    print dataset
    os.environ['CUDA_VISIBLE_DEVICES'] = str(device) # use nvidia-smi to see available options '0' means first gpu
    config = GleasonConfig() # loads pathology configuration 

    if dataset == 'val':  
        print "Using val..." 
        config.test_fn = os.path.join(config.main_dir, 'tfrecords/val.tfrecords')
    elif dataset == 'test':
        print "Using test..." 
        config.test_fn = os.path.join(config.main_dir, 'tfrecords/test.tfrecords')
    else:
        config.test_fn = None

    config.test_checkpoint = checkpoint
    print "Loading checkpoint: {0}".format(checkpoint)

    if int(num_classes) == 2:
        print "Converting to Output Shape to Binary..."
        config.output_shape = 2
    elif int(num_classes) == 4:
        config.output_shape = 4
    else:
        raise Exception('Invalid number of classes!')

    batch_size = 128
    # loading test data
    test_meta = np.load(tfrecord2metafilename(config.test_fn))
    print 'Using {0} tfrecords: {1} | {2} images'.format(dataset, config.test_fn, len(test_meta['labels']))
    test_filename_queue = tf.train.string_input_producer([config.test_fn] , num_epochs=1) # 1 epoch, passing through the
                                                                                            # the dataset once

    test_img, test_t_l, test_f_p  = read_and_decode(filename_queue = test_filename_queue,
                                           img_dims = config.input_image_size,
                                           model_dims = config.model_image_size,
                                           size_of_batch = batch_size,
                                           augmentations_dic = config.val_augmentations_dic,
                                           num_of_threads = 4,
                                           shuffle = False)
    if int(num_classes) == 2:
        print "Converting labels to Binary..."
        test_t_l = tf.clip_by_value(test_t_l, 0, 1)

    if num_layers == "50":
        print "Loading Resnet 50..."
        if normalization == "standard":
            print "Using standard normalization..."
            test_img = normalize(test_img)
        elif normalization == "unet":
            print "Using unet normalization..."
            test_img,_ = unet_preprocess.unet(test_img,
                                           is_training = False,
                                           is_batch_norm = False,
                                           num_channels = 1)
        else:
            raise Exception('Not known normalization! Options are: standard and unet.')
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay = config.l2_reg)):
            test_target_logits, _ = resnet_v2.resnet_v2_50(inputs = test_img, 
                                                       num_classes = config.output_shape,
                                                       is_training = False) 
    elif num_layers == "101":
        print "Loading Resnet 101..."
        if normalization == "standard":
            print "Using standard normalization..."
            test_img = normalize(test_img)
        elif normalization == "unet":
            print "Using unet normalization..."
            test_img,_ = unet_preprocess.unet(test_img,
                                           is_training = False,
                                           is_batch_norm = False,
                                           num_channels = 1)
        else:
            raise Exception('Not known normalization! Options are: standard and unet.')
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay = config.l2_reg)):
            test_target_logits, _ = resnet_v2.resnet_v2_101(inputs = test_img, 
                                                           num_classes = config.output_shape,
                                                           is_training = False)
    else:
        raise Expection("Wrong number of layers! allowed numbers are 50 and 101.")
        
    target_prob = tf.nn.softmax(test_target_logits)
    prob_and_label_files = [target_prob,  test_t_l, test_f_p]
    restorer = tf.train.Saver()
    print "Variables stored in checkpoint:"
    print_tensors_in_checkpoint_file(file_name=config.test_checkpoint, tensor_name='', all_tensors='')
    
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
      
        sess.run(tf.group(tf.global_variables_initializer(),tf.local_variables_initializer()))

        restorer.restore(sess, config.test_checkpoint)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        all_predictions_target = []
        all_predictions_t_n = []
        all_labels = []
        all_files = []

        batch_num = 1
        try:
            print "Total number of batch iterations needed: {0}".format(int(math.ceil(len(test_meta['labels'])/batch_size)))
            while not coord.should_stop():
               
                np_prob_and_label_files = sess.run(prob_and_label_files)

                target_probs = np_prob_and_label_files[0]
                labels = np_prob_and_label_files[1] 
                files = np_prob_and_label_files[2]
                
                all_labels += list(labels) 
                all_files += list(files)
                all_predictions_target += list(np.argmax(target_probs, axis=1)) 
                print "evaluating current batch number: {0}".format(batch_num)
                batch_num +=1
         
        except tf.errors.OutOfRangeError:
            print "{0} accuracy: {1:.2f}".format(dataset, (metrics.accuracy_score(all_labels, all_predictions_target)*100))
            if int(num_classes) == 2:
                print "{0} precision: {1:.2f}".format(dataset, (metrics.precision_score(all_labels, all_predictions_target)*100))
                print "{0} recall: {1:.2f}".format(dataset, (metrics.recall_score(all_labels, all_predictions_target)*100))
            print 
        finally:
            coord.request_stop()  
        coord.join(threads) 
Esempio n. 2
0
def main(_):
    # Import data
    image_train, label_train = tfrecord.read_and_decode("../license train.tfrecords", tfrecord.License)
    image_train_batch, label_train_batch = tf.train.shuffle_batch(
        [image_train, label_train], batch_size=BATCH_SIZE, capacity=CAPACITY, min_after_dequeue=100, num_threads=1)

    image_test, label_test = tfrecord.read_and_decode("../license test.tfrecords", tfrecord.License)
    image_test_batch, label_test_batch = tf.train.shuffle_batch(
        [image_test, label_test], batch_size=BATCH_SIZE, capacity=CAPACITY, min_after_dequeue=100, num_threads=1)

    # 定义神经网络的输入。
    x = tf.placeholder(tf.float32, [None, INPUT_NODE], name='x-input')

    # 定义神经网络的输出。
    y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input')

    # 调用卷积神经网络。
    y_conv, keep_prob = deepnn(x)

    # 定义交叉熵损失函数。
    with tf.name_scope('loss'):
        cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_, logits=y_conv)
        cross_entropy_mean = tf.reduce_mean(cross_entropy)
        tf.summary.scalar('loss', cross_entropy_mean)

    # 设置指数衰减学习率。学习率 = learning_rate * decay_rate^(global_step/decay_steps)
    with tf.name_scope('adam_optimizer'):
        global_step = tf.Variable(0)
        learning_rate = tf.train.exponential_decay(
            learning_rate=1e-5,
            global_step=global_step,
            decay_steps=1000,
            decay_rate=0.98,
            staircase=True)
        train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy_mean, global_step=global_step)
        tf.summary.histogram('learning_rate', learning_rate)

    with tf.name_scope('accuracy'):
        correct_prediction = tf.cast(tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)), tf.float32)
        accuracy = tf.reduce_mean(correct_prediction)
        tf.summary.scalar('accuracy', accuracy)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    saver = tf.train.Saver()
    with tf.Session(config=config) as sess:
        sess.run(tf.global_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
        writer_test = tf.summary.FileWriter('../Logs/License_test', sess.graph)
        writer_train = tf.summary.FileWriter('../Logs/License_train', sess.graph)
        merged = tf.summary.merge_all()
        if ckpt and ckpt.model_checkpoint_path:
            # 加载模型。
            saver.restore(sess, ckpt.model_checkpoint_path)
            # 通过文件名得到模型保存时迭代的轮数。
            global_step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("Loading success, global_step is %d " % global_step)
        else:
            global_step = 0
            print('No checkpoint file found')
        try:
            while not coord.should_stop():
                global_step = 1 + global_step
                train_xs, train_ys = sess.run([image_train_batch, label_train_batch])
                if (global_step % 10 == 0) and global_step != 0:
                    xs_test, ys_test = sess.run([image_test_batch, label_test_batch])
                    test_accuracy, test_summary = sess.run([accuracy, merged], feed_dict={
                        x: xs_test, y_: ys_test, keep_prob: 1.0})
                    print('step %d, test accuracy %g' % (global_step, test_accuracy))
                    writer_test.add_summary(test_summary, global_step)
                    saver.save(
                        sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME),
                        global_step=global_step)
                _, train_accuracy, train_summary = sess.run([train_step, accuracy, merged], feed_dict={
                    x: train_xs, y_: train_ys, keep_prob: 0.5})
                writer_train.add_summary(train_summary, global_step)
                print('train accuracy %g' % train_accuracy)
        except tf.errors.OutOfRangeError as e:
            coord.request_stop(e)
            print('Done training -- epoch limit reached')
        finally:
            coord.request_stop()
            coord.join(threads)
        writer_test.close()
        writer_train.close()
Esempio n. 3
0
BATCH_SIZE = 40
IMG_W = 100
IMG_H = 100
N_CLASSES = 6
learning_rate = 0.0001
epoch = 1000
dropout = 1.0
all_acc = []

train_filename = '/home/wu/TF_Project/action/sample_TFrecord/train1.tfrecords'
val_filename = '/home/wu/TF_Project/action/sample_TFrecord/val1.tfrecords'
logs_train_dir = '/home/wu/TF_Project/action/logdir_train_tfrecord_sample/'
logs_val_dir = '/home/wu/TF_Project/action/logdir_val_tfrecord_sample/'
model_dir = '/home/wu/TF_Project/action/model_tfrecord_sample/'

train_img, train_label = tfrecord.read_and_decode(train_filename)
train_batch, train_label_batch = tf.train.shuffle_batch(
    [train_img, train_label],
    batch_size=40,
    num_threads=64,
    capacity=2000,
    min_after_dequeue=1000)

val_img, val_label = tfrecord.read_and_decode(val_filename)
val_batch, val_label_batch = tf.train.shuffle_batch([val_img, val_label],
                                                    batch_size=40,
                                                    num_threads=64,
                                                    capacity=2000,
                                                    min_after_dequeue=1000)

x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])
import alexnet

BATCH_SIZE = 40
IMG_W = 100
IMG_H = 100
N_CLASSES = 6
learning_rate = 0.0001
epoch = 1000
dropout = 0.5

val_filename = '/home/wu/TF_Project/action/sample_TFrecord/val1.tfrecords'
model_dir = '/home/wu/TF_Project/action/model_tfrecord_sample/'

with tf.Graph().as_default():

    val_img, val_label = tfrecord.read_and_decode(val_filename)
    train_filename_queue = tf.train.string_input_producer([val_filename],
                                                          num_epochs=None)
    val_batch, val_label_batch = tf.train.shuffle_batch([val_img, val_label],
                                                        batch_size=40,
                                                        capacity=2000,
                                                        min_after_dequeue=1000)

    x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])
    y_ = tf.placeholder(tf.int32, shape=[BATCH_SIZE])

    train_model = alexnet.alexNet(x, dropout, N_CLASSES)
    logits = train_model.fc3
    loss = alexnet.losses(logits, y_)
    acc = alexnet.evaluation(logits, y_)
    train_op = alexnet.training(loss, learning_rate)