def test(device: torch.device = 'cuda'):
    submit = pd.read_csv('data/sample_submission.csv')

    model1 = load_model(0, 19)
    model2 = load_model(1, 19)
    model3 = load_model(2, 19)
    model4 = load_model(3, 19)
    model5 = load_model(4, 19)

    tta_model1 = tta.ClassificationTTAWrapper(model1, transforms)
    tta_model2 = tta.ClassificationTTAWrapper(model2, transforms)
    tta_model3 = tta.ClassificationTTAWrapper(model3, transforms)
    tta_model4 = tta.ClassificationTTAWrapper(model4, transforms)
    tta_model5 = tta.ClassificationTTAWrapper(model5, transforms)

    tta_model1.eval()
    tta_model2.eval()
    tta_model3.eval()
    tta_model4.eval()
    tta_model5.eval()

    testset = MnistDataset('data/test_dirty_mnist_2nd',
                           'data/sample_submission.csv', transforms_test)
    test_loader = DataLoader(testset,
                             batch_size=2,
                             shuffle=False,
                             num_workers=0)

    batch_size = test_loader.batch_size
    batch_index = 0
    for i, (images, targets) in enumerate(test_loader):
        images = images.to(device)
        targets = targets.to(device)

        outputs1 = tta_model1(images)
        outputs2 = tta_model2(images)
        outputs3 = tta_model3(images)
        outputs4 = tta_model4(images)
        outputs5 = tta_model5(images)

        outputs = (outputs1 + outputs2 + outputs3 + outputs4 + outputs5) / 5

        outputs = outputs > 0.0
        batch_index = i * batch_size
        submit.iloc[batch_index:batch_index+batch_size, 1:] = \
            outputs.long().squeeze(0).detach().cpu().numpy()

    submit.to_csv('effinet_b4-SAM_CosLR-kfold.csv', index=False)
예제 #2
0
def single_model_predict_tta():
    assert len(model_name_list) == 1
    model_name = model_name_list[0]
    model = Net(model_name).to(device)
    model_save_path = os.path.join(
        config.dir_weight, '{}.bin'.format(model_name))
    model.load_state_dict(torch.load(model_save_path))

    transforms = tta.Compose([
        tta.HorizontalFlip(),
        # tta.Rotate90(angles=[0, 180]),
        # tta.Scale(scales=[1, 2, 4]),
        # tta.Multiply(factors=[0.9, 1, 1.1]),
        tta.FiveCrops(224, 224)
    ])

    tta_model = tta.ClassificationTTAWrapper(model, transforms)

    pred_list = []
    with torch.no_grad():
        for batch_x, _ in tqdm(test_loader):
            batch_x = batch_x.to(device)
            probs = tta_model(batch_x)
            probs = torch.max(torch.softmax(probs, dim=1), dim=1)
            probs = probs[1].cpu().numpy()
            pred_list += probs.tolist()

    submission = pd.DataFrame({
        "id": range(len(pred_list)),
        "label": [int2label(x) for x in pred_list]
    })
    submission.to_csv(config.dir_csv_test, index=False, header=False)
예제 #3
0
def _get_inference_model(config: InferenceConfig) -> nn.Module:
    model = config.model
    model.load_state_dict(torch.load(config.checkpoint_path, map_location='cpu')['st_d'])
    model = ModelWrapper(model)
    model.to('cuda')
    model.eval()
    return tta.ClassificationTTAWrapper(model, _tta_transforms)
예제 #4
0
def evaluate(val_loader, model, loss_fn, device, use_tta=False):
    model.eval()

    if use_tta:
        transformations = tta.Compose([
            tta.Rotate90(angles=[0, 90, 180, 270]),
            tta.HorizontalFlip(),
            tta.VerticalFlip()
        ])

        tta_model = tta.ClassificationTTAWrapper(model, transformations)

    correct = 0
    total = 0
    total_loss = 0
    for i, batch in enumerate(val_loader):
        input_data, labels = batch
        input_data, labels = input_data.to(device), labels.to(device)
        with torch.no_grad():
            if use_tta:
                predictions = tta_model(input_data)
            else:
                predictions = model(input_data)
            total_loss += loss_fn(predictions, labels).item()
            correct += (predictions.argmax(axis=1) == labels).sum().item()
            total += len(labels)
            torch.cuda.empty_cache()

    model.train()
    return total_loss / total, correct / total
