def train():
    ids_test = pd.read_csv(config.TEST_IDS_PATH)
    # ids_valid = pd.read_csv(config.ARTUR_VALID_PATH)
    # ids_test = ids_valid
    print('Predicting on {} samples.'.format(ids_test.shape[0]))

    test_dataset = loading.CdiscountDatasetPandas(
        img_ids_df=ids_test,
        mode='test',
        transform=tta_predict.tta_transform(NORM_MEAN, NORM_STD))

    test_loader = torch.utils.data.DataLoader(
        test_dataset,
        batch_size=TEST_BATCH_SIZE,
        shuffle=False,
        num_workers=1
    )
    # print(len(test_loader))
    model = resnet50(pretrained=True, num_classes=config.CAT_COUNT)
    assert torch.cuda.is_available()
    model = nn.DataParallel(model, device_ids=[0, 1]).cuda()
    assert LOAD_WEIGHTS_FROM is not None
    model.load_state_dict(torch.load(LOAD_WEIGHTS_FROM))
    model.cuda()
    predict(model, dataloader=test_loader, test_size=len(test_loader))
Exemplo n.º 2
0
def train():
    ids_test = pd.read_csv(config.TEST_IDS_PATH)
    print('Predicting on {} samples.'.format(ids_test.shape[0]))

    test_dataset = loading.CdiscountDatasetPandas(
        img_ids_df=ids_test, mode='test', transform=tta_predict.frogs_tta())

    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=TEST_BATCH_SIZE,
                                              shuffle=False,
                                              num_workers=1)
    assert torch.cuda.is_available()
    # print(len(test_loader))
    model = SEInception3((3, 180, 180), num_classes=config.CAT_COUNT)
    assert LOAD_WEIGHTS_FROM is not None
    model.load_pretrain_pytorch_file(LOAD_WEIGHTS_FROM, skip=[])
    model = nn.DataParallel(model, device_ids=[0, 1]).cuda()
    model.cuda()
    predict(model, dataloader=test_loader, test_size=len(test_loader))
Exemplo n.º 3
0
def train():
    ids_train = pd.read_csv(config.ARTUR_TRAIN_PATH)
    ids_valid = pd.read_csv(config.ARTUR_VALID_PATH)
    print("Training on {} samples, validating on {} samples.".format(
        ids_train.shape[0], ids_valid.shape[0]))
    norm_mean = [0.485, 0.456, 0.406]
    norm_std = [0.229, 0.224, 0.225]
    train_dataset = loading.CdiscountDatasetPandas(
        ids_train,
        PHASE_TRAIN,
        transform=transforms.Compose([
            transforms.ToPILImage(),
            transforms.RandomCrop(160),
            transforms.ToTensor(),
            transforms.Normalize(mean=norm_mean, std=norm_std)
        ]),
        big_cats=False)

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=BATCH_SIZE,
                                               shuffle=True,
                                               num_workers=3)
    valid_dataset = loading.CdiscountDatasetPandas(
        ids_valid,
        PHASE_TRAIN,
        transform=transforms.Compose([
            transforms.ToPILImage(),
            transforms.RandomCrop(160),
            transforms.ToTensor(),
            transforms.Normalize(mean=norm_mean, std=norm_std)
        ]),
        big_cats=False)
    valid_loader = torch.utils.data.DataLoader(valid_dataset,
                                               batch_size=VAL_BATCH_SIZE,
                                               shuffle=True,
                                               num_workers=1)
    dataloaders = {PHASE_TRAIN: train_loader, PHASE_VAL: valid_loader}
    dataset_sizes = {
        PHASE_TRAIN: len(train_dataset),
        PHASE_VAL: len(valid_dataset)
    }

    model = resnet50(pretrained=True, num_classes=config.CAT_COUNT)
    assert torch.cuda.is_available()
    model = nn.DataParallel(model, device_ids=[0, 1]).cuda()
    if LOAD_WEIGHTS_FROM is not None:
        model.load_state_dict(torch.load(LOAD_WEIGHTS_FROM))
    model.cuda()
    criterion = nn.CrossEntropyLoss(size_average=False)
    criterion.cuda()

    # Freeze layers
    for param in model.parameters():
        param.requires_grad = False
    for param in model.module.fc.parameters():
        param.requires_grad = True
    # for param in model.parameters():
    #   param.requires_grad = True
    optimizer = optim.Adam(model.module.fc.parameters(), lr=0.0001)
    if LOAD_OPTIM_FROM is not None:
        optimizer.load_state_dict(torch.load(LOAD_OPTIM_FROM))
    model = train_model(model, dataloaders, dataset_sizes, criterion,
                        optimizer, EPOCHS)