{"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":"code","source":"!pip install timm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-29T01:53:47.724545Z","iopub.execute_input":"2022-05-29T01:53:47.725899Z","iopub.status.idle":"2022-05-29T01:54:01.880851Z","shell.execute_reply.started":"2022-05-29T01:53:47.725703Z","shell.execute_reply":"2022-05-29T01:54:01.879491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\nimport sys\nsys.path.append('../input/timm-pytorch-image-models')\n\nimport torch\nimport torch.nn as nn\nimport torchvision\nimport cv2\nfrom torchvision import models, transforms, utils\nfrom torch.autograd import Variable\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport scipy.misc\nfrom PIL import Image\nimport json\n# import timm\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\n%matplotlib inline\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":{"execution":{"iopub.status.busy":"2022-05-29T10:40:03.35393Z","iopub.execute_input":"2022-05-29T10:40:03.354646Z","iopub.status.idle":"2022-05-29T10:40:07.779159Z","shell.execute_reply.started":"2022-05-29T10:40:03.35451Z","shell.execute_reply":"2022-05-29T10:40:07.778058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transform = A.Compose([\n#             #Resize(CFG.size, CFG.size),\n#             A.RandomResizedCrop(512, 512),\n#             A.Transpose(p=0.5),\n#             A.HorizontalFlip(p=0.5),\n#             A.VerticalFlip(p=0.5),\n#             A.ShiftScaleRotate(p=0.5),\n#             A.Normalize(\n#                 mean=[0.485, 0.456, 0.406],\n#                 std=[0.229, 0.224, 0.225],\n#             ),\n#             ToTensorV2(),\n#         ])\nimage = Image.open(str('../input/cassava-leaf-disease-classification/train_images/147.jpg'))\nplt.imshow(image)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-29T09:01:33.961345Z","iopub.execute_input":"2022-05-29T09:01:33.961916Z","iopub.status.idle":"2022-05-29T09:01:33.977435Z","shell.execute_reply.started":"2022-05-29T09:01:33.961886Z","shell.execute_reply":"2022-05-29T09:01:33.976634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.resnet101(pretrained=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-29T09:01:36.542884Z","iopub.execute_input":"2022-05-29T09:01:36.543232Z","iopub.status.idle":"2022-05-29T09:01:58.091555Z","shell.execute_reply.started":"2022-05-29T09:01:36.543201Z","shell.execute_reply":"2022-05-29T09:01:58.090583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we will save the conv layer weights in this list\nmodel_weights =[]\n#we will save the 49 conv layers in this list\nconv_layers = []\n# get all the model children as list\nmodel_children = list(model.children())\n#counter to keep count of the conv layers\ncounter = 0\n#append all the conv layers and their respective wights to the list\nfor i in range(len(model_children)):\n    if type(model_children[i]) == nn.Conv2d:\n        counter+=1\n        model_weights.append(model_children[i].weight)\n        conv_layers.append(model_children[i])\n    elif type(model_children[i]) == nn.Sequential:\n        for j in range(len(model_children[i])):\n            for child in model_children[i][j].children():\n                if type(child) == nn.Conv2d:\n                    counter+=1\n                    model_weights.append(child.weight)\n                    conv_layers.append(child)\nprint(f\"Total convolution layers: {counter}\")\nprint(\"conv_layers\")","metadata":{"execution":{"iopub.status.busy":"2022-05-29T09:02:04.076854Z","iopub.execute_input":"2022-05-29T09:02:04.077252Z","iopub.status.idle":"2022-05-29T09:02:04.086189Z","shell.execute_reply.started":"2022-05-29T09:02:04.077217Z","shell.execute_reply":"2022-05-29T09:02:04.085449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_layers","metadata":{"execution":{"iopub.status.busy":"2022-05-29T09:02:07.612034Z","iopub.execute_input":"2022-05-29T09:02:07.612687Z","iopub.status.idle":"2022-05-29T09:02:07.622865Z","shell.execute_reply.started":"2022-05-29T09:02:07.612648Z","shell.execute_reply":"2022-05-29T09:02:07.621858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )\nmodel = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T09:02:14.418207Z","iopub.execute_input":"2022-05-29T09:02:14.418855Z","iopub.status.idle":"2022-05-29T09:02:17.175698Z","shell.execute_reply.started":"2022-05-29T09:02:14.418816Z","shell.execute_reply":"2022-05-29T09:02:17.174857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize((512, 512)),\n    transforms.RandomHorizontalFlip(p=0.5),\n    transforms.RandomVerticalFlip(),\n    transforms.ToTensor(),\n    transforms.Normalize( mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225],)\n])","metadata":{"execution":{"iopub.status.busy":"2022-05-29T09:02:22.123221Z","iopub.execute_input":"2022-05-29T09:02:22.12388Z","iopub.status.idle":"2022-05-29T09:02:22.140039Z","shell.execute_reply.started":"2022-05-29T09:02:22.123828Z","shell.execute_reply":"2022-05-29T09:02:22.138648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = transform(image)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T06:45:38.661712Z","iopub.execute_input":"2022-05-29T06:45:38.662465Z","iopub.status.idle":"2022-05-29T06:45:38.688547Z","shell.execute_reply.started":"2022-05-29T06:45:38.662428Z","shell.execute_reply":"2022-05-29T06:45:38.687696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#torch.Size([3, 512, 512])\nimage = transform(image)\nprint(f\"Image shape before: {image.shape}\")\ntorch.Size([1, 3, 512, 512])\nimage = image.unsqueeze(0)\nprint(f\"Image shape after: {image.shape}\")\nimage = image.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T09:02:27.523085Z","iopub.execute_input":"2022-05-29T09:02:27.523466Z","iopub.status.idle":"2022-05-29T09:02:27.551233Z","shell.execute_reply.started":"2022-05-29T09:02:27.523434Z","shell.execute_reply":"2022-05-29T09:02:27.550241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outputs = []\nnames = []\nfor layer in conv_layers[0:]:\n    image = layer(image)\n    print(image[0][0].shape)\n    outputs.append(image)\n    names.append(str(layer))\n# print(len(outputs))\n# #print feature_maps\n# for feature_map in outputs:\n#     print(feature_map.shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T09:02:31.458685Z","iopub.execute_input":"2022-05-29T09:02:31.459442Z","iopub.status.idle":"2022-05-29T09:02:36.254779Z","shell.execute_reply.started":"2022-05-29T09:02:31.459398Z","shell.execute_reply":"2022-05-29T09:02:36.253916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outputs[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-05-29T03:03:44.301693Z","iopub.execute_input":"2022-05-29T03:03:44.302342Z","iopub.status.idle":"2022-05-29T03:03:44.307893Z","shell.execute_reply.started":"2022-05-29T03:03:44.302306Z","shell.execute_reply":"2022-05-29T03:03:44.306964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names","metadata":{"execution":{"iopub.status.busy":"2022-05-29T03:46:35.522476Z","iopub.execute_input":"2022-05-29T03:46:35.522882Z","iopub.status.idle":"2022-05-29T03:46:35.529803Z","shell.execute_reply.started":"2022-05-29T03:46:35.522852Z","shell.execute_reply":"2022-05-29T03:46:35.529066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for feature_map in outputs:\n    print(feature_map[0].shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T03:05:19.883461Z","iopub.execute_input":"2022-05-29T03:05:19.884337Z","iopub.status.idle":"2022-05-29T03:05:19.88864Z","shell.execute_reply.started":"2022-05-29T03:05:19.884304Z","shell.execute_reply":"2022-05-29T03:05:19.887907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"processed = []\nfor feature_map in outputs:\n    feature_map = feature_map.squeeze(0)\n    gray_scale = torch.sum(feature_map,0)\n    gray_scale = gray_scale / feature_map.shape[0]\n    processed.append(gray_scale.data.cpu().numpy())\nfor fm in processed:\n    print(fm.shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T09:02:42.745892Z","iopub.execute_input":"2022-05-29T09:02:42.746526Z","iopub.status.idle":"2022-05-29T09:02:42.774148Z","shell.execute_reply.started":"2022-05-29T09:02:42.746488Z","shell.execute_reply":"2022-05-29T09:02:42.773373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2022-05-29T03:45:08.381454Z","iopub.execute_input":"2022-05-29T03:45:08.381808Z","iopub.status.idle":"2022-05-29T03:45:08.391468Z","shell.execute_reply.started":"2022-05-29T03:45:08.38178Z","shell.execute_reply":"2022-05-29T03:45:08.390596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(30, 50))\nfor i in range(len(processed)):\n    #resnet18\n    # a = fig.add_subplot(5, 4, i+1)\n    #resnet50\n    a = fig.add_subplot(10, 10, i+1)\n    imgplot = plt.imshow(processed[i])\n    a.axis(\"off\")\n    a.set_title(names[i].split('(')[0], fontsize=30)\nplt.savefig(str('feature_maps.jpg'), bbox_inches='tight')","metadata":{"execution":{"iopub.status.busy":"2022-05-29T09:02:47.726438Z","iopub.execute_input":"2022-05-29T09:02:47.727231Z","iopub.status.idle":"2022-05-29T09:02:55.403229Z","shell.execute_reply.started":"2022-05-29T09:02:47.727195Z","shell.execute_reply":"2022-05-29T09:02:55.402283Z"},"trusted":true},"execution_count":null,"outputs":[]}]}