Пример #1
0
def snapshot(model, valoader, epoch, best_miou, snapshot_dir, prefix):
    miou = val(model, valoader)
    snapshot = {'epoch': epoch, 'state_dict': model.state_dict(), 'miou': miou}
    if miou > best_miou:
        best_miou = miou
        torch.save(snapshot,
                   os.path.join(snapshot_dir, '{}.pth.tar'.format(prefix)))

    print("[{}] Curr mIoU: {:0.4f} Best mIoU: {}".format(
        epoch, miou, best_miou))
    return best_miou
def snapshot(model, optimG, trainloader_l, valoader, epoch, best_miou_unlabel,
             best_miou, snapshot_dir, prefix, f_path):

    miou_unlabel = val_unlabel(model, trainloader_l, epoch)
    miou = val(model, valoader, epoch)

    global best_epoch
    global best_epoch_unlabel
    snapshot = {
        'epoch': epoch,
        'state_dict': model.state_dict(),
        # 'optimizer_state_dict': optimG.state_dict(),
        'miou_unlabel': miou_unlabel,
        'miou': miou
    }
    if miou_unlabel > best_miou_unlabel:
        best_miou_unlabel = miou_unlabel
        best_epoch_unlabel = epoch
        torch.save(snapshot,
                   os.path.join(snapshot_dir, '{}.pth.tar'.format(prefix)))
    if miou > best_miou:
        best_miou = miou
        best_epoch = epoch
        torch.save(snapshot,
                   os.path.join(snapshot_dir, '{}.pth.tar'.format(prefix)))

    print("unlabelled::[{}] Curr mIoU: {:0.4f} Best mIoU: {} Best epoch: {}".
          format(epoch, miou_unlabel, best_miou_unlabel, best_epoch_unlabel))
    print("validation::[{}] Curr mIoU: {:0.4f} Best mIoU: {} Best epoch: {}".
          format(epoch, miou, best_miou, best_epoch))
    with open(f_path, 'a') as f:
        f.write(
            "unlabelled::[{}] Curr mIoU: {:0.4f} Best mIoU: {} Best epoch: {}".
            format(epoch, miou_unlabel, best_miou_unlabel, best_epoch_unlabel))
        f.write(
            "validation::[{}] Curr mIoU: {:0.4f} Best mIoU: {} Best epoch: {}".
            format(epoch, miou, best_miou, best_epoch))
        f.write('\n')

    return best_miou_unlabel, best_miou