예제 #5
0
def get_predictions(model_chosen, tta = False):

    model_chosen.cuda.eval()
    actual_values, predicted_values = [], []

    if tta == True:
        transformation = ttach.Compose(
            [
                ttach.HorizontalFlip(),
                ttach.VerticalFlip(),
                ttach.Rotate90(angles=[0, 90, 180, 270])
            ]
        )
        test_time_augmentation_wrapper = ttach.ClassificationTTAWrapper(model_chosen, transformation)
        with torch.no_grad():
            for batch in loader_of_test:
                test_image, test_label = batch
                predicted_value = test_time_augmentation_wrapper(test_image.cuda())
                predicted_value = torch.argmax(predicted_value, dim=1).detach().cpu().numpy()
                actual_values.append(test_label.cpu().numpy())
                predicted_values.append(predicted_value)
    else:
        with torch.no_grad():
            for batch in loader_of_test:
                test_image, test_label = batch
                predicted_value = model_chosen(test_image.cuda())
                predicted_value = torch.argmax(predicted_value, dim=1).detach().cpu().numpy()
                actual_values.append(test_label.cpu().numpy())
                predicted_values.append(predicted_value)

    return predicted_values
예제 #6
0
def multi_model_predict_tta():
    preds_dict = dict()
    for model_name in model_name_list:
        for fold_idx in range(5):
            model = Net(model_name).to(device)
            model_save_path = os.path.join(
                config.dir_weight, '{}_fold{}.bin'.format(model_name, fold_idx))
            model.load_state_dict(torch.load(model_save_path))
            '/home/muyun99/data/dataset/AIyanxishe/Image_Classification/weight/resnet18_train_size_256_fold0.bin'
            transforms = tta.Compose([
                tta.Resize([int(config.size_test_image), int(config.size_test_image)]),
                tta.HorizontalFlip(),
                # tta.Rotate90(angles=[0, 180]),
                # tta.Scale(scales=[1, 2, 4]),
                # tta.Multiply(factors=[0.9, 1, 1.1]),
                tta.FiveCrops(config.size_test_image, config.size_test_image)
            ])
            tta_model = tta.ClassificationTTAWrapper(model, transforms)

            pred_list = predict(tta_model)
            submission = pd.DataFrame(pred_list)
            submission.to_csv(
                '{}/{}_fold{}_submission.csv'.format(config.dir_submission, config.save_model_name, fold_idx),
                index=False,
                header=False
            )
            preds_dict['{}_{}'.format(model_name, fold_idx)] = pred_list

    pred_list = get_pred_list(preds_dict)
    submission = pd.DataFrame(
        {"id": range(len(pred_list)), "label": [int2label(x) for x in pred_list]})
    submission.to_csv(config.dir_csv_test, index=False, header=False)
    def _recommend_threshold_evaluate(self, dataloader, best_epoch=None):
        try:
            model_path = glob.glob('%s/*[-_]%04d[-_]*.pth' %
                                   (self.config.LOGS_PATH, best_epoch))[0]

        except Exception as e:
            message = f'Checkpoint {best_epoch} not found due to {e}'
            raise Exception(message)

        # Re-build model to avoid conflict with multi-gpus model
        self.config.WEIGHT_PATH = model_path
        evaluate_model = self.load_weight(weight_path=model_path,
                                          multi_gpus=False)
        evaluate_model.eval()
        tta_model = tta.ClassificationTTAWrapper(evaluate_model,
                                                 self.tta_option)
        tta_model.eval()
        highest_pass_score = 0.

        with torch.no_grad():
            for val_data in dataloader:
                inputs_val, labels_val = val_data[0], val_data[1]

                # if self.config.GPU_COUNT == 1:
                inputs_val = inputs_val.to(self.device, non_blocking=True)

                if self.tta_opt:
                    outputs_val = tta_model(inputs_val)

                else:
                    outputs_val = evaluate_model(inputs_val)

                # print(torch.nn.Softmax(dim=1)(outputs_val)) # For debug purpose
                labels_val = labels_val.to(outputs_val.device,
                                           non_blocking=True)
                val_scores, val_preds = torch.max(outputs_val, 1)

                # # Check if the predict values are in pass class or not
                if_pred_pass = _check_Fail_Pass_class(val_preds,
                                                      self.pass_class_index)

                # # Check if the label values are in fail class or not
                if_gth_fail = _check_Fail_Pass_class(labels_val,
                                                     self.fail_class_index)

                # # Final check: if images is actually a fail pass or not
                fail_passes = outputs_val[if_gth_fail & if_pred_pass]
                local_high = torch.softmax(
                    fail_passes,
                    dim=1).max() if fail_passes.size(0) > 0 else 0.0
                if local_high > highest_pass_score:
                    highest_pass_score = local_high

        message = 'Recommended PassThreshold: %.4f' % highest_pass_score
        self.config.LOGGING.write_log(message, message_type=0)
        print('[INFO]' + message)
