TensorFlow2.0-Examples icon indicating copy to clipboard operation
TensorFlow2.0-Examples copied to clipboard

How to do validation after some train steps automatically?

Open ghoshaw opened this issue 5 years ago • 3 comments

Hi, I want to add validation in your train.py in yolov3. So I will know when the network is overfitting.... So, what I need is just add a valid_step func then call it after some train steps?

ghoshaw avatar Nov 14 '19 09:11 ghoshaw

validate_writer = tf.summary.create_file_writer("./validate_log")
def validate_step(image_data, target):
    with tf.GradientTape() as tape:
        pred_result = model(image_data, training=False)
        giou_loss=conf_loss=prob_loss=0

        # optimizing process
        for i in range(3):
            conv, pred = pred_result[i*2], pred_result[i*2+1]
            loss_items = compute_loss(pred, conv, *target[i], i)
            giou_loss += loss_items[0]
            conf_loss += loss_items[1]
            prob_loss += loss_items[2]

        total_loss = giou_loss + conf_loss + prob_loss
        # writing summary data
        with validate_writer.as_default():
            tf.summary.scalar("lr", optimizer.lr, step=global_steps)
            tf.summary.scalar("validate_loss/total_loss", total_loss, step=global_steps)
            tf.summary.scalar("validate_loss/giou_loss", giou_loss, step=global_steps)
            tf.summary.scalar("validate_loss/conf_loss", conf_loss, step=global_steps)
            tf.summary.scalar("validate_loss/prob_loss", prob_loss, step=global_steps)
        validate_writer.flush()

YunYang1994 avatar Nov 14 '19 12:11 YunYang1994

@YunYang1994 Hi,I wonder whether the 'with tf.GradientTape() as tape' is necessary,we just want to do forward,while "tape" is to tape the cache to do backpropagation and it'll make some expensive cost.(I dont know,just an idea)

lazerliu avatar Nov 23 '19 09:11 lazerliu

@YunYang1994 , thanks for your answer. And how to use multi-gpus in yolov3?

ghoshaw avatar Nov 26 '19 07:11 ghoshaw