diff --git a/03_Advance/CNN/InceptionV3/tf_keras.ipynb b/03_Advance/CNN/InceptionV3/tf_keras.ipynb new file mode 100644 index 0000000..ac7b25f --- /dev/null +++ b/03_Advance/CNN/InceptionV3/tf_keras.ipynb @@ -0,0 +1,256 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "87807d1d-814c-4fff-a020-4e1339235387", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import cv2 as cv\n", + "import numpy as np\n", + "import tensorflow as tf\n", + "from matplotlib import pyplot as plt\n", + "from tensorflow.keras import Sequential,layers, models, losses, optimizers, datasets, utils" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "a2d7ddab-3787-4daf-980d-f27ec7606528", + "metadata": {}, + "outputs": [], + "source": [ + "from functools import reduce\n", + "def compose(*funcs):\n", + " \"\"\"Compose arbitrarily many functions, evaluated left to right.\n", + "\n", + " Reference: https://mathieularose.com/function-composition-in-python/\n", + " \"\"\"\n", + " if funcs:\n", + " return reduce(lambda f, g: lambda *a, **kw: g(f(*a, **kw)), funcs)\n", + " else:\n", + " raise ValueError('Composition of empty sequence not supported.')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b513cf4a-3fd3-4953-96b0-8a1267a06973", + "metadata": {}, + "outputs": [], + "source": [ + "def inceptionCBN(\n", + " filters,kernel_size=3,\n", + " activation='relu',strides=1,\n", + " padding=\"valid\"):\n", + " \n", + " conv=layers.Conv2D(\n", + " filters, kernel_size=kernel_size,\n", + " strides=strides,padding=padding,use_bias=False)\n", + " bn=layers.BatchNormalization(scale=False)\n", + " layers_list=[conv,bn]\n", + " if activation:\n", + " act=layers.Activation(activation=activation)\n", + " layers_list.append(act)\n", + " return compose(*layers_list)\n", + "\n", + "def reductionBlock(x,sorted_block=None):\n", + " x1=compose(\n", + " inceptionCBN(448,1,padding=\"same\"),\n", + " inceptionCBN(384,3,padding=\"same\")\n", + " )(x)\n", + " x2=inceptionCBN(384,1,padding=\"same\")(x)\n", + " blocks=[\n", + " layers.Concatenate()([\n", + " inceptionCBN(384,(1,3),padding=\"same\")(x1),\n", + " inceptionCBN(384,(3,1),padding=\"same\")(x1)]),\n", + " layers.Concatenate()([\n", + " inceptionCBN(384,(1,3),padding=\"same\")(x2),\n", + " inceptionCBN(384,(3,1),padding=\"same\")(x2)]),\n", + " compose(*[\n", + " layers.AveragePooling2D(pool_size=3,strides=1,padding=\"same\"),\n", + " inceptionCBN(192,1,padding=\"same\")])(x),\n", + " inceptionCBN(320,1,padding=\"same\")(x)\n", + " ]\n", + " if sorted_block:\n", + " blocks = [blocks[i] for i in sorted_block]\n", + " \n", + " x=layers.Concatenate()(\n", + " blocks\n", + " )\n", + " return x\n", + "\n", + "def inceptionV3Block(input_layer, configs_list, name=None, sorted_block = [3,1,0,2]):\n", + " blocks=list()\n", + " for i,configs in enumerate(configs_list):\n", + " block=list()\n", + " for config in configs:\n", + " if not isinstance(config, list):\n", + " raise ValueError(\"The filter list is a double list.\")\n", + " if i==2:\n", + " avg_pool=layers.AveragePooling2D(\n", + " pool_size=3,strides=1,padding=\"same\")\n", + " block.append(avg_pool)\n", + " inception_block=inceptionCBN(*config, padding=\"same\")\n", + " block.append(inception_block)\n", + " blocks.append(block)\n", + " # model weight 비교를 위한 정렬\n", + " blocks = [blocks[i] for i in sorted_block]\n", + " return layers.Concatenate(\n", + " name=None if name is None else f\"mixed_{name}_{i}\"\n", + " )([compose(*i)(input_layer) for i in blocks])" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "0a6ff82d-f4ad-43c6-b012-db87b5066e68", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-04-17 09:26:01.068452: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.068701: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.074319: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.074562: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.074751: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.074933: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.075588: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", + "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2024-04-17 09:26:01.225416: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.225631: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.226148: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.226331: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.226510: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.226697: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.917300: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.917551: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.917746: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.917937: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.918126: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.918288: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 10243 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:22:00.0, compute capability: 8.6\n", + "2024-04-17 09:26:01.918791: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:26:01.918930: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:1 with 10182 MB memory: -> device: 1, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:23:00.0, compute capability: 8.6\n" + ] + } + ], + "source": [ + "# configs list format [[filter, kernel_size]]\n", + "configs_list1=[[[64,1],[96,3],[96,3]], [[48,1],[64,5]], [[32,1]],[[64,1]]]\n", + "configs_list2=[[[64,1],[96,3],[96,3]], [[48,1],[64,5]], [[64,1]],[[64,1]]]\n", + "\n", + "configs_list3=[[[128,1],[128,(7,1)],[128,(1,7)],[128,(7,1)],[192,(1,7)]], \n", + " [[128,1],[128,(1,7)],[192,(7,1)]], [[192,1]],[[192,1]]]\n", + "configs_list4=[[[160,1],[160,(7,1)],[160,(1,7)],[160,(7,1)],[192,(1,7)]], \n", + " [[160,1],[160,(1,7)],[192,(7,1)]], [[192,1]],[[192,1]]]\n", + "configs_list5=[[[192,1],[192,(7,1)],[192,(1,7)],[192,(7,1)],[192,(1,7)]], \n", + " [[192,1],[192,(1,7)],[192,(7,1)]], [[192,1]],[[192,1]]]\n", + "\n", + "inp = layers.Input((299,299,3))\n", + "# head\n", + "head = compose(\n", + " inceptionCBN(32,strides=2),\n", + " inceptionCBN(32),\n", + " inceptionCBN(64,padding=\"same\"),\n", + " layers.MaxPooling2D(pool_size=3,strides=2),\n", + " inceptionCBN(80,1),\n", + " inceptionCBN(192),\n", + " layers.MaxPooling2D(pool_size=3,strides=2)\n", + ")(inp)\n", + "# inception_module_A\n", + "x = inceptionV3Block(head, configs_list1)\n", + "x = inceptionV3Block(x,configs_list2)\n", + "x = inceptionV3Block(x,configs_list2)\n", + "\n", + "# # grid_reduction_1\n", + "reduction_list1=[\n", + " inceptionCBN(384,3,strides=2),\n", + " compose(*[\n", + " inceptionCBN(64,1,padding=\"same\"),\n", + " inceptionCBN(96,3,padding=\"same\"),\n", + " inceptionCBN(96,3,strides=2)]\n", + " ),\n", + " layers.MaxPooling2D(pool_size=3,strides=2)]\n", + "x=layers.Concatenate()([layer(x) for layer in reduction_list1])\n", + "\n", + "# # # inception_module_B\n", + "x = inceptionV3Block(x,configs_list3)\n", + "x = inceptionV3Block(x,configs_list4)\n", + "x = inceptionV3Block(x,configs_list4)\n", + "x_a = inceptionV3Block(x,configs_list5)\n", + "\n", + "# # # grid_reduction_2\n", + "reduction_list2=[\n", + " compose(*[\n", + " inceptionCBN(192,1,padding=\"same\"),\n", + " inceptionCBN(320,3,strides=2)]\n", + " ),\n", + " compose(*[\n", + " inceptionCBN(192,1,padding=\"same\"),\n", + " inceptionCBN(192,(1,7),padding=\"same\"),\n", + " inceptionCBN(192,(7,1),padding=\"same\"),\n", + " inceptionCBN(192,3,strides=2)]\n", + " ),\n", + " layers.MaxPooling2D(pool_size=3,strides=2)]\n", + "x = layers.Concatenate()([layer(x_a) for layer in reduction_list2])\n", + "\n", + "# # # grid_reduction_3\n", + "x = reductionBlock(x,sorted_block=[3,1,0,2])\n", + "x = reductionBlock(x,sorted_block=[3,1,0,2])\n", + "\n", + "x=layers.GlobalAveragePooling2D()(x)\n", + "out=layers.Dense(units=1000,activation=\"softmax\")(x)\n", + "copy_model=models.Model(inp,out)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "d6d24425-3b1b-4be1-a156-db038d50ab56", + "metadata": {}, + "outputs": [], + "source": [ + "origin_model=tf.keras.applications.InceptionV3(\n", + " input_shape=(299,299,3),include_top=True)\n", + "\n", + "copy_model.set_weights(origin_model.get_weights())\n", + "\n", + "for org, new in zip(origin_model.layers, copy_model.layers):\n", + " new._name=org.name" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "65b655a5-043a-4eaa-b331-90c977a93ebe", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.8.19" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/03_Advance/CNN/MobileNetV1/tf_keras.ipynb b/03_Advance/CNN/MobileNetV1/tf_keras.ipynb new file mode 100644 index 0000000..16a7418 --- /dev/null +++ b/03_Advance/CNN/MobileNetV1/tf_keras.ipynb @@ -0,0 +1,191 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "1db2a99c-19ee-4d41-bdf4-ec82cd48949a", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import cv2 as cv\n", + "import numpy as np\n", + "import tensorflow as tf\n", + "import tensorflow.keras.backend as K\n", + "from matplotlib import pyplot as plt\n", + "from tensorflow.keras import activations,layers, models, losses, optimizers, datasets, utils" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "3768a694-9fcc-46a9-bc5f-4f382db789d3", + "metadata": {}, + "outputs": [], + "source": [ + "from functools import reduce\n", + "def compose(*funcs):\n", + " \"\"\"Compose arbitrarily many functions, evaluated left to right.\n", + "\n", + " Reference: https://mathieularose.com/function-composition-in-python/\n", + " \"\"\"\n", + " if funcs:\n", + " return reduce(lambda f, g: lambda *a, **kw: g(f(*a, **kw)), funcs)\n", + " else:\n", + " raise ValueError('Composition of empty sequence not supported.')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "8a6219ab-8a56-466b-8149-2d8140610f7d", + "metadata": {}, + "outputs": [], + "source": [ + "def custom_relu(x):\n", + " return K.relu(x, max_value=6.0, alpha=0, threshold=0)\n", + "\n", + "def mobileBlock(\n", + " dw_kernel_size, filters, \n", + " name=None,padding=\"valid\", \n", + " strides=1, activation=custom_relu):\n", + " \n", + " layers_list=[\n", + " layers.DepthwiseConv2D(\n", + " dw_kernel_size, strides=strides,padding=padding,use_bias=False),\n", + " layers.BatchNormalization(),\n", + " layers.Activation(activation=custom_relu),\n", + " layers.Conv2D(filters,1, padding=\"same\",use_bias=False),\n", + " layers.BatchNormalization(),\n", + " layers.Activation(activation=custom_relu),\n", + " ]\n", + " return compose(*layers_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ae28ae7a-1d92-4198-9089-09ab82f81c8c", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + }, + "scrolled": true + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-04-17 10:26:38.950861: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:38.951113: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:38.956853: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:38.957094: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:38.957284: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:38.957474: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:38.958195: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", + "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2024-04-17 10:26:39.096127: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:39.096348: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:39.096541: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:39.096722: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:39.096903: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:39.097083: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:39.783889: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:39.784138: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:39.784332: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:39.784524: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:39.784711: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:39.784876: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 10243 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:22:00.0, compute capability: 8.6\n", + "2024-04-17 10:26:39.785282: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:26:39.785424: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:1 with 10182 MB memory: -> device: 1, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:23:00.0, compute capability: 8.6\n" + ] + } + ], + "source": [ + "origin_model=tf.keras.applications.MobileNet(\n", + " input_shape=(224,224,3))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "d5b10ac8-1e94-4061-a5ea-1a1724144b03", + "metadata": {}, + "outputs": [], + "source": [ + "inp=layers.Input(shape=(224,224,3))\n", + "# head\n", + "x=compose(\n", + " layers.Conv2D(32,3,strides=2,padding=\"same\",use_bias= False),\n", + " layers.BatchNormalization(),\n", + " layers.Activation(activations.relu),# tf.nn.relu6\n", + ")(inp)\n", + "\n", + "x=mobileBlock(3, 64, padding=\"same\",name=\"block1\")(x)\n", + "x=layers.ZeroPadding2D(padding=((0,1),(0,1)))(x)\n", + "\n", + "x=mobileBlock(3, 128, strides=2,padding=\"valid\",name=\"block2\")(x)\n", + "x=mobileBlock(3, 128, padding=\"same\",name=\"block3\")(x)\n", + "x=layers.ZeroPadding2D(padding=((0,1),(0,1)))(x)\n", + "\n", + "x=mobileBlock(3, 256, strides=2,padding=\"valid\",name=\"block4\")(x)\n", + "x=mobileBlock(3, 256, padding=\"same\",name=\"block5\")(x)\n", + "x=layers.ZeroPadding2D(padding=((0,1),(0,1)))(x)\n", + "\n", + "x=mobileBlock(3, 512, strides=2,padding=\"valid\",name=\"block6\")(x)\n", + "x=mobileBlock(3, 512, padding=\"same\",name=\"block7\")(x)\n", + "x=mobileBlock(3, 512, padding=\"same\",name=\"block8\")(x)\n", + "x=mobileBlock(3, 512, padding=\"same\",name=\"block9\")(x)\n", + "x=mobileBlock(3, 512, padding=\"same\",name=\"block10\")(x)\n", + "x=mobileBlock(3, 512, padding=\"same\",name=\"block11\")(x)\n", + "x=layers.ZeroPadding2D(padding=((0,1),(0,1)))(x)\n", + "\n", + "x=mobileBlock(3, 1024, strides=2,padding=\"valid\",name=\"block12\")(x)\n", + "x=mobileBlock(3, 1024, padding=\"same\",name=\"block13\")(x)\n", + "\n", + "x=layers.GlobalAveragePooling2D(keepdims=True)(x)\n", + "x=layers.Dropout(rate=0.001)(x)\n", + "x=layers.Conv2D(1000,1,padding=\"same\")(x)\n", + "x=layers.Reshape((1000,))(x)\n", + "out=layers.Activation(activations.softmax)(x)\n", + "copy_model=models.Model(inp,out)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "676a14d4-8e7f-41bd-9ecb-f87b049823b0", + "metadata": {}, + "outputs": [], + "source": [ + "copy_model.set_weights(origin_model.get_weights())\n", + "\n", + "for org, new in zip(origin_model.layers, copy_model.layers):\n", + " new._name=org.name" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.8.19" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/03_Advance/CNN/ResNet/tf_keras.ipynb b/03_Advance/CNN/ResNet/tf_keras.ipynb new file mode 100644 index 0000000..d05dd66 --- /dev/null +++ b/03_Advance/CNN/ResNet/tf_keras.ipynb @@ -0,0 +1,190 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "32ad3db8-7a39-4db9-a236-c020e2dcff6a", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import cv2 as cv\n", + "import numpy as np\n", + "import tensorflow as tf\n", + "from matplotlib import pyplot as plt\n", + "from tensorflow.keras import activations, layers, models, losses, optimizers, datasets, utils" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "7dce18a6-9d6c-4543-b604-2f3b0c2ff83e", + "metadata": {}, + "outputs": [], + "source": [ + "from functools import reduce\n", + "def compose(*funcs):\n", + " \"\"\"Compose arbitrarily many functions, evaluated left to right.\n", + "\n", + " Reference: https://mathieularose.com/function-composition-in-python/\n", + " \"\"\"\n", + " if funcs:\n", + " return reduce(lambda f, g: lambda *a, **kw: g(f(*a, **kw)), funcs)\n", + " else:\n", + " raise ValueError('Composition of empty sequence not supported.')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "a6dd7e46-662b-4ba7-874a-3ff7a92c6284", + "metadata": {}, + "outputs": [], + "source": [ + "def ResCBN(\n", + " filters,kernel_size=3,\n", + " activation='relu',strides=1,\n", + " padding=\"valid\"):\n", + " \n", + " conv=layers.Conv2D(\n", + " filters, kernel_size=kernel_size,\n", + " strides=strides,padding=padding)\n", + " bn=layers.BatchNormalization(epsilon=1.001e-05)\n", + " layers_list=[conv,bn]\n", + " if activation:\n", + " act=layers.Activation(activation=activation)\n", + " layers_list.append(act)\n", + " return compose(*layers_list)\n", + "\n", + "def ResBlockV1(x,filters,kernel_sizes=[1,3,1,1],strides=1):\n", + " x1=compose(\n", + " ResCBN(filters[0],kernel_size=kernel_sizes[0],strides=strides),\n", + " ResCBN(filters[1],kernel_size=kernel_sizes[1],padding=\"same\"),\n", + " ResCBN(filters[2],kernel_size=kernel_sizes[2],activation=None)\n", + " )(x)\n", + " x2=ResCBN(\n", + " filters[3],kernel_size=kernel_sizes[3],\n", + " strides=strides,\n", + " activation=None)(x)\n", + " x=layers.Add()([x2,x1])\n", + " x=layers.Activation(activations.relu)(x)\n", + " return x\n", + " \n", + "def ResBlockV2(x,filters,kernel_sizes=[1,3,1]):\n", + " x1=compose(\n", + " ResCBN(filters[0],kernel_size=kernel_sizes[0]),\n", + " ResCBN(filters[1],kernel_size=kernel_sizes[1],padding=\"same\"),\n", + " ResCBN(filters[2],kernel_size=kernel_sizes[2],activation=None)\n", + " )(x)\n", + " x=layers.Add()([x1,x])\n", + " x=layers.Activation(activations.relu)(x)\n", + " return x" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "3b2785dc-f2a6-42c1-8e8f-02627195dd7f", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-04-17 10:22:28.085504: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.085760: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.091451: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.091725: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.091923: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.092109: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.092852: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", + "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2024-04-17 10:22:28.227874: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.228099: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.228297: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.228477: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.228658: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.228839: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.920115: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.920368: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.920564: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.920754: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.920943: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.921106: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 10243 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:22:00.0, compute capability: 8.6\n", + "2024-04-17 10:22:28.921632: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 10:22:28.921773: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:1 with 10182 MB memory: -> device: 1, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:23:00.0, compute capability: 8.6\n" + ] + } + ], + "source": [ + "inp=layers.Input(shape=(224,224,3))\n", + "x=compose(\n", + " layers.ZeroPadding2D(padding=3),\n", + " layers.Conv2D(64, 7, strides=2),\n", + " layers.BatchNormalization(epsilon=1.001e-05),\n", + " layers.Activation(activations.relu),\n", + " layers.ZeroPadding2D(),\n", + " layers.MaxPooling2D(pool_size=3,strides=2)\n", + ")(inp)\n", + "x=ResBlockV1(x,filters=[64,64,256,256])\n", + "x=ResBlockV2(x,filters=[64,64,256])\n", + "x=ResBlockV2(x,filters=[64,64,256])\n", + "\n", + "x=ResBlockV1(x,filters=[128,128,512,512],strides=2)\n", + "x=ResBlockV2(x,filters=[128,128,512])\n", + "x=ResBlockV2(x,filters=[128,128,512])\n", + "x=ResBlockV2(x,filters=[128,128,512])\n", + "\n", + "x=ResBlockV1(x,filters=[256,256,1024,1024],strides=2)\n", + "x=ResBlockV2(x,filters=[256,256,1024])\n", + "x=ResBlockV2(x,filters=[256,256,1024])\n", + "x=ResBlockV2(x,filters=[256,256,1024])\n", + "x=ResBlockV2(x,filters=[256,256,1024])\n", + "x=ResBlockV2(x,filters=[256,256,1024])\n", + "\n", + "x=ResBlockV1(x,filters=[512,512,2048,2048],strides=2)\n", + "x=ResBlockV2(x,filters=[512,512,2048])\n", + "x=ResBlockV2(x,filters=[512,512,2048])\n", + "x=layers.GlobalAveragePooling2D()(x)\n", + "out=layers.Dense(1000,activation=\"softmax\")(x)\n", + "copy_model=models.Model(inp,out)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "260ffd07-287a-450a-b9f7-53cac55e4730", + "metadata": {}, + "outputs": [], + "source": [ + "origin_model=tf.keras.applications.ResNet50(\n", + " input_shape=(224,224,3),include_top=True)\n", + "copy_model.set_weights(origin_model.get_weights())\n", + "\n", + "for org, new in zip(origin_model.layers, copy_model.layers):\n", + " new._name=org.name" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.8.19" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/03_Advance/CNN/VGGNet/tf_keras.ipynb b/03_Advance/CNN/VGGNet/tf_keras.ipynb new file mode 100644 index 0000000..1f80e40 --- /dev/null +++ b/03_Advance/CNN/VGGNet/tf_keras.ipynb @@ -0,0 +1,155 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "4261a02c-26c6-4cfb-8898-2be85b545748", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import cv2 as cv\n", + "import numpy as np\n", + "import tensorflow as tf\n", + "from matplotlib import pyplot as plt\n", + "from tensorflow.keras import layers, models, losses, optimizers, datasets, utils" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "8325c4e5-e8e2-493e-9df3-3ac64f4ef22e", + "metadata": {}, + "outputs": [], + "source": [ + "from functools import reduce\n", + "def compose(*funcs):\n", + " \"\"\"Compose arbitrarily many functions, evaluated left to right.\n", + "\n", + " Reference: https://mathieularose.com/function-composition-in-python/\n", + " \"\"\"\n", + " if funcs:\n", + " return reduce(lambda f, g: lambda *a, **kw: g(f(*a, **kw)), funcs)\n", + " else:\n", + " raise ValueError('Composition of empty sequence not supported.')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "387a71e5-d331-49b4-a1b4-8f4e2672a2d7", + "metadata": {}, + "outputs": [], + "source": [ + "def vggBlock(filters,kernel_sizes,name=None):\n", + " if not isinstance(filters, list):\n", + " filters = [filters] * len(kernel_sizes)\n", + " conv_layers = [\n", + " layers.Conv2D(\n", + " i, kernel_size=j, padding='same',\n", + " activation='relu') \n", + " for i, j in zip(filters, kernel_sizes)]\n", + " \n", + " layers_list = conv_layers + [layers.MaxPooling2D()]\n", + " return compose(*layers_list)\n", + "\n", + "def vggFC(filters, activations=\"softmax\", name=None):\n", + " if not isinstance(filters, list):\n", + " raise ValueError(\"filters must be a list\")\n", + " if activations is None:\n", + " raise ValueError(\"activations is not null\")\n", + " elif not isinstance(activations, list):\n", + " activations = [\"relu\"] * (len(filters) - 1) + [activations]\n", + "\n", + " dense_layers = [\n", + " layers.Dense(i, activation=j)\n", + " for i,j in zip(filters, activations)]\n", + " layers_list = [layers.Flatten()] + dense_layers\n", + " return compose(*layers_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "c9046e48-ddb3-43fc-81c4-fe337c6ca7f0", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-04-17 09:32:59.390349: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:32:59.390612: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:32:59.396357: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:32:59.396613: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:32:59.396808: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:32:59.396995: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:32:59.397725: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", + "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2024-04-17 09:32:59.534130: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:32:59.534357: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:32:59.534816: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:32:59.535007: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:32:59.535197: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:32:59.535389: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:33:00.231165: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:33:00.231429: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:33:00.231651: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:33:00.231845: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:33:00.232039: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:33:00.232204: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 10243 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:22:00.0, compute capability: 8.6\n", + "2024-04-17 09:33:00.232601: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", + "2024-04-17 09:33:00.232748: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:1 with 10182 MB memory: -> device: 1, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:23:00.0, compute capability: 8.6\n" + ] + } + ], + "source": [ + "inp = layers.Input((224, 224, 3))\n", + "x = vggBlock(64,[3,3],name=\"block1\")(inp)\n", + "x = vggBlock(128,[3,3],name=\"block2\")(x)\n", + "x = vggBlock(256,[3,3,3],name=\"block3\")(x)\n", + "x = vggBlock(512,[3,3,3],name=\"block4\")(x)\n", + "x = vggBlock(512,[3,3,3],name=\"block5\")(x)\n", + "out=vggFC([4096,4096,1000],activations=\"softmax\",name=\"FC\")(x)\n", + "copy_model=models.Model(inp,out)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "1eecc8e5-f702-4ef4-bfd6-58396f5d21d2", + "metadata": {}, + "outputs": [], + "source": [ + "origin_model=tf.keras.applications.VGG16(\n", + " input_shape=(224,224,3),include_top=True)\n", + "\n", + "copy_model.set_weights(origin_model.get_weights())\n", + "\n", + "for org, new in zip(origin_model.layers, copy_model.layers):\n", + " new._name=org.name" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.8.19" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}