예제 #8
0
def single_model_predict_tta(predict_model):
    transforms = tta.Compose([
        # tta.HorizontalFlip(),
        # tta.VerticalFlip(),
        # tta.FiveCrops(200, 200)
    ])

    tta_model = tta.ClassificationTTAWrapper(predict_model, transforms)
    pred_cls_list = single_model_predict(tta_model)
    return pred_cls_list
예제 #9
0
 def __init__(self):
     super(Net, self).__init__()
     self.transforms = ttach.Compose([
         ttach.HorizontalFlip(),
         # ttach.Scale(scales=[1, 1.05], interpolation="linear"),
         ttach.Multiply(factors=[0.95, 1, 1.05]),
     ])
     self.model = ttach.ClassificationTTAWrapper(InnerNet(),
                                                 transforms=self.transforms,
                                                 merge_mode="mean")
예제 #10
0
def main(hparams: Namespace):
    loader = get_test_dataloder(hparams)
    model_names = hparams.model_name.split(',')
    model_types = hparams.model_type.split(',')
    weights = hparams.weights.split(',')
    if len(model_types) != len(weights):
        print(
            f'Padding model_type to match weights length, {len(model_types)} and {len(weights)}'
        )
        model_types = cycle(model_types)
    if len(model_names) != len(weights):
        print(
            f'Padding model_name to match weights length, {len(model_names)} and {len(weights)}'
        )
        model_names = cycle(model_names)
    models = [
        load_model(model_name, model_type,
                   weight) for model_name, model_type, weight in zip(
                       model_names, model_types, weights)
    ]
    if hparams.tta == 'd4':
        models = [
            ttach.ClassificationTTAWrapper(model, ttach.aliases.d4_transform())
            for model in models
        ]
    if hparams.tta == 'ten_crop':
        models = [
            ttach.ClassificationTTAWrapper(model,
                                           ttach.aliases.ten_crop_transform())
            for model in models
        ]
    if hparams.tta == 'five_crop':
        models = [
            ttach.ClassificationTTAWrapper(model,
                                           ttach.aliases.five_crop_transform())
            for model in models
        ]
    predictions = run_predictions(models, loader)
    sample_submission = pd.read_csv(hparams.sample_submission_path)
    sample_submission.loc[:, 'target'] = predictions
    sample_submission.to_csv(f'./predictions/{hparams.submission_name}.csv',
                             index=False)
예제 #11
0
def predict(model_path, test_loader, saveFileName, iftta):

    ## predict
    model = initialize_model(num_classes=176)

    # create model and load weights from checkpoint
    model = model.to(device)
    model.load_state_dict(torch.load(model_path))

    if iftta:
        print("Using TTA")
        transforms = tta.Compose(
            [
                tta.HorizontalFlip(),
                tta.VerticalFlip(),
                tta.Rotate90(angles=[0, 180]),
                # tta.Scale(scales=[1, 0.3]), 
            ]
        )
        model = tta.ClassificationTTAWrapper(model, transforms)

    # Make sure the model is in eval mode.
    # Some modules like Dropout or BatchNorm affect if the model is in training mode.
    model.eval()
    
    # Initialize a list to store the predictions.
    predictions = []
    # Iterate the testing set by batches.
    for batch in tqdm(test_loader):

        imgs = batch
        with torch.no_grad():
            logits = model(imgs.to(device))
        
        # Take the class with greatest logit as prediction and record it.
        predictions.extend(logits.argmax(dim=-1).cpu().numpy().tolist())

    preds = []
    for i in predictions:
        preds.append(num_to_class[i])

    test_data = pd.read_csv('leaves_data/test.csv')
    test_data['label'] = pd.Series(preds)
    submission = pd.concat([test_data['image'], test_data['label']], axis=1)
    submission.to_csv(saveFileName, index=False)
    print("Done!!!!!!!!!!!!!!!!!!!!!!!!!!!")
