Exemple #1
0
def get_accuracy(learner, test_annos_file):
    """
    Calculates the accuracy in percentage

    :param learner: Model learner to classify images
    :param test_annos_file: Test annotations file as provided by https://ai.stanford.edu/~jkrause/cars/car_dataset.html
    :return: A float number, which is accuracy in percentage
    """
    preds, y = learner.TTA(ds_type=DatasetType.Test)
    a = preds
    print(a.shape)
    labels = scipy.io.loadmat(test_annos_file)
    b = np.array(labels['annotations']['class'], dtype=np.int) - 1
    b = torch.from_numpy(b)
    acc = accuracy(a, b)
    return 100.0 * acc.item()
Exemple #2
0
    val_preds, targs, [0.1, 0.2, 0.3, 0.5, 1.0, 2.0, 3.0])

####### Now features
print("Extracting train feats")
train_feats, train_labels = get_train_features(learn, augment=0)
distance_matrix_imgs = batched_dmv(val_feats, train_feats)
distance_matrix_classes = dm2cm(distance_matrix_imgs, train_labels)
class_sims = 0.5 * (2.0 - distance_matrix_classes)
class_sims_th, best_th_feats, score_feats_th = find_new_whale_th(
    class_sims, targs)
out_preds, thlist, best_score = find_mixing_proportions(
    best_preds, class_sims, class_sims_th, targs)
out_preds = out_preds.cuda()
targs = targs.cuda()
print("Best mix score = ", best_score)
print("Val top1 acc = ", accuracy(out_preds, targs).cpu().item())
print("Val map5 = ", map5(out_preds, targs).cpu().item())
print("Val top5 acc = ", top5acc(out_preds, targs).cpu().item())
thresholds = {}
thresholds['softmax'] = best_sm_th
thresholds['preds_th'] = best_th
thresholds['preds_th_feats'] = best_th_feats
thresholds['mix_list'] = thlist
torch.save(thresholds, name + '_thresholds.pth')

if SAVE_TRAIN_FEATS:
    print("Saving train feats")
    torch.save(
        {
            "train_labels": train_labels.detach().cpu(),
            "train_feats": train_feats.detach().cpu(),
clas_learner.recorder.plot(suggestion=True)
# lr = clas_learner.recorder.min_grad_lr
# clas_learner.fit_one_cycle(4, lr)

lr = clas_learner.recorder.min_grad_lr
clas_learner.fit_one_cycle(4, lr)

clas_learner.predict("This is a horrible movie")

clas_learner.predict("This is a great movie,")

# Check the label mapping
# suppose it returns {'negative': 0, 'positive': 1}
data_clas.train_ds.y.c2i

import pandas as pd

df = pd.read_csv(path + "/test.csv")
y_true = tensor(df["sentiment"] == "positive")

y_true

interp = TextClassificationInterpretation.from_learner(
    clas_learner, ds_type=DatasetType.Test)
interp.y_true = tensor(df["sentiment"] == "positive")
interp.plot_confusion_matrix()

from fastai.metrics import accuracy

accuracy(interp.preds, interp.y_true)
    class_preds, class_acc, class_sc = find_softmax_coef(
        class_sims, targs, coefs)
    min_class_sc = class_sc / 2.0
    max_class_sc = class_sc * 2.0
    step_class_sc = min_class_sc / 10.0
    coefs = list(np.arange(min_class_sc, max_class_sc, step_class_sc))
    class_preds, class_acc, class_sc = find_softmax_coef(
        class_sims, targs, coefs)
    class_preds = class_preds.to(get_device())

    out_preds, best_p, best_score = find_mixing_proportions(
        best_preds, class_preds, targs)

    out_preds = out_preds.to(get_device())
    targs = targs.to(get_device())
    print('Raw Val top1 acc   = ', accuracy(val_preds, targs).cpu().item())
    print('Raw Val top5 acc   = ', topkacc(val_preds, targs, k=5).cpu().item())
    print('Raw Val top12 acc  = ',
          topkacc(val_preds, targs, k=12).cpu().item())
    print('SM  Val top1 acc   = ', accuracy(best_preds, targs).cpu().item())
    print('SM  Val top5 acc   = ',
          topkacc(best_preds, targs, k=5).cpu().item())
    print('SM  Val top12 acc  = ',
          topkacc(best_preds, targs, k=12).cpu().item())
    print('Cls Val top1 acc   = ', accuracy(class_sims, targs).cpu().item())
    print('Cls Val top5 acc   = ',
          topkacc(class_sims, targs, k=5).cpu().item())
    print('Cls Val top12 acc  = ',
          topkacc(class_sims, targs, k=12).cpu().item())
    print('CM Val top1 acc   = ', accuracy(class_preds, targs).cpu().item())
    print('CM Val top5 acc   = ',
Exemple #5
0
# inspect data
data.show_batch()

# create a learner object with ResNet18 model and adam optimiser (default)
# https://github.com/LiyuanLucasLiu/RAdam
# https://github.com/lessw2020/Ranger-Deep-Learning-Optimizer?source=post_page-----2dc83f79a48d----------------------
learn = cnn_learner(data, resnet18, metrics=accuracy, ps=0.5, opt_func=Ranger)
learn = learn.mixup()

# find good learning rate
learn.lr_find()
learn.recorder.plot()

# save best model

learn.fit_one_cycle(10, 1e-2,
learn.save('pre')

preds, y = learn.TTA()
preds, y = learn.get_preds()

accuracy(preds, y)

learn.unfreeze()

learn.lr_find()
learn.recorder.plot()

learn.fit_one_cycle(3, slice(1e-5, 1e-4))