コード例 #1
0
ファイル: utils.py プロジェクト: anthonychen1251/BudgetNet
def get_model(model):

    from models import resnet, base

    if model == 'R32_C10':
        rnet_checkpoint = 'cv/pretrained/R32_C10/pk_E_164_A_0.923.t7'
        layer_config = [5, 5, 5]
        rnet = resnet.FlatResNet32(base.BasicBlock,
                                   layer_config,
                                   num_classes=10)
        agent = resnet.Policy32([1, 1, 1], num_blocks=15)

    elif model == 'R110_C10':
        rnet_checkpoint = 'cv/pretrained/R110_C10/pk_E_130_A_0.932.t7'
        layer_config = [18, 18, 18]
        rnet = resnet.FlatResNet32(base.BasicBlock,
                                   layer_config,
                                   num_classes=10)
        agent = resnet.Policy32([1, 1, 1], num_blocks=54)

    elif model == 'R32_C100':
        rnet_checkpoint = 'cv/pretrained/R32_C100/pk_E_164_A_0.693.t7'
        layer_config = [5, 5, 5]
        rnet = resnet.FlatResNet32(base.BasicBlock,
                                   layer_config,
                                   num_classes=100)
        agent = resnet.Policy32([1, 1, 1], num_blocks=15)

    elif model == 'R110_C100':
        rnet_checkpoint = 'cv/pretrained/R110_C100/pk_E_160_A_0.723.t7'
        layer_config = [18, 18, 18]
        rnet = resnet.FlatResNet32(base.BasicBlock,
                                   layer_config,
                                   num_classes=100)
        agent = resnet.Policy32([1, 1, 1], num_blocks=54)

    elif model == 'R101_ImgNet':
        rnet_checkpoint = 'cv/pretrained/R101_ImgNet/ImageNet_R101_224_76.464'
        layer_config = [3, 4, 23, 3]
        rnet = resnet.FlatResNet224(base.Bottleneck,
                                    layer_config,
                                    num_classes=1000)
        agent = resnet.Policy224([1, 1, 1, 1], num_blocks=33)

    # elif model=='ResNext_C100':
    #     agent = resnet.Policy32([1,1,1], num_blocks=4*18)
    # elif model=='ResNext_C10':
    #     agent = resnet.Policy32([1,1,1], num_blocks=4*18)

    # load pretrained weights into flat ResNet
    # rnet_checkpoint = torch.load(rnet_checkpoint)
    # load_weights_to_flatresnet(rnet_checkpoint, rnet)

    return agent
コード例 #2
0
def get_resnet_model(dataset_type, blocks_list=[18, 18, 18]):
    if dataset_type == Data.cifar10:
        return resnet.FlatResNet32(base.BasicBlock,
                                   blocks_list,
                                   num_classes=10)
    elif dataset_type == Data.cifar100:
        return resnet.FlatResNet32(base.BasicBlock,
                                   blocks_list,
                                   num_classes=100)
    elif dataset_type == Data.mini_imagenet:
        return resnet.FlatResNet224(base.Bottleneck,
                                    blocks_list,
                                    num_classes=80)
    else:
        return resnet.FlatResNet224(base.Bottleneck,
                                    blocks_list,
                                    num_classes=1000)
コード例 #3
0
    print('E:%d Test Acc: %.3f' % (epoch, correct / total))
    acc = acc + (correct/total)


# dataset and dataloader
trainset, testset = get_policy_datasets(Data.cifar10, Mode.with_policy, iter=args.iter)
trainloader = D.DataLoader(trainset, batch_size=args.batch_size, shuffle=True, num_workers=4)
testloader = D.DataLoader(testset, batch_size=256, shuffle=False, num_workers=4)

# global metric
best_test_acc = 0.0
epoch = 0

# configuration of the ResNet
if args.resume:
    rnet = resnet.FlatResNet32(base.BasicBlock, [18, 18, 18], num_classes=10)
    state = torch.load(os.path.join(savepath, 'rnet.t7'))
    rnet.load_state_dict(state['net'])
    best_test_acc = state['acc']
    epoch = state['epoch']
