{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "\n", "# Triskele\n", "import sys\n", "from pathlib import Path\n", "triskele_path = Path('../triskele/python')\n", "sys.path.append(str(triskele_path.resolve()))\n", "import triskele" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fs = np.array([16,3]) * 2 # Figure Size" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Load ground truth" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gt = triskele.read('../Data/ground_truth/2018_IEEE_GRSS_DFC_GT_TR.tif')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=fs)\n", "plt.imshow(gt, cmap=plt.get_cmap('GnBu'))\n", "plt.colorbar()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "count = np.unique(gt, return_counts=True)\n", "lbl = pd.read_csv('../labels.csv', index_col=0)\n", "lbl['Count'] = count[1]\n", "lbl" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.bar(*count, log=True)\n", "plt.xticks(lbl.index.values,lbl['label'], rotation=90)\n", "plt.show()\n", "\n", "plt.bar(count[0][1:], count[1][1:])\n", "plt.xticks(lbl.index.values[1:], lbl['label'][1:], rotation=90)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Split data\n", "\n", "Create a filter raster `split` with values:\n", "\n", "- `0` No Data\n", "- `1` Train\n", "- `2` Test" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "train_part = .9\n", "\n", "split = np.zeros_like(gt)\n", "\n", "for lbli, lblc in zip(count[0][1:], count[1][1:]):\n", " treshold = int(lblc * train_part)\n", " #print('lbli:{}, count:{}, train:{}'.format(lbli, lblc, treshold))\n", " f = np.nonzero(gt == lbli)\n", " split[f[0][:treshold], f[1][:treshold]] = 1\n", " split[f[0][treshold:], f[1][treshold:]] = 2\n", " \n", "plt.figure(figsize=fs)\n", "plt.imshow(split)\n", "#plt.imshow(split * (gt==1))\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## First Semantic Cross Validation Generator Prototype\n", "\n", "**WORK IN PROGRESS** you 'll have fun" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nb_split = 5\n", "\n", "def semantic_cvg(gt, nb_split, step=0):\n", " test_part = 1 / nb_split\n", "\n", " split = np.zeros_like(gt)\n", "\n", " for lbli, lblc in zip(count[0][1:], count[1][1:]):\n", " treshold = int(lblc * test_part)\n", " #print('lbli:{}, count:{}, train:{}'.format(lbli, lblc, treshold))\n", " f = np.nonzero(gt == lbli)\n", " t_int, t_ext = treshold * step, treshold * (step + 1)\n", " split[f[0], f[1]] = 1\n", " split[f[0][t_int:t_ext], f[1][t_int:t_ext]] = 2\n", "\n", " return split\n", "\n", "fig, axs = plt.subplots(nb_split, figsize=fs * nb_split)\n", "\n", "for i in range(nb_split):\n", " split = semantic_cvg(gt, nb_split, i)\n", "\n", " axs[i].imshow(split)\n", " axs[i].set_title(i)\n", "#plt.imshow(split * (gt==1))\n", "plt.savefig('../Res/cross_valid_split_{}.png'.format(nb_split))\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Metaclasses " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "metaclasses_A = {'Grass': [1, 2, 3],\n", " 'Trees': [4, 5],\n", " 'Hard Ground': [6, 17],\n", " 'Water': [7],\n", " 'Buildings': [8, 9],\n", " 'Roads': [10, 11, 12, 13, 14, 16],\n", " 'Railways': [15],\n", " 'Cars': [18],\n", " 'Trains': [19],\n", " 'Stadium': [20]}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=fs)\n", "plt.imshow(np.isin(gt, metaclasses_A['Roads']))\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=fs)\n", "plt.imshow(gt * np.isin(gt, metaclasses_A['Roads']))\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Database-style" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dfc_lbl = pd.read_csv('../labels.csv')\n", "meta_idx = pd.read_csv('../metaclass_indexes.csv')\n", "meta_lbl = pd.read_csv('../metaclass_labels.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dfc_meta_view = pd.merge(dfc_lbl, meta_idx)\n", "all_meta_view = pd.merge(dfc_meta_view, meta_lbl)\n", "all_meta_view" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.array(all_meta_view[all_meta_view['metaclass_label'] == 'Roads']['index'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=fs)\n", "plt.imshow(gt * np.isin(gt, np.array(all_meta_view[all_meta_view['metaclass_label'] == 'Roads']['index'])))\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "count = np.unique(gt, return_counts=True)\n", "df_count = pd.DataFrame(np.array(count).T, columns=['index', 'count'])\n", "df_count.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import seaborn as sns\n", "\n", "df_tmp = meta_idx.merge(df_count).merge(meta_lbl).merge(dfc_lbl).drop(index=0)\n", "display(df_tmp.head())\n", "\n", "g = sns.barplot(data=df_tmp, x='label', y='count')\n", "plt.xticks(rotation=80)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "meta_count_view = pd.merge(meta_idx, df_count)\n", "display(meta_count_view.head())\n", "meta_count_view.drop('index', 1, inplace=True)\n", "meta_count_view = meta_count_view.groupby('metaclass_index', as_index=False).sum()\n", "display(meta_count_view.head())\n", "\n", "pd.merge(meta_lbl, meta_count_view)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "meta_index = np.array(meta_idx['metaclass_index'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=fs)\n", "plt.imshow(meta_index[gt])\n", "plt.colorbar()\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 2 }