示例#1
0
def main():

    exp_name = f'baseline_{now()}'
    device, log, result_dir = setup(exp_name, conf)

    train_df = load_csv(conf.train_csv)
    if conf.npy:
        train_images = np.load(conf.train_images)
    else:
        train_images = pd.read_parquet(conf.train_images)

    train_df["gr"] = 0
    train_df["cd"] = 0
    train_df["vd"] = 0
    train_df["image_mean"] = 0

    models = [f"se_resnext50_f{i}.pkl" for i in range(5)]

    preds = np.zeros((len(train_df), conf.gr_size + conf.vd_size + conf.cd_size))
    image_stats = np.zeros((len(train_df), 2))

    log.info('done')
    for i in range(5):

        model = ResNet(conf, arch_name=conf.arch,
                          input_size=conf.image_size)
        model.load_state_dict(torch.load(models[i]))
        model.to(device)

        ds = val_split(train_df, train_images, fold=i)
        _, val_ds, _, val_images = ds['train'], ds['val'], ds['train_images'], ds['val_images']

        test_preds = predict(model, val_ds, val_images, valid_transform,
                             device)

        print(test_preds.shape)
        te_ind = ds['te_ind']
        preds[te_ind] += test_preds
        image_stats[te_ind, 0] = val_images.mean((1, 2))
        image_stats[te_ind, 0] = val_images.std((1, 2))

    preds = np.concatenate([preds, image_stats], axis=1)

    for t in ["grapheme_root", "vowel_diacritic", "consonant_diacritic"]:
        rf = RandomForestClassifier(n_jobs=16)
        # train = xgb.DMatrix(preds, label=train_df[t])
        # params = {"max_depth": 4, "nthread": 16, "objective": "multi:softmax",
        #           "eval_metric": ["merror", "mlogloss"], "num_class": conf.gr_size}
        # xgb.cv(params, train, num_boost_round=1000, nfold=5, seed=conf.seed,
        #        early_stopping_rounds=40, verbose_eval=10)
        rf.fit(preds, train_df[t])
        with open(f"{t}_rf2.pkl", "wb") as f:
            joblib.dump(rf, f)
示例#2
0
def loadModel(data_root, file_list, backbone_net, gpus='0,1,2,3', resume=None):

    if backbone_net == 'MobileFace':
        raise NotImplementedError
    elif backbone_net == 'Res50':
        net = ResNet(10575)
    elif backbone_net == 'Res101':
        net = NotImplementedError
    else:
        print(backbone_net, 'is not available!')

    # gpu init
    multi_gpus = False
    if len(gpus.split(',')) > 1:
        multi_gpus = True
    os.environ['CUDA_VISIBLE_DEVICES'] = gpus
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    net.load_state_dict(loadStateDict(resume))

    net = net.backbone
    if multi_gpus:
        net = DataParallel(net).to(device)
    else:
        net = net.to(device)

    transform = transforms.Compose([
        transforms.ToTensor(),  # range [0, 255] -> [0.0,1.0]
        transforms.Normalize(mean=(0.5, 0.5, 0.5),
                             std=(0.5, 0.5,
                                  0.5))  # range [0.0, 1.0] -> [-1.0,1.0]
    ])
    lfw_dataset = LFW(data_root, file_list, transform=transform)
    lfw_loader = torch.utils.data.DataLoader(lfw_dataset,
                                             batch_size=128,
                                             shuffle=False,
                                             num_workers=2,
                                             drop_last=False)

    return net.eval(), device, lfw_dataset, lfw_loader
示例#3
0
import scipy.misc
import os
import torchvision.transforms as transforms

# image transformation function
loader = transforms.Compose([transforms.ToTensor()])

# checking if the GPU is available for inference
is_use_cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if is_use_cuda else "cpu")

# initializing the model
net = ResNet(depth=14, in_channels=1, output=3)
# moving the net to GPU for testing
if is_use_cuda:
    net.to(device)
    net = nn.DataParallel(net, device_ids=range(torch.cuda.device_count()))
# loading the network parameters
net.load_state_dict(torch.load("./checkpoints/model.pth"))
net.eval()


def draw_circle(img, row, col, rad):
    rr, cc, val = circle_perimeter_aa(row, col, rad)
    valid = ((rr >= 0) & (rr < img.shape[0]) & (cc >= 0) & (cc < img.shape[1]))
    img[rr[valid], cc[valid]] = val[valid]


