def predict(image): image = image_loader(image=image) BACKBONE = IR_50(INPUT_SIZE) HEAD = ArcFace(in_features=EMBEDDING_SIZE, out_features=1000, device_id=GPU_ID) BACKBONE = BACKBONE.to(DEVICE) HEAD = HEAD.to(DEVICE) BACKBONE.load_state_dict( torch.load('./trained_model/Backbone_IR_50_ArcFace_30.pth')) HEAD.load_state_dict( torch.load('./trained_model/Head_IR_50_ArcFace_30.pth')) BACKBONE.eval() HEAD.eval() image = image.to(DEVICE) bs, ncrops, c, h, w = image.size() inputs = image.view(-1, c, h, w) features = BACKBONE(inputs) outputs = HEAD(features, None) outputs = outputs.view(bs, ncrops, -1).mean(1) top_probs, top_labs = outputs.data.topk(1) top_labs = top_labs.cpu().numpy() top_probs = top_probs.cpu().numpy() return int(top_labs), float(top_probs)
def __init__(self, device, head_name = cfg['HEAD_NAME'], NUM_CLASS = cfg['NUM_CLASS'], GPU_ID = cfg['GPU_ID'], EMBEDDING_SIZE = cfg['EMBEDDING_SIZE'], HEAD_RESUME_ROOT = cfg['HEAD_RESUME_ROOT']): super().__init__() HEAD_DICT = {'ArcFace': ArcFace(in_features = EMBEDDING_SIZE, out_features = NUM_CLASS, device_id = GPU_ID), 'CosFace': CosFace(in_features = EMBEDDING_SIZE, out_features = NUM_CLASS, device_id = GPU_ID), 'SphereFace': SphereFace(in_features = EMBEDDING_SIZE, out_features = NUM_CLASS, device_id = GPU_ID), 'Am_softmax': Am_softmax(in_features = EMBEDDING_SIZE, out_features = NUM_CLASS, device_id = GPU_ID)} self.device = device self.head = HEAD_DICT[head_name] self.head.load_state_dict(torch.load(HEAD_RESUME_ROOT)) self.head = self.head.to(device)
BACKBONE = shufflenetv2(cfg=cfg) elif BACKBONE_NAME == 'Mobilenet': BACKBONE = mobilenet() elif BACKBONE_NAME == 'Mobilenetv2': BACKBONE = mobilenetv2() elif BACKBONE_NAME == 'mobileface': BACKBONE = mobileface() else: raise NotImplementedError print("=" * 60) print(BACKBONE) print("{} Backbone Generated".format(BACKBONE_NAME)) print("=" * 60) if HEAD_NAME == 'ArcFace': HEAD = ArcFace(in_features = EMBEDDING_SIZE, out_features = NUM_CLASS, device_id = GPU_ID) elif HEAD_NAME == 'CosFace': HEAD = CosFace(in_features = EMBEDDING_SIZE, out_features = NUM_CLASS, device_id = GPU_ID) elif HEAD_NAME == 'SphereFace': HEAD = SphereFace(in_features = EMBEDDING_SIZE, out_features = NUM_CLASS, device_id = GPU_ID) elif HEAD_NAME == 'Am_softmax': HEAD = Am_softmax(in_features = EMBEDDING_SIZE, out_features = NUM_CLASS, device_id = GPU_ID) elif HEAD_NAME == 'Softmax': HEAD = Softmax(in_features = EMBEDDING_SIZE, out_features = NUM_CLASS, device_id = GPU_ID) else: raise NotImplementedError print("=" * 60) print(HEAD) print("{} Head Generated".format(HEAD_NAME)) print("=" * 60)
'IR_50': IR_50(INPUT_SIZE), 'IR_101': IR_101(INPUT_SIZE), 'IR_152': IR_152(INPUT_SIZE), 'IR_SE_50': IR_SE_50(INPUT_SIZE), 'IR_SE_101': IR_SE_101(INPUT_SIZE), 'IR_SE_152': IR_SE_152(INPUT_SIZE) } BACKBONE = BACKBONE_DICT[BACKBONE_NAME] print("=" * 60) print(BACKBONE) print("{} Backbone Generated".format(BACKBONE_NAME)) print("=" * 60) HEAD_DICT = { 'ArcFace': ArcFace(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS), 'CosFace': CosFace(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS), 'SphereFace': SphereFace(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS), 'Am_softmax': Am_softmax(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS) } HEAD = HEAD_DICT[HEAD_NAME] print("=" * 60) print(HEAD) print("{} Head Generated".format(HEAD_NAME)) print("=" * 60) LOSS_DICT = {'Focal': FocalLoss(), 'Softmax': nn.CrossEntropyLoss()} LOSS = LOSS_DICT[LOSS_NAME]
'IR_SE_101': IR_SE_101(INPUT_SIZE), 'IR_SE_152': IR_SE_152(INPUT_SIZE) } BACKBONE = BACKBONE_DICT[BACKBONE_NAME] if BACKBONE_NAME == 'ppResNet_50' and USE_PRETRAINED: pretrained_weight = download() load_weight(model=BACKBONE, weight_path=pretrained_weight) logger.info("=" * 60) # logger.info(BACKBONE) logger.info("{} Backbone Generated".format(BACKBONE_NAME)) logger.info("=" * 60) HEAD_DICT = { 'ArcFace': ArcFace(embedding_size=EMBEDDING_SIZE, class_dim=NUM_CLASS), 'CosFace': CosFace(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS), 'SphereFace': SphereFace(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS), 'Am_softmax': Am_softmax(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS), 'Softmax': Softmax(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS) } HEAD = HEAD_DICT[HEAD_NAME] logger.info("=" * 60) # logger.info(HEAD) logger.info("{} Head Generated".format(HEAD_NAME)) logger.info("=" * 60)
'IR_101': IR_101(INPUT_SIZE), 'IR_152': IR_152(INPUT_SIZE), 'IR_SE_50': IR_SE_50(INPUT_SIZE), 'IR_SE_101': IR_SE_101(INPUT_SIZE), 'IR_SE_152': IR_SE_152(INPUT_SIZE) } BACKBONE = BACKBONE_DICT[BACKBONE_NAME] print("=" * 60) print(BACKBONE) print("{} Backbone Generated".format(BACKBONE_NAME)) print("=" * 60) HEAD_DICT = { 'ArcFace': ArcFace(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS, device_id=GPU_ID), 'CosFace': CosFace(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS, device_id=GPU_ID), 'SphereFace': SphereFace(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS, device_id=GPU_ID), 'Am_softmax': Am_softmax(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS, device_id=GPU_ID) } HEAD = HEAD_DICT[HEAD_NAME]
BACKBONE_DICT = {'ResNet_50': ResNet_50(INPUT_SIZE), 'ResNet_101': ResNet_101(INPUT_SIZE), 'ResNet_152': ResNet_152(INPUT_SIZE), 'IR_50': IR_50(INPUT_SIZE), 'IR_101': IR_101(INPUT_SIZE), 'IR_152': IR_152(INPUT_SIZE), 'IR_SE_50': IR_SE_50(INPUT_SIZE), 'IR_SE_101': IR_SE_101(INPUT_SIZE), 'IR_SE_152': IR_SE_152(INPUT_SIZE)} BACKBONE = BACKBONE_DICT[BACKBONE_NAME] print("=" * 60) print(BACKBONE) print("{} Backbone Generated".format(BACKBONE_NAME)) print("=" * 60) HEAD_DICT = {'ArcFace': ArcFace(in_features = EMBEDDING_SIZE, out_features = NUM_CLASS, device_id = GPU_ID), 'CosFace': CosFace(in_features = EMBEDDING_SIZE, out_features = NUM_CLASS, device_id = GPU_ID), 'SphereFace': SphereFace(in_features = EMBEDDING_SIZE, out_features = NUM_CLASS, device_id = GPU_ID), 'Am_softmax': Am_softmax(in_features = EMBEDDING_SIZE, out_features = NUM_CLASS, device_id = GPU_ID)} HEAD = HEAD_DICT[HEAD_NAME] print("=" * 60) print(HEAD) print("{} Head Generated".format(HEAD_NAME)) print("=" * 60) LOSS_DICT = {'Focal': FocalLoss(), 'Softmax': nn.CrossEntropyLoss() 'AdaCos' : AdaCos(), 'AdaM_Softmax': AdaM_Softmax() , 'ArcFace' : ArcFace() , 'ArcNegFace': ArcNegFace(),
BACKBONE_DICT = {'ResNet_50': ResNet_50(INPUT_SIZE), 'ResNet_101': ResNet_101(INPUT_SIZE), 'ResNet_152': ResNet_152(INPUT_SIZE), 'IR_50': IR_50(INPUT_SIZE), 'IR_101': IR_101(INPUT_SIZE), 'IR_152': IR_152(INPUT_SIZE), 'IR_SE_50': IR_SE_50(INPUT_SIZE), 'IR_SE_101': IR_SE_101(INPUT_SIZE), 'IR_SE_152': IR_SE_152(INPUT_SIZE)} BACKBONE = BACKBONE_DICT[BACKBONE_NAME] print("=" * 60) print(BACKBONE) print("{} Backbone Generated".format(BACKBONE_NAME)) print("=" * 60) HEAD_DICT = {'ArcFace': ArcFace(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS), 'CosFace': CosFace(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS), 'SphereFace': SphereFace(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS), 'Am_softmax': Am_softmax(in_features=EMBEDDING_SIZE, out_features=NUM_CLASS)} HEAD = HEAD_DICT[HEAD_NAME] print("=" * 60) print(HEAD) print("{} Head Generated".format(HEAD_NAME)) print("=" * 60) LOSS_DICT = {'Focal': FocalLoss(), 'Softmax': nn.CrossEntropyLoss()} LOSS = LOSS_DICT[LOSS_NAME] print("=" * 60) print(LOSS) print("{} Loss Generated".format(LOSS_NAME))