{"metadata":{"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"}},"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 sys\nsys.path.append('..')\n\nimport numpy as np\nimport torch\nfrom torch import nn\nfrom torch.autograd import Variable\nfrom torchvision.datasets import CIFAR10\nfrom torchvision import transforms\nimport cv2\n\n#解压(unzip)\nimport zipfile\n#zipfile解压\nz = zipfile.ZipFile('../input/dogs-vs-cats/train.zip', 'r')\nz.extractall(path=r\"./\")\nz.close()\nz = zipfile.ZipFile('../input/dogs-vs-cats/test1.zip', 'r')\nz.extractall(path=r\"./\")\nz.close()\n\n\n# for dirname, _, filenames in os.walk('../input/dogs-vs-cats'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n# print(torch.__version__)\n# print(torch.cuda.is_available())\n\n\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-01-02T03:37:29.918922Z","iopub.execute_input":"2023-01-02T03:37:29.919301Z","iopub.status.idle":"2023-01-02T03:37:52.041118Z","shell.execute_reply.started":"2023-01-02T03:37:29.919207Z","shell.execute_reply":"2023-01-02T03:37:52.039970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import KFold\nimport os\npath111 = \"/kaggle/working/train\"\nname_list = os.listdir(path111)\nname_list = [path111 + '/' + i for i in name_list]\n# 做K-Fold划分\nkf = KFold(n_splits = 10, shuffle=True )\nkf_name_list = kf.split(name_list)\ntrain_kf_list = []\ntest_kf_list = []\ntrain_kf_name_list = []\ntest_kf_name_list = []\n# 获得标签(get the label)\nfor train_index, test_index in kf_name_list:\n    train_kf_list.append(train_index)\n    test_kf_list.append(test_index)\n# 获得标签所对应的图像路径(get the image path conrresponding to the label)\nfor i in range(len(train_kf_list)):\n    train_kf_name_list.append([name_list[j] for j in train_kf_list[i]])\n    test_kf_name_list.append([name_list[j] for j in test_kf_list[i]])","metadata":{"execution":{"iopub.status.busy":"2023-01-02T03:38:21.904824Z","iopub.execute_input":"2023-01-02T03:38:21.905363Z","iopub.status.idle":"2023-01-02T03:38:22.345902Z","shell.execute_reply.started":"2023-01-02T03:38:21.905328Z","shell.execute_reply":"2023-01-02T03:38:22.344957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset\n# reconstruct the dataset function\nclass mydataset(Dataset):\n    def __init__(self, img_path ,transform=None):\n        super(mydataset, self).__init__()\n        self.path = img_path\n        self.transform = transform\n        self.label = None\n        self.transform = transform\n        \n    # 可以在这对图像做一些操作，而不用transform\n    # \n    def __getitem__(self, index):\n        img = cv2.imread(self.path[index])\n        if self.transform:\n            img = self.transform(img)\n        if \"cat\" in self.path[index].split('\\\\')[-1]:\n            self.label = 1\n        else:\n            self.label = 0\n        return img, torch.tensor(self.label)\n    \n    def __len__(self):\n        return len(self.path)\n\n# setting the tranin_transform and test_transform\ntrain_transform = transforms.Compose([transforms.ToPILImage(),\n                                     transforms.Resize(96),\n                                     transforms.CenterCrop(64),\n                                     transforms.RandomHorizontalFlip(),\n                                     transforms.RandomVerticalFlip(),\n                                     transforms.ToTensor(),\n                                     transforms.Normalize(\n                                         mean=[0.485, 0.456, 0.406], \n                                         std=[0.229, 0.224, 0.225])\n                                    ])\n\ntest_transform = transforms.Compose([transforms.ToPILImage(),\n                                     transforms.Resize(96),\n                                     transforms.CenterCrop(64),\n                                     transforms.ToTensor(),\n                                     transforms.Normalize(\n                                         mean=[0.485, 0.456, 0.406], \n                                         std=[0.229, 0.224, 0.225])\n                                    ])\n \n# get the train_dataset and test_dataset\ntrain_dataset = mydataset(train_kf_name_list[0],transform=train_transform)\ntest_dataset = mydataset(test_kf_name_list[0],transform=train_transform)\n\ntrain_data = torch.utils.data.DataLoader(train_dataset, batch_size=128, shuffle=True)\ntest_data = torch.utils.data.DataLoader(test_dataset, batch_size=128, shuffle=True)\n\nfor a, b in train_data:\n    print(b)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-01-02T03:38:24.966337Z","iopub.execute_input":"2023-01-02T03:38:24.967064Z","iopub.status.idle":"2023-01-02T03:38:25.898829Z","shell.execute_reply.started":"2023-01-02T03:38:24.967027Z","shell.execute_reply":"2023-01-02T03:38:25.897557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# construct the traditional CNN similar to the vgg\ndef vgg_block(num_convs, in_channels, out_channels):\n    net = [nn.Conv2d(in_channels, out_channels ,kernel_size=3, padding=1), nn.BatchNorm2d(out_channels), nn.ReLU(True)] # 定义第一层\n    \n    for i in range(num_convs-1): # 定义后面的很多层\n        net.append(nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1))\n        net.append(nn.ReLU(True))\n        \n    net.append(nn.MaxPool2d(2, 2)) # 定义池化层\n    return nn.Sequential(*net)\n\n\ndef vgg_stack(num_convs, channels):\n    net = []\n    for n, c in zip(num_convs, channels):\n        in_c = c[0]\n        out_c = c[1]\n        net.append(vgg_block(n, in_c, out_c))\n    return nn.Sequential(*net)\n\n\nclass vgg(nn.Module):\n    def __init__(self):\n        super(vgg, self).__init__()\n        self.feature = vgg_net\n        self.fc = nn.Sequential(\n            nn.Linear(1024, 100),\n            nn.Dropout(p=0.5),\n            nn.ReLU(True),\n            nn.Linear(100, 2)\n        )\n    def forward(self, x):\n        x = self.feature(x)\n        x = x.view(x.shape[0], -1)\n        x = self.fc(x)\n        return x\n\n#  the cnn consist of  five convolution layer and two fc\nvgg_net = vgg_stack((1, 1, 1, 1,1), ((3, 64), (64, 128), (128, 256), (256,256), (256,256)))\n\nnet = vgg()\noptimizer = torch.optim.SGD(net.parameters(), lr=1e-1)\ncriterion = nn.CrossEntropyLoss()\n\nfrom datetime import datetime\ndef train(net, train_data, valid_data, num_epochs, optimizer, criterion):\n    if torch.cuda.is_available():\n        net = net.cuda()\n    prev_time = datetime.now()\n    for epoch in range(num_epochs):\n        train_loss = 0\n        train_acc = 0\n        net = net.train()\n        for im, label in train_data:\n            if torch.cuda.is_available():\n                im = Variable(im.cuda())  # (bs, 3, h, w)\n                label = Variable(label.cuda())  # (bs, h, w)\n            else:\n                im = Variable(im)\n                label = Variable(label)\n            # forward\n            output = net(im)\n            loss = criterion(output, label)\n            # backward\n            optimizer.zero_grad()\n            loss.backward()\n            optimizer.step()\n            \n            train_loss += loss.item()\n            train_acc += get_acc(output, label)\n\n        cur_time = datetime.now()\n        h, remainder = divmod((cur_time - prev_time).seconds, 3600)\n        m, s = divmod(remainder, 60)\n        time_str = \"Time %02d:%02d:%02d\" % (h, m, s)\n        if valid_data is not None:\n            valid_loss = 0\n            valid_acc = 0\n            net = net.eval()\n            for im, label in valid_data:\n                if torch.cuda.is_available():\n                    im = Variable(im.cuda(), volatile=True)\n                    label = Variable(label.cuda(), volatile=True)\n                else:\n                    im = Variable(im, volatile=True)\n                    label = Variable(label, volatile=True)\n                output = net(im)\n                loss = criterion(output, label)\n                valid_loss += loss.data.item()\n                valid_acc += get_acc(output, label)\n            epoch_str = (\n                \"Epoch %d. Train Loss: %f, Train Acc: %f, Valid Loss: %f, Valid Acc: %f, \"\n                % (epoch, train_loss / len(train_data),\n                   train_acc / len(train_data), valid_loss / len(valid_data),\n                   valid_acc / len(valid_data)))\n        else:\n            epoch_str = (\"Epoch %d. Train Loss: %f, Train Acc: %f, \" %\n                         (epoch, train_loss / len(train_data),\n                          train_acc / len(train_data)))\n        prev_time = cur_time\n        print(epoch_str + time_str)\n    \n# accuracy function\ndef get_acc(output, label):\n    total = output.shape[0]\n    _, pred_label = output.max(1)\n    num_correct = (pred_label == label).sum().item()\n    return num_correct / total\n\n                                     \ntrain(net, train_data, test_data, 30, optimizer, criterion)","metadata":{"execution":{"iopub.status.busy":"2023-01-02T03:38:31.618481Z","iopub.execute_input":"2023-01-02T03:38:31.618849Z","iopub.status.idle":"2023-01-02T03:38:40.790814Z","shell.execute_reply.started":"2023-01-02T03:38:31.618817Z","shell.execute_reply":"2023-01-02T03:38:40.789111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndd = pd.read_csv('/kaggle/input/dogs-vs-cats/sampleSubmission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-29T07:37:06.643746Z","iopub.execute_input":"2022-12-29T07:37:06.644117Z","iopub.status.idle":"2022-12-29T07:37:06.656959Z","shell.execute_reply.started":"2022-12-29T07:37:06.644084Z","shell.execute_reply":"2022-12-29T07:37:06.656053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\npred_list = []\nmodel_test = net.eval()\nfor path in os.listdir('/kaggle/working/test1'):\n    img = cv2.imread('/kaggle/working/test1/'+ str(path))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = test_transform(img).unsqueeze(0)\n    img = Variable(img.cuda(), volatile=True)\n    pred_img = model_test(img)\n    pred_list.append(pred_img.argmax(1)[0].item())","metadata":{"execution":{"iopub.status.busy":"2022-12-29T07:08:43.546177Z","iopub.execute_input":"2022-12-29T07:08:43.546527Z","iopub.status.idle":"2022-12-29T07:09:50.960236Z","shell.execute_reply.started":"2022-12-29T07:08:43.546496Z","shell.execute_reply":"2022-12-29T07:09:50.959213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dd.to_csv('output.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-29T07:13:20.165536Z","iopub.execute_input":"2022-12-29T07:13:20.165888Z","iopub.status.idle":"2022-12-29T07:13:20.184948Z","shell.execute_reply.started":"2022-12-29T07:13:20.165855Z","shell.execute_reply":"2022-12-29T07:13:20.184088Z"},"trusted":true},"execution_count":null,"outputs":[]}]}