def get_model_and_checkpoint(model, dataset, checkpoint_path, n_gpu=1): if dataset == 'imagenet': n_class = 1000 elif dataset == 'cifar10': n_class = 10 else: raise ValueError('unsupported dataset') if model == 'mobilenet': from mobilenet import MobileNet net = MobileNet(n_class=n_class) elif model == 'mobilenetv2': from mobilenet_v2 import MobileNetV2 net = MobileNetV2(n_class=n_class) elif model.startswith('resnet'): net = resnet.__dict__[model](pretrained=True) in_features = net.fc.in_features net.fc = nn.Linear(in_features, n_class) else: raise NotImplementedError if checkpoint_path: print('loading {}...'.format(checkpoint_path)) sd = torch.load(checkpoint_path, map_location=torch.device('cpu')) if 'state_dict' in sd: # a checkpoint but not a state_dict sd = sd['state_dict'] sd = {k.replace('module.', ''): v for k, v in sd.items()} net.load_state_dict(sd) if torch.cuda.is_available() and n_gpu > 0: net = net.cuda() if n_gpu > 1: net = torch.nn.DataParallel(net, range(n_gpu)) return net
def get_model(args): print('=> Building model..') if args.dataset == 'imagenet': n_class = 1000 elif args.dataset == 'cifar10': n_class = 10 else: raise NotImplementedError if args.model_type == 'mobilenet': net = MobileNet(n_class=n_class) elif args.model_type == 'mobilenetv2': net = MobileNetV2(n_class=n_class) elif args.model_type.startswith('resnet'): net = resnet.__dict__[args.model_type](pretrained=True) in_features = net.fc.in_features net.fc = nn.Linear(in_features, n_class) else: raise NotImplementedError if args.ckpt_path is not None: # the checkpoint can be state_dict exported by amc_search.py or saved by amc_train.py print('=> Loading checkpoint {} ..'.format(args.ckpt_path)) net.load_state_dict(torch.load(args.ckpt_path, torch.device('cpu'))) if args.mask_path is not None: SZ = 224 if args.dataset == 'imagenet' else 32 data = torch.randn(2, 3, SZ, SZ) ms = ModelSpeedup(net, data, args.mask_path, torch.device('cpu')) ms.speedup_model() net.to(args.device) if torch.cuda.is_available() and args.n_gpu > 1: net = torch.nn.DataParallel(net, list(range(args.n_gpu))) return net