Exemple #1
0
def BulidModel(args):
    """Bulid Model."""
    if args.local_feat:
        if args.net == 'ResNet18':
            model = IR(18, args.intra_gcn, args.inter_gcn, args.rand_mat, args.all1_mat, args.use_cov, args.use_cluster,args.class_num)
        elif args.net == 'ResNet50':
            model = IR(50, args.intra_gcn, args.inter_gcn, args.rand_mat, args.all1_mat, args.use_cov, args.use_cluster,args.class_num)
            print("USING LOCAL FEATURES")
        elif args.net == 'VGGNet':
            model = VGG(args.intra_gcn, args.inter_gcn, args.rand_mat, args.all1_mat, args.use_cov, args.use_cluster, args.class_num)
        elif args.net == 'MobileNet':
            model = MobileNetV2(args.intra_gcn, args.inter_gcn, args.rand_mat, args.all1_mat, args.use_cov, args.use_cluster, args.class_num)
    else:
        if args.net == 'ResNet18':
            model = IR_onlyGlobal(18)
        elif args.net == 'ResNet50':
            model = IR_onlyGlobal(50)
        elif args.net == 'VGGNet':
            model = VGG_onlyGlobal()
        elif args.net == 'MobileNet':
            model = MobileNetV2_onlyGlobal()

    if args.pretrained != 'None':
        print('Resume Model: {}'.format(args.pretrained))
        checkpoint = torch.load(args.pretrained, map_location='cpu')

        model.load_state_dict(checkpoint, strict=True)
    else:
        print('No Resume Model')
    
    if torch.cuda.device_count() > 1:
        model = nn.DataParallel(model)

    #model = model.cuda()
    model.cuda()
    return model
import torch

from ResNet import IR, IR_onlyGlobal

numOfLayer = 50  # [18, 50]
model = IR(numOfLayer=numOfLayer,
           useIntraGCN=True,
           useInterGCN=True,
           useRandomMatrix=False,
           useAllOneMatrix=False,
           useCov=False,
           useCluster=True)
# model = IR_onlyGlobal(numOfLayer=numOfLayer)

model_dict = model.state_dict()
checkpoint = torch.load('../preTrainedModel/backbone_ir50_ms1m_epoch120.pth') if numOfLayer == 50 else \
             torch.load('../preTrainedModel/backbone_IR_18_HeadFC_Softmax_112_512_1.0_Epoch_156_lfw_112_0.994_X4_112_0.990_agedb_30_112_0.949.pth')

indexToLayer = {'0':'layer1.0.', '1':'layer1.1.', '2':'layer1.2.', \
                '3':'layer2.0.', '4':'layer2.1.', '5':'layer2.2.', '6':'layer2.3.',\
                '7':'layer3.0.', '8':'layer3.1.', '9':'layer3.2.', '10':'layer3.3.', '11':'layer3.4.', '12':'layer3.5.', '13':'layer3.6.', '14':'layer3.7.', '15':'layer3.8.', '16':'layer3.9.', '17':'layer3.10.', '18':'layer3.11.', '19':'layer3.12.', '20':'layer3.13.',\
                '21':'layer4.0.', '22':'layer4.1.', '23':'layer4.2.'} if numOfLayer == 50 else \
               {'0':'layer1.0.', '1':'layer1.1.', \
                '2':'layer2.0.', '3':'layer2.1.', \
                '4':'layer3.0.', '5':'layer3.1.', \
                '6':'layer4.0.', '7':'layer4.1.'}

newCheckpoint = {}
for key, value in checkpoint.items():
    subStr = key.split('.', 2)
    if subStr[0] == 'body':
Exemple #3
0
import torch

from ResNet import IR

numOfLayer = 50  # [18, 50]
model = IR(numOfLayer)

model_dict = model.state_dict()
checkpoint = torch.load('../preTrainedModel/backbone_ir50_ms1m_epoch120.pth') if numOfLayer == 50 else \
             torch.load('../preTrainedModel/backbone_IR_18_HeadFC_Softmax_112_512_1.0_Epoch_156_lfw_112_0.994_X4_112_0.990_agedb_30_112_0.949.pth')

indexToLayer = {'0':'layer1.0.', '1':'layer1.1.', '2':'layer1.2.', \
                '3':'layer2.0.', '4':'layer2.1.', '5':'layer2.2.', '6':'layer2.3.',\
                '7':'layer3.0.', '8':'layer3.1.', '9':'layer3.2.', '10':'layer3.3.', '11':'layer3.4.', '12':'layer3.5.', '13':'layer3.6.', '14':'layer3.7.', '15':'layer3.8.', '16':'layer3.9.', '17':'layer3.10.', '18':'layer3.11.', '19':'layer3.12.', '20':'layer3.13.',\
                '21':'layer4.0.', '22':'layer4.1.', '23':'layer4.2.'} if numOfLayer == 50 else \
               {'0':'layer1.0.', '1':'layer1.1.', \
                '2':'layer2.0.', '3':'layer2.1.', \
                '4':'layer3.0.', '5':'layer3.1.', \
                '6':'layer4.0.', '7':'layer4.1.'}

