How to use evaluation_loop with train_loop in tf-slim

前端 未结 3 2024
长情又很酷
长情又很酷 2021-02-04 10:57

I\'m trying to implement a few different models and train them on CIFAR-10, and I want to use TF-slim to do this. It looks like TF-slim has two main loops that are useful during

3条回答
  •  天涯浪人
    2021-02-04 11:44

    Thanks to @kmalakoff, the TensorFlow issue gave a brilliant way to the problem that how to validate or test model in tf.slim training. The main idea is overriding train_step_fn function:

    import …
    from tensorflow.contrib.slim.python.slim.learning import train_step
    
    ...
    
    accuracy_validation = ...
    accuracy_test = ...
    
    def train_step_fn(session, *args, **kwargs):
        total_loss, should_stop = train_step(session, *args, **kwargs)
    
        if train_step_fn.step % FLAGS.validation_every_n_step == 0:
            accuracy = session.run(train_step_fn.accuracy_validation)
            print('your validation info')
    
        if train_step_fn.step % FLAGS.test_every_n_step == 0:
            accuracy = session.run(train_step_fn.accuracy_test)
            print('your test info')
    
        train_step_fn.step += 1
        return [total_loss, should_stop] 
    
    train_step_fn.step = 0
    train_step_fn.accuracy_validation = accuracy_validation
    train_step_fn.accuracy_test = accuracy_test
    
    # run training.
    slim.learning.train(
        train_op,
        FLAGS.logs_dir,
        train_step_fn=train_step_fn,
        graph=graph,
        number_of_steps=FLAGS.max_steps)
    

提交回复
热议问题