def after_train(self):
    print('Finished training')
    do_test = (not self.cfg.TEST.NO_TEST)
    if (do_test and (not self.cfg.NNI)):
        if (self.cfg.TEST.FINAL_MODEL == 'best_val'):
            print('Deploy the model with the best val performance')
            self.load_model(self.output_dir)
        if self.cfg.TEST.PER_CLASS_RESULT:
            (self.best_val_test_acc, per_class_accs) = self.test(return_per_class_results=True)
            perclass_path = osp.join(self.output_dir, 'perclass_result.txt')
            with open(perclass_path, 'w') as f:
                for acc in per_class_accs:
                    f.write('{:6f}\n'.format(acc))
        else:
            self.best_val_test_acc = self.test()
    if (self.cfg.TEST.FINAL_MODEL == 'best_val'):
        print('best_val_acc: {}\nbest_val_epoch: {}\nbest_val_test_acc: {}'.format(self.best_val_acc, self.best_val_epoch, self.best_val_test_acc))
        if self.cfg.TEST.TEST_EVERY_EPOCH:
            print('best_test_acc: {}\nbest_test_epoch: {}'.format(self.best_test_acc, self.best_test_epoch))
        result_path = osp.join(self.output_dir, 'result.txt')
        with open(result_path, 'w') as f:
            f.write('{:6f}\n'.format(self.best_val_test_acc))
        if self.cfg.NNI:
            nni.report_final_result(self.best_val_acc)
    elapsed = round((time.time() - self.time_start))
    elapsed = str(datetime.timedelta(seconds=elapsed))
    print('Elapsed: {}'.format(elapsed))
    self.close_writer()
def after_train(self):
    print('Finished training')
    do_test = (not self.cfg.TEST.NO_TEST)
    if (do_test and (not self.cfg.NNI)):
        if (self.cfg.TEST.FINAL_MODEL == 'best_val'):
            print('Deploy the model with the best val performance')
            self.load_model(self.output_dir)
        if self.cfg.TEST.PER_CLASS_RESULT:
            (self.best_val_test_acc, per_class_accs) = self.test(return_per_class_results=True)
            perclass_path = osp.join(self.output_dir, 'perclass_result.txt')
            with open(perclass_path, 'w') as f:
                for acc in per_class_accs:
                    f.write('{:6f}\n'.format(acc))
        else:
            self.best_val_test_acc = self.test()
    if (self.cfg.TEST.FINAL_MODEL == 'best_val'):
        print('best_val_acc: {}\nbest_val_epoch: {}\nbest_val_test_acc: {}'.format(self.best_val_acc, self.best_val_epoch, self.best_val_test_acc))
        if self.cfg.TEST.TEST_EVERY_EPOCH:
            print('best_test_acc: {}\nbest_test_epoch: {}'.format(self.best_test_acc, self.best_test_epoch))
        result_path = osp.join(self.output_dir, 'result.txt')
        with open(result_path, 'w') as f:
            f.write('{:6f}\n'.format(self.best_val_test_acc))
        if self.cfg.NNI:
            nni.report_final_result(self.best_val_acc)
    elapsed = round((time.time() - self.time_start))
    elapsed = str(datetime.timedelta(seconds=elapsed))
    print('Elapsed: {}'.format(elapsed))
    self.close_writer()