{"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":"gpu","dataSources":[{"sourceId":84616,"databundleVersionId":9494719,"sourceType":"competition"}],"dockerImageVersionId":30762,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torch.nn.functional as F\nimport torchvision.transforms as transforms\nfrom torch.utils.data import DataLoader, TensorDataset\nimport numpy as np\nimport pickle\nimport os\n","metadata":{"execution":{"iopub.status.busy":"2024-09-05T08:53:10.979848Z","iopub.execute_input":"2024-09-05T08:53:10.980194Z","iopub.status.idle":"2024-09-05T08:53:10.989079Z","shell.execute_reply.started":"2024-09-05T08:53:10.980146Z","shell.execute_reply":"2024-09-05T08:53:10.988218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def unpickle(file):\n    with open(file, 'rb') as fo:\n        dict = pickle.load(fo, encoding='bytes')\n    return dict\n\ndef load_cifar10_data(data_dir):\n    train_data = []\n    train_labels = []\n    \n    for batch_id in range(1, 6):\n        batch = unpickle(os.path.join(data_dir, f\"data_batch_{batch_id}\"))\n        train_data.append(batch[b'data'])\n        train_labels.extend(batch[b'labels'])\n    \n    train_data = np.concatenate(train_data)\n    train_data = train_data.reshape(-1, 3, 32, 32).astype(np.float32)  \n    train_labels = np.array(train_labels)\n\n    test_batch = unpickle(os.path.join(data_dir, \"test_batch\"))\n    test_data = test_batch[b'data']\n    test_labels = test_batch[b'labels']\n\n    test_data = test_data.reshape(-1, 3, 32, 32).astype(np.float32)  \n    test_labels = np.array(test_labels)\n    \n    return train_data, train_labels, test_data, test_labels\n","metadata":{"execution":{"iopub.status.busy":"2024-09-05T08:53:10.991650Z","iopub.execute_input":"2024-09-05T08:53:10.992305Z","iopub.status.idle":"2024-09-05T08:53:11.000700Z","shell.execute_reply.started":"2024-09-05T08:53:10.992272Z","shell.execute_reply":"2024-09-05T08:53:10.999798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MYCNN(nn.Module):\n    def __init__(self, num_classes=10):\n        super(MYCNN, self).__init__()\n        self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)  \n        self.bn1 = nn.BatchNorm2d(32)\n        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)\n        self.bn2 = nn.BatchNorm2d(64)\n        self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)\n        self.bn3 = nn.BatchNorm2d(128)\n        self.pool = nn.MaxPool2d(2, 2) \n        self.dropout = nn.Dropout(p=0.3)\n        self.fc1 = nn.Linear(128 * 4 * 4, 512)\n        self.fc2 = nn.Linear(512, num_classes)\n\n    def forward(self, x):\n        x = self.pool(F.relu(self.conv1(x))) \n        x = self.dropout(x) \n        x = self.pool(F.relu(self.conv2(x)))  \n        x = self.dropout(x) \n        x = self.pool(F.relu(self.conv3(x)))\n        x = x.view(-1, 128 * 4 * 4) \n        x = F.relu(self.fc1(x))\n        x = self.dropout(x) \n        x = self.fc2(x)  \n        return x","metadata":{"execution":{"iopub.status.busy":"2024-09-05T08:53:11.002057Z","iopub.execute_input":"2024-09-05T08:53:11.002471Z","iopub.status.idle":"2024-09-05T08:53:11.015468Z","shell.execute_reply.started":"2024-09-05T08:53:11.002429Z","shell.execute_reply":"2024-09-05T08:53:11.014656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model(model, train_loader, criterion, optimizer, device, num_epochs=10):\n    model.train()\n    model.to(device)  \n    for epoch in range(num_epochs):\n        running_loss = 0.0\n        correct = 0\n        total = 0\n        for inputs, labels in train_loader:\n            inputs, labels = inputs.to(device), labels.to(device)\n            optimizer.zero_grad()\n            outputs = model(inputs)\n            loss = criterion(outputs, labels)\n            loss.backward()\n            optimizer.step()\n\n            running_loss += loss.item()\n            \n            _, predicted = torch.max(outputs.data, 1)\n            total += labels.size(0)\n            correct += (predicted == labels).sum().item()\n\n        avg_loss = running_loss / len(train_loader)\n        accuracy = 100 * correct / total\n\n        print(f\"Epoch [{epoch + 1}/{num_epochs}] - Average Loss: {avg_loss:.4f}, Accuracy: {accuracy:.2f}%\")\n    print(\"Training completed.\")\n","metadata":{"execution":{"iopub.status.busy":"2024-09-05T08:53:11.017792Z","iopub.execute_input":"2024-09-05T08:53:11.018367Z","iopub.status.idle":"2024-09-05T08:53:11.026232Z","shell.execute_reply.started":"2024-09-05T08:53:11.018323Z","shell.execute_reply":"2024-09-05T08:53:11.025361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate_model(model, test_loader, device):\n    model.eval()\n    model.to(device)\n    correct = 0\n    total = 0\n    with torch.no_grad():\n        for inputs, labels in test_loader:\n            inputs, labels = inputs.to(device), labels.to(device)  # Move data to GPU or CPU\n            outputs = model(inputs)\n            _, predicted = torch.max(outputs, 1)\n            total += labels.size(0)\n            correct += (predicted == labels).sum().item()\n\n    print(f\"Test Accuracy: {100 * correct / total:.2f}%\")\n","metadata":{"execution":{"iopub.status.busy":"2024-09-05T08:53:11.027262Z","iopub.execute_input":"2024-09-05T08:53:11.027580Z","iopub.status.idle":"2024-09-05T08:53:11.039090Z","shell.execute_reply.started":"2024-09-05T08:53:11.027501Z","shell.execute_reply":"2024-09-05T08:53:11.038260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    data_dir = '/kaggle/input/2024-zju/'  \n    \n    train_data, train_labels, test_data, test_labels = load_cifar10_data(data_dir)\n    \n    transform = transforms.Compose([\n        transforms.ToTensor(),  \n        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),  \n        transforms.RandomRotation(10),\n        transforms.RandomResizedCrop((32,32),scale=(0.9,1),ratio=(1,1)),\n        transforms.RandomCrop(size=(32,32),padding=4),\n        transforms.RandomHorizontalFlip(p=0.5),\n        transforms.ColorJitter(brightness=0.2,contrast=0.2,saturation=0.2,hue=0.1),\n        transforms.RandomErasing(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3)) \n    ])\n    \n    train_data = torch.tensor(train_data).permute(0, 1, 2, 3)  \n    test_data = torch.tensor(test_data).permute(0, 1, 2, 3)    \n    train_labels = torch.tensor(train_labels, dtype=torch.long)\n    test_labels = torch.tensor(test_labels, dtype=torch.long)\n    \n    train_dataset = TensorDataset(train_data, train_labels)\n    test_dataset = TensorDataset(test_data, test_labels)\n\n    train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)\n    test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)\n    \n    model = MYCNN(num_classes=10)\n    criterion = nn.CrossEntropyLoss()\n    optimizer = optim.Adam(model.parameters(), lr=0.00004)\n    \n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    \n    train_model(model, train_loader, criterion, optimizer, device, num_epochs=100)\n    evaluate_model(model,test_loader,device)","metadata":{"execution":{"iopub.status.busy":"2024-09-05T08:53:11.040251Z","iopub.execute_input":"2024-09-05T08:53:11.040607Z","iopub.status.idle":"2024-09-05T09:01:32.712880Z","shell.execute_reply.started":"2024-09-05T08:53:11.040565Z","shell.execute_reply":"2024-09-05T09:01:32.711758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net = MYCNN()\ntorch.save(net.state_dict(),'cifar10_resnet.pth')\nnet.to(device)\nnet.load_state_dict(torch.load('cifar10_resnet.pth'))\nnet.eval()","metadata":{"execution":{"iopub.status.busy":"2024-09-05T09:01:32.714800Z","iopub.execute_input":"2024-09-05T09:01:32.715128Z","iopub.status.idle":"2024-09-05T09:01:32.751540Z","shell.execute_reply.started":"2024-09-05T09:01:32.715095Z","shell.execute_reply":"2024-09-05T09:01:32.750515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}