예제 #12
0
def validation(epoch, model, criterion, valid_loader):
    model.eval()
    model = tta.ClassificationTTAWrapper(model, tta.aliases.d4_transform())
    val_loss = []

    correct_samples = 0

    full_a = np.zeros(len(valid_loader) * valid_loader.batch_size)
    full_b = np.zeros(len(valid_loader) * valid_loader.batch_size)

    for batch_i, (data, target) in enumerate(valid_loader):
        if batch_i % 100 == 0:
            print(batch_i * valid_loader.batch_size)
        with torch.no_grad():
            data, target = data.cuda(), target.cuda()
            output = model(data)
            loss = criterion(output[:, 1], target.float())

            val_loss.append(loss.item())

            full_a[batch_i *
                   valid_loader.batch_size:batch_i * valid_loader.batch_size +
                   output.shape[0]] = target.data.cpu().numpy()
            full_b[batch_i *
                   valid_loader.batch_size:batch_i * valid_loader.batch_size +
                   output.shape[0]] = output[:, -1].detach().cpu().numpy()

            correct_samples += (target.detach().cpu().numpy() == np.argmax(
                output.detach().cpu().numpy(), axis=1)).sum()

            del data, target, output
            torch.cuda.empty_cache()

    valid_loss = np.mean(val_loss)
    val_auc = roc_auc_score(full_a, full_b)
    accuracy_score = correct_samples / (len(valid_loader) *
                                        valid_loader.batch_size)

    metrics = {
        'loss': valid_loss,
        'accuracy': accuracy_score,
        'val_auc': val_auc
    }
    print(metrics)

    return metrics
예제 #13
0
 def __init__(self,
              model_path,
              transforms=None,
              th=0.5,
              device='cpu',
              gradcam=False):
     self.device = torch.device(device)
     classifier = QualityDescriptor.load_from_checkpoint(model_path)
     classifier.model = torch.nn.Sequential(classifier.model, torch.nn.Sigmoid())
     classifier.freeze()
     self.mean = classifier.hparams['dataset']['mean']
     self.std = classifier.hparams['dataset']['std']
     self.size = classifier.hparams['dataset']['size']
     if transforms:
         classifier = tta.ClassificationTTAWrapper(classifier, transforms, merge_mode='mean')
     self.classifier = classifier.to(self.device)
     self.th = th
     self.gcam = None
     if gradcam:
         self.gcam = GradCAM(self.classifier)
예제 #14
0
def get_tta_model(model: nn.Module, crop_method: str,
                  input_size: List[int]) -> nn.Module:
    """Wraps input model to TTA model.

    Args:
        model: input model without TTA
        crop_method: one of {'resize', 'crop'}. Cropping method of the input images
        input_size: model's input size

    Returns:
        Model with TTA
    """

    transforms = [ttach.HorizontalFlip()]
    if crop_method == "crop":
        transforms.append(
            ThreeCrops(crop_height=input_size[0], crop_width=input_size[1]))
    transforms = ttach.Compose(transforms)
    model = ttach.ClassificationTTAWrapper(model, transforms)

    return model
    def predict_one(self, img, TTA=True):
        self.pytorch_model.eval()
        with torch.no_grad():
            if TTA:
                img = preprocess_input(img).to(self.device, non_blocking=True)
                tta_model = tta.ClassificationTTAWrapper(
                    self.pytorch_model, self.tta_option)

                outputs = tta_model(
                    img.unsqueeze(0).to(self.device, non_blocking=True))
                propability = torch.nn.Softmax(dim=1)(outputs)

                return self.manage_prediction(propability.tolist())

            else:

                img = preprocess_input(img).to(self.device, non_blocking=True)
                outputs = self.pytorch_model(img.unsqueeze(0))

                propability = torch.nn.Softmax(dim=1)(outputs)
                return self.manage_prediction(propability.tolist())
