Esempio n. 1
0
def eavluate(path,recordfile,resultfile):
    inputdata,arraylength = GetDate(path,recordfile)
    inputdata = tf.convert_to_tensor(inputdata)
    inputdata = tf.reshape(inputdata,(arraylength,140,400,1))
    inputdata = tf.cast(inputdata,dtype=tf.float32)
    is_training = False
    labels_nums = 2
    keep_prob=1
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        out, end_points = resnet_v1.resnet_v1_101(inputs=inputdata, num_classes=labels_nums, is_training=is_training,global_pool=True)
    #outputprobability = tf.nn.softmax(out)
    outputprobability = out
    saver = tf.train.Saver()
    with tf.Session() as sess:
        tf.get_variable_scope().reuse_variables()
        ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess,ckpt.model_checkpoint_path)
            probabilitylist = sess.run(outputprobability)
            probabilityresult = probabilitylist
            print (type(probabilitylist))
            print (probabilityresult)
            np.savetxt(resultfile,probabilityresult)
            #print (accuracy_score_test)
            #print (type(accuracy_score_test))
            #print ('After %s training step(s),validation''test_accury = %g'%(global_step,accuracy_score_test))
            #print ('After %smtraining step(s),validation''train_accury = %g'%(global_step,accuracy_score_train))
        else:
            print('No checkpoint file found')
            return
Esempio n. 2
0
def  predict(models_path,image_dir,labels_filename,labels_nums, data_format):
    [batch_size, resize_height, resize_width, depths] = data_format

    #labels = np.loadtxt(labels_filename, str, delimiter='\t')
    input_images = tf.placeholder(dtype=tf.float32, shape=[None, resize_height, resize_width, depths], name='input')

    #其他模型预测请修改这里
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        out, end_points = resnet_v1.resnet_v1_101(inputs=input_images, num_classes=labels_nums,is_training=False)

    # 将输出结果进行softmax分布,再求最大概率所属类别
    score = tf.nn.softmax(out,name='pre')
    class_id = tf.argmax(score, 1)

    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, models_path)
    images_list=glob.glob(os.path.join(image_dir,'*.jpg'))
    for image_path in images_list:
        im=read_image(image_path,resize_height,resize_width,normalization=True)
        im=im[np.newaxis,:]
        #pred = sess.run(f_cls, feed_dict={x:im, keep_prob:1.0})
        pre_score,pre_label = sess.run([score,class_id], feed_dict={input_images:im})
        max_score=pre_score[0,pre_label]
        print("{} is: pre labels:{},name:{} score: {}".format(image_path,pre_label,list(labels_filename.keys())[list(labels_filename.values()).index(pre_label)], max_score))
    sess.close()
Esempio n. 3
0
def train(val_record_file, labels_nums, data_shape, snapshot_prefix):
    '''
    :param train_record_file: 训练的tfrecord文件
    :param train_log_step: 显示训练过程log信息间隔
    :param train_param: train参数
    :param val_record_file: 验证的tfrecord文件
    :param val_log_step: 显示验证过程log信息间隔
    :param val_param: val参数
    :param labels_nums: labels数
    :param data_shape: 输入数据shape
    :param snapshot: 保存模型间隔
    :param snapshot_prefix: 保存模型文件的前缀名
    :return:
    '''
    #[base_lr,max_steps]=train_param
    [batch_size, resize_height, resize_width, depths] = data_shape

    val_images_batch, val_labels_batch = get_batch_images(val_record_file)
    # Define the model:
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        out, end_points = resnet_v1.resnet_v1_101(inputs=input_images,
                                                  num_classes=labels_nums,
                                                  is_training=is_training,
                                                  global_pool=True)

    accuracy = tf.reduce_mean(
        tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(input_labels, 1)),
                tf.float32))
    saver = tf.train.Saver()
    while True:
        with tf.Session() as sess:
            tf.get_variable_scope().reuse_variables()
            ckpt = tf.train.get_checkpoint_state(snapshot_prefix)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                print(global_step)
                coord = tf.train.Coordinator()
                threads = tf.train.start_queue_runners(sess=sess, coord=coord)
                #batch_input_images, batch_input_labels = sess.run([train_images_batch, train_labels_batch])
                mean_acc = net_evaluation(sess, accuracy, val_images_batch,
                                          val_labels_batch)
                print("%s: val accuracy :  %g" % (datetime.now(), mean_acc))
                coord.request_stop()
                coord.join(threads)
