Exemple #1
0
def main():
    # you should set data path on the top
    global train_path, val_path, test_path
    global train_batch_size
    if 'Flower102' in args.data_name:
        train_path = "./Flower102/train"
        test_path = "./Flower102/test"
        val_path = "./Flower102/val"
    elif 'Birds200' in args.data_name:
        train_path = "./Birds200/train"
        test_path = "./Birds200/test"

    # global train_path, test_path
    if 'Flower102' in train_path:
        model = ModifiedAlexNet(102)
        train_loader = dataset.train_loader(train_path,
                                            batch_size=train_batch_size,
                                            num_workers=4,
                                            pin_memory=True)
        val_loader = dataset.test_loader(val_path,
                                         batch_size=1,
                                         num_workers=4,
                                         pin_memory=True)
        test_loader = dataset.test_loader(test_path,
                                          batch_size=1,
                                          num_workers=4,
                                          pin_memory=True)
    elif 'Birds200' in train_path:
        model = ModifiedAlexNet(200)
        train_loader = dataset.train_loader(train_path,
                                            batch_size=train_batch_size,
                                            num_workers=4,
                                            pin_memory=True)
        test_loader = dataset.test_loader(test_path,
                                          batch_size=1,
                                          num_workers=4,
                                          pin_memory=True)
    if use_gpu:
        model = model.cuda()
        print("Use GPU!")
    else:
        print("Use CPU!")

    checkpoint = torch.load('./models/alexnet_Flower102_0.787.pth')
    model.load_state_dict(checkpoint, strict=True)

    if 'Flower102' in train_path:
        train_val_test(model,
                       train_loader,
                       val_loader,
                       test_loader,
                       optimizer=None,
                       epoches=50)
    elif 'Birds200' in train_path:
        train_test(model,
                   train_loader,
                   test_loader,
                   optimizer=None,
                   epoches=10)
Exemple #2
0
def main():
    global args, num_batches, use_gpu
    if not args.zeroTrain:
        model = models.sigmoid_ResnetModel(args.arch, 224, args.num_classs,
                                           args.Conv_Type, args.Branch,
                                           args.Conv_Num)
        if args.resume:
            checkpoint = torch.load(args.resume)
            model.load_state_dict(checkpoint, strict=True)

        if use_gpu:
            model = model.cuda()
            print("Use GPU!")
        else:
            print("Use CPU!")

        train_loader = dataset.train_loader(args.train_path,
                                            args.relative_path,
                                            batch_size=args.batch_size,
                                            num_workers=10,
                                            pin_memory=True)
        #test_train_loader = dataset.test_loader(args.test_path, args.relative_path, batch_size=1, num_workers=5, pin_memory=True)
        #val_loader = dataset.test_loader(args.val_path, args.relative_path, batch_size=1, num_workers=5, pin_memory=True)
        # test_loader = dataset.test_loader(rgs.test_path, args.relative_path, batch_size=1, num_workers=5, pin_memory=True)

    train_val(model,
              train_loader,
              test_train_loader=None,
              val_loader=None,
              print_freq=args.print_freq,
              optimizer=None,
              epoches=args.epochs,
              Branch=args.Branch)
def main():
    global args, num_batches, use_gpu
    model = models.sigmoid_ResnetModel('Resnet152', 224, args.num_classs, 2,
                                       17, 5)
    if args.resume:
        checkpoint = torch.load(args.resume)
        model.load_state_dict(checkpoint, strict=True)

    if use_gpu:
        model = model.cuda()
        print("Use GPU!")
    else:
        print("Use CPU!")

    csvfile = open('./Consumer_loss.csv', 'wb')
    fieldnames = ['img path', 'total loss']
    writer = csv.DictWriter(csvfile, delimiter=',', fieldnames=fieldnames)  #
    writer.writeheader()
    data_loader = dataset.train_loader(args.data_path,
                                       args.relative_path,
                                       batch_size=16,
                                       num_workers=10,
                                       pin_memory=True)

    get_data_loss(csvfile, writer, model, data_loader)
