{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-28T09:14:38.307163Z","iopub.execute_input":"2024-10-28T09:14:38.307605Z","iopub.status.idle":"2024-10-28T09:14:39.692583Z","shell.execute_reply.started":"2024-10-28T09:14:38.307563Z","shell.execute_reply":"2024-10-28T09:14:39.691296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/cifar-10/trainLabels.csv\")\ndf","metadata":{"execution":{"iopub.status.busy":"2024-10-28T09:15:04.39045Z","iopub.execute_input":"2024-10-28T09:15:04.390879Z","iopub.status.idle":"2024-10-28T09:15:04.454521Z","shell.execute_reply.started":"2024-10-28T09:15:04.390837Z","shell.execute_reply":"2024-10-28T09:15:04.453267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2024-10-28T09:17:12.261183Z","iopub.execute_input":"2024-10-28T09:17:12.261643Z","iopub.status.idle":"2024-10-28T09:17:28.297851Z","shell.execute_reply.started":"2024-10-28T09:17:12.261595Z","shell.execute_reply":"2024-10-28T09:17:28.29652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr\nwith py7zr.SevenZipFile('/kaggle/input/cifar-10/train.7z', mode='r') as z:\n    z.extractall()","metadata":{"execution":{"iopub.status.busy":"2024-10-28T09:17:59.673981Z","iopub.execute_input":"2024-10-28T09:17:59.675108Z","iopub.status.idle":"2024-10-28T09:18:27.260844Z","shell.execute_reply.started":"2024-10-28T09:17:59.675055Z","shell.execute_reply":"2024-10-28T09:18:27.259708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = []\ni = 0\nfor dirname, _, filenames in os.walk('/kaggle/working/train'):\n    for filename in filenames:\n        filenames.append(os.path.join(dirname, filename))\n        if i>50:\n            break\n        i = i + 1\nfilenames\nprint(len(filenames)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T10:59:50.073182Z","iopub.execute_input":"2024-10-28T10:59:50.073599Z","iopub.status.idle":"2024-10-28T10:59:50.140312Z","shell.execute_reply.started":"2024-10-28T10:59:50.073561Z","shell.execute_reply":"2024-10-28T10:59:50.139033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = \"/kaggle/working/train/\"+filenames[0]\nfilename","metadata":{"execution":{"iopub.status.busy":"2024-10-28T10:51:57.832566Z","iopub.execute_input":"2024-10-28T10:51:57.833032Z","iopub.status.idle":"2024-10-28T10:51:57.840678Z","shell.execute_reply.started":"2024-10-28T10:51:57.832985Z","shell.execute_reply":"2024-10-28T10:51:57.839379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom PIL import Image\nimport torchvision.transforms as transforms\n\n\ntransform = transforms.Compose([\n    transforms.PILToTensor()\n])\n\n\n\nimg_tensors = []\nfrom PIL import Image\nfor file_x in filenames:\n    filename = \"/kaggle/working/train/\"+file_x\n    #print(filename)\n    im = Image.open(filename)\n    img_tensor = transform(im)\n    img_tensors.append(img_tensor)\nprint(img_tensors)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T11:00:30.447824Z","iopub.execute_input":"2024-10-28T11:00:30.448314Z","iopub.status.idle":"2024-10-28T11:00:45.661162Z","shell.execute_reply.started":"2024-10-28T11:00:30.448272Z","shell.execute_reply":"2024-10-28T11:00:45.659669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nim = Image.open(filename)\nim","metadata":{"execution":{"iopub.status.busy":"2024-10-28T09:39:21.559801Z","iopub.execute_input":"2024-10-28T09:39:21.560864Z","iopub.status.idle":"2024-10-28T09:39:21.583789Z","shell.execute_reply.started":"2024-10-28T09:39:21.560807Z","shell.execute_reply":"2024-10-28T09:39:21.582436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im_array = np.asarray(im)\nim_array.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-28T09:41:58.468844Z","iopub.execute_input":"2024-10-28T09:41:58.469322Z","iopub.status.idle":"2024-10-28T09:41:58.47698Z","shell.execute_reply.started":"2024-10-28T09:41:58.469281Z","shell.execute_reply":"2024-10-28T09:41:58.475861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv(\"/kaggle/input/cifar-10/trainLabels.csv\")\nlabels[labels['id']==16422]","metadata":{"execution":{"iopub.status.busy":"2024-10-28T10:44:40.400835Z","iopub.execute_input":"2024-10-28T10:44:40.401885Z","iopub.status.idle":"2024-10-28T10:44:40.433437Z","shell.execute_reply.started":"2024-10-28T10:44:40.401841Z","shell.execute_reply":"2024-10-28T10:44:40.432387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_list = labels['label'].unique().tolist()\ntype(labels_list)\nlabels_list","metadata":{"execution":{"iopub.status.busy":"2024-10-28T10:44:49.624185Z","iopub.execute_input":"2024-10-28T10:44:49.62467Z","iopub.status.idle":"2024-10-28T10:44:49.63604Z","shell.execute_reply.started":"2024-10-28T10:44:49.624623Z","shell.execute_reply":"2024-10-28T10:44:49.634773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F\nclass Net(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1_1 = nn.Conv2d(3, 1, 3)\n        self.pool = nn.MaxPool2d(2, 2)\n        self.conv1_2 = nn.Conv2d(1, 1, 3)\n        self.conv2_1 = nn.Conv2d(1, 1, 3)\n        self.conv2_2 = nn.Conv2d(1, 1, 3)\n        self.fc3 = nn.Linear(25, 10)\n\n    def forward(self, x):\n        #print(x.shape)\n        x = self.conv1_1(x)\n        #print(x.shape)\n        x = F.relu(x)\n        #print(x.shape)\n        x = self.conv1_2(x)\n        #print(x.shape)\n        x = F.relu(x)\n        #print(x.shape)\n        x = self.pool(x)\n        #print(x.shape)\n        x = self.conv2_1(x)\n        #print(x.shape)\n        x = F.relu(x)\n        #print(x.shape)\n        x = self.conv2_2(x)\n        #print(x.shape)\n        x = F.relu(x)\n        #print(x.shape)\n        x = self.pool(x)\n        #print(x.shape)\n        x = torch.flatten(x, 1) # flatten all dimensions except batch\n        #print(\"Flatten \",x.shape)\n        x = self.fc3(x)\n        x = F.sigmoid(x)\n        #print(x.shape)\n        return x\n\n\nnet = Net()\n\nimport torch\nimg_tensor_result  = img_tensor.float()\noutputs = net(img_tensor_result)\nprint(outputs)\n_, predicted = torch.max(outputs, 1)\nprint(_, predicted)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T10:48:06.819594Z","iopub.execute_input":"2024-10-28T10:48:06.820433Z","iopub.status.idle":"2024-10-28T10:48:06.841821Z","shell.execute_reply.started":"2024-10-28T10:48:06.820386Z","shell.execute_reply":"2024-10-28T10:48:06.840552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.optim as optim\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T10:11:47.800977Z","iopub.execute_input":"2024-10-28T10:11:47.801472Z","iopub.status.idle":"2024-10-28T10:11:47.807855Z","shell.execute_reply.started":"2024-10-28T10:11:47.801427Z","shell.execute_reply":"2024-10-28T10:11:47.806609Z"},"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(trainloader, 0):\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":"2024-10-28T10:12:41.298964Z","iopub.execute_input":"2024-10-28T10:12:41.299352Z","iopub.status.idle":"2024-10-28T10:12:41.437992Z","shell.execute_reply.started":"2024-10-28T10:12:41.299315Z","shell.execute_reply":"2024-10-28T10:12:41.436096Z"},"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(trainloader, 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_count":null,"outputs":[]}]}