Esempio n. 4
0
def train(train_record_file, train_log_step, train_param, val_record_file,
          val_log_step, labels_nums, data_shape, snapshot, snapshot_prefix):
    '''
    :param train_record_file: 训练的tfrecord文件
    :param train_log_step: 显示训练过程log信息间隔
    :param train_param: train参数
    :param val_record_file: 验证的tfrecord文件
    :param val_log_step: 显示验证过程log信息间隔
    :param val_param: val参数
    :param labels_nums: labels数
    :param data_shape: 输入数据shape
    :param snapshot: 保存模型间隔
    :param snapshot_prefix: 保存模型文件的前缀名
    :return:
    '''
    [base_lr, max_steps] = train_param
    [batch_size, resize_height, resize_width, depths] = data_shape

    # 获得训练和测试的样本数
    #train_nums=get_example_nums(train_record_file)
    #val_nums=get_example_nums(val_record_file)
    print('train nums:%d,val nums:%d' % (train_nums, val_nums))

    # 从record中读取图片和labels数据
    # train数据,训练数据一般要求打乱顺序shuffle=True
    #train_images, train_labels = read_records(train_record_file, resize_height, resize_width, type='normalization')
    #train_images_batch, train_labels_batch = get_batch_images(train_images, train_labels,
    #                                                          batch_size=batch_size, labels_nums=labels_nums,
    #                                                          one_hot=True, shuffle=True)
    # val数据,验证数据可以不需要打乱数据
    #val_images, val_labels = read_records(val_record_file, resize_height, resize_width, type='normalization')
    #val_images_batch, val_labels_batch = get_batch_images(val_images, val_labels,
    #                                                      batch_size=batch_size, labels_nums=labels_nums,
    #                                                      one_hot=True, shuffle=False)
    train_images_batch, train_labels_batch = get_batch_images(
        train_record_file)
    val_images_batch, val_labels_batch = get_batch_images(val_record_file,
                                                          is_train=False)
    # Define the model:
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        out, end_points = resnet_v1.resnet_v1_101(inputs=input_images,
                                                  num_classes=labels_nums,
                                                  is_training=is_training,
                                                  global_pool=True)
    # with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope()):
    #     out, end_points = mobilenet_v1.mobilenet_v1(inputs=input_images, num_classes=labels_nums,
    #                                                 dropout_keep_prob=keep_prob, is_training=is_training,
    #                                                 global_pool=True)

    # Specify the loss function: tf.losses定义的loss函数都会自动添加到loss函数,不需要add_loss()了
    tf.losses.softmax_cross_entropy(onehot_labels=input_labels,
                                    logits=out)  # 添加交叉熵损失loss=1.6
    # slim.losses.add_loss(my_loss)
    loss = tf.losses.get_total_loss(
        add_regularization_losses=True)  # 添加正则化损失loss=2.2
    #global_step = tf.Variable(0,trainable = False)
    # Specify the optimization scheme:
    #variable_averages=tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step)
    #variable_averages_op=variable_averages.apply(tf.trainable_variables())
    #learning_rate=tf.train.exponential_decay(
    #base_lr,
    #global_step,
    #train_nums/batch_size,
    #LEARNING_RATE_DECAY)
    #train_step=tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step=global_step)
    #with tf.control_dependencies([train_step,variable_averages_op]):train_op=tf.np_op(name='train')
    # 在定义训练的时候, 注意到我们使用了`batch_norm`层时,需要更新每一层的`average`和`variance`参数,
    # 更新的过程不包含在正常的训练过程中, 需要我们去手动像下面这样更新
    # 通过`tf.get_collection`获得所有需要更新的`op`
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    # 使用`tensorflow`的控制流, 先执行更新算子, 再执行训练
    with tf.control_dependencies(update_ops):
        print("update_ops:{}".format(update_ops))
        # create_train_op that ensures that when we evaluate it to get the loss,
        # the update_ops are done and the gradient updates are computed.

        # train_op = tf.train.MomentumOptimizer(learning_rate=base_lr, momentum=0.9).minimize(loss)
        train_op = tf.train.AdadeltaOptimizer(
            learning_rate=base_lr).minimize(loss)
        #train_op=tf.np_op(name='train')

    accuracy = tf.reduce_mean(
        tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(input_labels, 1)),
                tf.float32))
    # 循环迭代过程
    step_train(train_op=train_op,
               loss=loss,
               accuracy=accuracy,
               train_images_batch=train_images_batch,
               train_labels_batch=train_labels_batch,
               train_nums=train_nums,
               train_log_step=train_log_step,
               val_images_batch=val_images_batch,
               val_labels_batch=val_labels_batch,
               val_nums=val_nums,
               val_log_step=val_log_step,
               snapshot_prefix=snapshot_prefix,
               snapshot=snapshot)
