Commit 20da0ff0 authored by Yannick TANGUY's avatar Yannick TANGUY

ENH: call TrainVectorClassifier properly

parent 3dc0b1aa
......@@ -18,17 +18,9 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 images will be used\n"
]
}
],
"outputs": [],
"source": [
"import otbApplication\n",
"import os\n",
......@@ -69,32 +61,9 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2019-07-30 09:40:22 (INFO) ConcatenateImages: Default RAM limit for OTB is 256 MB\n",
"2019-07-30 09:40:22 (INFO) ConcatenateImages: GDAL maximum cache size is 12847 MB\n",
"2019-07-30 09:40:22 (INFO) ConcatenateImages: OTB will use at most 48 threads\n",
"2019-07-30 09:40:22 (INFO): Estimated memory for full processing: 917.413MB (avail.: 256 MB), optimal image partitioning: 4 blocks\n",
"2019-07-30 09:40:22 (INFO): File output/image_stack.tif will be written in 5 blocks of 2410x417 pixels\n",
"Writing output/image_stack.tif...: 100% [**************************************************] (3s)\n"
]
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"app = otbApplication.Registry.CreateApplication(\"ConcatenateImages\")\n",
"app.SetParameterStringList(\"il\",images_list)\n",
......@@ -112,33 +81,9 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2019-07-30 09:40:25 (INFO) PolygonClassStatistics: Default RAM limit for OTB is 256 MB\n",
"2019-07-30 09:40:25 (INFO) PolygonClassStatistics: GDAL maximum cache size is 12847 MB\n",
"2019-07-30 09:40:25 (INFO) PolygonClassStatistics: OTB will use at most 48 threads\n",
"2019-07-30 09:40:25 (INFO) PolygonClassStatistics: Elevation management: setting default height above ellipsoid to 0 meters\n",
"2019-07-30 09:40:25 (INFO): Estimated memory for full processing: 458.478MB (avail.: 256 MB), optimal image partitioning: 2 blocks\n",
"2019-07-30 09:40:25 (INFO): Estimation will be performed in 3 blocks of 2410x694 pixels\n",
"Analyze polygons...: 100% [**************************************************] (8s)\n"
]
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"app = otbApplication.Registry.CreateApplication(\"PolygonClassStatistics\")\n",
"app.SetParameterString(\"in\",image_stack)\n",
......@@ -160,32 +105,9 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<?xml version=\"1.0\" ?>\n",
"\n",
"<GeneralStatistics>\n",
"\n",
" <Statistic name=\"samplesPerClass\">\n",
"\n",
" <StatisticMap key=\"1\" value=\"437540\" />\n",
"\n",
" <StatisticMap key=\"2\" value=\"1650038\" />\n",
"\n",
" <StatisticMap key=\"3\" value=\"147831\" />\n",
"\n",
" <StatisticMap key=\"4\" value=\"468701\" />\n",
"\n",
" </Statistic>\n",
"\n"
]
}
],
"outputs": [],
"source": [
"stats_file = open(stats,\"r\") \n",
"for cpt in range(8):\n",
......@@ -205,40 +127,9 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2019-07-30 09:40:33 (INFO) SampleSelection: Default RAM limit for OTB is 256 MB\n",
"2019-07-30 09:40:33 (INFO) SampleSelection: GDAL maximum cache size is 12847 MB\n",
"2019-07-30 09:40:33 (INFO) SampleSelection: OTB will use at most 48 threads\n",
"2019-07-30 09:40:33 (INFO) SampleSelection: Elevation management: setting default height above ellipsoid to 0 meters\n",
"2019-07-30 09:40:33 (INFO) SampleSelection: Sampling strategy : set a constant number of samples for all classes\n",
"2019-07-30 09:40:33 (INFO) SampleSelection: Sampling rates : className requiredSamples totalSamples rate\n",
"1\t80000\t437540\t0.18284\n",
"2\t80000\t1650038\t0.0484837\n",
"3\t80000\t147831\t0.541158\n",
"4\t80000\t468701\t0.170685\n",
"\n",
"2019-07-30 09:40:33 (INFO): Estimated memory for full processing: 458.478MB (avail.: 256 MB), optimal image partitioning: 2 blocks\n",
"2019-07-30 09:40:33 (INFO): Estimation will be performed in 4 blocks of 1584x1584 pixels\n",
"Selecting positions with periodic sampler...: 100% [**************************************************] (15s)\n"
]
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"app = otbApplication.Registry.CreateApplication(\"SampleSelection\")\n",
"app.SetParameterString(\"in\",image_stack)\n",
......@@ -265,32 +156,9 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2019-07-30 09:40:49 (INFO) SampleExtraction: Default RAM limit for OTB is 256 MB\n",
"2019-07-30 09:40:49 (INFO) SampleExtraction: GDAL maximum cache size is 12847 MB\n",
"2019-07-30 09:40:49 (INFO) SampleExtraction: OTB will use at most 48 threads\n",
"2019-07-30 09:40:49 (INFO): Estimated memory for full processing: 687.946MB (avail.: 256 MB), optimal image partitioning: 3 blocks\n",
"2019-07-30 09:40:49 (INFO): Estimation will be performed in 4 blocks of 2410x521 pixels\n",
"Extracting sample values...: 100% [**************************************************] (25s)\n"
]
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"app = otbApplication.Registry.CreateApplication(\"SampleExtraction\")\n",
"app.SetParameterString(\"in\",image_stack)\n",
......@@ -316,20 +184,49 @@
" * the names of the parameters to learn from"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"app = otbApplication.Registry.CreateApplication(\"TrainVectorClassifier\")\n",
"app.SetParameterStringList(\"io.vd\",[samples])\n",
"app.SetParameterString(\"io.out\",rf_model)\n",
"# OTB Python API trick : we need to call \"UpdateParameters\" so OTB opens the training set\n",
"# and list the possible field names...\n",
"app.UpdateParameters()\n",
"app.SetParameterString(\"cfield\",\"code\")\n",
"app.SetParameterStringList(\"feat\",['band_0', 'band_1', 'band_2', 'band_3', \n",
" 'band_4', 'band_5', 'band_6', 'band_7', \n",
" 'band_8', 'band_9', 'band_10', 'band_11'])\n",
"app.SetParameterString(\"classifier\",\"rf\")\n",
"app.SetParameterInt(\"classifier.rf.cat\",4)\n",
"app.SetParameterString(\"io.confmatout\",confmatrix)\n",
"app.ExecuteAndWriteOutput()\n",
"\n",
"# workaround because the UpdateParameters() did not seem to work fine for this app !\n",
"import subprocess\n",
"args = ['otbcli_TrainVectorClassifier', '-io.vd', samples, \n",
" '-cfield', 'code', '-io.out', rf_model, '-classifier', 'rf', \n",
" '-classifier.rf.cat', '4', '-feat', 'band_0', 'band_1', 'band_2', \n",
" 'band_3', 'band_4', 'band_5', 'band_6', 'band_7', 'band_8', \n",
" 'band_9', 'band_10', 'band_11','-io.confmatout',confmatrix]\n",
"subprocess.call(args)"
"#import subprocess\n",
"#args = ['otbcli_TrainVectorClassifier', '-io.vd', samples, \n",
"# '-cfield', 'code', '-io.out', rf_model, '-classifier', 'rf', #\n",
"# '-classifier.rf.cat', '4', '-feat', 'band_0', 'band_1', 'band_#2', \n",
"# 'band_3', 'band_4', 'band_5', 'band_6', 'band_7', 'band_8',# \n",
"# 'band_9', 'band_10', 'band_11','-io.confmatout',confmatrix]#\n",
"#subprocess.call(args)"
]
},
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment