def main():
    args = parse_args()
    print(args)

    cfg = get_cfg_defaults()
    cfg.merge_from_file(args.cfg)
    cfg.freeze()
    print('\n', cfg)

    # Prepare dataset and dataloader
    # Create transforms
    transform = transforms.Compose(
        [transforms.ToTensor(),
         transforms.Normalize((0.1307, ), (0.3801, ))])

    # Create datasets
    RawPath = cfg.DATA.RAW_PATH
    trainSet = myMnistDataset(path=RawPath, train=True, transform=transform)
    validSet = myMnistDataset(path=RawPath, train=False, transform=transform)

    # Create dataloaders
    BatchSize = cfg.TRAIN.BATCH_SIZE
    trainLoader = DataLoader(dataset=trainSet,
                             batch_size=BatchSize,
                             shuffle=True)
    validLoader = DataLoader(dataset=validSet,
                             batch_size=BatchSize,
                             shuffle=False)

    # CUDA for PyTorch
    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda:0" if use_cuda else "cpu")
    torch.backends.cudnn.benchmark = True
    print('\nGPU State:', device)

    input_size = cfg.TRAIN.INPUT_SIZE
    hidden_size1 = cfg.TRAIN.HIDDEN_SIZE1
    hidden_size2 = cfg.TRAIN.HIDDEN_SIZE2
    num_classes = cfg.TRAIN.NUM_CLASSES

    net = Net(input_size, hidden_size1, hidden_size2, num_classes).to(device)
    print()
    print(net)

    lr = cfg.TRAIN.LEARNING_RATE
    momentum = cfg.TRAIN.MOMENTUM
    num_epochs = cfg.TRAIN.NUM_EPOCHS

    # For train
    train(net, lr, momentum, num_epochs, trainLoader, device, BatchSize)

    # For test
    test(validLoader, device, net)
def main():
    args = parse_args()
    print(args)

    cfg = get_cfg_defaults()
    cfg.merge_from_file(args.cfg)
    cfg.freeze()
    print('\n', cfg)

    ProcPath = cfg.DATA.PROCESSED_PATH

    # If image data not exist then decompress
    RawPath = cfg.DATA.RAW_PATH
    RawFname = cfg.DATA.RAW_FNAME
    ZipAbspath = os.path.join(RawPath, RawFname)

    isExist = os.path.exists(ProcPath)
    if not isExist:
        decompressImages(ProcPath, ZipAbspath)

    # Normalize
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    # Transforms
    data_transforms = {
        'train':
        transforms.Compose([
            transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(), normalize
        ]),
        'valid':
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(), normalize
        ])
    }

    ClassNames = list(data_transforms.keys())
    print('\nClass names: ', ClassNames)

    # Datasets
    image_datasets = {
        x: datasets.ImageFolder(os.path.join(ProcPath, x), data_transforms[x])
        for x in ClassNames
    }

    trainSet = image_datasets['train']

    class_indexes = trainSet.class_to_idx
    print('class to index: ', class_indexes)

    trainX, trainY = trainSet[0]
    print('\nimage:')
    print(trainX)
    print('label:', trainY)

    BatchSize = cfg.TRAIN.BATCH_SIZE
    NumWorkers = cfg.TRAIN.NUM_WORKERS

    dataloaders = {
        'train':
        DataLoader(image_datasets['train'],
                   batch_size=BatchSize,
                   shuffle=True,
                   num_workers=NumWorkers),
        'valid':
        DataLoader(image_datasets['valid'],
                   batch_size=BatchSize,
                   shuffle=False,
                   num_workers=NumWorkers)
    }

    dataset_sizes = {x: len(image_datasets[x]) for x in ClassNames}
    print('dataset sizes: ', dataset_sizes)

    # Use GPU
    isUseGpu = torch.cuda.is_available()
    device = torch.device("cuda:0" if isUseGpu else "cpu")

    # Model summary
    model_ft = models.resnet18(pretrained=True)
    # print(); print(model_ft)

    num_ftrs = model_ft.fc.in_features

    # Here the size of each output sample is set to 2.
    # Alternatively, it can be generalized to nn.Linear(num_ftrs, len(class_names)).
    model_ft.fc = nn.Linear(num_ftrs, 2)

    model_ft = model_ft.to(device)

    criterion = nn.CrossEntropyLoss()

    # Observe that all parameters are being optimized
    lr = cfg.TRAIN.LEARNING_RATE
    momentum = cfg.TRAIN.MOMENTUM
    optimizer_ft = optim.SGD(model_ft.parameters(), lr=lr, momentum=momentum)

    # Decay LR by a factor of 0.1 every 7 epochs
    exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft,
                                           step_size=7,
                                           gamma=0.1)

    # Train and evaluate
    print()
    num_epochs = cfg.TRAIN.NUM_EPOCHS
    model_ft = train_model(
        dataloaders,
        device,
        dataset_sizes,
        model_ft,
        criterion,
        optimizer_ft,
        exp_lr_scheduler,
        num_epochs=num_epochs,
    )
    return (0)