예제 #16
0
def validate(args, model: nn.Module, valid_loader):
    model.eval()
    if args.tta:
        #model = tta.SegmentationTTAWrapper(model, tta.aliases.d4_transform(), merge_mode='mean')
        model = tta.ClassificationTTAWrapper(model,
                                             tta.aliases.d4_transform(),
                                             merge_mode='mean')

    all_preds, all_targets, all_loss = [], [], []
    with torch.no_grad():
        for inputs, targets in valid_loader:
            all_targets.append(targets)

            inputs, targets = inputs.cuda(), targets.cuda()
            outputs = model(inputs, nn.Sigmoid())
            loss = criterion(outputs, targets).sum()

            all_loss.append(loss.item())
            all_preds.append(outputs.cpu())

    all_preds = torch.cat(all_preds, 0).numpy()
    all_targets = torch.cat(all_targets, 0).numpy()

    metrics = {}
    metrics['loss'] = sum(all_loss) / valid_loader.num
    #metrics['dice'] = 0. #dice(all_preds, all_targets)

    pr_auc_mean = 0.
    for class_i in range(4):
        precision, recall, _ = precision_recall_curve(all_targets[:, class_i],
                                                      all_preds[:, class_i])
        pr_auc = auc(recall, precision)
        pr_auc_mean += pr_auc / 4
        #print(f"PR AUC {self.class_names[class_i]}, {self.stage}: {pr_auc:.3f}\n")
    metrics['dice'] = pr_auc_mean

    if args.val:
        print_metrics(all_preds, all_targets)

    return metrics
예제 #17
0
def predict_empty_masks(args,
                        recall_thresholds=[
                            0.2502807, 0.30874616, 0.47154653, 0.25778872
                        ]):
    #[0.30248615, 0.4076966, 0.55904335, 0.29780537] 0875):
    #[0.32873523, 0.44805834, 0.6001048, 0.3136805] 085):
    #[0.21345863, 0.1824504, 0.41996846, 0.20917079]095):
    # #[0.28479144, 0.35337192, 0.5124028, 0.27734384]090):
    model, model_file = create_model(args.encoder_type,
                                     work_dir=args.work_dir,
                                     ckp=args.ckp)
    model = model.cuda()
    model = DataParallel(model)
    model.eval()
    if args.tta:
        model = tta.ClassificationTTAWrapper(model,
                                             tta.aliases.d4_transform(),
                                             merge_mode='mean')
    test_loader = get_test_loader(batch_size=args.val_batch_size)
    all_preds = []
    with torch.no_grad():
        for inputs in tqdm(test_loader):
            inputs = inputs.cuda()
            outputs = model(inputs, nn.Sigmoid())
            all_preds.append(outputs.cpu())
    all_preds = torch.cat(all_preds, 0).numpy()
    img_ids = test_loader.img_ids
    print(all_preds.shape)
    image_labels_empty = []
    for i, (img, predictions) in enumerate(zip(img_ids, all_preds)):
        for class_i, class_name in enumerate(class_names):
            if predictions[class_i] < recall_thresholds[class_i]:
                image_labels_empty.append(f'{img}_{class_name}')

    pd.DataFrame({
        'empty_ids': image_labels_empty
    }).to_csv('empty_ids.csv', index=False)
예제 #18
0
def test_preds(model, data_dir, test_loader):
    model.eval()

    model = tta.ClassificationTTAWrapper(model, tta.aliases.d4_transform())

    sample_submission = pd.read_csv(
        os.path.join(data_dir, 'sample_submission.csv'))
    test_preds = np.zeros(sample_submission.shape[0])

    for batch_i, data in enumerate(test_loader):
        with torch.no_grad():
            data = data.cuda()
            output = model(data)

            test_preds[batch_i * test_loader.batch_size:batch_i *
                       test_loader.batch_size + output.shape[0]] = sigmoid(
                           output[:, 1].detach().cpu().numpy())

            del data, output
            torch.cuda.empty_cache()
        if batch_i % 100 == 0:
            print(batch_i * test_loader.batch_size)

    return test_preds
