Пример #1
0
        model_name = 'efficientnet-b0'
        print(model_name)

        model = EfficientNet.from_name(model_name)

        #model = EfficientNet.from_pretrained(model_name, num_classes=350)
        #summary(model,input_size=(3,224,224))
    else:
        model = Baseline(args.hidden_size, args.output_size)
    optimizer = optim.Adam(model.parameters(), args.learning_rate)
    lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer,
                                                        patience=1,
                                                        verbose=True)
    criterion = nn.CrossEntropyLoss()  #multi-class classification task

    model = model.to(device)
    model.train()

    # DONOTCHANGE: They are reserved for nsml
    bind_model(model)
    # below the nsml load
    nsml.load(checkpoint='15', session='team_62/airush1/40')
    nsml.save('stillgoing')

    if args.pause:
        nsml.paused(scope=locals())
    if args.mode == "train":
        # Warning: Do not load data before this line
        dataloader = train_dataloader(args.input_size, args.batch_size,
                                      args.num_workers)
        for epoch_idx in range(1, args.epochs + 1):
Пример #2
0
    def infer(test_image_data_path, test_meta_data_path):
        # DONOTCHANGE This Line
        test_meta_data = pd.read_csv(test_meta_data_path,
                                     delimiter=',',
                                     header=0)

        device = 0

        models = args.models.split(",")
        model_weights = [float(w) for w in args.model_weights.split(",")]
        nsml_sessionss = args.nsml_sessionss.split(",")
        nsml_checkpoints = args.nsml_checkpoints.split(",")
        loss_types = args.loss_types.split(",")

        transform_random_crop = args.transform_random_crop.split(",")
        transform_random_sized_crop = args.transform_random_sized_crop.split(
            ",")
        transform_norm = args.transform_norm.split(",")
        infer_transform_center_crop = args.infer_transform_center_crop.split(
            ",")

        total_output_probs = None
        for i, model_name in enumerate(models):
            batch_size = batch_size_map[model_name] // 2

            infer_transform_list = []

            if infer_transform_center_crop[i] == "True":
                infer_transform_list.append(transforms.Resize((248, 248)))
                infer_transform_list.append(
                    transforms.CenterCrop((args.input_size, args.input_size)))
                infer_transform_list.append(transforms.ToTensor())
                if transform_norm[i] == "True":
                    infer_transform_list.append(
                        transforms.Normalize(
                            [0.44097832, 0.44847423, 0.42528335],
                            [0.25748107, 0.26744914, 0.30532702]))
            else:
                if transform_random_crop[i] == "True":
                    infer_transform_list.append(transforms.Resize((256, 256)))
                    infer_transform_list.append(
                        transforms.CenterCrop(
                            (args.input_size, args.input_size)))
                elif transform_random_sized_crop[i] == "True":
                    infer_transform_list.append(transforms.Resize((256, 256)))
                    infer_transform_list.append(
                        transforms.CenterCrop(
                            (args.input_size, args.input_size)))
                else:
                    infer_transform_list.append(
                        transforms.Resize((args.input_size, args.input_size)))
                infer_transform_list.append(transforms.ToTensor())
                if transform_norm[i] == "True":
                    infer_transform_list.append(
                        transforms.Normalize(
                            [0.44097832, 0.44847423, 0.42528335],
                            [0.25748107, 0.26744914, 0.30532702]))

            print("transform", infer_transform_list)

            dataloader = DataLoader(
                AIRushDataset(
                    test_image_data_path,
                    test_meta_data,
                    label_path=None,
                    transform=transforms.Compose(infer_transform_list)
                ),  #[transforms.Resize((args.input_size, args.input_size)), transforms.ToTensor()])),
                batch_size=batch_size,
                shuffle=False,
                num_workers=0,
                pin_memory=True)

            if model_name == "Resnet18":
                model = Resnet18(args.output_size)
            elif model_name == "Resnet152":
                model = Resnet152(args.output_size)
            elif model_name == "baseline":
                model = Baseline(args.hidden_size, args.output_size)
            elif model_name.split("-")[0] == "efficientnet":
                model = EfficientNet.from_pretrained(args.model,
                                                     args.output_size)
            else:
                raise Exception("model type is invalid : " + args.model)

            model.to(device)

            def load_fn(dir_name):
                save_state_path = os.path.join(dir_name, 'state_dict.pkl')
                state = torch.load(save_state_path)
                model.load_state_dict(state['model'])
                print("model loaded", dir_name)

            model.eval()

            nsml.load(checkpoint=nsml_checkpoints[i],
                      load_fn=load_fn,
                      session="team_13/airush1/" + nsml_sessionss[i])

            output_probs = None
            for batch_idx, image in enumerate(dataloader):
                image = image.to(device)
                output = model(image).double()

                if loss_types[i] == "cross_entropy":
                    output_prob = F.softmax(output, dim=1)
                else:
                    output_prob = torch.sigmoid(output)

                if output_probs is None:
                    output_probs = to_np(output_prob)
                else:
                    output_probs = np.concatenate(
                        [output_probs, to_np(output_prob)], axis=0)
            if total_output_probs is None:
                total_output_probs = output_probs * model_weights[i]
            else:
                total_output_probs += (output_probs * model_weights[i])

        predict = np.argmax(total_output_probs, axis=1)

        return predict  # this return type should be a numpy array which has shape of (138343)