newCheckpoint = {}
for key, value in checkpoint.items():
    subStr = key.split('.', 2)
    if subStr[0] == 'body':
        newKey = indexToLayer[subStr[1]] + subStr[2]
        newCheckpoint[newKey] = value
    elif subStr[0] == 'output_layer':
        continue
    else:
        newCheckpoint[key] = value
import torch

from ResNet import IR, IR_onlyGlobal

numOfLayer = 50 # [18, 50]
model = IR(numOfLayer=numOfLayer, useIntraGCN=False, useInterGCN=False, useRandomMatrix=False, useAllOneMatrix=False, useCov=False, useCluster=False)
# model = IR_onlyGlobal(numOfLayer=numOfLayer)

model_dict = model.state_dict()
checkpoint = torch.load('../preTrainedModel/backbone_ir50_ms1m_epoch120.pth') if numOfLayer == 50 else \
             torch.load('../preTrainedModel/backbone_IR_18_HeadFC_Softmax_112_512_1.0_Epoch_156_lfw_112_0.994_X4_112_0.990_agedb_30_112_0.949.pth')

indexToLayer = {'0':'layer1.0.', '1':'layer1.1.', '2':'layer1.2.', \
                '3':'layer2.0.', '4':'layer2.1.', '5':'layer2.2.', '6':'layer2.3.',\
                '7':'layer3.0.', '8':'layer3.1.', '9':'layer3.2.', '10':'layer3.3.', '11':'layer3.4.', '12':'layer3.5.', '13':'layer3.6.', '14':'layer3.7.', '15':'layer3.8.', '16':'layer3.9.', '17':'layer3.10.', '18':'layer3.11.', '19':'layer3.12.', '20':'layer3.13.',\
                '21':'layer4.0.', '22':'layer4.1.', '23':'layer4.2.'} if numOfLayer == 50 else \
               {'0':'layer1.0.', '1':'layer1.1.', \
                '2':'layer2.0.', '3':'layer2.1.', \
                '4':'layer3.0.', '5':'layer3.1.', \
                '6':'layer4.0.', '7':'layer4.1.'}

newCheckpoint = {}
for key, value in checkpoint.items():
    subStr = key.split('.',2)
    if subStr[0]=='body':
        newKey = indexToLayer[subStr[1]] + subStr[2]
        newCheckpoint[newKey] = value
    elif subStr[0]=='output_layer':
        continue
    else:
        newCheckpoint[key] = value
import torch

from ResNet import IR

numOfLayer = 50 # [18, 50]
model = IR(numOfLayer)

model_dict = model.state_dict()
checkpoint = torch.load('../preTrainedModel/backbone_ir50_ms1m_epoch120.pth') if numOfLayer == 50 else \
             torch.load('../preTrainedModel/backbone_IR_18_HeadFC_Softmax_112_512_1.0_Epoch_156_lfw_112_0.994_X4_112_0.990_agedb_30_112_0.949.pth')

indexToLayer = {'0':'layer1.0.', '1':'layer1.1.', '2':'layer1.2.', \
                '3':'layer2.0.', '4':'layer2.1.', '5':'layer2.2.', '6':'layer2.3.',\
                '7':'layer3.0.', '8':'layer3.1.', '9':'layer3.2.', '10':'layer3.3.', '11':'layer3.4.', '12':'layer3.5.', '13':'layer3.6.', '14':'layer3.7.', '15':'layer3.8.', '16':'layer3.9.', '17':'layer3.10.', '18':'layer3.11.', '19':'layer3.12.', '20':'layer3.13.',\
                '21':'layer4.0.', '22':'layer4.1.', '23':'layer4.2.'} if numOfLayer == 50 else \
               {'0':'layer1.0.', '1':'layer1.1.', \
                '2':'layer2.0.', '3':'layer2.1.', \
                '4':'layer3.0.', '5':'layer3.1.', \
                '6':'layer4.0.', '7':'layer4.1.'}

newCheckpoint = {}
for key, value in checkpoint.items():
    subStr = key.split('.',2)
    if subStr[0]=='body':
        newKey = indexToLayer[subStr[1]] + subStr[2]
        newCheckpoint[newKey] = value
    elif subStr[0]=='output_layer':
        continue
    else:
        newCheckpoint[key] = value