Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
raise ValueError('The output file must be a pkl file.')
cfg = mmcv.Config.fromfile(args.config)
# set cudnn_benchmark
if cfg.get('cudnn_benchmark', False):
torch.backends.cudnn.benchmark = True
cfg.data.test.test_mode = True
if cfg.data.test.oversample == 'three_crop':
cfg.model.spatial_temporal_module.spatial_size = 8
dataset = obj_from_dict(cfg.data.test, datasets, dict(test_mode=True))
if args.gpus == 1:
model = build_recognizer(
cfg.model, train_cfg=None, test_cfg=cfg.test_cfg)
load_checkpoint(model, args.checkpoint, strict=True)
model = MMDataParallel(model, device_ids=[0])
data_loader = build_dataloader(
dataset,
imgs_per_gpu=1,
workers_per_gpu=cfg.data.workers_per_gpu,
num_gpus=1,
dist=False,
shuffle=False)
outputs = single_test(model, data_loader)
else:
model_args = cfg.model.copy()
model_args.update(train_cfg=None, test_cfg=cfg.test_cfg)
model_type = getattr(recognizers, model_args.pop('type'))
outputs = parallel_test(
model_type,
if args.checkpoint is not None:
cfg.load_from = args.checkpoint
# init logger
logger = get_root_logger(cfg.log_level)
logger.info('Distributed test: {}'.format(distributed))
# data loader
test_dataset = get_dataset(cfg.data.test)
print('dataset loaded')
# build model and load checkpoint
model = build_landmark_detector(cfg.model)
print('model built')
checkpoint = load_checkpoint(model, cfg.load_from, map_location='cpu')
print('load checkpoint from: {}'.format(cfg.load_from))
# test
test_landmark_detector(
model,
test_dataset,
cfg,
distributed=distributed,
validate=args.validate,
logger=logger)
def main():
args = parse_args()
cfg = Config.fromfile(args.config)
img = cv2.imread(args.input, -1)
img_tensor = img_to_tensor(img, squeeze=True, cuda=args.use_cuda)
model = build_predictor(cfg.model)
checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu')
if args.use_cuda:
model.cuda()
model.eval()
# predict probabilities for each attribute
attr_prob = model(img_tensor, attr=None, landmark=None, return_loss=False)
attr_predictor = AttrPredictor(cfg.data.test)
attr_predictor.show_prediction(attr_prob)
def init_weights(self, pretrained=None):
if isinstance(pretrained, str):
from mmdet.apis import get_root_logger
logger = get_root_logger()
load_checkpoint(self, pretrained, strict=False, logger=logger)
elif pretrained is None:
for m in self.modules():
if isinstance(m, nn.Conv2d):
kaiming_init(m)
elif isinstance(m, nn.BatchNorm2d):
constant_init(m, 1)
else:
raise TypeError('pretrained must be a str or None')
def init_weights(self, pretrained=None):
if isinstance(pretrained, str):
from mmdet.apis import get_root_logger
logger = get_root_logger()
load_checkpoint(self, pretrained, strict=False, logger=logger)
elif pretrained is None:
for m in self.features.modules():
if isinstance(m, nn.Conv2d):
kaiming_init(m)
elif isinstance(m, nn.BatchNorm2d):
constant_init(m, 1)
elif isinstance(m, nn.Linear):
normal_init(m, std=0.01)
else:
raise TypeError('pretrained must be a str or None')
for m in self.extra.modules():
if isinstance(m, nn.Conv2d):
xavier_init(m, distribution='uniform')
constant_init(self.l2_norm, self.l2_norm.scale)
def init_weights(self, pretrained=None):
if isinstance(pretrained, str):
logger = logging.getLogger()
load_checkpoint(self, pretrained, strict=False, logger=logger, map_location=torch.device('cpu'))
elif pretrained is None:
for m in self.modules():
if isinstance(m, nn.Conv2d):
kaiming_init(m)
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
constant_init(m, 1)
if self.dcn is not None:
for m in self.modules():
if isinstance(m, Bottleneck) and hasattr(
m, 'conv2_offset'):
constant_init(m.conv2_offset, 0)
if self.zero_init_residual:
for m in self.modules():
if isinstance(m, Bottleneck):
def init_weights(self, pretrained=None):
if isinstance(pretrained, str):
logger = logging.getLogger()
load_checkpoint(self, pretrained, strict=False, logger=logger)
elif pretrained is None:
for m in self.modules():
if isinstance(m, nn.Conv2d):
kaiming_init(m)
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
constant_init(m, 1)
if self.dcn is not None:
for m in self.modules():
if isinstance(m, Bottleneck) and hasattr(
m, 'conv2_offset'):
constant_init(m.conv2_offset, 0)
if self.zero_init_residual:
for m in self.modules():
if isinstance(m, Bottleneck):
def init_weights(self, pretrained=None):
if isinstance(pretrained, str):
from mmdet.apis import get_root_logger
logger = get_root_logger()
load_checkpoint(self, pretrained, strict=False, logger=logger)
elif pretrained is None:
for m in self.modules():
if isinstance(m, nn.Conv2d):
kaiming_init(m)
elif isinstance(m, (_BatchNorm, nn.GroupNorm)):
constant_init(m, 1)
if self.zero_init_residual:
for m in self.modules():
if isinstance(m, Bottleneck):
constant_init(m.norm3, 0)
elif isinstance(m, BasicBlock):
constant_init(m.norm2, 0)
else:
raise TypeError('pretrained must be a str or None')
def load_model():
model = build_detector(cfg.model, test_cfg=cfg.test_cfg)
_ = load_checkpoint(model, model_cfgs[0][1]) # 7 it/s
return model
def init_weights(self):
if isinstance(self.pretrained, str):
logger = logging.getLogger()
resnet2d = ResNet(self.depth)
load_checkpoint(resnet2d, self.pretrained, strict=False, logger=logger)
for name, module in self.named_modules():
if isinstance(module, NonLocalModule):
module.init_weights()
elif isinstance(module, nn.Conv3d) and rhasattr(resnet2d, name):
new_weight = rgetattr(resnet2d, name).weight.data.unsqueeze(2).expand_as(module.weight) / module.weight.data.shape[2]
module.weight.data.copy_(new_weight)
logging.info("{}.weight loaded from weights file into {}".format(name, new_weight.shape))
if hasattr(module, 'bias') and module.bias is not None:
new_bias = rgetattr(resnet2d, name).bias.data
module.bias.data.copy_(new_bias)
logging.info("{}.bias loaded from weights file into {}".format(name, new_bias.shape))
elif isinstance(module, nn.BatchNorm3d) and rhasattr(resnet2d, name):
for attr in ['weight', 'bias', 'running_mean', 'running_var']:
logging.info("{}.{} loaded from weights file into {}".format(name, attr, getattr(rgetattr(resnet2d, name), attr).shape))
setattr(module, attr, getattr(rgetattr(resnet2d, name), attr))
elif self.pretrained is None: