Exemplo n.º 1
0
def main_test():
    parser = argparse.ArgumentParser(description='Detector')
    parser.add_argument('--batch-size', type=int, default=50, metavar='N',				
                        help='input batch size for training (default: 64)')
    parser.add_argument('--test-batch-size', type=int, default=50, metavar='N',		
                        help='input batch size for testing (default: 64)')
    parser.add_argument('--epochs', type=int, default=100, metavar='N',
                        help='number of epochs to train (default: 100)')
    parser.add_argument('--lr', type=float, default=0.001, metavar='LR',
                        help='learning rate (default: 0.001)')
    parser.add_argument('--momentum', type=float, default=0.05, metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda', action='store_true', default=True,
                        help='disables CUDA training')
    parser.add_argument('--seed', type=int, default=1, metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument('--log-interval', type=int, default=20, metavar='N',
                        help='how many batches to wait before logging training status')
    parser.add_argument('--save-model', action='store_true', default=True,
                        help='save the current Model')
    parser.add_argument('--save-directory', type=str, default='trained_models',
                        help='learnt models are saving here')
    parser.add_argument('--phase', type=str, default='Train',   
                        help='training, test or predict')
    parser.add_argument('--pre_mode', type=str, default='',  
                        help='training, predicting or finetuning')
    args = parser.parse_args()
	###################################################################################
    torch.manual_seed(args.seed)
    use_cuda = not args.no_cuda and torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")    # cuda:0
	
    print('===> Loading Datasets')
    train_set, test_set = get_train_test_set()
    train_loader = torch.utils.data.DataLoader(train_set, batch_size=args.batch_size, shuffle=True)
    valid_loader = torch.utils.data.DataLoader(test_set, batch_size=args.test_batch_size)

    print('===> Building Model')
    model = Net().to(device)

    ####################################################################
    criterion_pts = nn.MSELoss()
    # optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum)
    optimizer = torch.optim.Adam(model.parameters(), lr = 0.0001)
	####################################################################
    if args.phase == 'Train' or args.phase == 'train':
        if args.pre_mode.endswith(".pt"):
            model.load_state_dict(torch.load(args.pre_mode))
        print('===> Start Training')
        train_losses, valid_losses = \
			train(args, train_loader, valid_loader, model, criterion_pts, optimizer, device)
        print('====================================================')
    elif args.phase == 'Test' or args.phase == 'test':
        print('===> Test')
        validPredict(args, "detector_epoch.pt", Net(), valid_loader)
    elif args.phase == 'Predict' or args.phase == 'predict':
        print('===> Predict')
        videoPredict(args, "detector_epoch.pt", Net())
Exemplo n.º 2
0
def main_test():
    parser = argparse.ArgumentParser(description='Detector')
    parser.add_argument('--batch-size', type=int, default=64, metavar='N',				
                        help='input batch size for training (default: 64)')
    parser.add_argument('--test-batch-size', type=int, default=64, metavar='N',		
                        help='input batch size for testing (default: 64)')
    parser.add_argument('--epochs', type=int, default=100, metavar='N',
                        help='number of epochs to train (default: 100)')
    parser.add_argument('--lr', type=float, default=0.001, metavar='LR',
                        help='learning rate (default: 0.001)')
    parser.add_argument('--momentum', type=float, default=0.5, metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda', action='store_true', default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed', type=int, default=1, metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument('--log-interval', type=int, default=20, metavar='N',
                        help='how many batches to wait before logging training status')
    parser.add_argument('--save-model', action='store_true', default=True,
                        help='save the current Model')
    parser.add_argument('--save-directory', type=str, default='trained_models',
                        help='learnt models are saving here')
    parser.add_argument('--phase', type=str, default='Train',   # Train/train, Predict/predict, Finetune/finetune
                        help='training, predicting or finetuning')
    args = parser.parse_args()
	###################################################################################
    torch.manual_seed(args.seed)
    # For single GPU
    use_cuda = not args.no_cuda and torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")    # cuda:0
    # For multi GPUs, nothing need to change here
    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
	
    print('===> Loading Datasets')
    train_set, test_set = get_train_test_set()
    train_loader = torch.utils.data.DataLoader(train_set, batch_size=args.batch_size, shuffle=True)
    valid_loader = torch.utils.data.DataLoader(test_set, batch_size=args.test_batch_size)

    print('===> Building Model')
    # For single GPU
    model = Net().to(device)
    ####################################################################
    criterion_pts = nn.MSELoss()
    optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum)
	####################################################################
    if args.phase == 'Train' or args.phase == 'train':
        print('===> Start Training')
        train_losses, valid_losses = \
			train(args, train_loader, valid_loader, model, criterion_pts, optimizer, device)
        print('====================================================')
	elif args.phase == 'Test' or args.phase == 'test'
		print('===> Test')
Exemplo n.º 3
0
def train_and_save_model():
    trainset, testset = data.get_train_test_set()
    trainloader = torch.utils.data.DataLoader(trainset,
                                              batch_size=BATCH_SIZE,
                                              shuffle=True)
    testloader = torch.utils.data.DataLoader(testset,
                                             batch_size=BATCH_SIZE,
                                             shuffle=False)
    net = resnet.resnet(in_channel=3, num_classes=1)
    optimizer = torch.optim.Adam(net.parameters(), lr=LR)
    criterion = nn.BCELoss()
    utils.train(
        net,
        trainloader,
        testloader,
        20,
        optimizer,
        criterion,
        debug=DEBUG,
    )
Exemplo n.º 4
0
def main_test():

    ###################################################################################
    torch.manual_seed(1)

    print('===> Loading Datasets')
    train_set, test_set = get_train_test_set()
    train_loader = torch.utils.data.DataLoader(train_set,
                                               batch_size=config.BATCH_TRAIN,
                                               shuffle=True)
    valid_loader = torch.utils.data.DataLoader(test_set,
                                               batch_size=config.BATCH_VAL)
    data_loaders = {'train': train_loader, 'val': valid_loader}

    print('===> Building Model')
    # For single GPU
    model = Net()

    ####################################################################
    criterion_pts = nn.MSELoss()
    weights = [1, 3]
    class_weights = torch.FloatTensor(weights)
    criterion_cls = nn.CrossEntropyLoss()

    ####################################################################
    if config.PHASE == 'Train' or config.PHASE == 'train':
        print('===> Start Training')
        _ = train(data_loaders, model, (criterion_pts, criterion_cls))
        print('====================================================')
    elif config.PHASE == 'Test' or config.PHASE == 'test':
        print('===> Test')
    # how to do test?
    elif config.PHASE == 'Finetune' or config.PHASE == 'finetune':
        print('===> Finetune')
        # how to do finetune?
    elif config.PHASE == 'Predict' or config.PHASE == 'predict':
        print('===> Predict')
        # how to do predict?
        test_img_name = 'test01.jpg'
        predict(model, test_img_name)
Exemplo n.º 5
0
def main_test():
    parser = argparse.ArgumentParser(description='Detector')
    parser.add_argument('--batch-size',
                        type=int,
                        default=64,
                        metavar='N',
                        help='input batch size for training (default: 64)')
    parser.add_argument('--test-batch-size',
                        type=int,
                        default=64,
                        metavar='N',
                        help='input batch size for testing (default: 64)')
    parser.add_argument('--epochs',
                        type=int,
                        default=100,
                        metavar='N',
                        help='number of epochs to train (default: 100)')
    parser.add_argument('--start-epoch',
                        type=int,
                        default=0,
                        metavar='N',
                        help='epoch number starts from (default: 0)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.01,
                        metavar='LR',
                        help='learning rate (default: 0.01)')
    parser.add_argument('--alg',
                        type=str,
                        default='adam',
                        help='select optimzer SGD, adam, or other')
    parser.add_argument('--loss', type=str, default='CE', help='loss function')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed',
                        type=int,
                        default=1,
                        metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=20,
        metavar='N',
        help='how many batches to wait before logging training status')
    parser.add_argument('--save-model',
                        action='store_true',
                        default=True,
                        help='save the current Model')
    parser.add_argument('--save-directory',
                        type=str,
                        default='trained_models',
                        help='learnt models are saving here')
    parser.add_argument(
        '--phase',
        type=str,
        default='Train',  # Train/train, Predict/predict, Finetune/finetune
        help='training, predicting or finetuning')
    parser.add_argument(
        '--net',
        type=str,
        default='resnet101',
        help='DefaultNet, ResNet***[18,34,50,101,152], MobileNet or GoogLeNet')
    parser.add_argument(
        '--angle',
        type=float,
        default=30,
        help='max (30) angle range to rotate original image on both side')
    parser.add_argument('--save-interval',
                        type=int,
                        default=20,
                        help='after # of epoch, save the current Model')
    parser.add_argument(
        '--checkpoint',
        type=str,
        default='',
        help='continuing the training from specified checkpoint')
    args = parser.parse_args()
    ###################################################################################
    torch.manual_seed(args.seed)
    # For single GPU
    use_cuda = not args.no_cuda and torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")  # cuda:0
    # For multi GPUs, nothing need to change here
    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}

    print('===> Loading Datasets')
    train_set, test_set = get_train_test_set(args.net, args.angle)
    train_loader = torch.utils.data.DataLoader(train_set,
                                               batch_size=args.batch_size,
                                               shuffle=True)
    valid_loader = torch.utils.data.DataLoader(test_set,
                                               batch_size=args.test_batch_size)
    # 输出类别定义
    categories = 62
    print('===> Building Model')
    # For single GPU
    if args.net == 'ResNet18' or args.net == 'resnet18':
        model = resnet18()
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, categories)
        model = model.to(device)
    elif args.net == 'ResNet34' or args.net == 'resnet34':
        model = resnet34()
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, categories)
        model = model.to(device)
    elif args.net == 'ResNet50' or args.net == 'resnet50':
        model = resnet50()
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, categories)
        model = model.to(device)
    elif args.net == 'ResNet101' or args.net == 'resnet101':
        model = resnet101()
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, categories)
        model = model.to(device)
    elif args.net == 'ResNet152' or args.net == 'resnet152':
        model = resnet152()
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, categories)
        model = model.to(device)
    elif args.net == 'GoogLeNet' or args.net == 'googlenet':
        model = GoogLeNet(num_classes=categories).to(device)
    else:
        model = resnet50()
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, categories)
        model = model.to(device)
    '''elif args.net == 'FPN':
        model = FPN101(args.batch_size).to(device)
    else:
        model = Net(args.use_bn).to(device)'''

    ####################################################################
    def load_data_t(phase):
        data_file = phase + '.csv'
        data_file = os.path.join('traffic-sign', data_file)
        with open(data_file) as f:
            lines = f.readlines()[1:]
        return lines

    train_set_t = load_data_t('train_label')
    cate_tr = [i[2] for i in [my_parse_line(i) for i in train_set_t]]
    w = Counter(cate_tr)
    total = sum(w.values())
    weight = torch.FloatTensor([w[str(i)] / total
                                for i in range(len(w))]).to(device)
    if args.loss == 'CE':
        criterion = nn.CrossEntropyLoss(weight=weight)
    else:
        criterion = nn.CrossEntropyLoss()
    ####################################################################
    # Freeze all layer except ip3, if current mode is finetune
    if args.phase == 'Finetune':
        start, end = (args.layer_lockdown.split(':'))
        for param in list(model.parameters())[start:end]:
            param.requires_grad = False
    if args.alg == 'SGD':
        optimizer = optim.SGD(model.parameters(),
                              lr=args.lr,
                              momentum=args.momentum)
    elif args.alg == 'adam' or args.alg == 'Adam':
        optimizer = optim.Adam(model.parameters(), lr=args.lr)
    else:
        optimizer = optim.Adam(model.parameters(), lr=args.lr)
    ####################################################################
    if args.phase == 'Train' or args.phase == 'train':
        print('===> Start Training')
        train(args,
              train_loader,
              valid_loader,
              model,
              criterion,
              optimizer,
              device,
              cuda=use_cuda)
        print('====================================================')
    elif args.phase == 'Test' or args.phase == 'test':
        print('===> Test')
        # how to do test?
    elif args.phase == 'Finetune' or args.phase == 'finetune':
        print('===> Finetune')
        # how to do finetune?
    elif args.phase == 'Predict' or args.phase == 'predict':
        print('===> Predict')
        # how to do predict?
    else:
        print('===> Verify')
