{"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"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os \nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nimport pickle\n\nimport torch\nimport torchvision\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import datasets, transforms\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-25T06:14:44.662188Z","iopub.execute_input":"2023-11-25T06:14:44.662523Z","iopub.status.idle":"2023-11-25T06:14:48.826316Z","shell.execute_reply.started":"2023-11-25T06:14:44.662497Z","shell.execute_reply":"2023-11-25T06:14:48.825305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hcd = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/train_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:14:48.831561Z","iopub.execute_input":"2023-11-25T06:14:48.831810Z","iopub.status.idle":"2023-11-25T06:14:49.151549Z","shell.execute_reply.started":"2023-11-25T06:14:48.831788Z","shell.execute_reply":"2023-11-25T06:14:49.150709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose( # Doing transforms\n    [transforms.ToTensor(), # to tensor object\n     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) # mean = 0.5, std = 0.5","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:14:49.152651Z","iopub.execute_input":"2023-11-25T06:14:49.152912Z","iopub.status.idle":"2023-11-25T06:14:49.157688Z","shell.execute_reply.started":"2023-11-25T06:14:49.152889Z","shell.execute_reply":"2023-11-25T06:14:49.156825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = datasets.ImageFolder(root='/kaggle/input/histopathologic-cancer-detection', transform=transform)\n#defining the dataset\n#loading the dataset \ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True, num_workers=0, drop_last=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:14:49.161335Z","iopub.execute_input":"2023-11-25T06:14:49.161977Z","iopub.status.idle":"2023-11-25T06:19:13.635667Z","shell.execute_reply.started":"2023-11-25T06:14:49.161942Z","shell.execute_reply":"2023-11-25T06:19:13.633174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = ('0', '1')","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:19:13.637628Z","iopub.execute_input":"2023-11-25T06:19:13.644280Z","iopub.status.idle":"2023-11-25T06:19:13.648083Z","shell.execute_reply.started":"2023-11-25T06:19:13.644240Z","shell.execute_reply":"2023-11-25T06:19:13.647111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nimg_height = 180\nimg_width = 180","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:19:13.649660Z","iopub.execute_input":"2023-11-25T06:19:13.650182Z","iopub.status.idle":"2023-11-25T06:19:13.662180Z","shell.execute_reply.started":"2023-11-25T06:19:13.650154Z","shell.execute_reply":"2023-11-25T06:19:13.661093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def imshow(img):\n    img = img / 2 + 0.5     # unnormalize\n    npimg = img.numpy()\n    plt.imshow(np.transpose(npimg, (1, 2, 0)))\n    plt.show()\n\n\n# get some random training images\ndataiter = iter(train_loader)\nimages, labels = next(dataiter)\n\n# show images\nimshow(torchvision.utils.make_grid(images))\n# print labels\nprint(' '.join(f'{classes[labels[j]]:5s}' for j in range(batch_size)))","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:19:13.664443Z","iopub.execute_input":"2023-11-25T06:19:13.664918Z","iopub.status.idle":"2023-11-25T06:19:14.441323Z","shell.execute_reply.started":"2023-11-25T06:19:13.664880Z","shell.execute_reply":"2023-11-25T06:19:14.440429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Place this function at the beginning of your notebook\ndef conv_output_size(input_size, filter_size, stride=1, padding=0, pool_filter=2, pool_stride=2):\n    output_size = (input_size - filter_size + 2 * padding) / stride + 1\n    output_size = (output_size - pool_filter) / pool_stride + 1\n    return int(output_size)\n\n# Use the function to calculate the input size for fc1\ninput_size = 180  # Assuming this is your image size\nsize_after_first_layer = conv_output_size(input_size, 5)\nsize_after_second_layer = conv_output_size(size_after_first_layer, 5)\n\n# Update this value in your Net class\nfc1_input_size = 16 * size_after_second_layer * size_after_second_layer","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:19:14.442503Z","iopub.execute_input":"2023-11-25T06:19:14.442793Z","iopub.status.idle":"2023-11-25T06:19:14.448710Z","shell.execute_reply.started":"2023-11-25T06:19:14.442767Z","shell.execute_reply":"2023-11-25T06:19:14.447891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Net(nn.Module):\n    ''' Models a simple Convolutional Neural Network'''\n\n    def __init__(self):\n        ''' initialize the network '''\n        super(Net, self).__init__()\n        # 3 input image channel, 6 output channels, 5x5 square convolution kernel\n        self.conv1 = nn.Conv2d(3, 6, 5)\n        # Max pooling over a (2, 2) window\n        self.pool = nn.MaxPool2d(2, 2)\n        self.conv2 = nn.Conv2d(6, 16, 5)\n        # Corrected input size for the first fully connected layer\n        self.fc1 = nn.Linear(16 * 21 * 21, 120)\n        self.fc2 = nn.Linear(120, 84)\n        self.fc3 = nn.Linear(84, 2)  # Two output units for binary classification\n\n    def forward(self, x):\n        x = self.pool(F.relu(self.conv1(x)))\n        x = self.pool(F.relu(self.conv2(x)))\n        x = x.view(-1, 16 * 21 * 21)  # Corrected view operation\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = self.fc3(x)\n        return x\n\nnet = Net()","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:19:14.449949Z","iopub.execute_input":"2023-11-25T06:19:14.450223Z","iopub.status.idle":"2023-11-25T06:19:14.476556Z","shell.execute_reply.started":"2023-11-25T06:19:14.450199Z","shell.execute_reply":"2023-11-25T06:19:14.475663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:19:14.477499Z","iopub.execute_input":"2023-11-25T06:19:14.477737Z","iopub.status.idle":"2023-11-25T06:19:14.482129Z","shell.execute_reply.started":"2023-11-25T06:19:14.477715Z","shell.execute_reply":"2023-11-25T06:19:14.481313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for epoch in range(2):  # loop over the dataset multiple times\n\n    running_loss = 0.0\n    for i, data in enumerate(train_loader, 0):\n        # get the inputs; data is a list of [inputs, labels]\n        inputs, labels = data\n\n        # zero the parameter gradients\n        optimizer.zero_grad()\n\n        # forward + backward + optimize\n        outputs = net(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        # print statistics\n        running_loss += loss.item()\n        if i % 2000 == 1999:    # print every 2000 mini-batches\n            print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')\n            running_loss = 0.0\n\nprint('Finished Training')","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:19:14.483539Z","iopub.execute_input":"2023-11-25T06:19:14.483847Z","iopub.status.idle":"2023-11-25T06:20:36.688206Z","shell.execute_reply.started":"2023-11-25T06:19:14.483824Z","shell.execute_reply":"2023-11-25T06:20:36.686891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Specify model name\nmodel_name = \"model_pt.pth\"\n# Use torch.save to save the model state\ntorch.save(net.state_dict(), f'/kaggle/working/{model_name}')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for epoch in range(2):  # loop over the dataset multiple times\n#     running_loss = 0.0\n#     for i, data in enumerate(train_loader, 0):\n#         inputs, labels = data\n\n#         # Additional debugging line\n#         print(\"Inputs shape:\", inputs.shape)\n\n#         optimizer.zero_grad()\n#         outputs = net(inputs)\n\n#         print(\"Outputs shape:\", outputs.shape, \"Labels shape:\", labels.shape)\n\n#         loss = criterion(outputs, labels)\n#         loss.backward()\n#         optimizer.step()\n#         # zero the parameter gradients\n#         optimizer.zero_grad()\n\n#         # forward + backward + optimize\n#         outputs = net(inputs)\n\n#         # Debugging line: print the shapes of outputs and labels\n#         print(\"Outputs shape:\", outputs.shape, \"Labels shape:\", labels.shape)\n\n#         loss = criterion(outputs, labels)\n#         loss.backward()\n#         optimizer.step()\n\n#         # print statistics\n#         running_loss += loss.item()\n#         if i % 2000 == 1999:    # print every 2000 mini-batches\n#             print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')\n#             running_loss = 0.0\n\n# print('Finished Training')","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:20:36.689619Z","iopub.status.idle":"2023-11-25T06:20:36.690000Z","shell.execute_reply.started":"2023-11-25T06:20:36.689826Z","shell.execute_reply":"2023-11-25T06:20:36.689844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def conv_output_size(input_size, filter_size, stride=1, padding=0, pool_filter=2, pool_stride=2):\n#     # Output size after convolution\n#     output_size = (input_size - filter_size + 2 * padding) / stride + 1\n#     # Output size after pooling\n#     output_size = (output_size - pool_filter) / pool_stride + 1\n#     return int(output_size)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:20:36.691194Z","iopub.status.idle":"2023-11-25T06:20:36.691569Z","shell.execute_reply.started":"2023-11-25T06:20:36.691373Z","shell.execute_reply":"2023-11-25T06:20:36.691388Z"},"trusted":true},"execution_count":null,"outputs":[]}]}