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
def auc_score(y_pred, y_true, tens=True): score = roc_auc_score(y_true, torch.sigmoid(y_pred)[:, 1]) if tens: score = fvision.tensor(score) else: score = score return score
def convert_biwi(coords, cal): c1 = coords[0] * cal[0][0] / coords[2] + cal[0][2] c2 = coords[1] * cal[1][1] / coords[2] + cal[1][2] return vision.tensor([c2, c1])
def __init__(self, origin_layer, target_layer, n_channels: int): super().__init__(origin_layer, target_layer) self.query = conv1d(n_channels, n_channels // 8) self.key = conv1d(n_channels, n_channels // 8) self.value = conv1d(n_channels, n_channels) self.gamma = nn.Parameter(fv.tensor([0.]), requires_grad=True)
def get(self, i): return Image(tensor(self.file[self.key][self.items[i]]))
def _pose_flip_lr(x): """Flip `x` horizontally.""" if isinstance(x, Pose): return x.flip_lr() return tensor(np.ascontiguousarray(np.array(x)[..., ::-1]))
def open(self, fn, dsn): with h5py.File(fn, 'r') as f: return Image(tensor(f[dsn][()]))
def convert_biwi(coords): c1 = coords[0] * RGB_CAL[0][0]/coords[2] + RGB_CAL[0][2] c2 = coords[1] * RGB_CAL[1][1]/coords[2] + RGB_CAL[1][2] return vision.tensor([c2, c1])