{"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"}},"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":"2023-09-01T15:53:38.18955Z","iopub.execute_input":"2023-09-01T15:53:38.190437Z","iopub.status.idle":"2023-09-01T15:53:38.527746Z","shell.execute_reply.started":"2023-09-01T15:53:38.190403Z","shell.execute_reply":"2023-09-01T15:53:38.526808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#about torch...\nimport torch\nimport torch.nn as nn\nimport torch\nimport torch.optim as optim\nimport torch.nn.functional as F\n\nfrom torchvision import datasets, models, transforms\nfrom torch.utils.data import DataLoader, Dataset","metadata":{"execution":{"iopub.status.busy":"2023-09-01T15:53:39.977568Z","iopub.execute_input":"2023-09-01T15:53:39.978032Z","iopub.status.idle":"2023-09-01T15:53:43.173224Z","shell.execute_reply.started":"2023-09-01T15:53:39.978001Z","shell.execute_reply":"2023-09-01T15:53:43.172257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\ntrain_zip = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip'\ntest_zip = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip'\ndir_path = '/kaggle/working'\nwith zipfile.ZipFile(train_zip, 'r') as zip_ref:\n    # 解压ZIP文件到目标文件夹\n    zip_ref.extractall(dir_path)\nwith zipfile.ZipFile(test_zip, 'r') as zip_ref:\n    # 解压ZIP文件到目标文件夹\n    zip_ref.extractall(dir_path)","metadata":{"execution":{"iopub.status.busy":"2023-09-01T15:53:44.183878Z","iopub.execute_input":"2023-09-01T15:53:44.184651Z","iopub.status.idle":"2023-09-01T15:54:01.687088Z","shell.execute_reply.started":"2023-09-01T15:53:44.184616Z","shell.execute_reply":"2023-09-01T15:54:01.686088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\ntrain_list = glob.glob('/kaggle/working/train/*')\ntest_list = glob.glob('/kaggle/working/test/*')\nprint(len(train_list))\nprint(train_list[0])\nprint(len(test_list))","metadata":{"execution":{"iopub.status.busy":"2023-09-01T15:54:10.837908Z","iopub.execute_input":"2023-09-01T15:54:10.838265Z","iopub.status.idle":"2023-09-01T15:54:10.996007Z","shell.execute_reply.started":"2023-09-01T15:54:10.838231Z","shell.execute_reply":"2023-09-01T15:54:10.995032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\ndata_transforms = transforms.Compose([   \n    transforms.Resize((224, 224)),\n    transforms.RandomResizedCrop(224),\n    transforms.RandomHorizontalFlip(),\n    transforms.ToTensor()\n    ])\nclass dataset(Dataset):\n    def __init__(self, data_list, transform=None):\n        self.data_list = data_list\n        self.transform = transform\n        \n    def __len__(self):\n        self.filelength = len(self.data_list)\n        return self.filelength\n        \n    def __getitem__(self, index):\n        img_path = self.data_list[index]\n        img = Image.open(img_path)\n        img_transfroms = self.transform(img)\n        \n        lable = img_path.split('/')[-1].split('.')[0]\n        if lable == 'dog':\n            lable = 1\n        elif lable == 'cat':\n            lable = 0\n        return img_transfroms, lable","metadata":{"execution":{"iopub.status.busy":"2023-09-01T15:54:36.811891Z","iopub.execute_input":"2023-09-01T15:54:36.812245Z","iopub.status.idle":"2023-09-01T15:54:36.821664Z","shell.execute_reply.started":"2023-09-01T15:54:36.81221Z","shell.execute_reply":"2023-09-01T15:54:36.820614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = 0.001\nbatch_size = 80\nepochs = 10\n\ntrain_data = dataset(train_list, data_transforms)\ntest_data = dataset(test_list, data_transforms)\n\ntrain_loader = torch.utils.data.DataLoader(dataset = train_data, batch_size=batch_size, shuffle=True)\ntest_loader = torch.utils.data.DataLoader(dataset = test_data, batch_size=batch_size, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-01T15:54:45.885414Z","iopub.execute_input":"2023-09-01T15:54:45.885783Z","iopub.status.idle":"2023-09-01T15:54:45.892663Z","shell.execute_reply.started":"2023-09-01T15:54:45.885753Z","shell.execute_reply":"2023-09-01T15:54:45.891595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.models import resnet18\nmodel = resnet18(pretrained=True)\n\n# Remove the last fully connected layer (fc) to customize the model\nnum_features = model.fc.in_features\nmodel.fc = nn.Sequential()\n\n# Add custom layers for binary classification\ncustom_classifier = nn.Sequential(\n    nn.Linear(num_features, 512),\n    nn.ReLU(),\n\n    nn.Linear(512, 1),\n    nn.Sigmoid()\n)\n\nmodel.fc = custom_classifier","metadata":{"execution":{"iopub.status.busy":"2023-09-01T15:54:47.859941Z","iopub.execute_input":"2023-09-01T15:54:47.860874Z","iopub.status.idle":"2023-09-01T15:54:48.464292Z","shell.execute_reply.started":"2023-09-01T15:54:47.86083Z","shell.execute_reply":"2023-09-01T15:54:48.46333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ['CUDA_LAUNCH_BLOCKING'] = \"1\"\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\nprint(device)\nmodel = model.to(\"cuda\")\n\ncriterion = nn.BCELoss()\noptimizer = optim.SGD(params=model.parameters(), lr=0.001, momentum=0.9)\n\nmodel.train()\n\n\nfor epoch in range(epochs):\n    epoch_loss = 0\n    epoch_accuracy = 0\n    \n    for data, label in train_loader:\n        data = data.to(device)\n        label = label.to(device)\n        label = label.type(torch.float)\n        output = model(data)\n        label = label.unsqueeze(1)\n        loss = criterion(output, label)\n    \n        \n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        \n        acc = ((output.argmax(dim=1) == label).float().mean())\n        epoch_accuracy += acc/len(train_loader)\n        epoch_loss += loss/len(train_loader)\n        \n    print('Epoch : {}, train accuracy : {}, train loss : {}'.format(epoch+1, epoch_accuracy,epoch_loss))\n    \n    \n    with torch.no_grad():\n        epoch_val_accuracy=0\n        epoch_val_loss =0\n        for data, label in val_loader:\n            data = data.to(device)\n            label = label.to(device)\n            \n            val_output = model(data)\n            val_loss = criterion(val_output,label)\n            \n            \n            acc = ((val_output.argmax(dim=1) == label).float().mean())\n            epoch_val_accuracy += acc/ len(val_loader)\n            epoch_val_loss += val_loss/ len(val_loader)\n            \n        print('Epoch : {}, val_accuracy : {}, val_loss : {}'.format(epoch+1, epoch_val_accuracy,epoch_val_loss))","metadata":{"execution":{"iopub.status.busy":"2023-09-01T15:56:14.897067Z","iopub.execute_input":"2023-09-01T15:56:14.897956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}