예제 #19
0
def main():
    device = "cuda:0" if torch.cuda.is_available() else "cpu"

    parser = argparse.ArgumentParser()
    parser.add_argument('--image_path', type=str, default="./data/test_dirty_mnist_2nd/")
    parser.add_argument('--label_path', type=str, default="./data/sample_submission.csv")
    parser.add_argument('--weight_path', type=str, default='./save/kfold_202119/')
    parser.add_argument('--out_path', type=str, default='./save/kfold_202119/')

    parser.add_argument('--model', type=str, default='efficientnet-b8')    
    parser.add_argument('--batch_size', type=int, default=1)

    parser.add_argument('--device', type=str, default=device)

    args = parser.parse_args()

    assert os.path.isdir(args.image_path), 'wrong path'
    assert os.path.isfile(args.label_path), 'wrong path'
    assert os.path.isdir(args.weight_path), 'wrong path' 
    assert os.path.isdir(args.out_path), 'wrong path'

    print('=' * 50)
    print('[info msg] arguments')
    for key, value in vars(args).items():
        print(key, ":", value)
    
    weights = glob(os.path.join(args.weight_path, '*.tar'))

    test_df = pd.read_csv(args.label_path)

    test_set = DatasetMNIST(
        image_folder=args.image_path,
        label_df=test_df,
        transforms=base_transforms['test']
    )

    submission_df = copy.copy(test_df)

    for weight in weights:   
        model = EfficientNet.from_name(args.model, in_channels=1, num_classes=26)
        model.load_state_dict(torch.load(weight, map_location=args.device))
        print('=' * 50)
        print('[info msg] weight {} is loaded'.format(weight))

        test_data_loader = torch.utils.data.DataLoader(
                test_set,
                batch_size = args.batch_size,
                shuffle = False,
            )

        model.to(args.device)
        model.eval()
        tta_model = tta.ClassificationTTAWrapper(model, tta_transforms)

        batch_size = args.batch_size
        batch_index = 0

        print('=' * 50)
        print('[info msg] inference start')

        for i, (images, _) in enumerate(tqdm(test_data_loader)):
            images = images.to(args.device)            
            outputs = tta_model(images).detach().cpu().numpy().squeeze() # soft vote
            # outputs = (outputs > 0.5).astype(int) # hard vote
            batch_index = i * batch_size
            submission_df.iloc[batch_index:batch_index+batch_size, 1:] += outputs

    
    submission_df.iloc[:,1:] = (submission_df.iloc[:,1:] / len(weights) >= 0.35).astype(int)
    
    SAVE_FN = os.path.join(args.out_path, datetime.now().strftime("%m%d%H%M") + '_ensemble_submission.csv')

    submission_df.to_csv(
        SAVE_FN,
        index=False
        )

    print('=' * 50)
    print('[info msg] submission fils is saved to {}'.format(SAVE_FN))
for i in range(5):
    train(i)


def load_model(fold: int, epoch: int, device: torch.device = 'cuda'):

    model = EfficientNetModel().to(device)
    model.load_state_dict(
        torch.load(f'models/effinet_b4_SAM_CosLR-f{fold}-{epoch}.pth'))

    return model


transforms = tta.Compose([tta.HorizontalFlip(), tta.VerticalFlip()])
tta_model = tta.ClassificationTTAWrapper(model, transforms)


def test(device: torch.device = 'cuda'):
    submit = pd.read_csv('data/sample_submission.csv')

    model1 = load_model(0, 19)
    model2 = load_model(1, 19)
    model3 = load_model(2, 19)
    model4 = load_model(3, 19)
    model5 = load_model(4, 19)

    tta_model1 = tta.ClassificationTTAWrapper(model1, transforms)
    tta_model2 = tta.ClassificationTTAWrapper(model2, transforms)
    tta_model3 = tta.ClassificationTTAWrapper(model3, transforms)
    tta_model4 = tta.ClassificationTTAWrapper(model4, transforms)
예제 #21
0
# 预测
probability = []

