예제 #1
0
    def evaluate(data_source):
        model.eval()
        PreNN, PreNNT, PreNNDR = 0, 0, 0
        with torch.no_grad():
            for i_batch, sample_batched in enumerate(data_source, 1):
                batch = parse_batch(sample_batched, device)

                TPNN, PNN, TPNNT, PNNT, TPNNDR, PNNDR = eval_model(
                    model.module if mgpu else model,
                    batch,
                    cfg.MODEL.DES_THRSH,
                    cfg.MODEL.COO_THRSH,
                )

                PreNN += TPNN / PNN
                PreNNT += TPNNT / PNNT
                PreNNDR += TPNNDR / PNNDR

        length = len(data_source)
        PreNN, PreNNT, PreNNDR = (PreNN / length, PreNNT / length,
                                  PreNNDR / length)
        meanms = (PreNN + PreNNT + PreNNDR) / 3
        checkpoint_name = (
            f"NN_{PreNN:.3f}_NNT_{PreNNT:.3f}_NNDR_{PreNNDR:.3f}_MeanMS_{meanms:.3f}"
        )
        return checkpoint_name, meanms
예제 #2
0
model = pretrainedmodels.__dict__[model_name](num_classes=1000,
                                              pretrained='imagenet')

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


def initialize_model(model, feature_extract):
    num_ftrs = model.last_linear.in_features
    model.last_linear = nn.Linear(num_ftrs, num_classes)
    return model


image_loader_dict = eval_utils.get_image_loader(train_list, val_list, model,
                                                label_num, num_classes,
                                                batch_size)

model_ft = initialize_model(model, feature_extract)
#model_ft = nn.DataParallel(model_ft)
model_ft = model_ft.to(device)

model_ft.load_state_dict(torch.load(model_path))

best_model = eval_utils.eval_model(num_classes,
                                   model_name,
                                   label_map,
                                   model_ft,
                                   image_loader_dict,
                                   device=device,
                                   is_inception=(model_name == "inception"))