{"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":"none","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":{"execution":{"iopub.status.busy":"2023-11-22T03:18:53.878256Z","iopub.execute_input":"2023-11-22T03:18:53.878813Z","iopub.status.idle":"2023-11-22T03:18:59.012017Z","shell.execute_reply.started":"2023-11-22T03:18:53.878771Z","shell.execute_reply":"2023-11-22T03:18:59.010666Z"},"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-22T03:18:59.014562Z","iopub.execute_input":"2023-11-22T03:18:59.015244Z","iopub.status.idle":"2023-11-22T03:18:59.449927Z","shell.execute_reply.started":"2023-11-22T03:18:59.015207Z","shell.execute_reply":"2023-11-22T03:18:59.448921Z"},"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-22T03:18:59.452066Z","iopub.execute_input":"2023-11-22T03:18:59.452973Z","iopub.status.idle":"2023-11-22T03:18:59.460047Z","shell.execute_reply.started":"2023-11-22T03:18:59.452926Z","shell.execute_reply":"2023-11-22T03:18:59.458486Z"},"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)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:31:56.455654Z","iopub.execute_input":"2023-11-22T03:31:56.456119Z","iopub.status.idle":"2023-11-22T03:33:40.286152Z","shell.execute_reply.started":"2023-11-22T03:31:56.456086Z","shell.execute_reply":"2023-11-22T03:33:40.284870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = ('0', '1')","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:38:40.709340Z","iopub.execute_input":"2023-11-22T03:38:40.709857Z","iopub.status.idle":"2023-11-22T03:38:40.716227Z","shell.execute_reply.started":"2023-11-22T03:38:40.709823Z","shell.execute_reply":"2023-11-22T03:38:40.714820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get random training images with iter function\ndataiter = iter(train_loader)\n\n# Iterate through the data loader to get one batch\nimages, labels = next(dataiter)\n\n# call function on our images\nimshow(torchvision.utils.make_grid(images))\n\n# print the class of the image\nprint(' '.join('%s' % classes[labels[j]] for j in range(batch_size)))\n","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:41:29.924027Z","iopub.execute_input":"2023-11-22T03:41:29.924560Z","iopub.status.idle":"2023-11-22T03:41:30.863982Z","shell.execute_reply.started":"2023-11-22T03:41:29.924525Z","shell.execute_reply":"2023-11-22T03:41:30.862255Z"},"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,\n        # 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        self.fc1 = nn.Linear(16 * 5 * 5, 120)  # 5x5 from image dimension\n        self.fc2 = nn.Linear(120, 84)\n        self.fc3 = nn.Linear(84, 10)\n\n    def forward(self, x):\n        ''' the forward propagation algorithm '''\n        x = self.pool(F.relu(self.conv1(x)))\n        x = self.pool(F.relu(self.conv2(x)))\n        x = x.view(-1, 16 * 5 * 5)\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()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:47:42.084262Z","iopub.execute_input":"2023-11-22T03:47:42.084779Z","iopub.status.idle":"2023-11-22T03:47:42.096849Z","shell.execute_reply.started":"2023-11-22T03:47:42.084744Z","shell.execute_reply":"2023-11-22T03:47:42.095964Z"},"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-22T03:48:24.514112Z","iopub.execute_input":"2023-11-22T03:48:24.514601Z","iopub.status.idle":"2023-11-22T03:48:24.520834Z","shell.execute_reply.started":"2023-11-22T03:48:24.514565Z","shell.execute_reply":"2023-11-22T03:48:24.519962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}