{"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":"# Some imports ","metadata":{}},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-11-12T22:21:06.00175Z","iopub.execute_input":"2023-11-12T22:21:06.002402Z","iopub.status.idle":"2023-11-12T22:21:06.010485Z","shell.execute_reply.started":"2023-11-12T22:21:06.002366Z","shell.execute_reply":"2023-11-12T22:21:06.008938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tensordict\n!pip install torchviz\nfrom torchvision.models import convnext_tiny,ConvNeXt_Tiny_Weights\nfrom torchvision.io import read_image","metadata":{"_kg_hide-output":true,"scrolled":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-11-13T00:21:38.904537Z","iopub.execute_input":"2023-11-13T00:21:38.905279Z","iopub.status.idle":"2023-11-13T00:22:00.208669Z","shell.execute_reply.started":"2023-11-13T00:21:38.905246Z","shell.execute_reply":"2023-11-13T00:22:00.207436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# Testing convnext for class prediction on imagenet\nconvnext_weights=ConvNeXt_Tiny_Weights.IMAGENET1K_V1\nmodel=convnext_tiny(weights=convnext_weights)\n#print(model)\nmodel.eval()\n\npreprocess=convnext_weights.transforms()\nimg=read_image(\"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/test/ILSVRC2012_test_00000046.JPEG\")\nbatch=preprocess(img).unsqueeze(0)\n\nprediction=model(batch).squeeze(0).softmax(0)\nclass_id= prediction.argmax().item()\nscore=prediction[class_id].item()\ncategory_name=convnext_weights.meta[\"categories\"][class_id]\nprint(f\"{category_name} : {score*100} % \")","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:00.211878Z","iopub.execute_input":"2023-11-13T00:22:00.212396Z","iopub.status.idle":"2023-11-13T00:22:02.115328Z","shell.execute_reply.started":"2023-11-13T00:22:00.21236Z","shell.execute_reply":"2023-11-13T00:22:02.112549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Remove the top layer and Extracting features vectors from the convnext ","metadata":{}},{"cell_type":"code","source":"import torch\nconvnext = convnext_tiny(weights=convnext_weights)\n\nconvnext_no_fc= torch.nn.Sequential(*list(convnext.children())[:-1]) # strips off last linear layer\n#print(convnext_no_fc)\npreprocess=convnext_weights.transforms()\nimg=read_image(\"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/test/ILSVRC2012_test_00000046.JPEG\")\nbatch=preprocess(img).unsqueeze(0)\n\nprediction=convnext_no_fc(batch)\n# Convert pytorch model to flow of data diagram\nfrom torchviz import make_dot, make_dot_from_trace\nmodel_dia=make_dot(convnext(batch),params=dict(convnext_no_fc.named_parameters()), show_attrs=True, show_saved=True)\nmodel_dia.format=\"png\"\nmodel_dia.render(\"convnext_model_dia\")\nprint(\"#####################SHOW THE MODEL NODES##############\")\nfrom torchvision.models.feature_extraction import get_graph_node_names\nfrom torchvision.models.feature_extraction import create_feature_extractor\ntrain_nodes, eval_nodes = get_graph_node_names(convnext)\nfor i,j in enumerate(train_nodes):\n    print(f\"Train node {i} : {j}\")\n    \nprint(\"##############THE NODES TO BE SELECTED FROM THE CONVNEXT MODEL#############################\")\nlist_of_layers_index=[9,48,87,120,153,175,192,225]\nlayers_to_be_printed=dict()\nfor i,j in enumerate(train_nodes):\n    if i in list_of_layers_index:\n        print(f\"Train node {i} : {j}\")\n        layers_to_be_printed[j]=f\"Output Layer {i}\"\nconvnext_return_nodes=layers_to_be_printed\nconvnext_feature_extractor=create_feature_extractor(convnext,return_nodes=convnext_return_nodes)\nimg=read_image(\"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/test/ILSVRC2012_test_00000046.JPEG\")\nbatch=preprocess(img).unsqueeze(0)\nfeature_list=convnext_feature_extractor(batch)\nprint(\"##########################PRINTING THE SHAPES OF THE FEATURES TO BE EXRACTED###############\")\nfor x in feature_list:\n    print(torch.flatten(feature_list[x]).shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:02.118706Z","iopub.execute_input":"2023-11-13T00:22:02.119577Z","iopub.status.idle":"2023-11-13T00:22:13.546498Z","shell.execute_reply.started":"2023-11-13T00:22:02.119504Z","shell.execute_reply":"2023-11-13T00:22:13.545436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"## Now features extraction from AlexNet","metadata":{}},{"cell_type":"code","source":"from torchvision.models import alexnet,AlexNet_Weights\nalexnet_weights=AlexNet_Weights.IMAGENET1K_V1\nmodel=alexnet(weights=alexnet_weights)\n#print(model)\nmodel.eval()\n\npreprocess=alexnet_weights.transforms()\nimg=read_image(\"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/test/ILSVRC2012_test_00000046.JPEG\")\nbatch=preprocess(img).unsqueeze(0)\n\nprediction=model(batch).squeeze(0).softmax(0)\nclass_id= prediction.argmax().item()\nscore=prediction[class_id].item()\ncategory_name=alexnet_weights.meta[\"categories\"][class_id]\nprint(f\"{category_name} : {score*100} % \")","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:13.549414Z","iopub.execute_input":"2023-11-13T00:22:13.549889Z","iopub.status.idle":"2023-11-13T00:22:15.721725Z","shell.execute_reply.started":"2023-11-13T00:22:13.549859Z","shell.execute_reply":"2023-11-13T00:22:15.720783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nalexnet_model=alexnet(weights=alexnet_weights)\n\nprint(alexnet_model)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:15.722837Z","iopub.execute_input":"2023-11-13T00:22:15.723124Z","iopub.status.idle":"2023-11-13T00:22:16.578216Z","shell.execute_reply.started":"2023-11-13T00:22:15.723095Z","shell.execute_reply":"2023-11-13T00:22:16.576332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocess=alexnet_weights.transforms()\nimg=read_image(\"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/test/ILSVRC2012_test_00000046.JPEG\")\nbatch=preprocess(img).unsqueeze(0)\nprint(batch.shape)\nprediction=alexnet_model(batch)\nfrom torchviz import make_dot, make_dot_from_trace\nmodel_dia=make_dot(alexnet_model(batch),params=dict(alexnet_model.named_parameters()), show_attrs=True, show_saved=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:16.580191Z","iopub.execute_input":"2023-11-13T00:22:16.581347Z","iopub.status.idle":"2023-11-13T00:22:16.646822Z","shell.execute_reply.started":"2023-11-13T00:22:16.581288Z","shell.execute_reply":"2023-11-13T00:22:16.645339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_dia.format=\"png\"\nmodel_dia.render(\"alexnet_model_dia\")","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:16.64831Z","iopub.execute_input":"2023-11-13T00:22:16.648653Z","iopub.status.idle":"2023-11-13T00:22:17.062635Z","shell.execute_reply.started":"2023-11-13T00:22:16.648623Z","shell.execute_reply":"2023-11-13T00:22:17.0608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.models.feature_extraction import get_graph_node_names\nfrom torchvision.models.feature_extraction import create_feature_extractor\ntrain_nodes, eval_nodes = get_graph_node_names(alexnet_model)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:17.064783Z","iopub.execute_input":"2023-11-13T00:22:17.065251Z","iopub.status.idle":"2023-11-13T00:22:17.084456Z","shell.execute_reply.started":"2023-11-13T00:22:17.065218Z","shell.execute_reply":"2023-11-13T00:22:17.082303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_of_layers_index=[1,4,7,9,11,17,20,22]","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:17.08861Z","iopub.execute_input":"2023-11-13T00:22:17.089165Z","iopub.status.idle":"2023-11-13T00:22:17.098801Z","shell.execute_reply.started":"2023-11-13T00:22:17.089126Z","shell.execute_reply":"2023-11-13T00:22:17.097087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,j in enumerate(train_nodes):\n    print(f\"Train node {i} : {j}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:17.102841Z","iopub.execute_input":"2023-11-13T00:22:17.103356Z","iopub.status.idle":"2023-11-13T00:22:17.114606Z","shell.execute_reply.started":"2023-11-13T00:22:17.103319Z","shell.execute_reply":"2023-11-13T00:22:17.112847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"layers_to_be_printed=dict()\nfor i,j in enumerate(train_nodes):\n    if i in list_of_layers_index:\n        print(f\"Train node {i} : {j}\")\n        layers_to_be_printed[j]=f\"Output Layer {i}\"\nprint(layers_to_be_printed)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:17.115608Z","iopub.execute_input":"2023-11-13T00:22:17.115835Z","iopub.status.idle":"2023-11-13T00:22:17.128555Z","shell.execute_reply.started":"2023-11-13T00:22:17.115813Z","shell.execute_reply":"2023-11-13T00:22:17.12727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alexnet_return_nodes=layers_to_be_printed\n\nalexnet_feature_extractor=create_feature_extractor(alexnet_model,return_nodes=alexnet_return_nodes)\nimg=read_image(\"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/test/ILSVRC2012_test_00000046.JPEG\")\nbatch=preprocess(img).unsqueeze(0)\nfeature_list=alexnet_feature_extractor(batch)\n\nfor x in feature_list:\n    print(torch.flatten(feature_list[x]).shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:17.13015Z","iopub.execute_input":"2023-11-13T00:22:17.130524Z","iopub.status.idle":"2023-11-13T00:22:17.202204Z","shell.execute_reply.started":"2023-11-13T00:22:17.130495Z","shell.execute_reply":"2023-11-13T00:22:17.201179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Now looping through all images used in the seen vs imagined study to regenrate the features and save them as files for decoding","metadata":{}},{"cell_type":"code","source":"# from PIL import Image\n# import requests\n# import torchvision.transforms.functional as TF\n# count=0\n# import pandas as pd \n# df_url=pd.read_csv(\"/kaggle/input/seen-vs-imagined-dataset-project/imageNet_URLTraining_fixed.csv\",header=None).astype(\"str\").values.tolist()\n# imagenet_train_loc=\"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/\"\n# for i,j in enumerate(df_url):\n    \n   \n#     try:\n#         image_file_path=imagenet_train_loc+f\"n0{j}/\"+df_url[i][2]\n#         url = df_url[i][0]\n#         #print(url)\n#         try:\n#             img = Image.open(requests.get(url, stream=True).raw)\n#             print(f\"URL ALL GOOD for : count:{i} file_ame:{df_url[i][1]}\")\n#         except:\n#             print(f\"URL:{df_url[i][0]} not opening trying for imagenet\")\n#             try:\n#                 img=Image.open(image_file_path)\n#                 print(\"file opened from imagenet\")\n#             except:\n#                 print(f\"image {df_url[i][1]} found online or in imagenet\")\n#         count+=1\n#     except:\n#         print(f\"{df_url[i][1]} file not found\")\n#     print(img.size)\n# print(f\"correctly loaded files are {count}/{len(df_url)}\")\n#         #print(\"ERROR at finding file name:\",count)\n    ","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-11-13T00:22:17.203568Z","iopub.execute_input":"2023-11-13T00:22:17.204179Z","iopub.status.idle":"2023-11-13T00:22:17.21357Z","shell.execute_reply.started":"2023-11-13T00:22:17.204148Z","shell.execute_reply":"2023-11-13T00:22:17.210859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# code for data loader","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nfrom torch.utils.data import DataLoader\nfrom PIL import Image\nfrom torch.utils.data import Dataset\nfrom pathlib import Path\nimport torchvision.io\nclass IMG_Dataset(Dataset):\n    def __init__(self, image_dir, transform = None):\n        self.image_dir = image_dir\n        \n        self.transform = transform\n        self.images = os.listdir(image_dir)\n\n    def __len__(self):\n        return len(self.images)\n\n    def __getitem__(self, index):\n        img_path = os.path.join(self.image_dir, self.images[index])\n        \n        image = read_image(img_path,mode=torchvision.io.ImageReadMode.RGB)\n\n\n        if self.transform is  not None:\n            try:\n                transformed_img= self.transform(image)\n                image = transformed_img\n            except:\n                print(f\"error found at file {img_path}\")\n                print(image.shape)\n\n        return image,img_path","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:17.215811Z","iopub.execute_input":"2023-11-13T00:22:17.216455Z","iopub.status.idle":"2023-11-13T00:22:17.225945Z","shell.execute_reply.started":"2023-11-13T00:22:17.216406Z","shell.execute_reply":"2023-11-13T00:22:17.225125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_loaders(\n        train_dir,\n        val_dir,\n        batch_size,\n        train_transform,\n        test_transform,\n        num_workers=4,\n        pin_memory=True\n):\n    train_dataset = IMG_Dataset(\n        image_dir=train_dir,\n        transform=train_transform\n    )\n\n    train_loader = DataLoader(\n        train_dataset,\n        batch_size=batch_size,\n        num_workers=num_workers,\n        pin_memory=pin_memory,\n        shuffle=True\n    )\n\n    test_dataset = IMG_Dataset(\n        image_dir=val_dir,\n        transform=test_transform\n    )\n\n    test_loader = DataLoader(\n        test_dataset,\n        batch_size=batch_size,\n        num_workers=num_workers,\n        pin_memory=pin_memory,\n        shuffle=True\n    )\n\n    return train_loader, test_loader\n","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:17.227466Z","iopub.execute_input":"2023-11-13T00:22:17.228095Z","iopub.status.idle":"2023-11-13T00:22:17.243746Z","shell.execute_reply.started":"2023-11-13T00:22:17.228065Z","shell.execute_reply":"2023-11-13T00:22:17.242202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Alexnet features ","metadata":{}},{"cell_type":"markdown","source":"# for training data imgs ","metadata":{}},{"cell_type":"code","source":"alex_train_transform =alexnet_weights.transforms()\nalex_test_transforms =alexnet_weights.transforms()\ntrain_dir=\"/kaggle/input/seen-vs-imagined-horikawa-et-al-imagenet-subset/images/training\"\ntest_dir=\"/kaggle/input/seen-vs-imagined-horikawa-et-al-imagenet-subset/images/test\"\nnum_workers=2\npin_memory=True\nbatch_size=1\ndevice=\"cpu\"\ntrain_loader, test_loader = get_loaders(\n        train_dir,\n        test_dir,\n        batch_size,\n        alex_train_transform,\n        alex_test_transforms,\n        num_workers,\n        pin_memory,\n    )","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:22:17.245079Z","iopub.execute_input":"2023-11-13T00:22:17.245634Z","iopub.status.idle":"2023-11-13T00:22:17.341452Z","shell.execute_reply.started":"2023-11-13T00:22:17.245605Z","shell.execute_reply":"2023-11-13T00:22:17.3401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nimport torch\nimport torch.nn as nn\n\nfrom tensordict import TensorDict\nfrom tensordict.nn import TensorDictModule, TensorDictSequential\nalexnet_feature_dict=dict()\nloop = tqdm(train_loader)\nfor batch_idx,(data,image_path)  in enumerate(loop):\n    \n    data = data.to(device=device)\n    feature_list=alexnet_feature_extractor(data)\n    flattened_feature_dict=dict()\n    for x in feature_list:\n        #print(feature_list[x].shape)\n        y=torch.flatten(feature_list[x]).detach().cpu().numpy()#flatten in code not working\n        sampled_size=1000\n        sampled_features=np.random.choice(y,size=(1000),replace=False)\n        flattened_feature_dict[x]=torch.unsqueeze(torch.from_numpy(sampled_features),0)\n        #print(flattened_feature_dict[x].shape)\n        \n    \n    img_name=str(*image_path).split(\"/\")[-1][1:-5]\n    alexnet_feature_dict[img_name]=flattened_feature_dict\n\n#     for x in feature_list:\n#         print(torch.flatten(feature_list[x]).shape)\n#print(alexnet_feature_dict)\nalex_train_fatures_dict_torch=TensorDict(alexnet_feature_dict,batch_size=1)\ntorch.save(alex_train_fatures_dict_torch,\"alexnet_dict_train.pt\")    ","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-11-13T00:22:17.342915Z","iopub.execute_input":"2023-11-13T00:22:17.343318Z","iopub.status.idle":"2023-11-13T00:23:13.918884Z","shell.execute_reply.started":"2023-11-13T00:22:17.343288Z","shell.execute_reply":"2023-11-13T00:23:13.91641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ny =alex_train_fatures_dict_torch[\"01518878_10042\"][\"Output Layer 11\"].detach().cpu().numpy()\nprint(y.shape)\n","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-11-13T00:08:16.12276Z","iopub.execute_input":"2023-11-13T00:08:16.123925Z","iopub.status.idle":"2023-11-13T00:08:16.129298Z","shell.execute_reply.started":"2023-11-13T00:08:16.123885Z","shell.execute_reply":"2023-11-13T00:08:16.128557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# for test data ","metadata":{}},{"cell_type":"code","source":"from tqdm import tqdm\nimport torch\nimport torch.nn as nn\n\nfrom tensordict import TensorDict\nfrom tensordict.nn import TensorDictModule, TensorDictSequential\nalexnet_feature_dict_test=dict()\nloop = tqdm(test_loader)\nfor batch_idx,(data,image_path)  in enumerate(loop):\n    #print(\"new img features\")\n    data = data.to(device=device)\n    feature_list=alexnet_feature_extractor(data)\n    flattened_feature_dict=dict()\n    for x in feature_list:\n        #print(feature_list[x].shape)\n        \n        y=torch.flatten(feature_list[x]).detach().cpu().numpy()#flatten in code not working\n        sampled_size=1000\n        sampled_features=np.random.choice(y,size=(1000),replace=False)\n        flattened_feature_dict[x]=torch.unsqueeze(torch.from_numpy(sampled_features),0)\n        #print(flattened_feature_dict[x].shape)\n    img_name=str(*image_path).split(\"/\")[-1][1:-5]\n    \n    alexnet_feature_dict_test[img_name]=flattened_feature_dict\n\n#     for x in feature_list:\n#         print(torch.flatten(feature_list[x]).shape)\n\nalex_train_fatures_dict_torch_test=TensorDict(alexnet_feature_dict_test,batch_size=1)\ntorch.save(alex_train_fatures_dict_torch_test,\"alexnet_dict_test.pt\")    ","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-11-13T00:08:30.828942Z","iopub.execute_input":"2023-11-13T00:08:30.829603Z","iopub.status.idle":"2023-11-13T00:08:34.621152Z","shell.execute_reply.started":"2023-11-13T00:08:30.82955Z","shell.execute_reply":"2023-11-13T00:08:34.619225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(alex_train_fatures_dict_torch_test)\n# x =alex_train_fatures_dict_torch_test[\"01443537_22563\"][\"Output Layer 11\"].detach().cpu().numpy()\n# print(x.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:08:42.213514Z","iopub.execute_input":"2023-11-13T00:08:42.21405Z","iopub.status.idle":"2023-11-13T00:08:42.221152Z","shell.execute_reply.started":"2023-11-13T00:08:42.213993Z","shell.execute_reply":"2023-11-13T00:08:42.219374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# For convnext ","metadata":{}},{"cell_type":"markdown","source":"## training data ","metadata":{}},{"cell_type":"code","source":"import os\nos.makedirs(\"/kaggle/working/convnext_train\", exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:12:06.168287Z","iopub.execute_input":"2023-11-13T00:12:06.168853Z","iopub.status.idle":"2023-11-13T00:12:06.176305Z","shell.execute_reply.started":"2023-11-13T00:12:06.168813Z","shell.execute_reply":"2023-11-13T00:12:06.174821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nimport torch\nimport torch.nn as nn\n\nfrom tensordict import TensorDict\nfrom tensordict.nn import TensorDictModule, TensorDictSequential\nconvnext_feature_dict_train=dict()\nloop = tqdm(train_loader)\nfor batch_idx,(data,image_path)  in enumerate(loop):       \n    #print(\"new img features\")\n    data = data.to(device=device)\n    feature_list=convnext_feature_extractor(data)\n    flattened_feature_dict=dict()\n    for x in feature_list:\n        #print(feature_list[x].shape)\n        \n        y=torch.flatten(feature_list[x]).detach().cpu().numpy()#flatten in code not working\n        sampled_size=1000\n        sampled_features=np.random.choice(y,size=(1000),replace=False)\n        flattened_feature_dict[x]=torch.unsqueeze(torch.from_numpy(sampled_features),0)\n        #print(flattened_feature_dict[x].shape)\n    img_name=str(*image_path).split(\"/\")[-1][1:-5]\n    \n    convnext_feature_dict_train[img_name]=flattened_feature_dict\n#     for x in feature_list:\n#         print(torch.flatten(feature_list[x]).shape)\n\nconvnext_train_fatures_dict_torch_train=TensorDict(convnext_feature_dict_train,batch_size=1)\ntorch.save(convnext_train_fatures_dict_torch_train,\"convnext_features_train.pt\")  ","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:23:48.888881Z","iopub.execute_input":"2023-11-13T00:23:48.890558Z","iopub.status.idle":"2023-11-13T00:27:10.555334Z","shell.execute_reply.started":"2023-11-13T00:23:48.890477Z","shell.execute_reply":"2023-11-13T00:27:10.552894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!zip -r \"conv_next_train.zip\" \"/kaggle/working/convnext_train/\"","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-11-12T22:43:04.404271Z","iopub.execute_input":"2023-11-12T22:43:04.404749Z","iopub.status.idle":"2023-11-12T22:46:32.625112Z","shell.execute_reply.started":"2023-11-12T22:43:04.404715Z","shell.execute_reply":"2023-11-12T22:46:32.623526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# convnext test img features ","metadata":{}},{"cell_type":"code","source":"from tqdm import tqdm\nimport torch\nimport torch.nn as nn\n\nfrom tensordict import TensorDict\nfrom tensordict.nn import TensorDictModule, TensorDictSequential\nconvnext_feature_dict_test=dict()\nloop = tqdm(test_loader)\nfor batch_idx,(data,image_path)  in enumerate(loop):\n    \n    data = data.to(device=device)\n    feature_list=convnext_feature_extractor(data)\n    flattened_feature_dict=dict()\n    for x in feature_list:\n        #print(feature_list[x].shape)\n        \n        y=torch.flatten(feature_list[x]).detach().cpu().numpy()#flatten in code not working\n        sampled_size=1000\n        sampled_features=np.random.choice(y,size=(1000),replace=False)\n        flattened_feature_dict[x]=torch.unsqueeze(torch.from_numpy(sampled_features),0)\n        #print(flattened_feature_dict[x].shape)\n    img_name=str(*image_path).split(\"/\")[-1][1:-5]\n    convnext_feature_dict_test[img_name]=flattened_feature_dict\n#     for x in feature_list:\n#         print(torch.flatten(feature_list[x]).shape)\n\nconvnext_train_fatures_dict_torch_test=TensorDict(convnext_feature_dict_test,batch_size=1)\ntorch.save(convnext_train_fatures_dict_torch_test,\"convnext_features_test.pt\")  ","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:29:37.589365Z","iopub.execute_input":"2023-11-13T00:29:37.589839Z","iopub.status.idle":"2023-11-13T00:29:45.609959Z","shell.execute_reply.started":"2023-11-13T00:29:37.589806Z","shell.execute_reply":"2023-11-13T00:29:45.608381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r \"test_train_img_features.zip\" \"/kaggle/working/alexnet_dict_test.pt\" \"/kaggle/working/alexnet_dict_train.pt\" \"/kaggle/working/convnext_features_test.pt\" \"/kaggle/working/convnext_features_train.pt\"","metadata":{"execution":{"iopub.status.busy":"2023-11-13T00:32:53.452162Z","iopub.execute_input":"2023-11-13T00:32:53.452647Z","iopub.status.idle":"2023-11-13T00:32:57.651589Z","shell.execute_reply.started":"2023-11-13T00:32:53.452613Z","shell.execute_reply":"2023-11-13T00:32:57.650095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}