Esempio n. 5
0
def train(data_csv_path_train, train_log_step, train_param, data_csv_path_val,
          val_log_step, labels_nums, data_shape, snapshot, snapshot_prefix):
    '''
    :param data_csv_path_train: 训练的csv文件
    :param train_log_step: 显示训练过程log信息间隔
    :param train_param: train参数
    :param data_csv_path_val: 验证的val文件
    :param val_log_step: 显示验证过程log信息间隔
    :param val_param: val参数
    :param labels_nums: labels数
    :param data_shape: 输入数据shape
    :param snapshot: 保存模型间隔
    :param snapshot_prefix: 保存模型文件的前缀名
    :return:
    '''
    [base_lr, max_steps] = train_param
    [batch_size, resize_height, resize_width, depths] = data_shape

    # 获得训练和测试的样本数
    with open(dataset_csv_path_train, 'r') as f:
        train_nums = len(f.readlines())
    with open(dataset_csv_path_val, 'r') as v:
        val_nums = len(v.readlines())
    print('train nums:%d,val nums:%d' % (train_nums, val_nums))

    train_batch = data_loader.load_data(data_csv_path_train,
                                        image_type=args.image_type,
                                        image_size_before_crop=resize_height,
                                        labels_nums=labels_nums)
    train_images_batch = train_batch['image']
    train_labels_batch = train_batch['label']
    #     print('......................................................')
    #     print(train_images_batch)
    #     print(train_labels_batch)

    # val数据,验证数据可以不需要打乱数据
    val_batch = data_loader.load_data(data_csv_path_val,
                                      image_type=args.image_type,
                                      image_size_before_crop=resize_height,
                                      labels_nums=labels_nums,
                                      do_shuffle=False)
    val_images_batch = val_batch['image']
    val_labels_batch = val_batch['label']

    # Define the model:
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        out, end_points = resnet_v1.resnet_v1_101(inputs=input_images,
                                                  num_classes=labels_nums,
                                                  is_training=is_training)

    # Specify the loss function: tf.losses定义的loss函数都会自动添加到loss函数,不需要add_loss()了
    tf.losses.softmax_cross_entropy(onehot_labels=input_labels,
                                    logits=out)  #添加交叉熵损失loss=1.6
    # slim.losses.add_loss(my_loss)
    loss = tf.losses.get_total_loss(
        add_regularization_losses=False)  #添加正则化损失loss=2.2
    accuracy = tf.reduce_mean(
        tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(input_labels, 1)),
                tf.float32))

    # Specify the optimization scheme:
    # optimizer = tf.train.GradientDescentOptimizer(learning_rate=base_lr)

    # global_step = tf.Variable(0, trainable=False)
    # learning_rate = tf.train.exponential_decay(0.05, global_step, 150, 0.9)
    # optimizer = tf.train.MomentumOptimizer(learning_rate=base_lr,momentum= 0.9)
    # optimizer = tf.train.GradientDescentOptimizer(learning_rate=base_lr)
    # # train_tensor = optimizer.minimize(loss, global_step)
    # train_op = slim.learning.create_train_op(loss, optimizer,global_step=global_step)

    # 在定义训练的时候, 注意到我们使用了`batch_norm`层时,需要更新每一层的`average`和`variance`参数,
    # 更新的过程不包含在正常的训练过程中, 需要我们去手动像下面这样更新
    # 通过`tf.get_collection`获得所有需要更新的`op`
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    # 使用`tensorflow`的控制流, 先执行更新算子, 再执行训练
    with tf.control_dependencies(update_ops):
        # create_train_op that ensures that when we evaluate it to get the loss,
        # the update_ops are done and the gradient updates are computed.
        # train_op = slim.learning.create_train_op(total_loss=loss,optimizer=optimizer)
        train_op = tf.train.AdadeltaOptimizer(
            learning_rate=base_lr).minimize(loss)

    # 循环迭代过程
    step_train(train_op, loss, accuracy, train_images_batch,
               train_labels_batch, train_nums, train_log_step,
               val_images_batch, val_labels_batch, val_nums, val_log_step,
               snapshot_prefix, snapshot)
# batch_label = batch_label.reshape(batch_size, -1)

# Create tensorflow graph for evaluation
eval_graph = tf.Graph()
with eval_graph.as_default():
    with eval_graph.gradient_override_map({'Relu': 'GuidedRelu'}):
        images = tf.placeholder("float", [batch_size, 224, 224, 3])
        labels = tf.placeholder(tf.float32, [batch_size, 1000])

        preprocessed_images = utils.resnet_preprocess(images)

        with slim.arg_scope(resnet_v1.resnet_arg_scope()):
            with slim.arg_scope([slim.batch_norm], is_training=False):
                # is_training=False means batch-norm is not in training mode. Fixing batch norm layer.
                # net is logit for resnet_v1. See is_training messing up issue: https://github.com/tensorflow/tensorflow/issues/4887
                net, end_points = resnet_v1.resnet_v1_101(
                    preprocessed_images, 1000)
        prob = end_points['predictions']  # after softmax

        cost = (-1) * tf.reduce_sum(tf.multiply(labels, tf.log(prob)), axis=1)
        print('cost:', cost)
        y_c = tf.reduce_sum(tf.multiply(net, labels), axis=1)
        print('y_c:', y_c)

        # Get last convolutional layer gradient for generating gradCAM visualization
        print('endpoints:', end_points.keys())
        target_conv_layer = end_points[
            'resnet_v1_101/block4/unit_2/bottleneck_v1']
        target_conv_layer_grad = tf.gradients(y_c, target_conv_layer)[0]

        # Guided backpropagtion back to input layer
        gb_grad = tf.gradients(cost, images)[0]