From d9ce89767325dd74f08795b6ef7c2cfcb1945a44 Mon Sep 17 00:00:00 2001 From: lixiaoping <648290310@qq.com> Date: Tue, 22 Dec 2020 09:37:17 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20lixiaoping?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework/class_2/lixiaoping/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 homework/class_2/lixiaoping/.keep diff --git a/homework/class_2/lixiaoping/.keep b/homework/class_2/lixiaoping/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From 9cc880849c2331f5d5c8698eb335bf0ebd889375 Mon Sep 17 00:00:00 2001 From: lixiaoping <648290310@qq.com> Date: Tue, 22 Dec 2020 09:38:22 +0800 Subject: [PATCH 2/9] add homework/class_2/lixiaoping. --- homework/class_2/lixiaoping/data.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 homework/class_2/lixiaoping/data.py diff --git a/homework/class_2/lixiaoping/data.py b/homework/class_2/lixiaoping/data.py new file mode 100644 index 0000000..e69de29 -- Gitee From 9bb116b68fba1c1d435f35a6381022bf95943f3b Mon Sep 17 00:00:00 2001 From: lixiaoping <648290310@qq.com> Date: Tue, 22 Dec 2020 09:38:50 +0800 Subject: [PATCH 3/9] update homework/class_2/lixiaoping/data.py. --- homework/class_2/lixiaoping/data.py | 65 +++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/homework/class_2/lixiaoping/data.py b/homework/class_2/lixiaoping/data.py index e69de29..29a4d1e 100644 --- a/homework/class_2/lixiaoping/data.py +++ b/homework/class_2/lixiaoping/data.py @@ -0,0 +1,65 @@ +import torch +import torchvision +import torchvision.transforms as transforms +import numpy as np +import matplotlib.pyplot as plt + + +def get_cifar_data(batch_size): + # data compose + transform = transforms.Compose([ + # padding to 36x36 + transforms.Pad(4), + # Random Horizontal Flip + transforms.RandomHorizontalFlip(), + # cut to 32x32 + transforms.RandomCrop(32), + # to tensor + transforms.ToTensor(), + # normalization + transforms.Normalize(mean=(0.5, 0.5, 0.5), + std=(0.5, 0.5, 0.5)) + ]) + + # cifar10 path + cifar10Path = '/home/lxp/newascend/pytorch/test/test_npu/test_cifar10' + + # train data + train_dataset = torchvision.datasets.CIFAR10(root=cifar10Path, + train=True, + transform=transform, + download=True) + + # test data + test_dataset = torchvision.datasets.CIFAR10(root=cifar10Path, + train=False, + transform=transform) + + train_loader = torch.utils.data.DataLoader(dataset=train_dataset, + batch_size=batch_size, + shuffle=True) + + test_loader = torch.utils.data.DataLoader(dataset=test_dataset, + batch_size=batch_size, + shuffle=False) + + return train_loader, test_loader + + +if __name__ == '__main__': + train_loader, test_loader = get_cifar_data(batch_size=32) + + data_iter = iter(train_loader) + images, labels = next(data_iter) + idx = 31 + image = images[idx].numpy() + image = np.transpose(image, (1, 2, 0)) + plt.imshow(image) + plt.show() + classes = ('plane', 'car', 'bird', 'cat', + 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') + print(classes[labels[idx].numpy()]) + print("1") + + + print(type(train_loader)) -- Gitee From f9fbdff19e32cff2faff41f8b4f2b7a557c81828 Mon Sep 17 00:00:00 2001 From: lixiaoping <648290310@qq.com> Date: Tue, 22 Dec 2020 09:39:19 +0800 Subject: [PATCH 4/9] add homework/class_2/lixiaoping/model.py. --- homework/class_2/lixiaoping/model.py | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 homework/class_2/lixiaoping/model.py diff --git a/homework/class_2/lixiaoping/model.py b/homework/class_2/lixiaoping/model.py new file mode 100644 index 0000000..c86885d --- /dev/null +++ b/homework/class_2/lixiaoping/model.py @@ -0,0 +1,40 @@ +import torch +import torch.nn as nn + + +class ConvNet(nn.Module): + def __init__(self, num_classes=10): + super(ConvNet, self).__init__() + self.conv1 = nn.Sequential( + # convolution + nn.Conv2d(3, 16, kernel_size=5, stride=1, padding=2), + # batch normalization + nn.BatchNorm2d(16), + # ReLU activation + nn.ELU(), + # max pool + nn.MaxPool2d(kernel_size=2, stride=2)) + + self.conv2 = nn.Sequential( + nn.Conv2d(16, 16, kernel_size=5, stride=1, padding=2), + nn.BatchNorm2d(16), + nn.ReLU(), + nn.AvgPool2d(kernel_size=2, stride=2) + ) + + self.conv3 = nn.Sequential ( + nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2), + nn.BatchNorm2d(32), + nn.Softplus(), + nn.MaxPool2d(kernel_size=2, stride=2)) + + self.fc = nn.Linear(4 * 4 * 32, num_classes) + + # forward channel + def forward(self, x): + out = self.conv1(x) + out = self.conv2(out) + out = self.conv3(out) + out = out.reshape(out.size(0), -1) + out = self.fc(out) + return out -- Gitee From 8397b9c5fda33e3c3dc9fd88a75f749550e8a6b0 Mon Sep 17 00:00:00 2001 From: lixiaoping <648290310@qq.com> Date: Tue, 22 Dec 2020 09:40:12 +0800 Subject: [PATCH 5/9] add homework/class_2/lixiaoping/train.py. --- homework/class_2/lixiaoping/train.py | 62 ++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 homework/class_2/lixiaoping/train.py diff --git a/homework/class_2/lixiaoping/train.py b/homework/class_2/lixiaoping/train.py new file mode 100644 index 0000000..0d8e499 --- /dev/null +++ b/homework/class_2/lixiaoping/train.py @@ -0,0 +1,62 @@ +import torch +import torch.nn as nn +import data +import model + + +def train(train_loader, test_loader, model, learning_rate, num_epochs): + # loss and optimization + criterion = nn.CrossEntropyLoss() + optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) + + # training + total_step = len(train_loader) + for epoch in range(num_epochs): + for i, (images, labels) in enumerate(train_loader): + # forward + outputs = model(images) + loss = criterion(outputs, labels) + + # backward and optimization + optimizer.zero_grad() + loss.backward() + optimizer.step() + + if (i + 1) % 100 == 0: + print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' + .format(epoch + 1, num_epochs, i + 1, total_step, loss.item())) + + # evaluation and test + model.eval() + with torch.no_grad(): + correct = 0 + total = 0 + for images, labels in test_loader: + outputs = model(images) + _, predicted = torch.max(outputs.data, 1) + total += labels.size(0) + correct += (predicted == labels).sum().item() + + print('Test Accuracy of the model on the test images: {} %'.format(100 * correct / total)) + + # save model + model_info = { + "iter_num": total_step * num_epochs, + "optimizer": optimizer, + "model": model.state_dict() + } + torch.save(model_info, 'model_info.ckpt') + + +if __name__ == '__main__': + # hyper parameters + num_epochs = 10 + num_classes = 10 + batch_size = 32 + learning_rate = 0.01 + + train_loader, test_loader = data.get_cifar_data(batch_size) + + model = model.ConvNet(num_classes) + + train(train_loader, test_loader, model, learning_rate, num_epochs) -- Gitee From 939b578041fb024193c764786e1494510b6b9384 Mon Sep 17 00:00:00 2001 From: lixiaoping <648290310@qq.com> Date: Tue, 22 Dec 2020 09:40:54 +0800 Subject: [PATCH 6/9] add homework/class_2/lixiaoping/test.py. --- homework/class_2/lixiaoping/test.py | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 homework/class_2/lixiaoping/test.py diff --git a/homework/class_2/lixiaoping/test.py b/homework/class_2/lixiaoping/test.py new file mode 100644 index 0000000..479ba43 --- /dev/null +++ b/homework/class_2/lixiaoping/test.py @@ -0,0 +1,35 @@ +import numpy as np +import matplotlib.pyplot as plt +import torch +import data +import model + + +if __name__ == '__main__': + # cifar10 classes + classes = ('plane', 'car', 'bird', 'cat', + 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') + + train_loader, test_loader = data.get_cifar_data(32) + data_iter = iter(test_loader) + images, labels = next(data_iter) + + idx = 2 + image = images[idx].numpy() + image = np.transpose(image, (1, 2, 0)) + plt.imshow(image) + plt.show() + print(classes[labels[idx].numpy()]) + + image_batch = image.reshape(-1, 3, 32, 32) + image_tensor = torch.from_numpy(image_batch) + + model = model.ConvNet(10) + model_info = torch.load("model_info.ckpt") + model.load_state_dict(model_info["model"]) + model.eval() + output = model(image_tensor) + _, predicted = torch.max(output.data, 1) + pre = predicted.numpy() + print(pre) + print(classes[pre[0]]) -- Gitee From 96c4557bdb26df2a2224c55dc0f18e6a718e6f6b Mon Sep 17 00:00:00 2001 From: lixiaoping <648290310@qq.com> Date: Tue, 22 Dec 2020 09:42:03 +0800 Subject: [PATCH 7/9] add homework/class_2/lixiaoping/common_device_type.py. --- .../class_2/lixiaoping/common_device_type.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 homework/class_2/lixiaoping/common_device_type.py diff --git a/homework/class_2/lixiaoping/common_device_type.py b/homework/class_2/lixiaoping/common_device_type.py new file mode 100644 index 0000000..d5d3329 --- /dev/null +++ b/homework/class_2/lixiaoping/common_device_type.py @@ -0,0 +1,22 @@ +# Copyright (c) 2020 Huawei Technologies Co., Ltd +# Copyright (c) 2019, Facebook CORPORATION. +# All rights reserved. +# +# Licensed under the BSD 3-Clause License (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://opensource.org/licenses/BSD-3-Clause +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import sys +common_path = os.path.dirname("../common/") +if common_path not in sys.path: + sys.path.append(common_path) +from common_device_type_new import * \ No newline at end of file -- Gitee From 25219b0eb575ffc6fbd0c16a1afd203a4616940e Mon Sep 17 00:00:00 2001 From: lixiaoping <648290310@qq.com> Date: Tue, 22 Dec 2020 09:42:41 +0800 Subject: [PATCH 8/9] add homework/class_2/lixiaoping/common_utils.py. --- homework/class_2/lixiaoping/common_utils.py | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 homework/class_2/lixiaoping/common_utils.py diff --git a/homework/class_2/lixiaoping/common_utils.py b/homework/class_2/lixiaoping/common_utils.py new file mode 100644 index 0000000..5fd21b1 --- /dev/null +++ b/homework/class_2/lixiaoping/common_utils.py @@ -0,0 +1,28 @@ +# Copyright (c) 2020 Huawei Technologies Co., Ltd +# Copyright (c) 2019, Facebook CORPORATION. +# All rights reserved. +# +# Licensed under the BSD 3-Clause License (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://opensource.org/licenses/BSD-3-Clause +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +r"""Importing this file must **not** initialize CUDA context. test_distributed +relies on this assumption to properly run. This means that when this is imported +no CUDA calls shall be made, including torch.cuda.device_count(), etc. + +torch.testing._internal.common_cuda.py can freely initialize CUDA context when imported. +""" +import os +import sys +common_path = os.path.dirname("../common/") +if common_path not in sys.path: + sys.path.append(common_path) +from common_utils_new import * \ No newline at end of file -- Gitee From 3b17d0c9441b613cc54f779fea1c2909996aec15 Mon Sep 17 00:00:00 2001 From: lixiaoping <648290310@qq.com> Date: Tue, 22 Dec 2020 09:43:20 +0800 Subject: [PATCH 9/9] add homework/class_2/lixiaoping/util_test.py. --- homework/class_2/lixiaoping/util_test.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 homework/class_2/lixiaoping/util_test.py diff --git a/homework/class_2/lixiaoping/util_test.py b/homework/class_2/lixiaoping/util_test.py new file mode 100644 index 0000000..f981826 --- /dev/null +++ b/homework/class_2/lixiaoping/util_test.py @@ -0,0 +1,22 @@ +# Copyright (c) 2020 Huawei Technologies Co., Ltd +# Copyright (c) 2019, Facebook CORPORATION. +# All rights reserved. +# +# Licensed under the BSD 3-Clause License (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://opensource.org/licenses/BSD-3-Clause +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import os +import sys +common_path = os.path.dirname("../common/") +if common_path not in sys.path: + sys.path.append(common_path) + +from util_test_new import * -- Gitee