def noisy_circle(size, radius, noise):
    img = np.zeros((size, size), dtype=np.float)
示例#4
0
def get_model(config, num_class=10, bn_types=None, data_parallel=True):
    name = config.model
    print('model name: {}'.format(name))
    print('bn_types: {}'.format(bn_types))
    if name == 'resnet50':
        if bn_types is None:
            model = ResNet(dataset='imagenet',
                           depth=50,
                           num_classes=num_class,
                           bottleneck=True)
        else:
            model = ResNetMultiBN(dataset='imagenet',
                                  depth=50,
                                  num_classes=num_class,
                                  bn_types=bn_types,
                                  bottleneck=True)
    elif name == 'resnet200':
        if bn_types is None:
            model = ResNet(dataset='imagenet',
                           depth=200,
                           num_classes=num_class,
                           bottleneck=True)
        else:
            model = ResNetMultiBN(dataset='imagenet',
                                  depth=200,
                                  num_classes=num_class,
                                  bn_types=bn_types,
                                  bottleneck=True)
    elif name == 'wresnet40_2':
        if bn_types is None:
            model = WideResNet(40, 2, dropout_rate=0.0, num_classes=num_class)
        else:
            raise Exception('unimplemented error')
    elif name == 'wresnet28_10':
        if bn_types is None:
            model = WideResNet(28, 10, dropout_rate=0.0, num_classes=num_class)
        else:
            model = WideResNetMultiBN(28,
                                      10,
                                      dropout_rate=0.0,
                                      num_classes=num_class,
                                      bn_types=bn_types)
    elif name == 'shakeshake26_2x32d':
        if bn_types is None:
            model = ShakeResNet(26, 32, num_class)
        else:
            model = ShakeResNetMultiBN(26, 32, num_class, bn_types)
    elif name == 'shakeshake26_2x64d':
        if bn_types is None:
            model = ShakeResNet(26, 64, num_class)
        else:
            model = ShakeResNetMultiBN(26, 64, num_class, bn_types)
    elif name == 'shakeshake26_2x96d':
        if bn_types is None:
            model = ShakeResNet(26, 96, num_class)
        else:
            model = ShakeResNetMultiBN(26, 96, num_class, bn_types)
    elif name == 'shakeshake26_2x112d':
        if bn_types is None:
            model = ShakeResNet(26, 112, num_class)
        else:
            model = ShakeResNetMultiBN(26, 112, num_class, bn_types)
    elif name == 'shakeshake26_2x96d_next':
        if bn_types is None:
            model = ShakeResNeXt(26, 96, 4, num_class)
        else:
            raise Exception('unimplemented error')

    elif name == 'pyramid':
        if bn_types is None:
            model = PyramidNet('cifar10',
                               depth=config.pyramidnet_depth,
                               alpha=config.pyramidnet_alpha,
                               num_classes=num_class,
                               bottleneck=True)
        else:
            model = PyramidNetMultiBN('cifar10',
                                      depth=config.pyramidnet_depth,
                                      alpha=config.pyramidnet_alpha,
                                      num_classes=num_class,
                                      bottleneck=True,
                                      bn_types=bn_types)
    else:
        raise NameError('no model named, %s' % name)

    if data_parallel:
        model = model.cuda()
        model = DataParallel(model)
    else:
        import horovod.torch as hvd
        device = torch.device('cuda', hvd.local_rank())
        model = model.to(device)
    cudnn.benchmark = True
    return model
示例#5
0
weight_path = "checkpoints/"  # directory to save or load trained weights

# Data loader
train_dset = FashionData("datasets/myntradataset/", "lists/train_list.txt")
train_loader = DataLoader(train_dset, batch_size=batch_size, shuffle=False)
test_dset = FashionData("datasets/myntradataset/", "lists/test_list.txt")
test_loader = DataLoader(test_dset, batch_size=1, shuffle=False)

# Gpu or cpu device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Model
# model = Shallow()
# model = LargeResNet()
model = ResNet()
model = model.to(device)

# Load saved model parameters (if pre-trained)
if not train_mode:
    map_loc = "cuda:0" if torch.cuda.is_available() else "cpu"
    state_dict = torch.load(os.path.join(weight_path, "resnet_lr4_ep80"),
                            map_location=map_loc)
    model.load_state_dict(state_dict)

# Loss function
criterion = nn.CrossEntropyLoss()
# Optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=learn_rate)
# Learning rate scheduler
scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=epochs)