def segmentation_eval(config, net,
                      mapping_assignment_dataloader,
                      mapping_test_dataloader,
                      sobel, using_IR=False, verbose=0, return_only=False):
    torch.cuda.empty_cache() if not config.nocuda else None
    net.eval()

    stats_dict = cluster_subheads_eval(config, net,
                                       mapping_assignment_dataloader=mapping_assignment_dataloader,
                                       mapping_test_dataloader=mapping_test_dataloader,
                                       sobel=sobel,
                                       using_IR=using_IR,
                                       get_data_fn=_segmentation_get_data,
                                       verbose=verbose)

    net.train()

    acc = stats_dict["best"]
    is_best = (len(config.epoch_acc) > 0) and (acc > max(config.epoch_acc))

    torch.cuda.empty_cache() if not config.nocuda else None

    if not return_only:
        config.epoch_stats.append(stats_dict)
        config.epoch_acc.append(acc)
        config.epoch_avg_subhead_acc.append(stats_dict["avg"])

        return is_best
    else:
        return stats_dict
Exemple #2
0
    # ------------------------------------------------------

    net = archs.__dict__[config.arch](config)
    model_path = os.path.join(config.out_dir, "best_net.pytorch")
    net.load_state_dict(
        torch.load(model_path, map_location=lambda storage, loc: storage))
    net.cuda()

    if given_config.use_eval:
        print("doing eval mode")
        net.eval()

    net = torch.nn.DataParallel(net)
    acc, nmi, ari, _ = cluster_subheads_eval(
        config,
        net,
        mapping_assignment_dataloader=mapping_assignment_dataloader,
        mapping_test_dataloader=mapping_test_dataloader,
        sobel=True)

    config.assign_set_szs_pc_acc[str(pc)] = (num_imgs, acc)

    print("for model %d assign set sz pc %f, got %f, compared to best stored "
          "acc %f" % (config.model_ind, pc, acc, max(config.epoch_acc)))
    print(config.assign_set_szs_pc_acc)
    sysout.flush()

if not given_config.dont_save:
    print("writing to new config")
    # store to config
    with open(os.path.join(config.out_dir, "config.pickle"), "wb") as outfile:
        pickle.dump(config, outfile)