{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import models, transforms, datasets\nimport zipfile\nimport os\nimport helper\nimport torch.nn.functional as F\n\n\n\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-09T21:01:31.285586Z","iopub.execute_input":"2024-04-09T21:01:31.286021Z","iopub.status.idle":"2024-04-09T21:01:38.406462Z","shell.execute_reply.started":"2024-04-09T21:01:31.285988Z","shell.execute_reply":"2024-04-09T21:01:38.405244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('train')\n\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip', 'r') as zip_ref:\n    zip_ref.extractall('test1')","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:01:38.408782Z","iopub.execute_input":"2024-04-09T21:01:38.409412Z","iopub.status.idle":"2024-04-09T21:01:57.957378Z","shell.execute_reply.started":"2024-04-09T21:01:38.409371Z","shell.execute_reply":"2024-04-09T21:01:57.956407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data transformations\ndata_transform = transforms.Compose([\n    transforms.Resize((224, 224)),#for pretrained pyrpose\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n])\n\ntrain_dataset = datasets.ImageFolder('/kaggle/working/train', transform=data_transform)\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\n\n#trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=4, shuffle=True)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:29:37.688404Z","iopub.execute_input":"2024-04-09T21:29:37.688824Z","iopub.status.idle":"2024-04-09T21:29:37.843355Z","shell.execute_reply.started":"2024-04-09T21:29:37.688794Z","shell.execute_reply":"2024-04-09T21:29:37.842059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = datasets.ImageFolder('/kaggle/working/test1', transform=data_transform)\ntest_loader = DataLoader(test_dataset, batch_size=1, shuffle=True)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:29:40.181453Z","iopub.execute_input":"2024-04-09T21:29:40.181842Z","iopub.status.idle":"2024-04-09T21:29:40.260009Z","shell.execute_reply.started":"2024-04-09T21:29:40.181813Z","shell.execute_reply":"2024-04-09T21:29:40.258457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Looping through it, get a batch on each loop \n#Here dataloader is a generator. To get data out of it, you need to loop through it or convert it to an iterator and call next().\nfor images, labels in train_loader:\n    pass\n\n# Get one batch\nimages, labels = next(iter(train_loader))","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:29:41.687521Z","iopub.execute_input":"2024-04-09T21:29:41.688039Z","iopub.status.idle":"2024-04-09T21:31:31.060109Z","shell.execute_reply.started":"2024-04-09T21:29:41.687996Z","shell.execute_reply":"2024-04-09T21:31:31.058923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimages.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:31:31.062392Z","iopub.execute_input":"2024-04-09T21:31:31.062822Z","iopub.status.idle":"2024-04-09T21:31:31.072752Z","shell.execute_reply.started":"2024-04-09T21:31:31.062782Z","shell.execute_reply":"2024-04-09T21:31:31.071443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#this is replacment for helper\nimport matplotlib.pyplot as plt\ndef imshow(image, normalize=False):\n    \"\"\"\n    Display an image.\n    \"\"\"\n    image = image.numpy().transpose((1, 2, 0))\n    \n    if normalize:\n        mean = np.array([0.485, 0.456, 0.406])\n        std = np.array([0.229, 0.224, 0.225])\n        image = std * image + mean\n        image = np.clip(image, 0, 1)\n    \n    plt.imshow(image)\n    plt.axis(\"off\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-06T22:24:05.553542Z","iopub.execute_input":"2024-04-06T22:24:05.553929Z","iopub.status.idle":"2024-04-06T22:24:05.561387Z","shell.execute_reply.started":"2024-04-06T22:24:05.553898Z","shell.execute_reply":"2024-04-06T22:24:05.560131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Run this to test your data loader\n\nimages, labels = next(iter(train_loader))\nimshow(images[0], normalize=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-06T22:24:06.066326Z","iopub.execute_input":"2024-04-06T22:24:06.066712Z","iopub.status.idle":"2024-04-06T22:24:06.390877Z","shell.execute_reply.started":"2024-04-06T22:24:06.066676Z","shell.execute_reply":"2024-04-06T22:24:06.389702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#let us do some augmentation \ntrain_transforms = transforms.Compose([transforms.RandomRotation(30),\n                                       transforms.RandomResizedCrop(100),\n                                       transforms.RandomHorizontalFlip(),\n                                       transforms.ToTensor(),\n                                       transforms.Normalize([0.5, 0.5, 0.5],\n                                                            [0.5, 0.5, 0.5])])\n\n\n\n# Pass transforms in here, then run the next cell to see how the transforms look\ntrain_data = datasets.ImageFolder('/kaggle/working/train' , transform=train_transforms)\n\n\ntrainloader = torch.utils.data.DataLoader(train_data, batch_size=32)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-06T22:24:08.427212Z","iopub.execute_input":"2024-04-06T22:24:08.428124Z","iopub.status.idle":"2024-04-06T22:24:08.574472Z","shell.execute_reply.started":"2024-04-06T22:24:08.428074Z","shell.execute_reply":"2024-04-06T22:24:08.573233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images, labels = next(iter(train_loader))\nimshow(images[0], normalize=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-06T22:24:09.262878Z","iopub.execute_input":"2024-04-06T22:24:09.263275Z","iopub.status.idle":"2024-04-06T22:24:09.509499Z","shell.execute_reply.started":"2024-04-06T22:24:09.263246Z","shell.execute_reply":"2024-04-06T22:24:09.508018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# change this to the trainloader or testloader \ndata_iter = iter(trainloader)\n\nimages, labels = next(data_iter)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-06T22:24:11.846473Z","iopub.execute_input":"2024-04-06T22:24:11.846901Z","iopub.status.idle":"2024-04-06T22:24:11.997424Z","shell.execute_reply.started":"2024-04-06T22:24:11.846866Z","shell.execute_reply":"2024-04-06T22:24:11.996341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CNN model\nclass SimpleCNN(nn.Module):\n    def __init__(self):\n        super(SimpleCNN, self).__init__()\n        self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1)\n        self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=2, padding=1)\n        self.conv3 = nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1)\n        self.fc1 = nn.Linear(64*28*28, 512)\n        self.fc2 = nn.Linear(512, 2)\n\n    def forward(self, x):\n        x = F.relu(self.conv1(x))\n        # x = self.dropout(x)\n        x = F.relu(self.conv2(x))\n        # x = self.dropout(x)\n        x = F.relu(self.conv3(x))\n        # x = self.dropout(x)\n        x = x.view(-1, 64*28*28)\n        x = F.relu(self.fc1(x))\n        x = self.fc2(x)\n        x = F.sigmoid(x)\n        return x\n#model = SimpleCNN()\n#model","metadata":{"execution":{"iopub.status.busy":"2024-04-06T22:33:08.025583Z","iopub.execute_input":"2024-04-06T22:33:08.026002Z","iopub.status.idle":"2024-04-06T22:33:08.036677Z","shell.execute_reply.started":"2024-04-06T22:33:08.025971Z","shell.execute_reply":"2024-04-06T22:33:08.035615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(model.fc1.weight)\nprint(model.fc1.bias)","metadata":{"execution":{"iopub.status.busy":"2024-04-06T22:24:15.391558Z","iopub.execute_input":"2024-04-06T22:24:15.392155Z","iopub.status.idle":"2024-04-06T22:24:15.426535Z","shell.execute_reply.started":"2024-04-06T22:24:15.392122Z","shell.execute_reply":"2024-04-06T22:24:15.425704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* equivalent neural netwrok\n* # Hyperparameters for our network\n* input_size = \n* hidden_sizes = \n* output_size = \n* \n* # Build a feed-forward network\n* model = nn.Sequential(nn.Linear(input_size, hidden_sizes[0]),\n*                       nn.ReLU(),\n*                       nn.Linear(hidden_sizes[0], hidden_sizes[1]),\n*                       nn.ReLU(),\n*                       nn.Linear(hidden_sizes[1], output_size),\n*                       nn.Softmax(dim=1))\n* print(model)\n* \n* # Forward pass through the network and display output\n* images, labels = next(iter(trainloader))\n* images.resize_(images.shape[0], number channels, input size)\n* ps = model.forward(images[0,:])\n* helper.view_classify(images[0].view(number channel, hight, width), ps)","metadata":{}},{"cell_type":"code","source":"# Initialization - model, loss function, and optimizer\nmodel = SimpleCNN().to(device)\ncriterion = nn.CrossEntropyLoss()# for classification purpose\noptimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# Training\nnum_epochs = 2\nfor epoch in range(num_epochs):\n    running_loss = 0.0\n    for i, data in enumerate(train_loader, 0):\n        inputs, labels = data[0].to(device), data[1].to(device)\n\n        optimizer.zero_grad()# Clear the gradients, do this because gradients are accumulated\n# Forward pass, then backward pass, then update weights\n        outputs = model(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        running_loss += loss.item()\n        if i % 100 == 99:\n            print(f'Epoch {epoch + 1}, Batch {i + 1}, Loss: {running_loss / 100}')\n            running_loss = 0.0\n\nprint('Finished Training')","metadata":{"execution":{"iopub.status.busy":"2024-04-06T23:16:49.925363Z","iopub.execute_input":"2024-04-06T23:16:49.925809Z","iopub.status.idle":"2024-04-06T23:33:41.553260Z","shell.execute_reply.started":"2024-04-06T23:16:49.925773Z","shell.execute_reply":"2024-04-06T23:33:41.551999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prediction of labels for test1_subset\n\nmodel.eval()\npredictions = []\n\nwith torch.no_grad():# Turn off gradients to speed up this part\n    for inputs, _ in test_loader:\n        inputs = inputs.to(device)\n        outputs = model_ft(inputs)\n        _, preds = torch.max(outputs, 1)\n        predictions.extend(preds.cpu().numpy())\n\nsubmission_file = 'submission.csv'\nwith open(submission_file, mode='w', newline='') as f:\n    f.write('id,label\\n')\n    for i, (image, _) in enumerate(test_dataset.samples):\n        image_id = os.path.splitext(os.path.basename(str(image)))[0]\n        f.write(f'{image_id},{predictions[i]}\\n')\n\nprint('Predictions saved to predictions.csv')\n","metadata":{"execution":{"iopub.status.busy":"2024-04-07T01:17:33.220498Z","iopub.status.idle":"2024-04-07T01:17:33.221178Z","shell.execute_reply.started":"2024-04-07T01:17:33.220885Z","shell.execute_reply":"2024-04-07T01:17:33.220913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#another way\n# Implement a function for the validation pass\ndef validation(model, testloader, criterion):\n    test_loss = 0\n    accuracy = 0\n    for images, labels in testloader:\n\n\n        output = model(images)\n        test_loss += criterion(output, labels).item()\n\n        ps = torch.exp(output)\n        equality = (labels.data == ps.max(dim=1)[1])\n        accuracy += equality.type(torch.FloatTensor).mean()\n    \n    return test_loss, accuracy","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs =2\nsteps = 0\nrunning_loss = 0\nprint_every = 40\nfor e in range(epochs):\n    model.train()\n    for images, labels in trainloader:\n        steps += 1\n        \n        \n        optimizer.zero_grad()\n        \n        output = model.forward(images)\n        loss = criterion(output, labels)\n        loss.backward()\n        optimizer.step()\n        \n        running_loss += loss.item()\n        \n        if steps % print_every == 0:\n            # Make sure network is in eval mode for inference\n            model.eval()\n            \n            # Turn off gradients for validation, saves memory and computations\n            with torch.no_grad():\n                test_loss, accuracy = validation(model, testloader, criterion)\n                \n            print(\"Epoch: {}/{}.. \".format(e+1, epochs),\n                  \"Training Loss: {:.3f}.. \".format(running_loss/print_every),\n                  \"Test Loss: {:.3f}.. \".format(test_loss/len(testloader)),\n                  \"Test Accuracy: {:.3f}\".format(accuracy/len(testloader)))\n            \n            running_loss = 0\n            \n            # Make sure training is back on\n            model.train()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test out your network!\n\nmodel.eval()\n\ndataiter = iter(testloader)\nimages, labels = dataiter.next()\nimg = images[0]\n# Convert 2D image to 1D vector\nimg = img.view(1, 50176)\n\n# Calculate the class probabilities (softmax) for img\nwith torch.no_grad():\n    output = model.forward(img)\n\nps = torch.exp(output)\n\n# Plot the image and probabilities\nhelper.view_classify(img.view(1, 224, 224), ps, version='Fashion')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ps = F.softmax(logits, dim=1)\n#helper.view_classify(img.view(1, 28, 28), ps)","metadata":{"execution":{"iopub.status.busy":"2024-04-06T22:20:22.760746Z","iopub.status.idle":"2024-04-06T22:20:22.761122Z","shell.execute_reply.started":"2024-04-06T22:20:22.760938Z","shell.execute_reply":"2024-04-06T22:20:22.760954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}