Beispiel #1
0
def predict_dataset(
        ds_type: str = 'test',
        cpu=True,
        sample_size=config.BEST_MODEL_PARAMS['sample_size'],
        with_oversampling=config.BEST_MODEL_PARAMS['with_oversampling'],
        with_focal_loss=config.BEST_MODEL_PARAMS['with_focal_loss'],
        with_weighted_loss=config.BEST_MODEL_PARAMS['with_weighted_loss'],
        confusion_matrix_filename='test_confusion_matrix'):

    learn = load_saved_learner(sample_size=sample_size,
                               with_oversampling=with_oversampling,
                               with_focal_loss=with_focal_loss,
                               with_weighted_loss=with_weighted_loss,
                               cpu=cpu)

    classes = {c: i for i, c in enumerate(learn.data.classes)}

    # create a DF with filepath and class label (int)
    data = pd.read_csv(config.PROCESSED_DATA_DIR / 'labels_full.csv')
    data = data[data['ds_type'] == ds_type][['name', 'label']]
    data['label_int'] = data.label.apply(lambda x: classes[x])

    print(f'Running predictions on {data.shape[0]} data samples')

    data['pred_probability'] = pd.Series(dtype=object)
    for k, i in enumerate(data.index):
        pred = learn.predict(
            open_image(config.PROCESSED_DATA_DIR / data.loc[i, 'name'],
                       convert_mode='L'))
        data.loc[i, 'y_pred'] = pred[0].data.item()
        data.at[i, 'pred_probability'] = pred[2].numpy()
        if k % 200 == 0 and k > 0:
            print(f'{k} images done..')

    data.to_csv(config.DATA_DIR / 'predictions.csv', index=False)

    print(f'Building classification interpretation..')
    interp = ClassificationInterpretation(
        learn=learn,
        losses=np.zeros(data.shape[0]),
        preds=tensor(data['pred_probability'].to_list()),
        y_true=tensor(data.label_int.to_list()))
    mat = interp.confusion_matrix()

    # sum diagonal / all data_size *100
    accuracy = np.trace(mat) / mat.sum() * 100

    print(mat)
    print(f'Accuracy: {accuracy}')

    interp.plot_confusion_matrix(return_fig=True).savefig(
        f'test_{confusion_matrix_filename}', dpi=200)
    joblib.dump(mat, f'test_{confusion_matrix_filename}.pkl')

    return mat, accuracy
Beispiel #2
0
def plot_learning_rate(sample_size=300, image_size=224, load_learner=True):

    data = load_dataset(sample_size=sample_size, image_size=image_size)

    if load_learner:
        learn = load_saved_learner()
        learn.data = data
    else:
        learn = cnn_learner(data, models.resnet50, metrics=accuracy)
        learn.model = torch.nn.DataParallel(learn.model)

    learn.lr_find()
    learn.recorder.plot(return_fig=True).savefig('learning_rate.png', dpi=200)
Beispiel #3
0
def improve_saved_model(sample_size=300, image_size=224, n_cycles=5,
                        max_lr=slice(1e-6,1e-4), save=False,
                        with_focal_loss=False, with_oversampling=True,
                        with_weighted_loss=True):

    data = load_dataset(sample_size=sample_size, image_size=image_size)

    learn = load_saved_learner(with_focal_loss=with_focal_loss, with_oversampling=with_oversampling,
                               sample_size=sample_size, with_weighted_loss=with_weighted_loss)
    learn.data = data

    learn.unfreeze()
    learn.fit_one_cycle(n_cycles, max_lr=max_lr)

    if save:
        save_learner(learn, with_focal_loss=with_focal_loss, with_oversampling=with_oversampling,
                 sample_size=sample_size, with_weighted_loss=with_weighted_loss)

    _save_classification_interpert(learn)
Beispiel #4
0
def make_prediction_sample(
    image_path,
    cpu=True,
    sample_size=config.BEST_MODEL_PARAMS['sample_size'],
    with_oversampling=config.BEST_MODEL_PARAMS['with_oversampling'],
    with_focal_loss=config.BEST_MODEL_PARAMS['with_focal_loss'],
    with_weighted_loss=config.BEST_MODEL_PARAMS['with_weighted_loss'],
):

    learn = load_saved_learner(sample_size=sample_size,
                               with_oversampling=with_oversampling,
                               with_focal_loss=with_focal_loss,
                               with_weighted_loss=with_weighted_loss,
                               cpu=cpu)

    # load image in grayscale
    image = open_image(image_path, convert_mode='L')
    cat = learn.predict(image)
    print(cat)

    class_prob = {k: float(v) for k, v in zip(learn.data.classes, cat[2])}

    return cat[0].obj, class_prob