def finetune():
    global infoLogger
    global use_gpu, num_batches, train_batch_size

    if not zero_train:
        global train_path, test_path
        global train_batch_size
        if 'Flower102' in data_name:
            train_path = "./Flower102/train"
            test_path = "./Flower102/test"
            num_classes = 102
        elif 'Birds200' in data_name:
            train_path = "./Birds200/train"
            test_path = "./Birds200/test"
            num_classes = 200
        elif 'catdog' in data_name:
            train_path = "./CatDog/train"
            test_path = "./CatDog/test"
            num_classes = 2

        if not checkpoint:
            if use_gpu:
                print("Use GPU!")
                model = firstreloadParam(num_classes)
        else:
            checkpointPath = './1x1models/finetune/alexnet1x1_Flower102_0.821.pth'
            state = torch.load(checkpointPath)
            if use_gpu:
                print("Use GPU!")
                model = add1x1layers(num_classes).cuda()
            else:
                print("Use CPU!")
                model = add1x1layers(num_classes)
            model.load_state_dict(state)
        train_loader = dataset.train_loader(train_path,
                                            batch_size=train_batch_size,
                                            num_workers=4,
                                            pin_memory=True)
        test_loader = dataset.test_loader(test_path,
                                          batch_size=1,
                                          num_workers=4,
                                          pin_memory=True)
        infoLogger.info("dataset is: " + args.data_name)
        optimizer = SGD(
            [{
                'params': model.classifier.parameters()
            }  # 浅层1x1不好剪枝, 先冻住
             ],
            weight_decay2=5e-4,
            lr=1e-3,
            momentum=0.9)
        infoLogger.info("weight_decay2 is: " + str(5e-4))
        infoLogger.info("learning rate is: " + str(1e-3))
        train_test(model,
                   train_loader,
                   test_loader,
                   optimizer=optimizer,
                   epoches=20)
def main():
    global args, num_batches, use_gpu
    if not args.zeroTrain:
        if args.arch == "densenet201":
            model = models.Modified_densenet201()
        elif args.arch == "Resnet50":
            model = models.Modified_Resnet50()
        elif args.arch == "Resnet101":
            model = models.Modified_Resnet101()
        elif args.arch == "Resnet152":
            model = models.Modified_Resnet152()
        elif args.arch == "nasnetalarge":
            model = models.Modified_nasnetalarge()

        if args.resume:
            checkpoint = torch.load(args.resume)
            model.load_state_dict(checkpoint, strict=True)

        if use_gpu:
            model = model.cuda()
            print("Use GPU!")
        else:
            print("Use CPU!")

        train_path = args.data_path[0]
        val_path = args.data_path[1]
        test_path = args.data_path[2]

        train_loader = dataset.train_loader(train_path,
                                            batch_size=args.batch_size,
                                            num_workers=10,
                                            pin_memory=True)
        val_loader = dataset.test_loader(val_path,
                                         batch_size=1,
                                         num_workers=10,
                                         pin_memory=True)
        test_loader = dataset.test_loader(test_path,
                                          batch_size=1,
                                          num_workers=10,
                                          pin_memory=True)

        if args.TenCrop:
            TenCroptest_loader = dataset.testTenCrop_loader(test_path,
                                                            batch_size=1,
                                                            num_workers=10,
                                                            pin_memory=True)

    train_val_test(model,
                   train_loader,
                   val_loader,
                   test_loader,
                   print_freq=50,
                   TenCroptest_loader=TenCroptest_loader,
                   optimizer=None,
                   epoches=args.epochs)
