research-rainfallradar/aimodel/src/rainfallwater_identity_TEST.ipynb

178 lines
10 KiB
Plaintext
Raw Normal View History

2022-12-09 15:50:27 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
2022-12-09 15:50:27 +00:00
"id": "1f6fdebf-69c5-46ab-a5a8-f9c91f000ff3",
"metadata": {},
"outputs": [],
2022-12-09 15:50:27 +00:00
"source": [
"import os\n",
"\n",
"import tensorflow as tf\n",
"\n",
"from lib.dataset.parse_heightmap import parse_heightmap\n",
"from lib.ai.model_rainfallwater_mono import model_rainfallwater_mono\n",
"from lib.ai.helpers.make_callbacks import make_callbacks\n",
"from lib.ai.helpers.summarywriter import summarywriter"
]
},
{
"cell_type": "code",
"execution_count": 7,
2022-12-09 15:50:27 +00:00
"id": "07093079",
"metadata": {},
"outputs": [],
"source": [
"filepath_heightmap=\"/mnt/research-data/main/terrain50-nimrodsized.json.gz\"\n",
"\n",
"dir_output = \"/tmp/x/mono_segment_TEST\"\n",
"if not os.path.exists(os.path.join(dir_output, \"checkpoints\")):\n",
"\tos.makedirs(os.path.join(dir_output, \"checkpoints\"))"
]
},
{
"cell_type": "code",
"execution_count": 8,
2022-12-09 15:50:27 +00:00
"id": "f4466ac9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"RAINFALL channels 1 width 64 height 64 HEIGHTMAP_INPUT False\n",
"convnext:shape IN x (None, 64, 64, 1)\n",
"DEBUG:convnext shape x (None, 64, 64, 1)\n",
2022-12-09 15:50:27 +00:00
"DEBUG:model ENCODER output_shape (None, 512)\n",
"DEBUG:model BOTTLENECK:stack2image output_shape (None, 4, 4, 512)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2022-12-09 17:35:07.608 | WARNING | lib.ai.model_rainfallwater_mono:model_rainfallwater_mono:70 - Warning: TODO implement attention from https://ieeexplore.ieee.org/document/9076883\n",
"2022-12-09 17:35:07.656 | INFO | lib.ai.model_rainfallwater_mono:model_rainfallwater_mono:80 - learning_rate: None\n"
2022-12-09 15:50:27 +00:00
]
}
],
"source": [
"model = model_rainfallwater_mono(\n",
"\tmetadata={ \"rainfallradar\": [ 1, 64, 64 ] },\n",
"\tmodel_arch_dec=\"convnext_i_xxtiny\"\n",
")\n",
"\n",
"summarywriter(model, filepath_output=os.path.join(dir_output, \"summary.txt\"))"
]
},
{
"cell_type": "code",
"execution_count": 9,
2022-12-09 15:50:27 +00:00
"id": "78c633e1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cells 4096 cells/2 2048.0 shape+ (64, 64, 2) tf.Tensor(2401, shape=(), dtype=int64)\n"
]
}
],
"source": [
"heightmap = parse_heightmap(filepath_heightmap) / 100\n",
"heightmap = tf.image.crop_to_bounding_box(tf.expand_dims(heightmap, axis=-1), 0, 0, 64, 64)\n",
"heightmap_labels = tf.one_hot(tf.cast(tf.math.greater(tf.squeeze(heightmap)/10, 0.05), dtype=tf.int32), 2)\n",
"\n",
"dataset = tf.data.Dataset.from_tensor_slices([heightmap])\n",
"dataset_labels = tf.data.Dataset.from_tensor_slices([heightmap_labels])\n",
"\n",
"for item in dataset_labels:\n",
"\tprint(\"cells\", 64*64, \"cells/2\", (64*64)/2, \"shape+\", item.shape, tf.math.reduce_sum(tf.math.argmax(item, axis=-1)))\n",
"\tbreak\n",
"dataset = tf.data.Dataset.zip((dataset_labels, dataset_labels)) \\\n",
2022-12-09 15:50:27 +00:00
"\t.repeat(64 * 64) \\\n",
"\t.batch(64) \\\n",
"\t.prefetch(tf.data.AUTOTUNE)\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
2022-12-09 15:50:27 +00:00
"id": "3dbc95eb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/50\n",
"WARNING:tensorflow:Model was constructed with shape (None, 64, 64, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 64, 64, 1), dtype=tf.float32, name='input_3'), name='input_3', description=\"created by layer 'input_3'\"), but it was called on an input with incompatible shape (None, 64, 64, 2).\n",
"WARNING:tensorflow:Model was constructed with shape (None, 64, 64, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 64, 64, 1), dtype=tf.float32, name='input_4'), name='input_4', description=\"created by layer 'input_4'\"), but it was called on an input with incompatible shape (None, 64, 64, 2).\n"
]
},
{
"ename": "ValueError",
"evalue": "in user code:\n\n File \"/home/bryan-smithl/.local/lib/python3.10/site-packages/keras/engine/training.py\", line 1051, in train_function *\n return step_function(self, iterator)\n File \"/home/bryan-smithl/.local/lib/python3.10/site-packages/keras/engine/training.py\", line 1040, in step_function **\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n File \"/home/bryan-smithl/.local/lib/python3.10/site-packages/keras/engine/training.py\", line 1030, in run_step **\n outputs = model.train_step(data)\n File \"/home/bryan-smithl/.local/lib/python3.10/site-packages/keras/engine/training.py\", line 889, in train_step\n y_pred = self(x, training=True)\n File \"/home/bryan-smithl/.local/lib/python3.10/site-packages/keras/utils/traceback_utils.py\", line 67, in error_handler\n raise e.with_traceback(filtered_tb) from None\n File \"/home/bryan-smithl/.local/lib/python3.10/site-packages/keras/engine/input_spec.py\", line 248, in assert_input_compatibility\n raise ValueError(\n\n ValueError: Exception encountered when calling layer \"convnext1\" (type Functional).\n \n Input 0 of layer \"downsample_layers.0.0_conv\" is incompatible with the layer: expected axis -1 of input shape to have value 1, but received input with shape (None, 64, 64, 2)\n \n Call arguments received by layer \"convnext1\" (type Functional):\n • inputs=tf.Tensor(shape=(None, 64, 64, 2), dtype=float32)\n • training=True\n • mask=None\n",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn [10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m model\u001b[39m.\u001b[39mfit(\n\u001b[1;32m 2\u001b[0m \tdataset,\n\u001b[1;32m 3\u001b[0m \tepochs\u001b[39m=\u001b[39m\u001b[39m50\u001b[39m,\n\u001b[1;32m 4\u001b[0m \tcallbacks\u001b[39m=\u001b[39mmake_callbacks(\u001b[39m\"\u001b[39m\u001b[39m/tmp/x/mono_segment_TEST\u001b[39m\u001b[39m\"\u001b[39m, model)\n\u001b[1;32m 5\u001b[0m )\n",
"File \u001b[0;32m~/.local/lib/python3.10/site-packages/keras/utils/traceback_utils.py:67\u001b[0m, in \u001b[0;36mfilter_traceback.<locals>.error_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mException\u001b[39;00m \u001b[39mas\u001b[39;00m e: \u001b[39m# pylint: disable=broad-except\u001b[39;00m\n\u001b[1;32m 66\u001b[0m filtered_tb \u001b[39m=\u001b[39m _process_traceback_frames(e\u001b[39m.\u001b[39m__traceback__)\n\u001b[0;32m---> 67\u001b[0m \u001b[39mraise\u001b[39;00m e\u001b[39m.\u001b[39mwith_traceback(filtered_tb) \u001b[39mfrom\u001b[39;00m \u001b[39mNone\u001b[39m\n\u001b[1;32m 68\u001b[0m \u001b[39mfinally\u001b[39;00m:\n\u001b[1;32m 69\u001b[0m \u001b[39mdel\u001b[39;00m filtered_tb\n",
"File \u001b[0;32m/tmp/__autograph_generated_filey37nprp8.py:15\u001b[0m, in \u001b[0;36mouter_factory.<locals>.inner_factory.<locals>.tf__train_function\u001b[0;34m(iterator)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m 14\u001b[0m do_return \u001b[39m=\u001b[39m \u001b[39mTrue\u001b[39;00m\n\u001b[0;32m---> 15\u001b[0m retval_ \u001b[39m=\u001b[39m ag__\u001b[39m.\u001b[39mconverted_call(ag__\u001b[39m.\u001b[39mld(step_function), (ag__\u001b[39m.\u001b[39mld(\u001b[39mself\u001b[39m), ag__\u001b[39m.\u001b[39mld(iterator)), \u001b[39mNone\u001b[39;00m, fscope)\n\u001b[1;32m 16\u001b[0m \u001b[39mexcept\u001b[39;00m:\n\u001b[1;32m 17\u001b[0m do_return \u001b[39m=\u001b[39m \u001b[39mFalse\u001b[39;00m\n",
"\u001b[0;31mValueError\u001b[0m: in user code:\n\n File \"/home/bryan-smithl/.local/lib/python3.10/site-packages/keras/engine/training.py\", line 1051, in train_function *\n return step_function(self, iterator)\n File \"/home/bryan-smithl/.local/lib/python3.10/site-packages/keras/engine/training.py\", line 1040, in step_function **\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n File \"/home/bryan-smithl/.local/lib/python3.10/site-packages/keras/engine/training.py\", line 1030, in run_step **\n outputs = model.train_step(data)\n File \"/home/bryan-smithl/.local/lib/python3.10/site-packages/keras/engine/training.py\", line 889, in train_step\n y_pred = self(x, training=True)\n File \"/home/bryan-smithl/.local/lib/python3.10/site-packages/keras/utils/traceback_utils.py\", line 67, in error_handler\n raise e.with_traceback(filtered_tb) from None\n File \"/home/bryan-smithl/.local/lib/python3.10/site-packages/keras/engine/input_spec.py\", line 248, in assert_input_compatibility\n raise ValueError(\n\n ValueError: Exception encountered when calling layer \"convnext1\" (type Functional).\n \n Input 0 of layer \"downsample_layers.0.0_conv\" is incompatible with the layer: expected axis -1 of input shape to have value 1, but received input with shape (None, 64, 64, 2)\n \n Call arguments received by layer \"convnext1\" (type Functional):\n • inputs=tf.Tensor(shape=(None, 64, 64, 2), dtype=float32)\n • training=True\n • mask=None\n"
2022-12-09 15:50:27 +00:00
]
}
],
"source": [
"model.fit(\n",
"\tdataset,\n",
"\tepochs=50,\n",
"\tcallbacks=make_callbacks(\"/tmp/x/mono_segment_TEST\", model)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7f8c33f",
"metadata": {},
"outputs": [],
"source": [
"prediction = model.predict(tf.expand_dims(heightmap, axis=0))\n",
"print(tf.math.argmax(prediction, axis=-1))\n",
"print(tf.math.reduce_sum(tf.math.argmax(prediction, axis=-1)))\n",
"print(prediction)\n"
]
}
],
"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",
"version": "3.10.6"
},
"vscode": {
"interpreter": {
"hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}