else:
    rnet = resnet.FlatResNet32(base.BasicBlock, [18, 18, 18], num_classes=10)
    rnet_dict = torch.load(os.path.join(rootpath, 'rnet.t7'))
    rnet.load_state_dict(rnet_dict['net'])

# Policy Network: ResNet-10
agent = Networks.PolicyNet10()
ckpt_dict = torch.load(os.path.join(rootpath, 'agent_best.t7'))
agent.load_state_dict(ckpt_dict['net'])

# Loss Fn and Optimizer
コード例 #4
0
def get_model(model):

    from models import resnet, base

    if model == 'policy_satellite':
        # agent = resnet.Policy224([1,1,1,1], num_blocks=289, num_feat=20)
        # agent = resnet.Policy224GRU([1,1,1,1], num_blocks=289, num_feat=128)
        # agent = resnet.PolicySeq()
        agent = resnet.Policy2x2([1, 1, 1, 1], num_blocks=4)
        rnet_hr = None
        rnet_lr = None

    elif model == 'R32_C10':
        layer_config = [5, 5, 5]
        rnet_hr = resnet.FlatResNet32(base.BasicBlock,
                                      layer_config,
                                      num_classes=10)
        rnet_lr = resnet.FlatResNet32(base.BasicBlock,
                                      layer_config,
                                      num_classes=10)
        agent = resnet.Policy32([1, 1, 1], num_blocks=16)

    elif model == 'R32_C100':
        layer_config = [5, 5, 5]
        rnet_hr = resnet.FlatResNet32(base.BasicBlock,
                                      layer_config,
                                      num_classes=100)
        rnet_lr = resnet.FlatResNet32(base.BasicBlock,
                                      layer_config,
                                      num_classes=100)
        agent = resnet.Policy32([1, 1, 1], num_blocks=16)

    elif model == 'R50_ImgNet':
        agent = resnet.Policy224([1, 1, 1, 1], num_blocks=16)
        """ High Res. Classifier """
        rnet_hr = torchmodels.resnet50(pretrained=False)
        set_parameter_requires_grad(rnet_hr, False)
        num_ftrs = rnet_hr.fc.in_features
        rnet_hr.fc = torch.nn.Linear(num_ftrs, 1000)
        """ Low Res. Classifier """
        rnet_lr = torchmodels.resnet50(pretrained=False)
        set_parameter_requires_grad(rnet_lr, False)
        num_ftrs = rnet_lr.fc.in_features
        rnet_lr.fc = torch.nn.Linear(num_ftrs, 1000)

    elif model == 'R34_fMoW':
        agent = resnet.Policy224([1, 1, 1, 1], num_blocks=16)
        """ High Res. Classifier """
        rnet_hr = torchmodels.resnet34(pretrained=True)
        set_parameter_requires_grad(rnet_hr, False)
        num_ftrs = rnet_hr.fc.in_features
        rnet_hr.fc = torch.nn.Linear(num_ftrs, 62)
        """ Low Res. Classifier """
        rnet_lr = torchmodels.resnet34(pretrained=True)
        set_parameter_requires_grad(rnet_lr, False)
        num_ftrs = rnet_lr.fc.in_features
        rnet_lr.fc = torch.nn.Linear(num_ftrs, 62)

    elif model == 'R34_CARS':
        agent = resnet.Policy224([1, 1, 1, 1], num_blocks=16)
        """ High Res. Classifier """
        rnet_hr = torchmodels.resnet34(pretrained=True)
        set_parameter_requires_grad(rnet_hr, False)
        num_ftrs = rnet_hr.fc.in_features
        rnet_hr.fc = torch.nn.Linear(num_ftrs, 196)
        """ Low Res. Classifier """
        rnet_lr = torchmodels.resnet34(pretrained=True)
        set_parameter_requires_grad(rnet_lr, False)
        num_ftrs = rnet_lr.fc.in_features
        rnet_lr.fc = torch.nn.Linear(num_ftrs, 196)

    return rnet_hr, rnet_lr, agent