Пример #3
0
def main():
    args = parse_args()
    print(args)

    cfg = get_cfg_defaults()
    cfg.merge_from_file(args.cfg)
    cfg.freeze()
    print('\n', cfg)

    # Transform
    transform = transforms.Compose(
        [transforms.ToTensor(),
         transforms.Normalize((0.1307, ), (0.3801, ))])

    # Data
    data_path = cfg.DATA.PATH
    trainSet = datasets.MNIST(root=data_path,
                              download=True,
                              train=True,
                              transform=transform)
    testSet = datasets.MNIST(root=data_path,
                             download=True,
                             train=False,
                             transform=transform)

    BatchSize = cfg.TRAIN.BATCH_SIZE

    trainLoader = dset.DataLoader(trainSet, batch_size=BatchSize, shuffle=True)
    testLoader = dset.DataLoader(testSet, batch_size=BatchSize, shuffle=False)

    print('\nImage size: ', trainSet.train_data.size())
    print('\nLabel size: ', trainSet.train_labels.size())

    # GPU
    device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
    print('\nGPU State:', device)

    net = Net().to(device)
    print(net)

    # Parameters
    lr = cfg.TRAIN.LEARNING_RATE
    num_mom = cfg.TRAIN.MOMENTUM
    criterion = nn.NLLLoss()
    optimizer = optim.SGD(net.parameters(), lr=0.002, momentum=0.9)

    # Train
    num_epochs = cfg.TRAIN.NUM_EPOCHS
    print('Training start...')
    for epoch in range(num_epochs):
        running_loss = 0.0

        for i, data in enumerate(trainLoader):
            inputs, labels = data[0].to(device), data[1].to(device)
            inputs = inputs.view(inputs.shape[0], -1)

            # Zero the parameter gradients
            optimizer.zero_grad()

            # Foward + backward + optimize
            outputs = net(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

            # Print statistics
            running_loss += loss.item()
            if (i + 1) % BatchSize == 0 or i + 1 == len(trainLoader):
                print('[%d/%d, %d/%d] loss: %.3f' %
                      (epoch + 1, num_epochs, i + 1, len(trainLoader),
                       running_loss / 2000))

    print('Training Finished.')

    # Test
    correct = 0
    total = 0

    with torch.no_grad():
        for data in testLoader:
            inputs, labels = data
            inputs, labels = inputs.to(device), labels.to(device)
            inputs = inputs.view(inputs.shape[0], -1)

            outputs = net(inputs)
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()

    print('\nAccuracy of the network on the 10000 test images: %d %%' %
          (100 * correct / total))

    class_correct = [0 for i in range(10)]
    class_total = [0 for i in range(10)]

    with torch.no_grad():
        for data in testLoader:
            inputs, labels = data[0].to(device), data[1].to(device)
            inputs = inputs.view(inputs.shape[0], -1)

            outputs = net(inputs)
            _, predicted = torch.max(outputs, 1)
            c = (predicted == labels).squeeze()
            for i in range(10):
                label = labels[i]
                class_correct[label] += c[i].item()
                class_total[label] += 1
                # print(class_correct)
                # print(class_total)

    for i in range(10):
        print('Accuracy of %d: %3f' % (i, (class_correct[i] / class_total[i])))
Пример #4
0
def main():
    print("PyTorch Version: ",torch.__version__)
    print("Torchvision Version: ",torchvision.__version__)

    args = parse_args()
    print(args)

    cfg = get_cfg_defaults()
    cfg.merge_from_file(args.cfg)
    cfg.freeze()
    print('\n', cfg)

    RawPath = cfg.DATA.RAW_PATH
    ProcPath = cfg.DATA.PROCESSED_PATH

    # Normalize
    normalize = transforms.Normalize(
        mean = [0.485, 0.456, 0.406],
        std  = [0.229, 0.224, 0.225]
    )

    # Transforms
    train_transform = transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        normalize
    ])

    valid_transform = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        normalize
    ])

    # Datasets
    trainSet = datasets.ImageFolder(os.path.join(ProcPath, 'train'), train_transform)
    validSet = datasets.ImageFolder(os.path.join(ProcPath, 'valid'), valid_transform)

    ClassNames = trainSet.classes
    print('\nTrain classes: ', ClassNames)

    TrainSize = len(trainSet)
    ValidSize = len(validSet)
    print('\nTrainSet size: ', TrainSize)
    print('ValidSet size: ', ValidSize)

    # DataLoaders
    BatchSize = cfg.TRAIN.BATCH_SIZE
    NumWorkers = cfg.TRAIN.NUM_WORKERS

    trainLoader = DataLoader(
        trainSet, batch_size=BatchSize, shuffle=True,  num_workers=NumWorkers
    )

    validLoader = DataLoader(
        validSet, batch_size=BatchSize, shuffle=False, num_workers=NumWorkers
    )

    images, classes = next(iter(trainLoader))
    print('\nTrainLoader images shape: ', images.shape)
    print('TrainLoader classes shape: ', classes.shape)

    # GPU Device
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    print('\nDevice: ', device)

    # Model Finetune
    model_ft = finetuneModel(ClassNames).to(device)
    print('\n', model_ft)

    # loss function
    criterion = nn.CrossEntropyLoss()

    # Observe that all parameters are being optimized
    lr = cfg.TRAIN.LEARNING_RATE
    optimizer_ft = optim.SGD(model_ft.parameters(), lr=lr, momentum=0.9)

    # Decay LR by a factor of 0.1 every 7 epochs
    exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)

    # Training and Validate model
    since = time.time()
    print('\nTraining start...')

    best_model_wts = copy.deepcopy(model_ft.state_dict())
    best_acc = 0.0
    
    num_epochs = cfg.TRAIN.NUM_EPOCHS
    for epoch in range(num_epochs):
        print('Epoch {}/{}'.format(epoch, num_epochs - 1))
        print('-' * 30)
    
        train(
            model_ft, trainLoader, device, optimizer_ft, criterion, TrainSize, 
            exp_lr_scheduler
        )
    
        best_acc, best_model_wts = valid(
            model_ft, validLoader, device, optimizer_ft, criterion, ValidSize, 
            best_model_wts, best_acc
        )
        print()

    time_elapsed = time.time() - since
    print('Training complete in {:.0f}m {:.0f}s'.format(
        time_elapsed // 60, time_elapsed % 60))

    print('Best val Acc: {:4f}'.format(best_acc))

    # load best model weights
    model_ft.load_state_dict(best_model_wts)
    # print(best_model_wts)
    return