Exemplo n.º 6
0
def main():
    parser = argparse.ArgumentParser(description='Detector')
    parser.add_argument('--batch_size',
                        type=int,
                        default=64,
                        metavar='N',
                        help='input batch size for training (default: 256)')
    parser.add_argument('--test_batch_size',
                        type=int,
                        default=256,
                        metavar='N',
                        help='input batch size for testing (default: 256)')
    parser.add_argument('--predict_batch_size',
                        type=int,
                        default=1,
                        metavar='N',
                        help='input batch size for predict (default: 1)')
    parser.add_argument('--epochs',
                        type=int,
                        default=50,
                        metavar='N',
                        help='number of epochs to train (default: 100)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.001,
                        metavar='LR',
                        help='learning rate (default: 0.001)')
    parser.add_argument('--momentum',
                        type=float,
                        default=0.5,
                        metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--cuda',
                        action='store_true',
                        default=False,
                        help='enables CUDA training')
    parser.add_argument('--seed',
                        type=int,
                        default=10,
                        metavar='S',
                        help='random seed (default: 10)')
    parser.add_argument(
        '--log_interval',
        type=int,
        default=10,
        metavar='N',
        help='how many batches to wait before logging training status')
    parser.add_argument('--save_model',
                        action='store_true',
                        default=False,
                        help='save the current Model')
    parser.add_argument('--save_directory',
                        type=str,
                        default='trained_models',
                        help='learnt models are saving here')
    parser.add_argument(
        '--phase',
        type=str,
        default='Train',  # Train/train, Predict/predict, Finetune/finetune
        help='training, predicting or finetuning')
    args = parser.parse_args([
        '--batch_size=64', '--test_batch_size=64', '--predict_batch_size=1',
        '--epochs=101', '--lr=0.001', '--momentum=0.5', '--seed=1',
        '--log_interval=10', '--save_model', '--save_directory=trained_models',
        '--phase=train'
    ])
    ##############################################################################################################
    #设置随机种子
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)
    #设置CPU/GPU
    use_cuda = args.cuda and torch.cuda.is_available()
    device = torch.device('cuda' if use_cuda else 'cpu')
    #For multi GPUs, nothing need to change here
    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
    ###############################################################################################################
    print('===> Loading Datasets')
    train_set, test_set = get_train_test_set()
    train_loader = torch.utils.data.DataLoader(train_set,
                                               batch_size=args.batch_size,
                                               shuffle=True)
    valid_loader = torch.utils.data.DataLoader(test_set,
                                               batch_size=args.test_batch_size)
    predict_loader = torch.utils.data.DataLoader(
        test_set, batch_size=args.predict_batch_size)
    ###############################################################################################################
    print('===> Building Model')
    # For single GPU
    print('===> runing on {}'.format(device))
    ###############################################################################################################
    print('===> init model')
    model = Net_Bn()
    model = model_parameters_init(model)
    ###############################################################################################################
    model.to(device)
    criterion_pts = nn.MSELoss()
    #     optimizer = optim.Adam(model.parameters(), lr= args.lr)
    optimizer = optim.SGD(model.parameters(),
                          lr=args.lr,
                          momentum=args.momentum)
    #学习率衰减
    scheduler = optim.lr_scheduler.StepLR(optimizer, 10, 0.9)
    ###############################################################################################################
    if args.phase == 'Train' or args.phase == 'train':
        print('===> Start Training')
        train_losses, valid_losses = train(args, train_loader, valid_loader,
                                           model, criterion_pts, optimizer,
                                           scheduler, device)
        print('===> Done!')
        return train_losses, valid_losses

    elif args.phase == 'Test' or args.phase == 'test':
        print('===> Test')
        path_model = os.path.join(args.save_directory,
                                  'detector_epoch' + '_' + str(85) + '.pt')
        model.load_state_dict(torch.load(path_model))
        model = model.to(device)
        model.eval()
        with torch.no_grad():
            valid_batch_cnt = 0
            valid_loss = 0
            for valid_batch_idx, batch in enumerate(valid_loader):
                valid_batch_cnt += 1
                valid_img = batch['image']
                landmarks = batch['landmarks']

                input_img = valid_img.to(device)
                target_pts = landmarks.to(device)
                #                 print(input_img.shape)
                output_pts = model(input_img)
                #                 print(type(output_pts))

                valid_loss_batch = criterion_pts(output_pts, target_pts)
                valid_loss += valid_loss_batch.item()

            valid_loss /= valid_batch_cnt * 1.0
            print('Valid: pts_loss: {:.6f}'.format(valid_loss))
        print('===> Done!')
        return None, None

    elif args.phase == 'Finetune' or args.phase == 'finetune':
        print('===> Finetune')
        path_model = os.path.join(args.save_directory,
                                  'detector_epoch' + '_' + str(50) + '.pt')
        model.load_state_dict(torch.load(path_model))
        model = model.to(device)
        train_losses, valid_losses = train(args, train_loader, valid_loader,
                                           model, criterion_pts, optimizer,
                                           scheduler, device)
        print('===> Done!')
        return train_losses, valid_losses

    elif args.phase == 'Predict' or args.phase == 'predict':
        print('===> Predict')
        path_model = os.path.join(args.save_directory,
                                  'detector_epoch' + '_' + str(50) + '.pt')
        model.load_state_dict(torch.load(path_model))
        model = model.to(device)
        model.eval()
        idx = 99
        with torch.no_grad():
            for i, data in enumerate(predict_loader):
                if i == idx:
                    img = data['image'].to(device)
                    output_pts = model(img)
                    landmarks = output_pts[0].numpy()
                    xs = landmarks[::2]
                    ys = landmarks[1::2]
                    img = transforms.ToPILImage()(img[0].type(torch.uint8))
                    draw = ImageDraw.Draw(img)
                    draw.point(list(zip(xs, ys)), fill=(0))
                    img.show()
                elif i > idx:
                    break
        print('===> Done!')
        return None, None
