ld2daps/Notebooks/CrossVal spatial.ipynb
2018-09-21 15:51:04 +02:00

277 lines
6.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"\n",
"sys.path.append('../triskele/python/')\n",
"import triskele\n",
"\n",
"figsize = np.array((16, 3)) * 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Spatial Cross Validation Generator\n",
"\n",
"Due to the operating of attribute profiles, we need a spatial cross validation generator to ensure we do not train and test samples with the same root in the hierarchical representation used during the description process."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load ground truth & metadata"
]
},
{
"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=figsize)\n",
"plt.imshow(gt, cmap=plt.get_cmap('GnBu'))\n",
"plt.colorbar()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dfc_lbl = pd.read_csv('../GroundTruth/labels.csv')\n",
"meta_idx = pd.read_csv('../GroundTruth/jurse_meta_idx.csv')\n",
"meta_lbl = pd.read_csv('../GroundTruth/jurse_meta_lbl.csv')\n",
"\n",
"imap = np.array(meta_idx['metaclass_index'])\n",
"\n",
"all_meta_view = dfc_lbl.merge(meta_idx).merge(meta_lbl)\n",
"all_meta_view"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=figsize)\n",
"plt.imshow(imap[gt], cmap=plt.get_cmap('GnBu'))\n",
"plt.colorbar()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Labels map"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, axs = plt.subplots(dfc_lbl['label'].size, figsize=figsize * dfc_lbl['label'].size)\n",
"\n",
"for i, lbl in enumerate(dfc_lbl['label']):\n",
" omap = gt == np.array(dfc_lbl[dfc_lbl['label'] == lbl ]['index'])\n",
" axs[i].set_title(lbl)\n",
" axs[i].imshow(1. * omap, cmap=plt.get_cmap('GnBu'))\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Meta labels map"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, axs = plt.subplots(meta_lbl['metaclass_label'].size, figsize=figsize * meta_lbl['metaclass_label'].size)\n",
"\n",
"for i, lbl in enumerate(meta_lbl['metaclass_label']):\n",
" omap = np.isin(gt, np.array(all_meta_view[all_meta_view['metaclass_label'] == lbl]['index']))\n",
" axs[i].set_title(lbl)\n",
" axs[i].imshow(1. * omap, cmap=plt.get_cmap('GnBu'))\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Split data "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"count"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"col, count = np.unique(cut, return_counts=True)\n",
"df = pd.DataFrame(count[np.newaxis,:], columns=col)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"count = 4\n",
"\n",
"step = int(gt.shape[0] / count)\n",
"\n",
"fig, axs = plt.subplots(count, figsize=figsize * count)\n",
"\n",
"col, count_all = np.unique(imap[gt], return_counts=True)\n",
"\n",
"metrics = pd.DataFrame(columns=col)\n",
"counts = list()\n",
"for i in range(count):\n",
" cut = imap[gt][i*step:(i+1)*step+1]\n",
" axs[i].imshow(cut, cmap=plt.get_cmap('GnBu'))\n",
" axs[i].set_title('Cut {}'.format(i))\n",
" \n",
" col, count = np.unique(cut, return_counts=True)\n",
" df = pd.DataFrame(count[np.newaxis,:], columns=col, index=[i])\n",
" counts.append(df)\n",
"\n",
"plt.savefig('../Res/meta.png',bbox_inches='tight', transparent=\"True\", pad_inches=0)\n",
"plt.show()\n",
"\n",
"metrics = pd.concat(counts)\n",
"metrics / count_all * 100"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Try to remove filtered object"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ld2dap"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tif = ld2dap.LoadTIFF('../Data/phase1_rasters/DSM_C12/UH17c_GEF051_TR.tif')\n",
"trh = ld2dap.Treshold(70)\n",
"dsp = ld2dap.ShowFig(symb=True)\n",
"\n",
"aps = ld2dap.SelfDualAttributeProfiles(area=[100, 1000, 1e4, 1e5, 1e6, 1e7])\n",
"dif = ld2dap.Differential()\n",
"\n",
"dif.input = aps\n",
"aps.input = trh\n",
"dsp.input = dif\n",
"trh.input = tif\n",
"\n",
"dsp.run()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tif = ld2dap.LoadTIFF('../Data/phase1_rasters/DSM_C12/UH17c_GEF051_TR.tif')\n",
"#trh = ld2dap.Treshold(1e4)\n",
"aps = ld2dap.SelfDualAttributeProfiles(area=[100, 1e3, 1e4])\n",
"dsp = ld2dap.ShowFig(symb=True)\n",
"dif = ld2dap.Differential()\n",
"ddsp = ld2dap.ShowFig(symb=True)\n",
"\n",
"ddsp.input = dif\n",
"dif.input = aps\n",
"dsp.input = aps\n",
"aps.input = tif\n",
"#trh.input = tif\n",
"\n",
"tif.run()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}