コード例 #1
0
                    required=True)
parser.add_argument('-b',
                    '--batch_size',
                    type=int,
                    help='batch size',
                    default=512)
parser.add_argument('--dataroot',
                    type=str,
                    help='datatset stroage directory',
                    default='/data/datasets')

args = vars(parser.parse_args())
print(args)

# ----- load pre-trained model -----
model = get_model(args['ind'], args['model_arch'])

# ----- load dataset -----
transform = get_transform(args['ind'])
std = get_std(args['ind'])
img_size = get_img_size(args['ind'])
inp_channel = get_inp_channel(args['ind'])
batch_size = args['batch_size']  # recommend: 64 for ImageNet, CelebA, MS1M

ind_train_loader = get_dataloader(args['ind'],
                                  transform,
                                  "train",
                                  dataroot=args['dataroot'],
                                  batch_size=batch_size)
ind_test_loader = get_dataloader(args['ind'],
                                 transform,
コード例 #2
0
    args['ood'], ood_test_loader, [500, 500, -1])

# ----- Calculating and averaging maximum softmax probabilities -----
from lib.inference.ODIN import get_ODIN_score
best_temperature = 1.0
best_magnitude = 0.0

ind_ensemble_val = []
ood_ensemble_val = []
ind_ensemble_test = []
ood_ensemble_test = []
for id, ckpt in enumerate(os.listdir(modeldir)[:ensemble_num]):
    model_path = modeldir + args['ind'] + '_' + args[
        'model_arch'] + f'_{id}.pth'
    model = get_model(args['ind'],
                      args['model_arch'],
                      target_model_path=model_path)

    ind_scores_val_for_train = get_ODIN_score(model,
                                              ind_dataloader_val_for_train,
                                              best_magnitude,
                                              best_temperature,
                                              std=std)
    ood_scores_val_for_train = get_ODIN_score(model,
                                              ood_dataloader_val_for_train,
                                              best_magnitude,
                                              best_temperature,
                                              std=std)
    ind_ensemble_val.append(ind_scores_val_for_train)
    ood_ensemble_val.append(ood_scores_val_for_train)
コード例 #3
0
    get_dataloader,
)
from lib.utils import split_dataloader
import argparse
parser = argparse.ArgumentParser(description='Description of your program')
parser.add_argument('-i','--ind', type=str, help='in distribution dataset', required=True)
parser.add_argument('-o','--ood', type=str, help='out of distribution dataset', required=True)
parser.add_argument('-m','--model_arch', type=str, help='model architecture', required=True)
parser.add_argument('-b','--batch_size', type=int, default=64)
parser.add_argument('--dataroot',type=str, help='datatset stroage directory',default='/data/datasets')
parser.add_argument('--test_oe', action='store_true', help='whether to use model trained with outlier exposure')
args = vars(parser.parse_args())
print(args)

# ----- load pre-trained model -----
model = get_model(args['ind'], args['model_arch'], test_oe=args['test_oe'])

# ----- load dataset -----
transform = get_transform(args['ind'])
std = get_std(args['ind'])
ind_test_loader = get_dataloader(args['ind'], transform, "test",dataroot=args['dataroot'],batch_size=args['batch_size'])
ood_test_loader = get_dataloader(args['ood'], transform, "test",dataroot=args['dataroot'],batch_size=args['batch_size'])
ind_dataloader_val_for_train, ind_dataloader_val_for_test, ind_dataloader_test = split_dataloader(args['ind'], ind_test_loader, [500,500,-1])
ood_dataloader_val_for_train, ood_dataloader_val_for_test, ood_dataloader_test = split_dataloader(args['ood'], ood_test_loader, [500,500,-1])


# ----- Get Maximum Softmax Probability using get_ODIN_score function -----
from lib.inference.ODIN import get_ODIN_score

# No need to search temperature and magnitude for baseline and OE
best_temperature = 1.0