赞
踩
门槛最低的深度学习引导 - 知乎 (zhihu.com)https://zhuanlan.zhihu.com/p/463019160MindSpore入门实践 - 知乎 (zhihu.com)https://zhuanlan.zhihu.com/p/463229660
1. 访问 https://gitee.com/mindspore/vision
2. 克隆/下载
3. 在Terminal里面输出 git clone https://gitee.com/mindspore/vision.git
4. 在Terminal里面输出 cd vision
5. 在Terminal里面输出 python setup.py install
- from mindvision.dataset import Mnist
-
- download_train = Mnist(path="./mnist", split="train", batch_size=32, shuffle=True, resize=32, download=True)
- download_eval = Mnist(path="./mnist", split="test", batch_size=32, resize=32, download=True)
-
- dataset_train = download_train.run()
- dataset_eval = download_eval.run()
-
- # 参数说明
- # path:数据集路径
- # split:数据集类型,支持train、test、infer,默认train
- # batch_size:每个训练批次设定的数据大小,默认为32,当前数据集轮次每一个迭代的大小,一起塞到网络模型里面训练的最小单位
- # repeat_num:训练时遍历数据集的次数,默认为1
- # shuffle:是否需要将数据集随机打乱 (可选参数)
- # resize:输出图像的图像大小,默认为32*32
- # download:是否需要下载数据集,默认为false
- from mindvision.classification.models import lenet
-
- network = lenet(num_classes=10)
- import mindspore.nn as nn
- from mindspore.train import Model
-
- # 定义损失函数
- net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
-
- # 定义优化器函数
- net_opt = nn.Momentum(network.trainable_params(), learning_rate=0.01, momentum=0.9)
-
- # 一般来说,学习率learning_rate不会设置太大,它控制着数据每次更新的幅度
- # 动量momentum
- from mindspore.train.callback import ModelCheckpoint, CheckpointConfig
-
- # 设置模型保存参数
- config_ck = CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=10)
- # save_checkpoint_steps=1875 - 每1875次就会保存一次模型
- # keep_checkpoint_max=10 - 最大的保存次数是10
-
- # 应用模型保存参数
- ckpoint = ModelCheckpoint(prefix="lenet", directory="./lenet", config=config_ck)
- from mindvision.engine.callback import LossMonitor
-
- # 初始化模型参数
- model = Model(network, loss_fn=net_loss, optimizer=net_opt, metrics={'acc'})
-
- # 训练网络模型
- model.train(1, dataset_train, callbacks=[ckpoint, LossMonitor(0.01)])
- acc = model.eval(dataset_eval)
-
- print("{}".format(acc))
- from mindspore import load_checkpoint, load_param_into_net
-
- # 加载已经保存的用于测试的模型
- param_dict = load_checkpoint("./lenet/lenet-1_1875.ckpt")
-
- # 加载参数到网络中
- load_param_into_net(network, param_dict)
- import numpy as np
- from mindspore import Tensor
- import matplotlib.pyplot as plt
-
- mnist = Mnist("./mnist", split="test", batch_size=6, resize=32)
- dataset_infer = mnist.run()
- ds_test = dataset_infer.create_dict_iterator() # 构建迭代器,把数据集拿出来放到内存里面
- data = next(ds_test)
- images = data["image"].asnumpy()
- labels = data["label"].asnumpy()
-
- plt.figure()
- for i in range(1, 7):
- plt.subplot(2, 3, i)
- plt.imshow(images[i-1][0], interpolation="None", cmap="gray")
- plt.show()
-
- # 使用函数model.predict预测image对应的分类
- output = model.predict(Tensor(data['image']))
- predicted = np.argmax(output.asnumpy(), axis=1)
-
- # 输出预测分类和实际分类
- print(f'Predicted: "{predicted}", Actual: "{labels}"')
- from mindspore import Tensor
-
- x1 = 0.5
- tensor1 = Tensor(x1)
-
- print(x1) # 0.5
- print(type(x1)) # <class 'float'>
-
- print(tensor1) # 0.5
- print(type(tensor1)) # <class 'mindspore.common.tensor.Tensor'>
-
- x2 = [0.5, 0.6]
- tensor2 = Tensor(x2)
-
- print(x2) # [0.5, 0.6]
- print(type(x2)) # <class 'list'>
-
- print(tensor2) # [0.5 0.6]
- print(type(tensor2)) # <class 'mindspore.common.tensor.Tensor'>
- import numpy as np
-
- arr = np.array([1, 0, 1, 0])
- x_np = Tensor(arr)
-
- print(arr) # [1 0 1 0]
- print(type(arr)) # <class 'numpy.ndarray'>
-
- print(x_np) # [1 0 1 0]
- print(type(x_np)) # <class 'mindspore.common.tensor.Tensor'>
- from mindspore import Tensor
- from mindspore import set_seed
- from mindspore import dtype as mstype
- from mindspore.common import initializer as init
-
- set_seed(1)
-
- tensor3 = Tensor(shape=(2, 2), dtype=mstype.float32, init=init.One())
- print(tensor3) # [[1. 1.] [1. 1.]]
- print(type(tensor3)) # <class 'mindspore.common.tensor.Tensor'>
-
- tensor4 = Tensor(shape=(2, 2), dtype=mstype.float32, init=init.Normal())
- print(tensor4) # [[-0.00128023 -0.01392901] [ 0.0130886 -0.00107818]]
- print(type(tensor4)) # <class 'mindspore.common.tensor.Tensor'>
- from mindspore import ops
-
- x3 = Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))
- print(x3) # [[0 1] [2 1]]
- print(type(x3)) # <class 'mindspore.common.tensor.Tensor'>
-
- onelike = ops.OnesLike()
- output1 = onelike(x3)
- print(output1) # [[1 1] [1 1]]
- print(type(output1)) # <class 'mindspore.common.tensor.Tensor'>
- tensor5 = Tensor(np.array([[0, 1], [2, 3]]).astype(np.int32))
-
- print("Tensor: ", tensor5) # Tensor: [[0 1] [2 3]]
- print("First row: ", tensor5[0]) # First row: [0 1]
- print("Value of top right corner: ", tensor5[0, 1]) # Value of top right corner: 1
- print("Last column: ", tensor5[:, -1]) # Last column: [1 3]
- print("Last column: ", tensor5[..., -1]) # Last column: [1 3]
- print("First column: ", tensor5[:, 0]) # First column: [0 2]
- print("First column: ", tensor5[..., 0]) # First column: [0 2]
- tensor6 = Tensor(np.array([[0, 1], [2, 3]]).astype(np.int32))
- tensor7 = Tensor(np.array([[4, 5], [6, 7]]).astype(np.int32))
- op = ops.Concat()
- output2 = op((tensor6, tensor7))
-
- print("tensor6: ", tensor6) # Tensor6: [[0 1] [2 3]]
- print("tensor7: ", tensor7) # Tensor7: [[4 5] [6 7]]
- print("output2: ", output2) # output2: [[0 1] [2 3] [4 5] [6 7]]
- print("output2 shape: ", output2.shape) # output2 shape: (4, 2)
- # Tensor转换为NumPy,使用asnumpy()将Tensor变量转换为NumPy变量
- zeros = ops.Zeros()
- tensor_x = zeros((2, 2), mstype.float32)
- numpy_x = tensor_x.asnumpy()
-
- print(type(tensor_x)) # <class 'mindspore.common.tensor.Tensor'>
- print(type(numpy_x)) # <class 'numpy.ndarray'>
-
- # NumPy转换为Tensor,与张量创建相同,使用Tensor()将NumPy变量转换为Tensor变量
- output3 = np.array([1, 0, 1, 0])
- t_output3 = Tensor(output3)
-
- print(type(output3)) # <class 'numpy.ndarray'>
- print(type(t_output3)) # <class 'mindspore.common.tensor.Tensor'>
- from mindvision.dataset import Cifar10
-
- # 数据集根目录
- data_dir = "./datasets"
-
- # 下载解压并加载CIFAR-10训练数据集
- dataset = Cifar10(path=data_dir, split="train", batch_size=6, resize=32, download=True)
- dataset = dataset.run()
-
- CIFAR-10数据集文件的目录结构
- datasets/
- ├── cifar-10-batches-py
- │ ├── batches.meta
- │ ├── data_batch_1
- │ ├── data_batch_2
- │ ├── data_batch_3
- │ ├── data_batch_4
- │ ├── data_batch_5
- │ ├── readme.html
- │ └── test_batch
- └── cifar-10-python.tar.gz
- # next():把下一批的数据集通过迭代的方式去访问
- data = next(dataset.create_dict_iterator())
- # Data type:<class 'mindspore.common.tensor.Tensor'>
- # Image shape: (6, 3, 32, 32)
- # 6*3*32*32的张量
- # 32是图片的大小32*32
- # 3是3个通道,意味着图片是彩色的,
- # 6是batch_size的大小,即每一批塞到内存里面的数据集有多少张32*32*3图片
- # Label: [7 1 2 8 7 8]
- print(f"Data type:{type(data['image'])}\nImage shape: {data['image'].shape}\nLabel: {data['label']}")
-
- # Data type:<class 'numpy.ndarray'>
- # Image shape: (6, 3, 32, 32)
- # Label: [8 0 0 2 6 1]
- data = next(dataset.create_dict_iterator(output_numpy=True))
- print(f"Data type:{type(data['image'])}\nImage shape: {data['image'].shape}\nLabel: {data['label']}")
- import numpy as np
- import matplotlib.pyplot as plt
-
- import mindspore.dataset.vision.c_transforms as transforms
-
- trans = [transforms.HWC2CHW()]
- dataset = Cifar10(data_dir, batch_size=6, resize=32, repeat_num=1, shuffle=True, transform=trans)
- data = dataset.run()
- data = next(data.create_dict_iterator())
-
- images = data["image"].asnumpy()
- labels = data["label"].asnumpy()
- print(f"Image shape: {images.shape}, Label: {labels}") # Image shape: (6, 3, 32, 32), Label: [9 3 8 9 6 8]
-
- plt.figure()
- for i in range(1, 7):
- plt.subplot(2, 3, i)
- image_trans = np.transpose(images[i-1], (1, 2, 0))
- plt.title(f"{dataset.index2label[labels[i-1]]}")
- plt.imshow(image_trans, interpolation="None")
- plt.show()
- import numpy as np
- import matplotlib.pyplot as plt
-
- import mindspore.dataset.vision.c_transforms as transforms
-
- # 图像增强
- trans = [
- transforms.RandomCrop((32, 32), (4, 4, 4, 4)), # 对图像进行自动裁剪
- transforms.RandomHorizontalFlip(prob=0.5), # 对图像进行随机水平翻转
- transforms.HWC2CHW(), # (h, w, c)转换为(c, h, w)
- ]
-
- dataset = Cifar10(data_dir, batch_size=6, resize=32, transform=trans)
- data = dataset.run()
- data = next(data.create_dict_iterator())
- images = data["image"].asnumpy()
- labels = data["label"].asnumpy()
- print(f"Image shape: {images.shape}, Label: {labels}") # Image shape: (6, 3, 32, 32), Label: [9 3 8 9 6 8]
-
- plt.figure()
- for i in range(1, 7):
- plt.subplot(2, 3, i)
- image_trans = np.transpose(images[i-1], (1, 2, 0))
- plt.title(f"{dataset.index2label[labels[i-1]]}")
- plt.imshow(image_trans, interpolation="None")
- plt.show()
- import mindspore.nn as nn
-
-
- class LeNet5(nn.Cell):
- """
- LeNet-5网络结构
- """
- def __init__(self, num_class=10, num_channel=1):
- super(LeNet5, self).__init__()
- # 卷积层,输入的通道数为num_channel,输出的通道数为6,卷积核大小为5*5
- self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')
- # 卷积层,输入的通道数为6,输出的通道数为16,卷积核大小为5*5
- self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
- # 全连接层,输入个数为16*5*5,输出个数为120
- self.fc1 = nn.Dense(16 * 5 * 5, 120)
- # 全连接层,输入个数为120,输出个数为84
- self.fc2 = nn.Dense(120, 84)
- # 全连接层,输入个数为84,分类的个数为num_class
- self.fc3 = nn.Dense(84, num_class)
- # ReLU激活函数
- self.relu = nn.ReLU()
- # 池化层
- self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
- # 多维数组展平为一维数组
- self.flatten = nn.Flatten()
-
- def construct(self, x):
- # 使用定义好的运算构建前向网络
- x = self.conv1(x)
- x = self.relu(x)
- x = self.max_pool2d(x)
- x = self.conv2(x)
- x = self.relu(x)
- x = self.max_pool2d(x)
- x = self.flatten(x)
- x = self.fc1(x)
- x = self.relu(x)
- x = self.fc2(x)
- x = self.relu(x)
- x = self.fc3(x)
- return x
-
-
- model = LeNet5()
-
- print(model)
-
- '''
- LeNet5<
- (conv1): Conv2d<input_channels=1, output_channels=6, kernel_size=(5, 5), stride=(1, 1), pad_mode=valid, padding=0, dilation=(1, 1), group=1, has_bias=False, weight_init=normal, bias_init=zeros, format=NCHW>
- (conv2): Conv2d<input_channels=6, output_channels=16, kernel_size=(5, 5), stride=(1, 1), pad_mode=valid, padding=0, dilation=(1, 1), group=1, has_bias=False, weight_init=normal, bias_init=zeros, format=NCHW>
- (fc1): Dense<input_channels=400, output_channels=120, has_bias=True>
- (fc2): Dense<input_channels=120, output_channels=84, has_bias=True>
- (fc3): Dense<input_channels=84, output_channels=10, has_bias=True>
- (relu): ReLU<>
- (max_pool2d): MaxPool2d<kernel_size=2, stride=2, pad_mode=VALID>
- (flatten): Flatten<>
- >
- '''
- import numpy as np
-
- from mindspore import Tensor
- from mindspore import dtype as mstype
-
- # 输入的通道数为1,输出的通道数为6,卷积核大小为5*5,使用normal算子初始化参数,不填充像素
- conv2d = nn.Conv2d(1, 6, 5, has_bias=False, weight_init='normal', pad_mode='valid')
- input_x = Tensor(np.ones([1, 1, 32, 32]), mstype.float32)
-
- print(conv2d(input_x).shape) # (1, 6, 28, 28)
- # 28*28是因为卷积的特殊计算方式,在卷积核进行滤波操作时,图像最边缘的地方因为卷积核没有办法覆盖,所以最终计算的时候,就会去掉边缘的值
-
- # 输入的通道数为1,输出的通道数为6,卷积核大小为5*5,使用normal算子初始化参数,不填充像素
- conv2d = nn.Conv2d(1, 6, 5, has_bias=False, weight_init='normal', pad_mode='same')
- input_x = Tensor(np.ones([1, 1, 32, 32]), mstype.float32)
-
- print(conv2d(input_x).shape) # (1, 6, 32, 32)
- # 32*32是因为卷积之前自动对边缘值进行补充,所以卷积的输出和输入是一样的
- relu = nn.ReLU()
-
- input_x = Tensor(np.array([-1, 2, -3, 2, -1]), mstype.float16)
-
- output = relu(input_x)
- print(output) # [0. 2. 0. 2. 0.]
- max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2) # 采样的窗口设置为2*2,步长设置为2
- input_x = Tensor(np.ones([1, 6, 28, 28]), mstype.float32)
-
- print(max_pool2d(input_x).shape) # (1, 6, 14, 14)
-
- max_pool2d = nn.MaxPool2d(kernel_size=4, stride=4) # 采样的窗口设置为4*4,步长设置为4
- input_x = Tensor(np.ones([1, 6, 28, 28]), mstype.float32)
-
- print(max_pool2d(input_x).shape) # (1, 6, 7, 7)
- flatten = nn.Flatten()
- input_x = Tensor(np.ones([1, 16, 5, 5]), mstype.float32)
- output = flatten(input_x)
-
- print(output.shape) # (1, 400)
- dense = nn.Dense(400, 120, weight_init='normal')
- input_x = Tensor(np.ones([1, 400]), mstype.float32)
- output = dense(input_x)
-
- print(output.shape) # (1, 120)
- for m in model.get_parameters():
- print(f"layer:{m.name}, shape:{m.shape}, dtype:{m.dtype}, requeires_grad:{m.requires_grad}")
-
- # layer:backbone.conv1.weight, shape:(6, 1, 5, 5), dtype:Float32, requeires_grad:True
- # layer:backbone.conv2.weight, shape:(16, 6, 5, 5), dtype:Float32, requeires_grad:True
- # layer:backbone.fc1.weight, shape:(120, 400), dtype:Float32, requeires_grad:True
- # layer:backbone.fc1.bias, shape:(120,), dtype:Float32, requeires_grad:True
- # layer:backbone.fc2.weight, shape:(84, 120), dtype:Float32, requeires_grad:True
- # layer:backbone.fc2.bias, shape:(84,), dtype:Float32, requeires_grad:True
- # layer:backbone.fc3.weight, shape:(10, 84), dtype:Float32, requeires_grad:True
- # layer:backbone.fc3.bias, shape:(10,), dtype:Float32, requeires_grad:True
lenet
接口直接构建LeNet-5网络模型- from mindvision.classification.models import lenet
-
- # num_classes表示分类的数量,pretrained表示是否使用与训练模型进行训练
- model = lenet(num_classes=10, pretrained=False)
-
- for m in model.get_parameters():
- print(f"layer:{m.name}, shape:{m.shape}, dtype:{m.dtype}, requeires_grad:{m.requires_grad}")
-
- # layer:backbone.conv1.weight, shape:(6, 1, 5, 5), dtype:Float32, requeires_grad:True
- # layer:backbone.conv2.weight, shape:(16, 6, 5, 5), dtype:Float32, requeires_grad:True
- # layer:backbone.fc1.weight, shape:(120, 400), dtype:Float32, requeires_grad:True
- # layer:backbone.fc1.bias, shape:(120,), dtype:Float32, requeires_grad:True
- # layer:backbone.fc2.weight, shape:(84, 120), dtype:Float32, requeires_grad:True
- # layer:backbone.fc2.bias, shape:(84,), dtype:Float32, requeires_grad:True
- # layer:backbone.fc3.weight, shape:(10, 84), dtype:Float32, requeires_grad:True
- # layer:backbone.fc3.bias, shape:(10,), dtype:Float32, requeires_grad:True
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。