Beispiel #1
0
def load_model_unet(_model_weights, is_inference=False):
    print('Using weights {}'.format(_model_weights))
    if _model_weights == 'imagenet':
        model = Unet(unet_encoder,
                     encoder_weights="imagenet",
                     classes=4,
                     activation=None,
                     attention_type=ATTENTION_TYPE)
        if is_inference:
            model.eval()
        return model
    else:
        model = Unet(
            unet_encoder,
            encoder_weights=None,  # "imagenet",
            classes=4,
            activation=None,
            attention_type=ATTENTION_TYPE)
        if is_inference:
            model.eval()
    if _model_weights is not None:
        device = torch.device("cuda")
        model.to(device)
        state = torch.load(
            _model_weights)  # , map_location=lambda storage, loc: storage)
        model.load_state_dict(state["state_dict"])
        optimizer_state = state['optimizer']
        return model, optimizer_state
        # new_state_dict = OrderedDict()
        #
        # for k, v in state['state_dict'].items():
        #     if k in model.state_dict():
        #         new_state_dict[k] = v
        # model = model.load_state_dict(new_state_dict)
    return model
Beispiel #2
0
def unet(backbone, pretrained_weights=None, classes=1, activation='sigmoid'):
    device = torch.device("cuda")
    model = Unet(encoder_name=backbone,
                 encoder_weights=pretrained_weights,
                 classes=classes,
                 activation=activation)
    model.to(device)
    model.eval()  # 위치 확인해볼것

    return model
Beispiel #3
0
mean = (0.485, 0.456, 0.406)
std = (0.229, 0.224, 0.225)
df = pd.read_csv(sample_submission_path)
testset = DataLoader(
    TestDataset(test_data_folder, df, mean, std),
    batch_size=batch_size,
    shuffle=False,
    num_workers=num_workers,
    pin_memory=True
)

# Initialize mode and load trained weights
ckpt_path = "../input/model_dump1/model.pth"
device = torch.device("cuda")
model = Unet("efficientnet-b5", encoder_weights=None, classes=4, activation=None)
model.to(device)
model.eval()
state = torch.load(ckpt_path, map_location=lambda storage, loc: storage)
model.load_state_dict(state["state_dict"])

# start prediction
predictions = []
for i, batch in enumerate(tqdm(testset)):
    fnames, images = batch
    batch_preds = torch.sigmoid(model(images.to(device)))
    batch_preds = batch_preds.detach().cpu().numpy()
    for fname, preds in zip(fnames, batch_preds):
        for cls, pred in enumerate(preds):
            pred, num = post_process(pred, best_threshold[cls], min_size[cls])
            rle = mask2rle(pred)
            name = fname + f"_{cls+1}"
    classes=4,
    activation='sigmoid',
)

state = torch.load("../input/bce-clf/unet_res34_525.pth")
unet_resnet34.load_state_dict(state['model_state_dict'])

unet_resnet34 = unet_resnet34.cuda()
unet_resnet34 = unet_resnet34.eval()

device = torch.device("cuda")
model_senet = Unet('se_resnext50_32x4d',
                   encoder_weights=None,
                   classes=4,
                   activation=None)
model_senet.to(device)
model_senet.eval()
state = torch.load(
    '../input/senetmodels/senext50_30_epochs_high_threshold.pth',
    map_location=lambda storage, loc: storage)
model_senet.load_state_dict(state["state_dict"])

model_fpn91lb = FPN(encoder_name="se_resnext50_32x4d",
                    classes=4,
                    activation=None,
                    encoder_weights=None)
model_fpn91lb.to(device)
model_fpn91lb.eval()
#state = torch.load('../input/fpnseresnext/model_se_resnext50_32x4d_fold_0_epoch_7_dice_0.935771107673645.pth', map_location=lambda storage, loc: storage)
state = torch.load(
    '../input/fpnse50dice944/model_se_resnext50_32x4d_fold_0_epoch_26_dice_0.94392.pth',