{"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":"import sys\nimport os\nimport sys\nimport json\nimport math\nimport random\nimport cv2\ntimm_path = \"../input/timm-pytorch-image-models/pytorch-image-models-master\"\nsys.path.append(timm_path)\nimport timm\nfrom timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn import model_selection\nfrom sklearn.metrics import mean_squared_error\nfrom tqdm.notebook import tqdm\nimport random\nimport glob\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset,DataLoader\nfrom torch import optim\nfrom torchvision import transforms\nfrom transformers import  get_cosine_schedule_with_warmup\nimport warnings\nwarnings.filterwarnings('ignore')\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"_uuid":"159303a1-fb2a-4ad1-a63c-971e5b737131","_cell_guid":"b063e629-f50a-4c6b-b51f-4a2fcc538140","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:42.686767Z","iopub.execute_input":"2023-09-01T14:19:42.687165Z","iopub.status.idle":"2023-09-01T14:19:57.512440Z","shell.execute_reply.started":"2023-09-01T14:19:42.687129Z","shell.execute_reply":"2023-09-01T14:19:57.511433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed):\n    random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    if torch.cuda.is_available():\n        torch.cuda.manual_seed_all(seed)\n        torch.backends.cudnn.deterministic = True\nset_seed(42)","metadata":{"_uuid":"cf3582c0-0996-4c18-9f19-5d4b40370be7","_cell_guid":"e5878bd3-bcf7-413d-8198-7f71b4f0200e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:57.515601Z","iopub.execute_input":"2023-09-01T14:19:57.516878Z","iopub.status.idle":"2023-09-01T14:19:57.529859Z","shell.execute_reply.started":"2023-09-01T14:19:57.516848Z","shell.execute_reply":"2023-09-01T14:19:57.528846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/train.csv')","metadata":{"_uuid":"08447492-158d-407c-a54d-7aff8147c3b6","_cell_guid":"3a1c48a2-85c7-46bb-a465-7464d26c6481","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:57.533391Z","iopub.execute_input":"2023-09-01T14:19:57.535743Z","iopub.status.idle":"2023-09-01T14:19:57.627312Z","shell.execute_reply.started":"2023-09-01T14:19:57.535715Z","shell.execute_reply":"2023-09-01T14:19:57.626338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"_uuid":"3bbf8157-5b50-493b-b0e8-30b7a31307aa","_cell_guid":"b407c6dc-8637-465b-a7e6-0d976c00399b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:57.631799Z","iopub.execute_input":"2023-09-01T14:19:57.632106Z","iopub.status.idle":"2023-09-01T14:19:57.698701Z","shell.execute_reply.started":"2023-09-01T14:19:57.632066Z","shell.execute_reply":"2023-09-01T14:19:57.697787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['sex'] = df['sex'].map({'male': 1, 'female': 0})\ndf['sex'] = df['sex'].fillna(-1)\n\n\ndf['age_approx'] /= 90\ndf['age_approx'] = df['age_approx'].fillna(0)\n\n\ndf['n_images'] = df.patient_id.map(df.groupby(['patient_id']).image_name.count())\ndf.loc[df['patient_id'] == -1, 'n_images'] = 1","metadata":{"_uuid":"f187e19b-4173-4881-8ea9-14f75bdbb026","_cell_guid":"517a558f-08dc-4fcd-95c0-6337f4532dcc","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:57.700010Z","iopub.execute_input":"2023-09-01T14:19:57.700395Z","iopub.status.idle":"2023-09-01T14:19:57.738023Z","shell.execute_reply.started":"2023-09-01T14:19:57.700362Z","shell.execute_reply":"2023-09-01T14:19:57.737135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['path'] = [f\"/kaggle/input/siic-isic-224x224-images/train/{x}.png\" for x in df[\"image_name\"].values]\ndense_features = [\n    'sex', 'age_approx', 'n_images'\n]","metadata":{"_uuid":"9c366400-0b29-4f7d-a6d8-2eeb7255f6e5","_cell_guid":"fb3897d7-3c74-4193-8845-bf0179f76af4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:57.739234Z","iopub.execute_input":"2023-09-01T14:19:57.739975Z","iopub.status.idle":"2023-09-01T14:19:57.757494Z","shell.execute_reply.started":"2023-09-01T14:19:57.739940Z","shell.execute_reply":"2023-09-01T14:19:57.756541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"strat_kfold = model_selection.StratifiedGroupKFold(n_splits=5, random_state=42, shuffle=True)\n\n# Create an empty 'fold' column in the DataFrame\ndf['fold'] = -1  # Initialize with a default value\n\nfor i, (_, train_index) in enumerate(strat_kfold.split(df.index, y=df['target'], groups=df['patient_id'])):\n    df.loc[df.index[train_index], 'fold'] = i\n\ndf['fold'] = df['fold'].astype('int')","metadata":{"_uuid":"45809ab1-0190-4eef-a9bd-5dd06a65617a","_cell_guid":"f10b6296-5acd-4961-83b8-5071c99fce42","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:57.759707Z","iopub.execute_input":"2023-09-01T14:19:57.760050Z","iopub.status.idle":"2023-09-01T14:19:58.746658Z","shell.execute_reply.started":"2023-09-01T14:19:57.760019Z","shell.execute_reply":"2023-09-01T14:19:58.745636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"_uuid":"5d047a4b-a94b-4c2e-b760-2e3e6973059b","_cell_guid":"c7bb0f97-c14e-4d94-987c-be3060b3be84","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:58.747960Z","iopub.execute_input":"2023-09-01T14:19:58.748503Z","iopub.status.idle":"2023-09-01T14:19:58.770198Z","shell.execute_reply.started":"2023-09-01T14:19:58.748469Z","shell.execute_reply":"2023-09-01T14:19:58.769335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sex.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-09-01T14:19:58.771419Z","iopub.execute_input":"2023-09-01T14:19:58.771687Z","iopub.status.idle":"2023-09-01T14:19:58.782296Z","shell.execute_reply.started":"2023-09-01T14:19:58.771664Z","shell.execute_reply":"2023-09-01T14:19:58.781170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.n_images = df.n_images/df.n_images.max()","metadata":{"execution":{"iopub.status.busy":"2023-09-01T14:19:58.788147Z","iopub.execute_input":"2023-09-01T14:19:58.788517Z","iopub.status.idle":"2023-09-01T14:19:58.794555Z","shell.execute_reply.started":"2023-09-01T14:19:58.788464Z","shell.execute_reply":"2023-09-01T14:19:58.793341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 224\ntrain_aug = A.Compose(\n    [   A.RandomResizedCrop(image_size,image_size,p= 0.8),\n        A.Resize(image_size,image_size,p=1.0),\n        A.HorizontalFlip(p=0.5),   \n        A.RandomBrightnessContrast(p=0.5),\n        A.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.1, rotate_limit=30, p=0.5),\n          \n   A.Normalize(mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD),\n        ToTensorV2()\n    ]\n)\nval_aug = A.Compose(\n    [ \n     A.Resize(image_size,image_size,p=1.0),\n        A.Normalize(mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD),\n        ToTensorV2()\n    ]\n)","metadata":{"_uuid":"371b8c63-e79e-46d3-8350-cf10596f3c08","_cell_guid":"61fa726f-690c-40d5-8434-25d20a37ae60","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:58.796504Z","iopub.execute_input":"2023-09-01T14:19:58.796866Z","iopub.status.idle":"2023-09-01T14:19:58.808772Z","shell.execute_reply.started":"2023-09-01T14:19:58.796834Z","shell.execute_reply":"2023-09-01T14:19:58.807747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Op4bio_data(Dataset):\n    def __init__(self,df, augs):\n        self.df = df\n        self.augs = augs\n    \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        \n        img_src = self.df.loc[idx,'path']\n        image = cv2.imread(img_src)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        \n        transformed = self.augs(image=image)\n        image = transformed['image']\n        \n        meta = self.df[dense_features].iloc[idx, :].values\n        \n        label  = self.df['target'][idx]\n        \n        return image , torch.FloatTensor(meta) , label","metadata":{"_uuid":"586582cf-ad04-4a7d-8836-20284ce1de92","_cell_guid":"6a4f4db2-6cbd-4f5b-891f-2f516d60cb28","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:58.810434Z","iopub.execute_input":"2023-09-01T14:19:58.810855Z","iopub.status.idle":"2023-09-01T14:19:58.820671Z","shell.execute_reply.started":"2023-09-01T14:19:58.810823Z","shell.execute_reply":"2023-09-01T14:19:58.819715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_data = Op4bio_data(df, augs = train_aug)","metadata":{"execution":{"iopub.status.busy":"2023-09-01T14:19:58.822214Z","iopub.execute_input":"2023-09-01T14:19:58.822627Z","iopub.status.idle":"2023-09-01T14:19:58.832405Z","shell.execute_reply.started":"2023-09-01T14:19:58.822594Z","shell.execute_reply":"2023-09-01T14:19:58.831480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_data[11]","metadata":{"execution":{"iopub.status.busy":"2023-09-01T14:19:58.833958Z","iopub.execute_input":"2023-09-01T14:19:58.834360Z","iopub.status.idle":"2023-09-01T14:19:59.000140Z","shell.execute_reply.started":"2023-09-01T14:19:58.834328Z","shell.execute_reply":"2023-09-01T14:19:58.999165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Model(nn.Module):\n    def __init__(self,pretrained = True):\n        super().__init__()\n        self.backbone = timm.create_model('tf_efficientnet_b2_ns', pretrained=pretrained, num_classes=0, drop_rate=0.1,global_pool='',in_chans=3)\n        self.pool = nn.AdaptiveAvgPool2d(1)\n        self.lin = nn.Linear(1408,64)\n        self.norm = nn.BatchNorm1d(1408)\n        self.do = nn.Dropout(p=0.4)\n    \n    def forward(self,image ):\n        image = self.backbone(image)     \n        image = self.pool(image)\n        image = image.view(image.shape[0], -1)\n        image = self.norm(image)\n        x = self.lin(image)\n        return x","metadata":{"execution":{"iopub.status.busy":"2023-09-01T14:19:59.002067Z","iopub.execute_input":"2023-09-01T14:19:59.002508Z","iopub.status.idle":"2023-09-01T14:19:59.010474Z","shell.execute_reply.started":"2023-09-01T14:19:59.002475Z","shell.execute_reply":"2023-09-01T14:19:59.009337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedding_size = 64\n\nclass Lenet(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.cnn1 = nn.Conv2d(3, 6, kernel_size=5)\n        self.cnn2 = nn.Conv2d(6, 16, kernel_size=5)\n        \n        self.pool1 = nn.AvgPool2d(kernel_size=2, stride=2)\n        self.pool2 = nn.AvgPool2d(kernel_size=2, stride=2)\n        \n        self.relu1 = nn.ReLU()\n        self.relu2 = nn.ReLU()\n\n        # Calculate the input size for the fully connected layers\n        self.fc_input_size = 16 * 53 * 53  # This is for 224x224 input images\n        \n        self.fc1 = nn.Linear(self.fc_input_size, 120)\n        self.fc2 = nn.Linear(120, embedding_size)\n    \n    def forward(self, x):\n        x = self.relu1(self.cnn1(x))\n        x = self.pool1(x)\n        x = self.relu2(self.cnn2(x))\n        x = self.pool2(x)\n        x = x.view(x.size(0), -1)\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n\n        return x\n\n    \nclass LogisticRegression(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.layer = nn.Sequential(\n            nn.Linear(3, embedding_size),\n            nn.LeakyReLU(),\n            nn.BatchNorm1d(embedding_size),\n            nn.Linear( embedding_size,embedding_size),\n        )\n        \n        self.sigmoid = nn.Sigmoid()\n        \n    def forward(self, x):\n        x = x.squeeze(1)\n        x = self.layer(x)\n        #x = self.sigmoid(x)\n        return x\n    \n    \nclass CLIPModel(nn.Module):\n    def __init__(self, model1, model2):\n        super(CLIPModel, self).__init__()\n        self.model1 = model1  # Lenet\n        self.model2 = model2  # Logistic\n        self.t = nn.Parameter(torch.Tensor([1]))  # Learnable parameter\n\n    def forward(self, x, y):\n        out1 = self.model1(x)\n        out2 = self.model2(y)\n        \n        #print(out1)\n        #print(out2)\n\n        # Apply L2 normalization\n        out1_norm = F.normalize(out1, p=2, dim=1)\n        out2_norm = F.normalize(out2, p=2, dim=1)\n        \n        out1_norm = out1_norm.to(out2_norm.dtype)  # Cast out1_norm to the same dtype as out2_norm\n\n\n        # Compute dot product\n        dot_product = torch.matmul(out1_norm, out2_norm.t())\n\n        # Apply exponential weighting\n        dot_product_weighted = torch.exp(self.t) * dot_product\n\n        return dot_product_weighted","metadata":{"_uuid":"e2a54b70-5b08-4863-838f-9638706b0bfe","_cell_guid":"b6af3f44-1baa-4e32-8b87-a2234f2c0939","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:59.012026Z","iopub.execute_input":"2023-09-01T14:19:59.012654Z","iopub.status.idle":"2023-09-01T14:19:59.029009Z","shell.execute_reply.started":"2023-09-01T14:19:59.012622Z","shell.execute_reply":"2023-09-01T14:19:59.028151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AverageMeter(object):\n    \"\"\"Computes and stores the average and current value\"\"\"\n    def __init__(self):\n        self.reset()\n\n    def reset(self):\n        self.val = 0\n        self.avg = 0\n        self.sum = 0\n        self.count = 0\n\n    def update(self, val, n=1):\n        self.val = val\n        self.sum += val * n\n        self.count += n\n        self.avg = self.sum / self.count","metadata":{"_uuid":"b23c516d-0f98-481b-9055-41aea58a9031","_cell_guid":"27ff72c8-4c14-49be-9b2e-e4de36862798","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:59.031635Z","iopub.execute_input":"2023-09-01T14:19:59.031895Z","iopub.status.idle":"2023-09-01T14:19:59.044493Z","shell.execute_reply.started":"2023-09-01T14:19:59.031872Z","shell.execute_reply":"2023-09-01T14:19:59.043590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_one_epoch(train_loader,model,optimizer,criterion,e,epochs,device):\n    '''Trains the model for a single epoch and returns Loss,Accuracy, AUC for that epoch'''\n\n    losses = AverageMeter()\n    model.train()\n    global_step = 0\n    loop = tqdm(enumerate(train_loader),total = len(train_loader))\n    \n    for step,(image,tabular,l) in loop:\n        image = image.to(device)\n        tabular = tabular.to(device)\n        \n        batch_size = l.size(0)\n\n        output = model(image,tabular)\n        \n        #print(output)\n        \n        labels = torch.arange(batch_size) \n        labels = labels.to(device)\n        \n        loss1 = criterion(output, labels)\n        loss2 = criterion(output.T, labels)\n        loss = (loss1 + loss2) / 2.0\n        \n        losses.update(loss.item(), batch_size)\n\n        loss.backward()\n         # Clip gradients to avoid gradient explosion\n        torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)  # Adjust max_norm value as needed\n        \n        \n        optimizer.step()\n        optimizer.zero_grad()\n\n        global_step += 1\n\n        loop.set_description(f\"Epoch {e+1}/{epochs}\")\n        loop.set_postfix(model_loss = loss.item() ,stage = 'train')        \n\n    return losses.avg","metadata":{"_uuid":"ceb7a311-2a5f-4cb5-b710-b6c28eaaa1cf","_cell_guid":"f6ead9da-d31a-424e-a14e-677752dfd77b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:59.046022Z","iopub.execute_input":"2023-09-01T14:19:59.046652Z","iopub.status.idle":"2023-09-01T14:19:59.063931Z","shell.execute_reply.started":"2023-09-01T14:19:59.046621Z","shell.execute_reply":"2023-09-01T14:19:59.062956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def val_one_epoch(loader,model,criterion,device):\n    '''Validates the model for a single epoch and returns Loss,Accuracy, AUC for that epoch'''\n    losses = AverageMeter()\n    model.eval()\n    global_step = 0\n    loop = tqdm(enumerate(loader),total = len(loader))\n    \n    for step,(image,tabular,l) in loop:\n        image = image.to(device)\n        tabular = tabular.to(device)\n        batch_size = l.size(0)\n        \n        labels = torch.arange(batch_size) \n        labels = labels.to(device)\n        \n        with torch.no_grad():\n            output = model(image,tabular)\n\n        loss1 = criterion(output, labels)\n        loss2 = criterion(output.T, labels)\n        loss = (loss1 + loss2) / 2.0        \n        losses.update(loss.item(), batch_size)\n        loop.set_postfix(model_loss = loss.item() ,stage = 'val')\n        global_step += 1\n\n    return losses.avg","metadata":{"_uuid":"350bd546-2097-46c0-bac7-4b839a96633a","_cell_guid":"6f127cfc-ecae-4de0-9f4d-0f7deb1d110b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:59.066206Z","iopub.execute_input":"2023-09-01T14:19:59.067290Z","iopub.status.idle":"2023-09-01T14:19:59.079544Z","shell.execute_reply.started":"2023-09-01T14:19:59.067255Z","shell.execute_reply":"2023-09-01T14:19:59.078662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fit(t_loader ,v_loader, model, OUTPUT_DIR,device,optimizer):\n    \n    T_LOSS1 = []\n    V_LOSS1 = []\n    model.to(device)\n    #model.to(device)\n    \n    criterion = nn.CrossEntropyLoss() # Loss function\n    optimizer = optimizer\n    \n    epochs = 100\n\n    loop = range(epochs)\n    for e in loop:\n      \n        loss = train_one_epoch(t_loader,model,optimizer,criterion,e,epochs,device)\n        \n        print(f'For epoch {e+1}/{epochs}')\n        print(f'average model train_loss {loss}')\n        \n        T_LOSS1.append(loss)\n\n        val_loss = val_one_epoch(v_loader,model,criterion,device)\n        \n        print(f'average model val_loss {val_loss}')\n        \n        V_LOSS1.append(val_loss)\n\n        if (e == epochs -1):\n            torch.save(model.state_dict(),OUTPUT_DIR+ f'model_val_loss {val_loss}.pth')\n\n    return T_LOSS1,V_LOSS1","metadata":{"_uuid":"e97a342b-2e7f-447a-90f8-87f23684d9e8","_cell_guid":"6f2a2a46-9dcb-4652-b559-22b1bad01f21","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:59.082285Z","iopub.execute_input":"2023-09-01T14:19:59.083416Z","iopub.status.idle":"2023-09-01T14:19:59.097389Z","shell.execute_reply.started":"2023-09-01T14:19:59.083381Z","shell.execute_reply":"2023-09-01T14:19:59.096521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data= df[df.fold != 0]\nval_data  = df[df.fold == 0]\n    \nt_data = Op4bio_data(train_data.reset_index(drop=True) , augs = train_aug)\nv_data = Op4bio_data(val_data.reset_index(drop=True) , augs = val_aug)\n\n\nt_loader = DataLoader(t_data, shuffle=True,\n                        num_workers=2,\n                        batch_size=16*6,drop_last =True)\n\nv_loader = DataLoader(v_data, shuffle=False,\n                        num_workers=2,\n                        batch_size=16*8,drop_last =False)","metadata":{"_uuid":"5d351733-e22e-4066-8336-90ba48fea98c","_cell_guid":"683918f5-50ee-4830-a8fb-29efcb24c08f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:59.098864Z","iopub.execute_input":"2023-09-01T14:19:59.099218Z","iopub.status.idle":"2023-09-01T14:19:59.117297Z","shell.execute_reply.started":"2023-09-01T14:19:59.099188Z","shell.execute_reply":"2023-09-01T14:19:59.116172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OUTPUT_DIR = './'\nif not os.path.exists(OUTPUT_DIR):\n    os.makedirs(OUTPUT_DIR)","metadata":{"_uuid":"e3782ec8-9e01-4ea0-8bab-bf8c6ac124ff","_cell_guid":"919fdf4b-3e76-4a0c-a743-47b75c4c2649","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:59.118834Z","iopub.execute_input":"2023-09-01T14:19:59.119825Z","iopub.status.idle":"2023-09-01T14:19:59.124244Z","shell.execute_reply.started":"2023-09-01T14:19:59.119799Z","shell.execute_reply":"2023-09-01T14:19:59.123324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 =  Model()\nmodel2 =  LogisticRegression()","metadata":{"_uuid":"8692c7b3-486d-4132-8130-f854454a8065","_cell_guid":"c6d42433-5f59-4a16-a8a8-3b75b44a3ba2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:19:59.125750Z","iopub.execute_input":"2023-09-01T14:19:59.126394Z","iopub.status.idle":"2023-09-01T14:20:00.028768Z","shell.execute_reply.started":"2023-09-01T14:19:59.126363Z","shell.execute_reply":"2023-09-01T14:20:00.027837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combined_model = CLIPModel(model1, model2)\noptimizer = optim.AdamW(combined_model.parameters(), lr=1e-6 , weight_decay = 1e-5 ) \nT_LOSS1, V_LOSS1= fit(t_loader ,v_loader, combined_model, OUTPUT_DIR,device,optimizer )","metadata":{"_uuid":"e7003288-813c-499f-af29-fc890505773f","_cell_guid":"7050e2ab-4583-4cf8-aac9-db22a96bff84","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T14:20:00.030207Z","iopub.execute_input":"2023-09-01T14:20:00.030643Z","iopub.status.idle":"2023-09-01T15:27:51.503579Z","shell.execute_reply.started":"2023-09-01T14:20:00.030610Z","shell.execute_reply":"2023-09-01T15:27:51.502007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Generate x-axis values\nepochs = len(T_LOSS1)\nx = list(range(1, epochs + 1))\n\n# Plot training loss\nplt.plot(x, T_LOSS1, label='Training Loss')\n\n# Plot validation loss\nplt.plot(x, V_LOSS1, label='Validation Loss')\n\n# Set plot labels and title\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss')\nplt.legend()\n\n# Show the plot\nplt.show()","metadata":{"_uuid":"cb74c080-fee6-4a9e-943f-afabd1d43e0c","_cell_guid":"41ebf9dd-8e4d-4bb4-a147-0852c8740488","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-09-01T15:27:51.505205Z","iopub.status.idle":"2023-09-01T15:27:51.505978Z","shell.execute_reply.started":"2023-09-01T15:27:51.505733Z","shell.execute_reply":"2023-09-01T15:27:51.505757Z"},"trusted":true},"execution_count":null,"outputs":[]}]}