ld2daps/Notebooks/DFC2018 Ground truth split train and validation.ipynb

223 lines
4.9 KiB
Plaintext

{
"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)\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('../Data/ground_truth/labels.csv', header=None, names=['Label'], 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 lbl, lblc in zip(count[0][1:], count[1][1:]):\n",
" treshold = int(lblc * train_part)\n",
" #print('lbl:{}, count:{}, train:{}'.format(lbl, lblc, treshold))\n",
" f = np.nonzero(gt == lbl)\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": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import matplotlib.animation\n",
"import numpy as np\n",
"\n",
"t = np.linspace(0,2*np.pi)\n",
"x = np.sin(t)\n",
"\n",
"fig, ax = plt.subplots()\n",
"ax.axis([0,2*np.pi,-1,1])\n",
"l, = ax.plot([],[])\n",
"\n",
"def animate(i):\n",
" l.set_data(t[:i], x[:i])\n",
"\n",
"ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))\n",
"\n",
"from IPython.display import HTML\n",
"HTML(ani.to_jshtml())"
]
},
{
"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, [10, 11]))#metaclasses_A['Roads']))\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"~~on~~"
]
}
],
"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
}