Exemplo n.º 7
0
def main_test(phase):
    parser = argparse.ArgumentParser(description='Detector')
    # 当参数名称中间带'-'时,提取变量要变成’_‘
    parser.add_argument('--batch-size',
                        type=int,
                        default=64,
                        metavar='N',
                        help='input batch size for training (default: 64)')
    parser.add_argument('--test-batch-size',
                        type=int,
                        default=64,
                        metavar='N',
                        help='input batch size for testing (default: 64)')
    parser.add_argument('--epochs',
                        type=int,
                        default=100,
                        metavar='N',
                        help='number of epochs to train (default: 100)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.001,
                        metavar='LR',
                        help='learning rate (default: 0.001)')
    parser.add_argument('--momentum',
                        type=float,
                        default=0.3,
                        metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed',
                        type=int,
                        default=1,
                        metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=20,
        metavar='N',
        help='how many batches to wait before logging training status')
    parser.add_argument('--save-model',
                        action='store_true',
                        default=True,
                        help='save the current Model')
    parser.add_argument('--save-directory',
                        type=str,
                        default='trained_models',
                        help='learnt models are saving here')
    parser.add_argument(
        '--phase',
        type=str,
        default='Train',  # Train/train, Predict/predict, Finetune/finetune
        help='training, predicting or finetuning')
    args = parser.parse_args()
    # # 以下定义以下通用的参数
    torch.manual_seed(args.seed)
    # For single GPU
    use_cuda = not args.no_cuda and torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")  # cuda:0
    # For multi GPUs, nothing need to change here
    # kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}

    print('===> Loading Datasets')
    train_set, test_set = get_train_test_set()
    train_loader = torch.utils.data.DataLoader(train_set,
                                               batch_size=args.batch_size,
                                               shuffle=True)
    valid_loader = torch.utils.data.DataLoader(test_set,
                                               batch_size=args.test_batch_size,
                                               shuffle=True)

    args.phase = phase

    # # 训练
    if args.phase == 'Train' or args.phase == 'train':
        print('===> Building Model')
        model = Model().to(device)

        # # 均方误差
        # criterion_pts = torch.nn.MSELoss()
        criterion_pts = torch.nn.SmoothL1Loss()
        # # 二分类最后可以只输出1维,然后使用BCELoss()或BCEWithLogitsLoss()
        criterion_label = torch.nn.CrossEntropyLoss()
        criterion = [criterion_label, criterion_pts]

        # # SGD优化,总是出现loss为NAN,因为计算值与真实值差距太大了,约为100倍,
        # # optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum)
        # # 采用Adam时可以不用缩小真实值,能够快速收敛
        optimizer = optim.Adam(model.parameters(), lr=0.001)
        # # RMSprop和SGD一样,不缩放容易爆表
        # # optimizer = optim.RMSprop(model.parameters())

        # # 新建一个文件夹,命名方式为损失函数+优化器+学习率+当前日期
        args.save_directory = 'trained_models_' + criterion_pts.__class__.__name__ + '_' \
                              + optimizer.__class__.__name__ + '_' + str(args.lr)+'_'\
                              +time.strftime('%Y-%m-%d-%H-%M-%S')

        print('===> Start Training')
        train_losses, valid_losses = \
   train(args, train_loader, valid_loader, model, criterion, optimizer, device)
        print(train_losses, valid_losses)
        print('====================================================')

    # # 测试
    elif args.phase == 'Test' or args.phase == 'test':
        print('===> Building Model')
        model_path = 'trained_models_SmoothL1Loss_Adam_0.001_2020-03-28-15-11-28'
        args.save_directory = model_path

        # # 使用模型
        trained_model = 'detector_epoch_99.pt'
        # # model.eval()在predict函数中进行
        model = Model()

        print('===> Test')
        predict(args, trained_model, model, valid_loader)

    # # 微调
    elif args.phase == 'Finetune' or args.phase == 'finetune':
        print('===> Building Model')
        # # 首先加载需要微调的模型
        path = 'trained_models_SmoothL1Loss_Adam_0.001_2020-03-28-15-11-28'
        # # 使用模型
        file = 'detector_epoch_99.pt'
        model_path = os.path.join(path, file)

        model = Model()
        model.load_state_dict(torch.load(model_path))
        # # 首先对冻结之前训练的参数
        for param in model.parameters():
            param.requires_grad = False
        # # 对于需要调整的网络层重新定义,默认参数需要更新
        model.FullConnection = torch.nn.Sequential(
            torch.nn.Linear(4 * 4 * 80, 128), torch.nn.ReLU(),
            torch.nn.Linear(128, 128), torch.nn.ReLU(),
            torch.nn.Linear(128, 42))
        model.to(device)

        print('===> Finetune')
        criterion_pts = torch.nn.MSELoss()
        criterion_label = torch.nn.CrossEntropyLoss()
        criterion = [criterion_label, criterion_pts]
        optimizer = optim.SGD(model.parameters(),
                              lr=args.lr,
                              momentum=args.momentum)
        # 新建一个文件夹,命名方式为损失函数+优化器+学习率+Finetune
        args.save_directory = 'trained_models_' + criterion_pts.__class__.__name__ + '_' \
                              + optimizer.__class__.__name__ + '_' + str(args.lr)+'_'+'Finetune'

        train_losses, valid_losses = \
            train(args, train_loader, valid_loader, model, criterion, optimizer, device)
        print(train_losses, valid_losses)
        print('====================================================')

    # # 预测
    elif args.phase == 'Predict' or args.phase == 'predict':
        print('===> Building Model')
        img_path = '.\\test_img'
        path = 'trained_models_SmoothL1Loss_Adam_0.001_2020-03-28-15-11-28'
        file = 'detector_epoch_99.pt'
        model_path = os.path.join(path, file)
        model = Model()
        model.load_state_dict(torch.load(model_path))

        # # 如果模型是finetune的,那么相关的层需要改变,或者直接加载整个模型
        # file='trained_models_MSELoss_SGD_0.001_Finetune.pth'
        # model.load(file)
        model.eval()

        print('===> Predict')
        # # my_predict是针对文件夹中的图片直接拿来预测的
        my_predict(img_path, model)