for file in tqdm(image_id):
    img_path = os.path.join(path, file)
    image = cv2.imread(img_path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    augmented = get_inference_transforms()(image=image)
    image = augmented['image'][None]
    image = image.to(device)

    image_preds_all = []
    with torch.no_grad():
        for model in net0:
            tta_model = tta.ClassificationTTAWrapper(
                model, tta.aliases.d4_transform())
            image_preds = tta_model(image)
            image_preds_all += [
                torch.softmax(image_preds, 1).detach().cpu().numpy()
            ]

    image_preds_all = np.concatenate(image_preds_all, axis=0)

    tta_preds = np.mean(image_preds_all, axis=0)
    prob = np.argmax(tta_preds)
    probability.append((prob))

df_submit = pd.DataFrame({'image_name': image_id, 'label': probability})
df_submit.to_csv(f'{result_path}/result.csv', index=False)
    def _custom_eval_one_epoch(self, dataloader):

        evaluate_dict = {"y_gth_list": [], "y_pred_list": []}

        # Evaluate metric part
        print("====================================")
        print("Calculating Custom Metric.....")
        print("====================================")

        if self.config.GPU_COUNT > 1:
            evaluate_model = self.pytorch_model.module
            tta_model = tta.ClassificationTTAWrapper(evaluate_model,
                                                     self.tta_option)
            tta_model = nn.DataParallel(tta_model)

        else:
            evaluate_model = self.pytorch_model
            tta_model = tta.ClassificationTTAWrapper(evaluate_model,
                                                     self.tta_option)

        tta_model.eval()

        with torch.no_grad():
            draft_img = preprocess_input(
                np.zeros(
                    (self.input_size, self.input_size, 3)).astype(np.float32))
            if self.config.GPU_COUNT > 1:
                pass

            else:
                draft_img = draft_img.to(self.device, non_blocking=True)

            if self.tta_opt:
                tta_model(draft_img.unsqueeze(0))

            else:
                self.pytorch_model(draft_img.unsqueeze(0))

            for eval_data in dataloader:
                if self.tta_opt:
                    inputs_eval, labels_eval = eval_data[0], eval_data[1]

                    if self.config.GPU_COUNT == 1:
                        inputs_eval = inputs_eval.to(self.device,
                                                     non_blocking=True)

                    outputs_eval = tta_model(inputs_eval)
                    labels_eval = labels_eval.to(outputs_eval.device,
                                                 non_blocking=True)
                    _, eval_preds = torch.max(outputs_eval, 1)
                    evaluate_dict['y_pred_list'].extend(eval_preds.tolist())

                else:
                    inputs_eval, labels_eval = eval_data[0], eval_data[1]

                    if self.config.GPU_COUNT == 1:
                        inputs_eval = inputs_eval.to(self.device,
                                                     non_blocking=True)

                    labels_val = labels_val.to(self.device, non_blocking=True)
                    outputs_eval = self.pytorch_model(inputs_eval)
                    _, eval_preds = torch.max(outputs_eval, 1)
                    evaluate_dict['y_pred_list'].extend(eval_preds.tolist())

                evaluate_dict['y_gth_list'].extend(labels_eval.tolist())

        UK_ratio = _FP_FN_metric(evaluate_dict['y_pred_list'],
                                 evaluate_dict['y_gth_list'],
                                 self.fail_class_index)
        OK_ratio = _FP_FN_metric(evaluate_dict['y_pred_list'],
                                 evaluate_dict['y_gth_list'],
                                 self.pass_class_index)

        # Delete when done evaluate to prevent OOM
        try:
            del evaluate_model

        except NameError:
            pass

        # Tensorboard part
        self.config.LOGGING.write_log(f"False Pass rate: {UK_ratio} %",
                                      message_type=0)
        self.config.LOGGING.write_log(f"False Reject rate: {OK_ratio} %",
                                      message_type=0)

        print(f"False Pass rate: {UK_ratio} %")
        print(f"False Reject rate: {OK_ratio} %")

        return UK_ratio, OK_ratio
예제 #23
0
cfg = Config()

desc_test = os.path.join(cfg.ds_folder, 'test.csv')
_, transform_test = get_transforms(cfg.img_size)
valid_data = TestDataset(desc_test,
                         data_folder=os.path.join(cfg.ds_folder, "test"),
                         transform=transform_test)
test_loader = DataLoader(dataset=valid_data, batch_size=cfg.bs, shuffle=False)

models = []
for path in get_kfold_model(opt.weights):
    model = get_model_by_name(cfg.model_name)
    model.load_state_dict(torch.load(path)['state_dict'])
    if opt.tta == 'yes':
        model = tta.ClassificationTTAWrapper(model,
                                             get_tta_transforms(),
                                             merge_mode='mean')
    models.append(model)

rst = [[] for i in range(len(models))]
for index in range(len(models)):
    net = models[index]
    net.to("cuda")
    net.eval()

    files = []
    with torch.no_grad():
        for x, y in tqdm(test_loader):
            x, filename = x.cuda(), y
            out = net(x)
            _, pred = torch.max(out.data, 1)
예제 #24
0
                                        n_iter // 20,
                                        after_scheduler=schedulerD)

    # plot_scheduler(schedulerG, n_iter, "Gen LR Scheduler")
    # plot_scheduler(schedulerD, n_iter * NUM_DISCRIMINATOR_TRAIN, "Dis LR Scheduler")

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    gen.to(device)
    dis.to(device)
    criterionG.to(device)
    criterionD.to(device)

    tta_transform = tta.Compose([tta.HorizontalFlip(), tta.VerticalFlip()])

    tta_gen = tta.SegmentationTTAWrapper(gen, tta_transform)
    tta_dis = tta.ClassificationTTAWrapper(dis, tta_transform)
    history = []
    img_test = plt.imread(r'data\testing_lr_images\09.png')
    img_valid = plt.imread(r'data\valid_hr_images\t20.png')
    for epoch in range(1, NUM_EPOCHS + 1):
        train_bar = tqdm(train_loader)
        running_results = {
            'batch_sizes': 0,
            'lossG': 0,
            'lossD': 0,
            'gradeHR': 0,
            'gradeSR': 0,
            'psnr': 0
        }

        # Train a epoch:
