{"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 torch\nfrom torch import nn\n\nclass MyNN_model(nn.Module):\n  def __init__(self, in2):\n    super().__init__()\n    self.layers= nn.Sequential(\n    nn.Linear(in2, 3),\n    nn.Sigmoid()\n    )\n  def forward(self, x):\n      return self.layers(x)","metadata":{"id":"0pNykk9drbXb","execution":{"iopub.status.busy":"2022-10-18T11:21:22.408869Z","iopub.execute_input":"2022-10-18T11:21:22.409475Z","iopub.status.idle":"2022-10-18T11:21:24.796042Z","shell.execute_reply.started":"2022-10-18T11:21:22.409342Z","shell.execute_reply":"2022-10-18T11:21:24.794125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"ujudRR4-9cSP","outputId":"e16c0653-445d-42d8-d565-53fdb196cbc6"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader\nfrom tqdm.auto import tqdm\nfrom PIL import Image\nimport glob\n\npath_train = \"../input/histopathologic-cancer-detection/train\"\n\npath_valid = \"../input/histopathologic-cancer-detection/test\"\n\n#To retrieve files names\n\n# images_train= glob.glob(path_train + \"/*.jpg\")\n# Images_valid= glob.glob(path_valid + \"/*.jpg\")","metadata":{"id":"_1zcjFYSsDED","execution":{"iopub.status.busy":"2022-10-18T11:21:24.799421Z","iopub.execute_input":"2022-10-18T11:21:24.801586Z","iopub.status.idle":"2022-10-18T11:21:24.810038Z","shell.execute_reply.started":"2022-10-18T11:21:24.801511Z","shell.execute_reply":"2022-10-18T11:21:24.808403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"from torchvision import transforms\nclass MyDataset(Dataset):\n  def __init__(self, image_files ):\n    self.transforms = transforms.Resize((224,224), Image.BICUBIC)\n    self. image_files = image_files\n  def __getitem__(self, idx):\n    img = Image.open(self. Image_files [idx])\n    img = self.transforms(img)\n    img = np.array(img)\n    img = transforms.ToTensor()(img)\n    return img\n  def __len__(self):\n    return len(self. image_files)\n\ndataset_train = MyDataset(path_train )\ndataloader = DataLoader(dataset_train, batch_size=64, shuffle=True)\ndataset_valid = MyDataset(path_valid )\ndataloader1 = DataLoader(dataset_valid, batch_size=64, shuffle=True)\"\"\"","metadata":{"id":"ZBxg91G7sqvK","outputId":"f77a09a1-9483-4ffd-d4a9-a2092d9e87d0","execution":{"iopub.status.busy":"2022-10-18T11:21:24.811777Z","iopub.execute_input":"2022-10-18T11:21:24.813196Z","iopub.status.idle":"2022-10-18T11:21:24.831603Z","shell.execute_reply.started":"2022-10-18T11:21:24.813145Z","shell.execute_reply":"2022-10-18T11:21:24.830048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model=MyNN_model()\n# criterion=nn.BCELoss()\n# optimizer =torch.optim.SGD(model.parameters(), lr=0.01)\n# for I,y in range(n_epochs):\n#   for img in tqdm(dataloader):\n#     y_pred = model(img)\n#     loss = criterion(y_pred, y)\n#   optimizer.zero_grad()\n#   loss.backward()\n#   optimizer.step()","metadata":{"id":"c59bCoxygzHx","execution":{"iopub.status.busy":"2022-10-18T11:21:24.835538Z","iopub.execute_input":"2022-10-18T11:21:24.836649Z","iopub.status.idle":"2022-10-18T11:21:24.845337Z","shell.execute_reply.started":"2022-10-18T11:21:24.836547Z","shell.execute_reply":"2022-10-18T11:21:24.843782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"num_classes = 2\n\nclass CNN(nn.Module):\n  def __init__(self, num_classes):\n    super(CNN, self).__init__()\n    #first Convolutional layers\n    self.cnn1=nn.Conv2d(in_channels=3, out_channels=16,kernel_size=3, padding=0)\n    self.maxpool1=nn.MaxPool2d(kernel_size=2, stride=1)\n    #second Convolutional layers\n    self.cnn2=nn.Conv2d(in_channels=16, out_channels=32, kernel_size=2,stride=1,padding=0)\n    self.maxpool2=nn.MaxPool2d(kernel_size=2 ,stride=1)\n    #max pooling\n    #fully connected layer\n    self.fc1=nn.Linear(1534752, num_classes)\n  def forward(self, x):\n    #first Convolutional layers\n    x=self.cnn1(x)\n    #activation function\n    x=torch.relu(x)\n    #max pooling\n    x=self.maxpool1(x)\n    #first Convolutional layers\n    x=self.cnn2(x)\n    #activation function\n    x=torch.relu(x)\n    #max pooling\n    x=self.maxpool2(x)\n    #flatten output\n    x=x.view(x.size(0),-1)\n    #fully connected layer\n    x=self.fc1(x)\n    return x \"\"\"\n  ","metadata":{"id":"KggQXh4UhvuU","outputId":"1804c8a4-9c7f-48d1-b4c6-f7cd42c877f1","execution":{"iopub.status.busy":"2022-10-18T11:21:24.847366Z","iopub.execute_input":"2022-10-18T11:21:24.848089Z","iopub.status.idle":"2022-10-18T11:21:24.862592Z","shell.execute_reply.started":"2022-10-18T11:21:24.848031Z","shell.execute_reply":"2022-10-18T11:21:24.860934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"BG8khGLEluzO"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision\nfrom torchvision.models import resnet50\nclass Identity(nn.Module):\n  def __init__(self):\n    super(Identity, self).__init__()\n  def forward(self, x):\n    return x\n\n# Model\nclass MyModel(nn.Module):\n  def __init__(self, num_classes):\n    super().__init__()\n    resnet50 = torchvision.models.resnet50(pretrained=True)\n    resnet50.fc = Identity()\n    self.encoder = resnet50\n    self.head = nn.Sequential(nn.Linear(2048, 1024),\n    nn.ReLU(),\n    nn.Linear(1024, num_classes)\n    )\n\n  def forward(self, x):\n    encoding = self.encoder(x)\n    pred = self.head(encoding)\n    return pred","metadata":{"id":"QPdb_48PmXmw","execution":{"iopub.status.busy":"2022-10-18T11:21:24.864686Z","iopub.execute_input":"2022-10-18T11:21:24.865323Z","iopub.status.idle":"2022-10-18T11:21:25.245688Z","shell.execute_reply.started":"2022-10-18T11:21:24.865278Z","shell.execute_reply":"2022-10-18T11:21:25.244234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import transforms\ntransformations = transforms.Compose([\n    transforms.Resize(255),\n    transforms.CenterCrop(224),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n])","metadata":{"id":"R_dgXKNgm7K6","execution":{"iopub.status.busy":"2022-10-18T11:21:25.247852Z","iopub.execute_input":"2022-10-18T11:21:25.248421Z","iopub.status.idle":"2022-10-18T11:21:25.25667Z","shell.execute_reply.started":"2022-10-18T11:21:25.248365Z","shell.execute_reply":"2022-10-18T11:21:25.254894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport PIL\nclass CustomDataset(torch.utils.data.Dataset):\n    def __init__(self, csv_path, images_folder, transform = None):\n        self.df = pd.read_csv(csv_path)\n        self.images_folder = images_folder\n        self.transform = transform\n        self.class2index = {\"0\":0, \"1\":1}\n\n    def __len__(self):\n        return len(self.df)\n    def __getitem__(self, index):\n        filename = self.df.loc[index][\"id\"]\n        label = int(self.df.loc[index][\"label\"])\n        image = PIL.Image.open(os.path.join(self.images_folder, filename+'.tif'))\n        if self.transform is not None:\n            image = self.transform(image)\n        return image, label\n\ntrain_dataset = CustomDataset(\"../input/histopathologic-cancer-detection/train_labels.csv\", \"../input/histopathologic-cancer-detection/train\",transformations)\ntest_dataset = CustomDataset(\"../input/histopathologic-cancer-detection/sample_submission.csv\", \"../input/histopathologic-cancer-detection/test\",transformations)\n\ntrain_dataset[0]\n\n#print(f\"Train data:\\n{train_data}\\nTest data:\\n{test_data}","metadata":{"id":"ZRhfJsfvnK0E","execution":{"iopub.status.busy":"2022-10-18T11:21:25.259197Z","iopub.execute_input":"2022-10-18T11:21:25.260585Z","iopub.status.idle":"2022-10-18T11:21:26.080584Z","shell.execute_reply.started":"2022-10-18T11:21:25.260513Z","shell.execute_reply":"2022-10-18T11:21:26.079117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndataloader = DataLoader(train_dataset, batch_size=64, shuffle=True)\n\ndataloader1 = DataLoader(test_dataset, batch_size=64, shuffle=True)","metadata":{"id":"Q-uU3bQ1ppip","execution":{"iopub.status.busy":"2022-10-18T11:21:26.081936Z","iopub.execute_input":"2022-10-18T11:21:26.08234Z","iopub.status.idle":"2022-10-18T11:21:26.090052Z","shell.execute_reply.started":"2022-10-18T11:21:26.082306Z","shell.execute_reply":"2022-10-18T11:21:26.088125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=MyModel(1)\ncriterion=torch.nn.BCEWithLogitsLoss()\n#criterion=nn.CrossEntropyLoss() # for multi\noptimizer =torch.optim.SGD(model.parameters(), lr=0.01)\nfor i in range(1): \n    for img,y in tqdm(dataloader):\n      y_pred = model(img)\n      y = y.unsqueeze(1).float()\n      loss = criterion(y_pred, y)\n      optimizer.zero_grad()\n      loss.backward()\n      optimizer.step()\n    model.eval()\n    for img,y in tqdm(dataloader1):\n      y_pred = model(img)\n      y = y.unsqueeze(1).float()\n      val_loss = criterion(y_pred, y)\n      \n","metadata":{"id":"S9xfkcE3sbzr","outputId":"898547cb-76f2-43e9-c95b-8620e002ebe7","execution":{"iopub.status.busy":"2022-10-18T11:21:26.0941Z","iopub.execute_input":"2022-10-18T11:21:26.094665Z"},"trusted":true},"execution_count":null,"outputs":[]}]}