{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Feature Mapping**\n\nSimple coding to view how the Tensorflow convolutional layer reads the image and map it ot the features.\n","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-16T08:25:26.583975Z","iopub.execute_input":"2022-08-16T08:25:26.584884Z","iopub.status.idle":"2022-08-16T08:25:26.590558Z","shell.execute_reply.started":"2022-08-16T08:25:26.584818Z","shell.execute_reply":"2022-08-16T08:25:26.589549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2","metadata":{"execution":{"iopub.status.busy":"2022-08-16T08:25:29.193246Z","iopub.execute_input":"2022-08-16T08:25:29.193644Z","iopub.status.idle":"2022-08-16T08:25:29.423793Z","shell.execute_reply.started":"2022-08-16T08:25:29.193611Z","shell.execute_reply":"2022-08-16T08:25:29.422891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2022-08-16T08:25:33.761217Z","iopub.execute_input":"2022-08-16T08:25:33.761666Z","iopub.status.idle":"2022-08-16T08:25:33.767009Z","shell.execute_reply.started":"2022-08-16T08:25:33.761622Z","shell.execute_reply":"2022-08-16T08:25:33.765957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread('/kaggle/input/dog-image/images.jpg')\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nimg=cv2.resize(img, (240,240))\nplt.imshow(img)\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-16T08:25:35.609546Z","iopub.execute_input":"2022-08-16T08:25:35.610008Z","iopub.status.idle":"2022-08-16T08:25:35.898985Z","shell.execute_reply.started":"2022-08-16T08:25:35.609969Z","shell.execute_reply":"2022-08-16T08:25:35.897907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers, Model\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\nfrom tensorflow.keras.models import Sequential","metadata":{"execution":{"iopub.status.busy":"2022-08-16T08:25:38.315096Z","iopub.execute_input":"2022-08-16T08:25:38.315525Z","iopub.status.idle":"2022-08-16T08:25:44.883981Z","shell.execute_reply.started":"2022-08-16T08:25:38.315486Z","shell.execute_reply":"2022-08-16T08:25:44.882892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(input_shape=(240,240,3),filters=64, kernel_size =(3,3), name='conv1'))\nmodel.add(MaxPooling2D(pool_size=(2,2), name='maxpool1'))\nmodel.add(Conv2D(filters=32, kernel_size =(3,3), name='conv2', activation='tanh'))\nmodel.add(MaxPooling2D(pool_size=(2,2), name='maxpool2'))\nmodel.add(Conv2D(filters=16, kernel_size =(3,3), name='conv3', activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2), name='maxpool3'))\nmodel.add(Conv2D(filters=8, kernel_size =(3,3), name='conv4', activation='relu'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-16T08:26:10.029594Z","iopub.execute_input":"2022-08-16T08:26:10.030061Z","iopub.status.idle":"2022-08-16T08:26:10.092752Z","shell.execute_reply.started":"2022-08-16T08:26:10.030019Z","shell.execute_reply":"2022-08-16T08:26:10.091917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.layers","metadata":{"execution":{"iopub.status.busy":"2022-08-16T07:31:49.533623Z","iopub.execute_input":"2022-08-16T07:31:49.534040Z","iopub.status.idle":"2022-08-16T07:31:49.542821Z","shell.execute_reply.started":"2022-08-16T07:31:49.534008Z","shell.execute_reply":"2022-08-16T07:31:49.541841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from math import sqrt\nfor l in model.layers:\n    print(l.name)\n    feature_map=Model(inputs=model.input, outputs=model.get_layer(l.name).output).predict(np.array([img]))\n    #print(feature_map.shape)\n    \n    #v=round(sqrt(feature_map.shape[-1]))\n    #print(feature_map.shape[-1])\n    \n    ncols = 2\n    nrows = feature_map.shape[-1] // ncols + (feature_map.shape[-1] % ncols > 0)\n    fig,ax=plt.subplots(nrows,ncols, sharex=True, sharey=True, figsize=(16,8))\n    plt.suptitle(\"Layer Name \"+l.name,fontsize=30)\n    ax=ax.flatten()\n    #print(ax)\n    plt.subplots_adjust(wspace=0, hspace=0)\n    for i,x in enumerate(ax):\n    #print(x)\n        ax[i].imshow(feature_map[0,:,:,i])\n    #plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-16T08:26:14.208481Z","iopub.execute_input":"2022-08-16T08:26:14.208954Z","iopub.status.idle":"2022-08-16T08:26:43.027534Z","shell.execute_reply.started":"2022-08-16T08:26:14.208914Z","shell.execute_reply":"2022-08-16T08:26:43.026544Z"},"trusted":true},"execution_count":null,"outputs":[]}]}