{"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":"!mkdir ./dogs-vs-cats\n!mkdir ./dogs-vs-cats-redux-kernels-edition\n!unzip  -o ../input/dogs-vs-cats/test1.zip -d ./dogs-vs-cats >a.txt\n!unzip  -o ../input/dogs-vs-cats/train.zip -d ./dogs-vs-cats >a.txt\n!unzip  -o ../input/dogs-vs-cats-redux-kernels-edition/train.zip -d ./dogs-vs-cats-redux-kernels-edition>a.txt\n!unzip  -o ../input/dogs-vs-cats-redux-kernels-edition/test.zip -d ./dogs-vs-cats-redux-kernels-edition >a.txt","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:32:13.152468Z","iopub.execute_input":"2021-10-18T12:32:13.153117Z","iopub.status.idle":"2021-10-18T12:32:51.553337Z","shell.execute_reply.started":"2021-10-18T12:32:13.153008Z","shell.execute_reply":"2021-10-18T12:32:51.549396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset, DataLoader, random_split\nimport torchvision.transforms as transforms\nimport torch.optim as optim\nimport torchvision.models as models\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom sklearn.metrics import recall_score\nfrom sklearn.metrics import precision_score\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import f1_score\nfrom transformers import AdamW\nfrom tqdm import tqdm, trange\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:32:51.556871Z","iopub.execute_input":"2021-10-18T12:32:51.557258Z","iopub.status.idle":"2021-10-18T12:32:57.785940Z","shell.execute_reply.started":"2021-10-18T12:32:51.557215Z","shell.execute_reply":"2021-10-18T12:32:57.785182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainLabel=[]\ntrianImagePaths=[]\nfor dirname, _, filenames in os.walk('./dogs-vs-cats/train'):\n    for filename in filenames:\n        trianImagePaths.append(os.path.join(dirname, filename))\n        if filename.split('.')[0]=='cat':\n            trainLabel.append(0)\n        else:\n            trainLabel.append(1)","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:46:43.678078Z","iopub.execute_input":"2021-10-18T12:46:43.678625Z","iopub.status.idle":"2021-10-18T12:46:46.332297Z","shell.execute_reply.started":"2021-10-18T12:46:43.678589Z","shell.execute_reply":"2021-10-18T12:46:46.331521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train,x_test, y_train, y_test = train_test_split(trianImagePaths,trainLabel,test_size=0.3, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:32:57.883102Z","iopub.execute_input":"2021-10-18T12:32:57.883376Z","iopub.status.idle":"2021-10-18T12:32:57.916092Z","shell.execute_reply.started":"2021-10-18T12:32:57.883338Z","shell.execute_reply":"2021-10-18T12:32:57.915426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DogsvsCats(Dataset):\n    def __init__(self, imgDirList, labels=None, transform_apply=None, iftrain=True):\n        self.imgDirList = imgDirList\n        self.transform = transform_apply\n        self.train = iftrain\n        self.labels=labels\n    def __len__(self):\n        return len(self.imgDirList)\n    \n    def __getitem__(self, index):\n        img = Image.open(self.imgDirList[index])\n        \n        if self.transform:\n            img = self.transform(img)\n            \n        if self.train:\n            label = torch.tensor(self.labels[index])\n            return img,label\n        else:\n            return img","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:47:58.871047Z","iopub.execute_input":"2021-10-18T12:47:58.871329Z","iopub.status.idle":"2021-10-18T12:47:58.880001Z","shell.execute_reply.started":"2021-10-18T12:47:58.871297Z","shell.execute_reply":"2021-10-18T12:47:58.879240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transforms = transforms.Compose([\n    transforms.Resize((224,224)),\n    transforms.RandomHorizontalFlip(0.5),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.4884, 0.4551, 0.4170], std=[0.2256, 0.2210, 0.2214])\n])\n\nval_transforms = transforms.Compose([\n    transforms.Resize((224,224)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.4884, 0.4551, 0.4170], std=[0.2256, 0.2210, 0.2214])\n])","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:47:58.881797Z","iopub.execute_input":"2021-10-18T12:47:58.882212Z","iopub.status.idle":"2021-10-18T12:47:58.889771Z","shell.execute_reply.started":"2021-10-18T12:47:58.882178Z","shell.execute_reply":"2021-10-18T12:47:58.889088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = DogsvsCats(x_train,labels=y_train, transform_apply=train_transforms)\nval_dataset = DogsvsCats(x_test,labels=y_test, transform_apply=val_transforms)\n\ntrain_loader = torch.utils.data.DataLoader(train_dataset,batch_size=32,shuffle=True)\nval_loader = torch.utils.data.DataLoader(val_dataset,batch_size=32,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:47:58.891109Z","iopub.execute_input":"2021-10-18T12:47:58.891354Z","iopub.status.idle":"2021-10-18T12:47:58.901521Z","shell.execute_reply.started":"2021-10-18T12:47:58.891321Z","shell.execute_reply":"2021-10-18T12:47:58.900822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class myVGG(nn.Module):\n    def __init__(self):\n        super(myVGG,self).__init__()\n        self.num_classes = 2\n        # define an empty for Conv_ReLU_MaxPool\n        net = []\n\n        # block 1\n        net.append(nn.Conv2d(in_channels=3, out_channels=64, padding=1, kernel_size=3, stride=1))\n        net.append(nn.ReLU())\n        net.append(nn.Conv2d(in_channels=64, out_channels=64, padding=1, kernel_size=3, stride=1))\n        net.append(nn.ReLU())\n        net.append(nn.MaxPool2d(kernel_size=2, stride=2))\n\n        # block 2\n        net.append(nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1))\n        net.append(nn.ReLU())\n        net.append(nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1))\n        net.append(nn.ReLU())\n        net.append(nn.MaxPool2d(kernel_size=2, stride=2))\n\n        # block 3\n        net.append(nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1, stride=1))\n        net.append(nn.ReLU())\n        net.append(nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1, stride=1))\n        net.append(nn.ReLU())\n        net.append(nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1, stride=1))\n        net.append(nn.ReLU())\n        net.append(nn.MaxPool2d(kernel_size=2, stride=2))\n\n        # block 4\n        net.append(nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=1, stride=1))\n        net.append(nn.ReLU())\n        net.append(nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1, stride=1))\n        net.append(nn.ReLU())\n        net.append(nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1, stride=1))\n        net.append(nn.ReLU())\n        net.append(nn.MaxPool2d(kernel_size=2, stride=2))\n\n        # block 5\n        net.append(nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1, stride=1))\n        net.append(nn.ReLU())\n        net.append(nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1, stride=1))\n        net.append(nn.ReLU())\n        net.append(nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1, stride=1))\n        net.append(nn.ReLU())\n        net.append(nn.MaxPool2d(kernel_size=2, stride=2))\n\n        # add net into class property\n        self.extract_feature = nn.Sequential(*net)\n\n        # define an empty container for Linear operations\n        classifier = []\n        classifier.append(nn.Linear(in_features=512*7*7, out_features=4096))\n        classifier.append(nn.ReLU())\n        classifier.append(nn.Dropout(p=0.5))\n        classifier.append(nn.Linear(in_features=4096, out_features=4096))\n        classifier.append(nn.ReLU())\n        classifier.append(nn.Dropout(p=0.5))\n        classifier.append(nn.Linear(in_features=4096, out_features=self.num_classes))\n\n        # add classifier into class property\n        self.classifier = nn.Sequential(*classifier)\n\n\n    def forward(self, x):\n        feature = self.extract_feature(x)\n        feature = feature.view(x.size(0), -1)\n        classify_result = self.classifier(feature)\n        return classify_result","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:47:58.954826Z","iopub.execute_input":"2021-10-18T12:47:58.955069Z","iopub.status.idle":"2021-10-18T12:47:58.977287Z","shell.execute_reply.started":"2021-10-18T12:47:58.955023Z","shell.execute_reply":"2021-10-18T12:47:58.976348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\nmodel= myVGG()\nmodel.to(device)\n# 损失函数\ncriteon = nn.CrossEntropyLoss()\n# 优化器\noptimizer = AdamW(model.parameters(),lr=3e-5,eps=1e-8)","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:47:58.979215Z","iopub.execute_input":"2021-10-18T12:47:58.980125Z","iopub.status.idle":"2021-10-18T12:48:00.173070Z","shell.execute_reply.started":"2021-10-18T12:47:58.980088Z","shell.execute_reply":"2021-10-18T12:48:00.172249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def doEval(model,valid_dataloader):\n    model.eval()\n    eval_loss=0\n    predictions, true_labels = [], []\n    for batch in valid_dataloader:\n        batch = tuple(t.to(device) for t in batch)\n        data, label = batch\n        with torch.no_grad():\n            outputs = model(data)\n            \n        predictions.extend(torch.argmax(outputs, dim=-1))\n        \n        label_ids = label.to('cpu').numpy()\n        true_labels.extend(label_ids)\n        \n        eval_loss += criteon(outputs, label).mean().item()\n    eval_loss = eval_loss / len(valid_dataloader)\n    rue_labels,predictions=torch.tensor(true_labels),torch.tensor(predictions)\n    acc = accuracy_score(true_labels,predictions)\n    f1 = f1_score(true_labels,predictions)\n    recall = recall_score(true_labels,predictions)\n    precision=precision_score(true_labels,predictions)\n    return acc,precision,recall,f1,eval_loss","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:48:00.174492Z","iopub.execute_input":"2021-10-18T12:48:00.174846Z","iopub.status.idle":"2021-10-18T12:48:00.184032Z","shell.execute_reply.started":"2021-10-18T12:48:00.174806Z","shell.execute_reply":"2021-10-18T12:48:00.183299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def doTrain(model,train_loader):\n    model.train()\n    total_loss = 0\n    predictions, true_labels = [], []\n    for batch in train_loader:\n        optimizer.zero_grad()\n        batch = tuple(t.to(device) for t in batch)\n        data, label = batch\n        outputs = model(data)\n        loss=criteon(outputs, label).mean()\n        loss.backward()\n        total_loss += loss.item()\n        optimizer.step()\n        predictions.extend(torch.argmax(outputs, dim=-1))\n        label_ids = label.to('cpu').numpy()\n        true_labels.extend(label_ids)\n    train_loss = total_loss / len(train_loader)\n    rue_labels,predictions=torch.tensor(true_labels),torch.tensor(predictions)\n    acc = accuracy_score(true_labels,predictions)\n    f1 = f1_score(true_labels,predictions)\n    recall = recall_score(true_labels,predictions)\n    precision=precision_score(true_labels,predictions)\n    return acc,precision,recall,f1,train_loss","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:48:00.186603Z","iopub.execute_input":"2021-10-18T12:48:00.187059Z","iopub.status.idle":"2021-10-18T12:48:00.200074Z","shell.execute_reply.started":"2021-10-18T12:48:00.187013Z","shell.execute_reply":"2021-10-18T12:48:00.199141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc_eva = []\nf1_eva = []\nrecall_eva = []\nprecision_eva=[]\n\nacc_train = []\nf1_train = []\nrecall_train = []\nprecision_train=[]\n\nloss_train=[]\nloss_eva=[]\n## Store the average loss after each epoch so we can plot them.\nfor _ in trange(20, desc=\"Epoch\"):\n    acc,precision,recall,f1,train_loss=doTrain(model,train_loader)\n    acc_train.append(acc)\n    f1_train.append(f1)\n    recall_train.append(recall)\n    precision_train.append(precision)\n    loss_train.append(train_loss)\n    print(\"Train Accuracy-Score: {}\".format(acc))\n    print(\"Train F1-Score: {}\".format(f1))\n    print(\"Train recall-Score: {}\".format(recall))\n    print(\"Train precision-Score: {}\".format(precision))\n    print(\"Average Train loss: {}\".format(train_loss))\n    print()\n    \n    acc,precision,recall,f1,eval_loss=doEval(model,val_loader)\n    acc_eva.append(acc)\n    f1_eva.append(f1)\n    recall_eva.append(recall)\n    precision_eva.append(precision)\n    loss_eva.append(eval_loss)\n    print(\"Validation Accuracy-Score: {}\".format(acc))\n    print(\"Validation F1-Score: {}\".format(f1))\n    print(\"Validation recall-Score: {}\".format(recall))\n    print(\"Validation precision-Score: {}\".format(precision))\n    print(\"Average Validation loss: {}\".format(eval_loss))\n    print()","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:48:00.203514Z","iopub.execute_input":"2021-10-18T12:48:00.203728Z","iopub.status.idle":"2021-10-18T12:50:31.382236Z","shell.execute_reply.started":"2021-10-18T12:48:00.203702Z","shell.execute_reply":"2021-10-18T12:50:31.381007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\nimport seaborn as sns\n\n# Use plot styling from seaborn.\nsns.set(style='darkgrid')\n\n# Increase the plot size and font size.\nsns.set(font_scale=1.5)\nplt.rcParams[\"figure.figsize\"] = (12,6)\n\n# Plot the learning curve.\nplt.plot(loss_train, '-o', label=\"training loss\")\nplt.plot(loss_eva, '-o', label=\"validation loss\")\n# Label the plot.\nplt.title(\"Learning curve\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:50:31.383554Z","iopub.status.idle":"2021-10-18T12:50:31.384022Z","shell.execute_reply.started":"2021-10-18T12:50:31.383748Z","shell.execute_reply":"2021-10-18T12:50:31.383770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\nimport seaborn as sns\nsns.set(style='darkgrid')\nsns.set(font_scale=1.5)\nplt.rcParams[\"figure.figsize\"] = (12,6)\nplt.plot(acc_eva, 'b-o', label=\"evalation\")\nplt.plot(acc_train, 'r-o', label=\"target train\")\nplt.title(\"Acc curve\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"acc\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:50:31.385287Z","iopub.status.idle":"2021-10-18T12:50:31.386117Z","shell.execute_reply.started":"2021-10-18T12:50:31.385851Z","shell.execute_reply":"2021-10-18T12:50:31.385876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\nimport seaborn as sns\nsns.set(style='darkgrid')\nsns.set(font_scale=1.5)\nplt.rcParams[\"figure.figsize\"] = (12,6)\nplt.plot(f1_eva, 'b-o', label=\"evalation\")\nplt.plot(f1_train, 'r-o', label=\"target train\")\nplt.title(\"F1 curve\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"f1\")\nplt.legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:50:31.387396Z","iopub.status.idle":"2021-10-18T12:50:31.387810Z","shell.execute_reply.started":"2021-10-18T12:50:31.387591Z","shell.execute_reply":"2021-10-18T12:50:31.387614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\nimport seaborn as sns\nsns.set(style='darkgrid')\nsns.set(font_scale=1.5)\nplt.rcParams[\"figure.figsize\"] = (12,6)\nplt.plot(recall_eva, 'b-o', label=\"evalation\")\nplt.plot(recall_train, 'r-o', label=\"target train\")\nplt.title(\"recall curve\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"recall\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:50:31.389176Z","iopub.status.idle":"2021-10-18T12:50:31.389592Z","shell.execute_reply.started":"2021-10-18T12:50:31.389363Z","shell.execute_reply":"2021-10-18T12:50:31.389385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\nimport seaborn as sns\nsns.set(style='darkgrid')\nsns.set(font_scale=1.5)\nplt.rcParams[\"figure.figsize\"] = (12,6)\nplt.plot(precision_eva, 'b-o', label=\"evalation\")\nplt.plot(precision_train, 'r-o', label=\"target train\")\nplt.title(\"precision curve\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"precision\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-18T12:50:31.391080Z","iopub.status.idle":"2021-10-18T12:50:31.391495Z","shell.execute_reply.started":"2021-10-18T12:50:31.391266Z","shell.execute_reply":"2021-10-18T12:50:31.391288Z"},"trusted":true},"execution_count":null,"outputs":[]}]}