Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
256 changes: 256 additions & 0 deletions 03_Advance/CNN/InceptionV3/tf_keras.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
Loading