Exemplo n.º 8
0
from utils import train
import data
from resnet import resnet
import torch
from torch import nn

DEBUG = False
BATCH_SIZE = 64
LR = 1e-3

if __name__ == "__main__":
    trainset, testset = data.get_train_test_set()
    trainloader = torch.utils.data.DataLoader(trainset,
                                              batch_size=BATCH_SIZE,
                                              shuffle=True)
    testloader = torch.utils.data.DataLoader(testset,
                                             batch_size=BATCH_SIZE,
                                             shuffle=False)
    net = resnet(in_channel=3, num_classes=1)
    optimizer = torch.optim.Adam(net.parameters(), lr=LR)
    criterion = nn.BCELoss()
    train(
        net,
        trainloader,
        testloader,
        20,
        optimizer,
        criterion,
        debug=DEBUG,
    )
Exemplo n.º 9
0
def main_test():
    parser = argparse.ArgumentParser(description='Detector')
    parser.add_argument('--batch-size',
                        type=int,
                        default=64,
                        metavar='N',
                        help='input batch size for training (default: 64)')
    parser.add_argument('--test-batch-size',
                        type=int,
                        default=64,
                        metavar='N',
                        help='input batch size for testing (default: 64)')
    parser.add_argument('--epochs',
                        type=int,
                        default=50,
                        metavar='N',
                        help='number of epochs to train (default: 100)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.001,
                        metavar='LR',
                        help='learning rate (default: 0.001)')
    parser.add_argument('--momentum',
                        type=float,
                        default=0.5,
                        metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed',
                        type=int,
                        default=1,
                        metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=20,
        metavar='N',
        help='how many batches to wait before logging training status')
    parser.add_argument('--save-model',
                        action='store_true',
                        default=True,
                        help='save the current Model')
    parser.add_argument('--save-directory',
                        type=str,
                        default='trained_models',
                        help='learnt models are saving here')
    parser.add_argument(
        '--phase',
        type=str,
        default='Train',  # Train/train, Predict/predict, Finetune/finetune
        help='train, predict or finetune')
    parser.add_argument('--save-log',
                        type=str,
                        default='log.txt',
                        help='save the log of training')
    parser.add_argument('--rotation_type',
                        type=str,
                        default='none',
                        help='choose from: allangle,none,flip')
    parser.add_argument('--no-bn',
                        action='store_true',
                        default=False,
                        help='without batch normalization')
    args = parser.parse_args()
    ###################################################################################
    torch.manual_seed(args.seed)
    # For single GPU
    use_cuda = not args.no_cuda and torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")  # cuda:0
    # For multi GPUs, nothing need to change here
    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}

    print('===> Loading Datasets')
    args.rotation_type = args.rotation_type.lower()
    if (args.rotation_type == 'flip'):
        rotation_class = Rotation_flip
    elif (args.rotation_type == 'allangle'):
        rotation_class = Rotation_allangle
    else:
        rotation_class = Rotation_none
    train_set, test_set = get_train_test_set(rotation_class)
    train_loader = torch.utils.data.DataLoader(train_set,
                                               batch_size=args.batch_size,
                                               shuffle=True)
    valid_loader = torch.utils.data.DataLoader(test_set,
                                               batch_size=args.test_batch_size,
                                               shuffle=True)

    print('===> Building Model')
    # For single GPU
    model = Net(args.no_bn).to(device)
    ####################################################################
    #criterion_pts = nn.MSELoss()
    criterion_pts = mymse_withlabel
    criterion_classes = nn.CrossEntropyLoss()
    #optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum)
    optimizer = optim.Adam(model.parameters(), lr=args.lr)
    ####################################################################
    if args.phase == 'Train' or args.phase == 'train':
        print('===> Start Training')
        train_losses, valid_losses = \
   train(args, train_loader, valid_loader, model, criterion_pts, criterion_classes, optimizer, device)
        print('====================================================')
    elif args.phase == 'Test' or args.phase == 'test':
        print('===> Test')
        # how to do test?
    elif args.phase == 'Finetune' or args.phase == 'finetune':
        print('===> Finetune')
        # how to do finetune?
        model_name = input('enter model name: ')
        model_loaded = torch.load(model_name)
        model.load_state_dict(model_loaded['model_state_dict'], strict=False)
        model.no_bn = model_loaded['no_bn']
        for para in model.parameters():
            para.requires_grad = False
        model.bn1.track_running_stats = False
        model.bn2.track_running_stats = False
        model.bn3.track_running_stats = False
        model.ic1.weight.requires_grad = True
        model.ic1.bias.requires_grad = True
        model.ic2.weight.requires_grad = True
        model.ic2.bias.requires_grad = True
        model.ic3.weight.requires_grad = True
        model.ic3.bias.requires_grad = True
        model.preluic1.weight.requires_grad = True
        model.preluic2.weight.requires_grad = True
        model.finetune = True
        train_losses, valid_losses = \
                        train(args, train_loader, valid_loader, model, criterion_pts, criterion_classes, optimizer, device)
        print('====================================================')
    elif args.phase == 'Predict' or args.phase == 'predict':
        print('===> Predict')
        # how to do predict?
        model_name = input('enter model name: ')
        valid_loader = torch.utils.data.DataLoader(test_set,
                                                   batch_size=1,
                                                   shuffle=True)
        model_loaded = torch.load(model_name)
        model.load_state_dict(model_loaded['model_state_dict'])
        model.no_bn = model_loaded['no_bn']
        model.eval()
        print('enter bn:', model.no_bn)
        model.bn1.track_running_stats = False
        model.bn2.track_running_stats = False
        model.bn3.track_running_stats = False
        predict(model, valid_loader)