Exemple #6
0
def main():
    global args, num_batches, use_gpu
    input_size = args.input_size
    if not args.zeroTrain:
        if args.resume:
            model = models.Modified_Resnet(args.arch, 46, input_size, False)
            checkpoint = torch.load(args.resume)
            model.load_state_dict(checkpoint, strict=True)
        else:
            model = models.Modified_Resnet(args.arch, 46, input_size,
                                           not args.zeroTrain)
    elif args.zeroTrain:
        model = models.Modified_Resnet(args.arch, 46, input_size,
                                       not args.zeroTrain)

    if use_gpu:
        model = model.cuda()
        print("Use GPU!")
    else:
        print("Use CPU!")

    if not os.path.exists('./checkpoint'):
        os.mkdir('./checkpoint')

    train_root = args.train_path
    val_root = args.val_path
    test_root = args.test_path

    train_loader = dataset.train_loader(train_root,
                                        args.input_size,
                                        batch_size=args.batch_size,
                                        num_workers=5,
                                        pin_memory=True)
    # test_train_loader = dataset.test_loader(train_root, args.input_size, batch_size=1, num_workers=5, pin_memory=True)
    # val_loader = dataset.test_loader(val_root, args.input_size, batch_size=1, num_workers=5, pin_memory=True)
    # test_loader = dataset.test_loader(test_root, args.input_size, batch_size=1, num_workers=5, pin_memory=True)
    train_val(model,
              train_loader,
              test_train_loader=None,
              val_loader=None,
              test_loader=None,
              print_freq=args.print_freq,
              optimizer=None,
              epoches=args.epochs)
Exemple #7
0
if 'Flower102' in data_name:
    train_path = "./Flower102/train"
    test_path = "./Flower102/test"
    num_classes = 102
elif 'Birds200' in data_name:
    train_path = "./Birds200/train"
    test_path = "./Birds200/test"
    num_classes = 200
elif 'catdog' in data_name:
    train_path = "./CatDog/train"
    test_path = "./CatDog/test"
    num_classes = 2

train_loader = dataset.train_loader(train_path,
                                    batch_size=train_batch_size,
                                    num_workers=4,
                                    pin_memory=True)
test_loader = dataset.test_loader(test_path,
                                  batch_size=1,
                                  num_workers=4,
                                  pin_memory=True)
infoLogger.info("dataset is: " + args.data_name)


def test(model, test_loader):
    model.eval()
    test_correct = 0
    test_total = 0

    for i, (batch, label) in enumerate(test_loader):
        batch = batch.cuda()
Exemple #8
0
def finetune():
    global train_batch_size
    if arch == "mobilenetv1":
        model = models.MobileNet(20)
        if use_gpu:
            model = model.cuda()
    elif arch == "mobilenetv2":
        model = models.MobileNetV2(20)
        if use_gpu:
            model = model.cuda()
    elif arch == 'squeezenet1_1':
        model = models.squeezenet1_1(20, pretrained=False)
        if use_gpu:
            model = model.cuda()

    if checkpoint:
        if arch == "mobilenetv2":
            checkpointPath = './checkpoint/mobilenetv20.865.pth'
            state = torch.load(checkpointPath)
            model.load_state_dict(state)
            optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
        if arch == 'squeezenet1_1':
            checkpointPath = './checkpoint/squeezenet1_10.762.pth'
            state = torch.load(checkpointPath)
            model.load_state_dict(state)
            optimizer = optim.SGD(model.newclassifier.parameters(),
                                  lr=0.01,
                                  momentum=0.9)
    else:
        if arch == "mobilenetv1":
            state = torch.load(
                './checkpoint/mobilenet_sgd_rmsprop_69.526.tar')['state_dict']
            model.load_state_dict(state, strict=False)
            optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
        elif arch == "mobilenetv2":
            state = torch.load(
                './checkpoin t/mobilenetv2_Top1_71.806_Top2_90.410.pth.tar')
            model.load_state_dict(state, strict=False)
            optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
        elif arch == 'squeezenet1_1':
            model = models.squeezenet1_1(20, pretrained=True)
            if use_gpu:
                model = model.cuda()
            optimizer = optim.SGD(model.newclassifier.parameters(),
                                  lr=0.01,
                                  momentum=0.9)

    # train_path = '/home/smiles/ModelPruning/CatDog/train'
    # test_path = '/home/smiles/ModelPruning/CatDog/test'

    train_path = './train'
    # test_path = train_path
    test_path = './test'
    train_loader = dataset.train_loader(train_path,
                                        batch_size=train_batch_size,
                                        num_workers=4,
                                        pin_memory=True)
    test_loader = dataset.test_loader(test_path,
                                      batch_size=1,
                                      num_workers=4,
                                      pin_memory=True)

    train_test('./checkpoint/',
               arch,
               model,
               train_loader,
               test_loader,
               optimizer=optimizer,
               epoches=40)