예제 #25
0
    print('Training done.')
    print(f'================')
    print(f'Started testing...')
    print(f'================')
    # testing with train data
    _, train_acc = test_model(model, train_loader)
    print(f'train set acc: {train_acc}')

    # testing with test data
    _, test_acc = test_model(model, test_loader)
    print(f'test set acc: {test_acc}')

    # this is a 5x tta with test data
    # wrapping a model
    tta_model = tta.ClassificationTTAWrapper(
        model, tta.aliases.five_crop_transform(config.tta_crop,
                                               config.tta_crop))
    # creating dataset
    tta_dataset = dataset.dataset(valid_df, transforms.tta_transform_cls)
    # creating the loder
    tta_loader = DataLoader(tta_dataset, batch_size=1, shuffle=False)
    # testing
    _, tta_acc = test_model(tta_model, tta_loader)

    print(f'TTA acc: {tta_acc}')
    print(f'================')
    print(f'Testing done.')
    # total run time
    total_time = time.time() - start
    print(f'================')
예제 #26
0
# #### Load model
multilabel_resnet34 = models.resnet34(pretrained=False)
multilabel_resnet34.fc = torch.nn.Linear(multilabel_resnet34.fc.in_features,
                                         len(CLASSES))
multilabel_resnet34 = load_dataparallel_model(
    multilabel_resnet34, torch.load(model_multilabel_path))
multilabel_resnet34 = torch.nn.DataParallel(multilabel_resnet34,
                                            device_ids=range(
                                                torch.cuda.device_count()))
multilabel_resnet34.to(DEVICE)
multilabel_resnet34.eval()

multilabel_resnet34 = tta.ClassificationTTAWrapper(multilabel_resnet34,
                                                   tta.Compose([
                                                       tta.HorizontalFlip(),
                                                       tta.VerticalFlip(),
                                                       tta.Scale([0.85, 1.15]),
                                                   ]),
                                                   merge_mode="mean")

# #### Inference
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class_th = torch.tensor([0.0005, 0.2, 0.005, 0.02, 0.005, 0.005, 0.008,
                         0.005]).to(DEVICE)

total_preds, total_paths = [], []


@toma.batch(initial_batchsize=batch_size_multilabel)
def run_multilabel(batch_size):
    inference_loader = DataLoader(inference_dataset,
예제 #27
0
desc_test = os.path.join(cfg.ds_folder, 'test.csv')

_, test_tfms = get_transforms(IMG_SIZE)

valid_data = TestDataset(desc_test,
                         data_folder=os.path.join(cfg.ds_folder, "test"),
                         transform=test_tfms)
test_loader = DataLoader(dataset=valid_data, batch_size=cfg.bs, shuffle=False)

net = get_model_by_name(cfg.model_name)
net.load_state_dict(torch.load(opt.weights)['state_dict'])
net.to("cuda")
net.eval()
if opt.tta == 'yes':
    transforms = tta.Compose([
        tta.HorizontalFlip(),
    ])
    net = tta.ClassificationTTAWrapper(net, transforms, merge_mode='mean')

rst = []
files = []
for x, y in tqdm(test_loader):
    with torch.no_grad():
        x, filename = x.cuda(), y
        out = net(x)
        _, pred = torch.max(out.data, 1)
        rst.extend(list(pred.cpu().numpy()))
        files.extend(list(filename.numpy()))

submit = pd.DataFrame({'id': files, 'label': rst})
submit.to_csv("submit.csv", encoding="utf8", index=False)