{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%matplotlib inline\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport random\nfrom PIL import Image\nimport PIL.ImageOps    \n\nimport torchvision\nimport torchvision.datasets as datasets\nimport torchvision.transforms as transforms\nfrom torch.utils.data import DataLoader, Dataset\nimport torchvision.utils\nimport torch\nfrom torch.autograd import Variable\nimport torch.nn as nn\nfrom torch import optim\nimport torch.nn.functional as F\nimport pandas as pd\nimport os\nimport cv2\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-31T17:54:56.852272Z","iopub.execute_input":"2023-08-31T17:54:56.852686Z","iopub.status.idle":"2023-08-31T17:54:56.862402Z","shell.execute_reply.started":"2023-08-31T17:54:56.852654Z","shell.execute_reply":"2023-08-31T17:54:56.861404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom PIL import Image\n\nmain_directory = \"/kaggle/input/bioverse-hackathon-a/train\"  # Replace with the actual path to your train folder\n\nclass_labels = [\"beneign\", \"malignant\", \"normal\"]\ntrain_imgs = []\ntrain_label = []  # Create an empty list to store processed images along with labels\n\nfor label in class_labels:\n    label_folder_path = os.path.join(main_directory, label)\n    # List all subfolders (image instances) within the label folder\n    image_instances = [instance for instance in os.listdir(label_folder_path) if os.path.isdir(os.path.join(label_folder_path, instance))]\n    \n    for instance in image_instances:\n        instance_folder_path = os.path.join(label_folder_path, instance)\n        \n        # List all image files in the instance folder\n        image_files = [file for file in os.listdir(instance_folder_path) if file.lower().endswith(('.png', '.jpg', '.jpeg'))]\n        \n        for image_file in image_files:\n            image_path = os.path.join(instance_folder_path, image_file)\n            \n            # Open the image using PIL (Python Imaging Library)\n            image = Image.open(image_path)\n            \n            # Get the index of the label in the class_labels list\n            label_index = class_labels.index(label)\n            \n            # Create a tuple with the image and its corresponding label index\n            # Append the tuple to the train list\n            train_imgs.append(image)\n            train_label.append(label_index)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:54:56.903997Z","iopub.execute_input":"2023-08-31T17:54:56.904277Z","iopub.status.idle":"2023-08-31T17:54:57.525391Z","shell.execute_reply.started":"2023-08-31T17:54:56.904254Z","shell.execute_reply":"2023-08-31T17:54:57.524354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(train_imgs[0])\nplt.axis('off')  # Turn off axes\nplt.show()\nlen(train_imgs)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:54:57.527500Z","iopub.execute_input":"2023-08-31T17:54:57.527889Z","iopub.status.idle":"2023-08-31T17:54:58.081698Z","shell.execute_reply.started":"2023-08-31T17:54:57.527852Z","shell.execute_reply":"2023-08-31T17:54:58.079459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs = np.array(train_imgs)\ntrain_label = np.array(train_label)\n\ntrain_imgs, val_imgs, train_label, val_label = train_test_split(train_imgs, train_label, test_size=0.01)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:54:58.082982Z","iopub.execute_input":"2023-08-31T17:54:58.084446Z","iopub.status.idle":"2023-08-31T17:55:18.569945Z","shell.execute_reply.started":"2023-08-31T17:54:58.084408Z","shell.execute_reply":"2023-08-31T17:55:18.568332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(train_label).hist()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:18.572858Z","iopub.execute_input":"2023-08-31T17:55:18.573212Z","iopub.status.idle":"2023-08-31T17:55:18.916390Z","shell.execute_reply.started":"2023-08-31T17:55:18.573178Z","shell.execute_reply":"2023-08-31T17:55:18.915268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class customDataset(torch.utils.data.Dataset):\n    def __init__(self,imgs,labels,transform=None):\n        super(customDataset).__init__()\n        self.imgs = imgs\n        self.label = labels\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.label)\n        \n    def __getitem__(self,idx):\n        label1 = self.label[idx]\n        img1 = self.imgs[idx]\n        same_class = random.randint(0,1) \n        if same_class:\n            while True:\n                idx_rand = random.randint(0,self.__len__()-1)\n                if idx == idx_rand:\n                    continue\n                label2 = self.label[idx_rand]\n                if label2 == label1 :\n                    img2 = self.imgs[idx_rand]\n                    break\n                \n        else:\n            while True:\n                idx_rand = random.randint(0,self.__len__()-1)\n                if idx == idx_rand:\n                    continue\n                label2 = self.label[idx_rand]\n                if label2 != label1 :\n                    img2 = self.imgs[idx_rand]\n                    break\n\n        if self.transform:\n            img1 = self.transform(img1)\n            img2 = self.transform(img2)\n        return img1,img2,torch.from_numpy(np.array([int(label1 != label2)], dtype=np.float32)),torch.tensor(label1),torch.tensor(label2)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:18.918174Z","iopub.execute_input":"2023-08-31T17:55:18.919238Z","iopub.status.idle":"2023-08-31T17:55:18.929966Z","shell.execute_reply.started":"2023-08-31T17:55:18.919203Z","shell.execute_reply":"2023-08-31T17:55:18.929025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_size = 256\ntransform_train = transforms.Compose(\n[\ntransforms.Grayscale(num_output_channels=1),  # Convert to grayscale\n transforms.Resize((img_size, img_size)),\n  transforms.RandAugment(8,2),\n  transforms.ToTensor(),\n#       transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n#   transforms.RandomErasing(0.1),\n])\ntransform_tester = transforms.Compose(\n    [\n    transforms.Grayscale(num_output_channels=1),  # Convert to grayscale\n     transforms.Resize((img_size, img_size)),\n      transforms.ToTensor(),\n#       transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n    ])\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:18.931509Z","iopub.execute_input":"2023-08-31T17:55:18.931931Z","iopub.status.idle":"2023-08-31T17:55:18.946546Z","shell.execute_reply.started":"2023-08-31T17:55:18.931898Z","shell.execute_reply":"2023-08-31T17:55:18.945601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = customDataset(train_imgs,train_label,transform_train)\nval_dataset = customDataset(val_imgs,val_label,transform_tester)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:18.947832Z","iopub.execute_input":"2023-08-31T17:55:18.948221Z","iopub.status.idle":"2023-08-31T17:55:18.956733Z","shell.execute_reply.started":"2023-08-31T17:55:18.948187Z","shell.execute_reply":"2023-08-31T17:55:18.955834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader = DataLoader(train_dataset,\n                        shuffle=True,\n                        batch_size=1)\nval_dataloader = DataLoader(val_dataset,\n                        shuffle=False,\n                        batch_size=1)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:18.958046Z","iopub.execute_input":"2023-08-31T17:55:18.958509Z","iopub.status.idle":"2023-08-31T17:55:18.969436Z","shell.execute_reply.started":"2023-08-31T17:55:18.958478Z","shell.execute_reply":"2023-08-31T17:55:18.968554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in val_dataloader:\n    img1,img2,_,label,_ = i\n    print(img1.shape)\n    plt.imshow(img1.reshape(img1.shape[2:4]))\n    plt.axis('off')  # Turn off axes\n    plt.show()\n    print(label)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:18.970867Z","iopub.execute_input":"2023-08-31T17:55:18.971114Z","iopub.status.idle":"2023-08-31T17:55:19.152327Z","shell.execute_reply.started":"2023-08-31T17:55:18.971093Z","shell.execute_reply":"2023-08-31T17:55:19.151055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class SiameseNetwork(nn.Module):\n\n#     def __init__(self):\n#         super(SiameseNetwork, self).__init__()\n\n#         # Setting up the Sequential of CNN Layers\n#         self.cnn1 = nn.Sequential(\n#             nn.Conv2d(1, 64, kernel_size=3),\n#             nn.ReLU(inplace=True),\n#             nn.MaxPool2d(3, stride=3),\n            \n#             nn.Conv2d(64, 256, kernel_size=3),\n#             nn.ReLU(inplace=True),\n#             nn.MaxPool2d(2, stride=2),\n\n#             nn.Conv2d(256, 512, kernel_size=3,stride=1),\n#             nn.ReLU(inplace=True),\n#             nn.MaxPool2d(2, stride=2),\n\n#             nn.Conv2d(512, 1024, kernel_size=3,stride=1),\n#             nn.ReLU(inplace=True),\n\n#         )\n\n#         # Setting up the Fully Connected Layers\n#         self.fc1 = nn.Sequential(\n#             nn.Linear(1024*17*17, 1024),\n#             nn.ReLU(inplace=True),\n            \n#             nn.Linear(1024, 256),\n#             nn.ReLU(inplace=True),\n            \n#             nn.Linear(256,2)\n#         )\n        \n#     def forward_once(self, x):\n#         # This function will be called for both images\n#         # Its output is used to determine the similiarity\n#         output = self.cnn1(x)\n#         output = output.flatten()\n#         output = self.fc1(output)\n#         return output\n\n#     def forward(self, input1, input2):\n#         # In this function we pass in both images and obtain both vectors\n#         # which are returned\n#         output1 = self.forward_once(input1)\n#         output2 = self.forward_once(input2)\n\n#         return output1, output2\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:19.162459Z","iopub.execute_input":"2023-08-31T17:55:19.165775Z","iopub.status.idle":"2023-08-31T17:55:19.177411Z","shell.execute_reply.started":"2023-08-31T17:55:19.165723Z","shell.execute_reply":"2023-08-31T17:55:19.176060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the Contrastive Loss Function\nclass ContrastiveLoss(torch.nn.Module):\n    def __init__(self, margin=2.0):\n        super(ContrastiveLoss, self).__init__()\n        self.margin = margin\n\n    def forward(self, output1, output2, label):\n      # Calculate the euclidean distance and calculate the contrastive loss\n      euclidean_distance = F.pairwise_distance(output1, output2, keepdim = True)\n\n      loss_contrastive = torch.mean((1-label) * torch.pow(euclidean_distance, 2) +\n                                    (label) * torch.pow(torch.clamp(self.margin - euclidean_distance, min=0.0), 2))\n\n\n      return loss_contrastive\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:19.179087Z","iopub.execute_input":"2023-08-31T17:55:19.179791Z","iopub.status.idle":"2023-08-31T17:55:19.191285Z","shell.execute_reply.started":"2023-08-31T17:55:19.179758Z","shell.execute_reply":"2023-08-31T17:55:19.189091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# net = SiameseNetwork().cuda()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:19.193619Z","iopub.execute_input":"2023-08-31T17:55:19.194508Z","iopub.status.idle":"2023-08-31T17:55:19.202065Z","shell.execute_reply.started":"2023-08-31T17:55:19.194470Z","shell.execute_reply":"2023-08-31T17:55:19.201062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# counter = []\n# loss_history = [] \n# iteration_number= 0\n\n# # Iterate throught the epochs\n# for epoch in range(20):\n\n#     # Iterate over batches\n#     for i, (img0, img1, label) in enumerate(train_dataloader, 0):\n\n#         # Send the images and labels to CUDA\n#         img0, img1, label = img0.cuda(), img1.cuda(), label.cuda()\n\n#         # Zero the gradients\n#         optimizer.zero_grad()\n\n#         # Pass in the two images into the network and obtain two outputs\n#         output1, output2 = net(img0, img1)\n\n#         # Pass the outputs of the networks and label into the loss function\n#         loss_contrastive = criterion(output1, output2, label)\n\n#         # Calculate the backpropagation\n#         loss_contrastive.backward()\n\n#         # Optimize\n#         optimizer.step()\n\n#         # Every 10 batches print out the loss\n#         if i % 10 == 0 :\n#             print(f\"Epoch number {epoch}\\n Current loss {loss_contrastive.item()}\\n\")\n#             iteration_number += 10\n\n#             counter.append(iteration_number)\n#             loss_history.append(loss_contrastive.item())","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:19.203832Z","iopub.execute_input":"2023-08-31T17:55:19.204192Z","iopub.status.idle":"2023-08-31T17:55:19.212651Z","shell.execute_reply.started":"2023-08-31T17:55:19.204135Z","shell.execute_reply":"2023-08-31T17:55:19.211729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def imshow(img, text=None):\n    npimg = img.numpy()\n    plt.axis(\"off\")\n    if text:\n        plt.text(75, 8, text, style='italic',fontweight='bold',\n            bbox={'facecolor':'white', 'alpha':0.8, 'pad':10})\n        \n    plt.imshow(np.transpose(npimg, (1, 2, 0)))\n    plt.show()    \n","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:19.213853Z","iopub.execute_input":"2023-08-31T17:55:19.214695Z","iopub.status.idle":"2023-08-31T17:55:19.222683Z","shell.execute_reply.started":"2023-08-31T17:55:19.214664Z","shell.execute_reply":"2023-08-31T17:55:19.222046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset = customDataset(train_imgs,train_label,True,transform_train)\n# meanF = 0\n# meanT = 0\n# counter = 0\n# correct = 0\n# for i in range(len(val_dataset)):\n#     ancher,_,label0 = val_dataset.__getitem__(i)\n#     min_distance = 100000\n#     prediction = -1\n#     for j in range(len(train_dataset)):\n#         if i == j:\n#             continue\n#         img,_,label1 = train_dataset.__getitem__(j)\n#         output1, output2 = net(ancher.cuda(), img.cuda())\n#         euclidean_distance = F.pairwise_distance(output1, output2)\n#         distance = euclidean_distance.item()\n#         if min_distance > distance:\n#             min_distance = distance\n#             prediction = label1\n        \n#         if label0==label1:\n#             meanT += euclidean_distance.item()\n#         else:\n#             meanF += euclidean_distance.item()\n#         concatenated = torch.cat((ancher.reshape(256,256), img.reshape(256,256)), 0)\n#     print(label0,prediction,min_distance)\n#     counter += 1\n#     if label0 == prediction:\n#         correct += 1\n    \n# print(meanF,meanT)\n# print(correct/counter , correct, counter)\n# #         imshow(torchvision.utils.make_grid(concatenated), f'Dissimilarity: {euclidean_distance.item():.2f}')","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:19.223994Z","iopub.execute_input":"2023-08-31T17:55:19.224525Z","iopub.status.idle":"2023-08-31T17:55:19.236995Z","shell.execute_reply.started":"2023-08-31T17:55:19.224494Z","shell.execute_reply":"2023-08-31T17:55:19.236374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# counter, correct, correct/counter","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:19.238250Z","iopub.execute_input":"2023-08-31T17:55:19.239057Z","iopub.status.idle":"2023-08-31T17:55:19.246233Z","shell.execute_reply.started":"2023-08-31T17:55:19.238980Z","shell.execute_reply":"2023-08-31T17:55:19.245649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs = []\ntest_path = \"/kaggle/input/bioverse-hackathon-a/test\"\ntest_imgs_names = []\nfor img_name in os.listdir(test_path):\n    img = transform_tester(Image.open(test_path + \"/\" + img_name))\n    test_imgs_names.append(img_name)\n    test_imgs.append(np.array(img))\ntest_imgs = np.array(test_imgs)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:19.247525Z","iopub.execute_input":"2023-08-31T17:55:19.248673Z","iopub.status.idle":"2023-08-31T17:55:29.962660Z","shell.execute_reply.started":"2023-08-31T17:55:19.248640Z","shell.execute_reply":"2023-08-31T17:55:29.961624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(test_imgs[0]),type(test_imgs)\nresult = np.concatenate(test_imgs)\nresult.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:29.964122Z","iopub.execute_input":"2023-08-31T17:55:29.964580Z","iopub.status.idle":"2023-08-31T17:55:29.987572Z","shell.execute_reply.started":"2023-08-31T17:55:29.964542Z","shell.execute_reply":"2023-08-31T17:55:29.986503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs = torch.from_numpy(test_imgs)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:29.989287Z","iopub.execute_input":"2023-08-31T17:55:29.989665Z","iopub.status.idle":"2023-08-31T17:55:29.994185Z","shell.execute_reply.started":"2023-08-31T17:55:29.989633Z","shell.execute_reply":"2023-08-31T17:55:29.993173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataloader = DataLoader(test_imgs,\n                        shuffle=False,\n                        num_workers=2,\n                        batch_size=1,\n                        )","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:29.995688Z","iopub.execute_input":"2023-08-31T17:55:29.996391Z","iopub.status.idle":"2023-08-31T17:55:30.006018Z","shell.execute_reply.started":"2023-08-31T17:55:29.996358Z","shell.execute_reply":"2023-08-31T17:55:30.005059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install timm","metadata":{"execution":{"iopub.status.busy":"2023-08-31T17:55:30.007189Z","iopub.execute_input":"2023-08-31T17:55:30.008036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import timm\nmodel = timm.create_model('resnet50.a1_in1k', pretrained=True,)\nmodel = model.eval()\nmodel.cuda()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"activation = {}\ndef get_activation(name):\n    def hook(model, input, output):\n        activation[name] = output.detach()\n    return hook\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_ext(loader):\n    feature = []\n    label = []\n    for j in range(len(loader)):\n        img,_,sim,label1,label2 = train_dataset.__getitem__(j)\n        img = np.repeat(img,3,0)\n        img = img.reshape(1,3,256,256)\n        model.layer4[2].conv1.register_forward_hook(get_activation('la'))\n        output = model(img.cuda())\n        output = nn.MaxPool2d(2, stride=4)(activation['la']).flatten()\n        feature.append(output)\n        label.append(label1)\n    return feature,label\ntrain_features, train_label = feature_ext(train_dataloader)\nval_features, val_label = feature_ext(val_dataloader)\ntest_features, test_label = feature_ext(test_dataloader)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_label)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SiameseNetwork(nn.Module):\n\n    def __init__(self):\n        super(SiameseNetwork, self).__init__()\n\n        # Setting up the Fully Connected Layers\n        self.fc1 = nn.Sequential(\n            nn.Linear(2048, 1024),\n            nn.ReLU(inplace=True),\n            \n            nn.Linear(1024, 256),\n            nn.ReLU(inplace=True),\n            \n            nn.Linear(256,2)\n        )\n        \n    def forward_once(self, x):\n        # This function will be called for both images\n        # Its output is used to determine the similiarity\n        output = self.fc1(x)\n        return output\n\n    def forward(self, input1, input2):\n        # In this function we pass in both images and obtain both vectors\n        # which are returned\n        output1 = self.forward_once(input1)\n        output2 = self.forward_once(input2)\n\n        return output1, output2\nnet = SiameseNetwork().cuda()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = ContrastiveLoss()\noptimizer = optim.Adam(net.parameters(), lr = 0.001 )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class dataset(torch.utils.data.Dataset):\n    def __init__(self,imgs,labels,transform=None):\n        super(dataset).__init__()\n        self.imgs = imgs\n        self.label = labels\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.label)\n        \n    def __getitem__(self,idx):\n        label1 = self.label[idx]\n        img1 = self.imgs[idx]\n        same_class = random.randint(0,1) \n        if same_class:\n            while True:\n                idx_rand = random.randint(0,self.__len__()-1)\n                if idx == idx_rand:\n                    continue\n                label2 = self.label[idx_rand]\n                if label2 == label1 :\n                    img2 = self.imgs[idx_rand]\n                    break\n                \n        else:\n            while True:\n                idx_rand = random.randint(0,self.__len__()-1)\n                if idx == idx_rand:\n                    continue\n                label2 = self.label[idx_rand]\n                if label2 != label1 :\n                    img2 = self.imgs[idx_rand]\n                    break\n\n        if self.transform:\n            img1 = self.transform(img1)\n            img2 = self.transform(img2)\n\n        return img1,img2,torch.from_numpy(np.array([int(label1 != label2)], dtype=np.float32)),torch.tensor(label1),torch.tensor(label2)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transformation = transforms.Compose(\n[\n  transforms.ToTensor(),\n])\n\n\ntrain_dataset = dataset(train_features, train_label,)\nval_dataset = dataset(val_features, val_label,)\ntest_dataset = dataset(test_features, test_label,)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader = DataLoader(train_dataset,\n                        shuffle=True,\n                        batch_size=1)\n                        \n                        \nval_dataloader = DataLoader(val_dataset,\n                        shuffle=False,\n                        batch_size=1)\n\ntest_dataloader = DataLoader(test_dataset,\n                        shuffle=False,\n                        batch_size=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counter = []\nloss_history = [] \niteration_number= 0","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for epoch in range(3000):\n\n    # Iterate over batches\n    for i, (img0, img1, similarity,label0,label1) in enumerate(train_dataloader, 0):\n\n        # Send the images and labels to CUDA\n        img0, img1, similarity,label0,label1 = img0.cuda(), img1.cuda(), similarity.cuda(), label0.cuda(),label1.cuda()\n\n        # Zero the gradients\n        optimizer.zero_grad()\n\n        # Pass in the two images into the network and obtain two outputs\n        output1, output2 = net(img0, img1)\n\n        # Pass the outputs of the networks and label into the loss function\n        loss_contrastive = criterion(output1, output2, similarity)\n\n        # Calculate the backpropagation\n        loss_contrastive.backward()\n\n        # Optimize\n        optimizer.step()\n\n        # Every 10 batches print out the loss\n        if i % 10 == 0 :\n#             print(f\"Epoch number {epoch}\\n Current loss {loss_contrastive.item()}\\n\")\n            iteration_number += 10\n            counter.append(iteration_number)\n            loss_history.append(loss_contrastive.item())\n    print(epoch)\n            \n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_plot(iteration,loss):\n    plt.plot(iteration,loss)\n    plt.show()\nshow_plot(counter, loss_history)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import Counter\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meanF = 0\nmeanT = 0\ntest_counter = 0\ncorrect = 0\nfor i in range(len(val_dataset)):\n    ancher,_,_,label0,_ = val_dataset.__getitem__(i)\n    min_distance = 100000\n    prediction = -1\n    for j in range(len(train_dataset)):\n        if i == j:\n            continue\n        img,_,_,label1,_ = train_dataset.__getitem__(j)\n        output1, output2 = net(ancher.cuda(), img.cuda())\n        euclidean_distance = F.pairwise_distance(output1, output2)\n        distance = euclidean_distance.item()\n        if min_distance > distance:\n            min_distance = distance\n            prediction = label1\n        \n        if label0==label1:\n            meanT += euclidean_distance.item()\n        else:\n            meanF += euclidean_distance.item()\n    print(label0,prediction,min_distance)\n    test_counter += 1\n    if label0 == prediction:\n        correct += 1\n    \nprint(meanF,meanT)\nprint(correct/test_counter , correct, test_counter)\n#         imshow(torchvision.utils.make_grid(concatenated), f'Dissimilarity: {euclidean_distance.item():.2f}')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def sort(arr1, arr2):\n#     # Create pairs of values from arr1 and arr2\n#     pairs = list(zip(arr1, arr2))\n    \n#     # Sort pairs based on the values of arr1\n#     sorted_pairs = sorted(pairs, key=lambda x: x[0])\n    \n#     # Separate the sorted pairs back into sorted arrays\n#     sorted_arr1 = [pair[0] for pair in sorted_pairs]\n#     sorted_arr2 = [pair[1] for pair in sorted_pairs]\n    \n#     return sorted_arr1, sorted_arr2\n# def calculate_mode(arr):\n#     count_dict = {}\n    \n#     for num in arr:\n#         if num in count_dict:\n#             count_dict[num] += 1\n#         else:\n#             count_dict[num] = 1\n    \n#     mode = max(count_dict, key=count_dict.get)\n#     return mode\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meanF = 0\nmeanT = 0\ntest_counter = 0\ncorrect = 0\ntest_prediction =[]\nfor i in range(len(test_dataset)):\n    ancher,_,_,label0,_ = test_dataset.__getitem__(i)\n    min_distance = 100000\n    prediction = -1\n    for j in range(len(train_dataset)):\n        if i == j:\n            continue\n        img,_,_,label1,_ = train_dataset.__getitem__(j)\n        output1, output2 = net(ancher.cuda(), img.cuda())\n        euclidean_distance = F.pairwise_distance(output1, output2)\n        distance = euclidean_distance.item()\n        if min_distance > distance:\n            min_distance = distance\n            prediction = label1\n    test_prediction.append(prediction)\n    \nprint(test_prediction)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 2 beneign \n# 0 malignant \n# 1 normal\nsad = []\nfor i in test_prediction:\n    if i.item() == 0:\n        sad.append(\"beneign\")\n    if i.item() == 1:\n        sad.append(\"malignant\")\n    if i.item() == 2:\n        sad.append(\"normal\")\n\nsad","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs_names","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submn = pd.DataFrame({'filename' : test_imgs_names,'class': sad})\nsubmn.to_csv('my allah help us%.csv', index=False)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submn","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\n\nimg,_,_,_,_  = train_dataset.__getitem__(1) ## takes next batch from this dataloader which is just 1 sample\nimg2,_,_,_,_  =test_dataset.__getitem__(2) ## takes next batch from this dataloader which is just 1 sample\n\nimg = img.cuda()\nimg2 = img2.cuda()\n\nstart = time.time() ## record start time\n_ = net(img,img2)\nend = time.time() ## records end time\n \nprint(end - start)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}