S01E02 深度学习介绍
图片分类 image-net.org/
物体检测与分割 github.com/matterport/…
人脸合成
文件生成图片 openai.com/blog/dall-e
文字生成
大语言模型
无人驾驶
S01E03 环境搭建
创建环境
conda create -n d2l-zh python=3.8 pip
conda activate d2l-zh
安装依赖
pip install jupyter d2l torch torchvision
安装依赖过程中的报错
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. torchattacks 3.4.0 requires requests~=2.25.1, but you have requests 2.31.0 which is incompatible.
wget
https://zh-v2.d2l.ai/d2l-zh.zip
unzip d2l-zh.zip
jupyter notebook
S01E04 pytorch 数据操作
向量访问
S01E05 pytorch 数据操作实现
import torch
x=torch.arange(12)
x
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
x.shape
torch.Size([12])
X = x.reshape(3,4)
X
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
torch.zeros((2,3,4))
tensor([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]])
torch.tensor([[4,3,2,1],[2,3,4,5],[6,5,4,3]])
tensor([[4, 3, 2, 1],
[2, 3, 4, 5],
[6, 5, 4, 3]])
x=torch.tensor([1.0,2,3,4])
y=torch.tensor([2,2,2,2])
x+y,x-y,x*y,x/y,x**y
(tensor([3., 4., 5., 6.]),
tensor([-1., 0., 1., 2.]),
tensor([2., 4., 6., 8.]),
tensor([0.5000, 1.0000, 1.5000, 2.0000]),
tensor([ 1., 4., 9., 16.]))
torch.exp(x)
tensor([ 2.7183, 7.3891, 20.0855, 54.5981])
x = torch.arange(12, dtype=torch.float32).reshape((3, 4))
y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
# 在0维进行连接 在2维矩阵中指行维度 所以此处是在行上连接(两个3行连接成6行)
torch.cat((x, y), dim=0)
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[ 2., 1., 4., 3.],
[ 1., 2., 3., 4.],
[ 4., 3., 2., 1.]])
# 两个4列连接成8列
torch.cat((x, y), dim=1)
tensor([[ 0., 1., 2., 3., 2., 1., 4., 3.],
[ 4., 5., 6., 7., 1., 2., 3., 4.],
[ 8., 9., 10., 11., 4., 3., 2., 1.]])
x==y
tensor([[False, True, False, True],
[False, False, False, False],
[False, False, False, False]])
y.sum()
tensor(30.)
A = x.numpy()
B = torch.tensor(A)
type(A), type(B)
(numpy.ndarray, torch.Tensor)
a=torch.tensor([3.5])
a,a.item(),float(a)
(tensor([3.5000]), 3.5, 3.5)
使用 openvino 调用 onnx 模型
import cv2
import torchvision.transforms as transforms
from openvino.runtime import Core
def predict(onnx_file_path, image_path):
img_bgr = cv2.imread(image_path, -1)
PIC_RESIZE = (299, 299)
img_bgr_resize = cv2.resize(img_bgr, PIC_RESIZE)
img_rgb = cv2.cvtColor(img_bgr_resize, cv2.COLOR_BGR2RGB)
data_transforms = transforms.Compose([
transforms.ToTensor(),
transforms.Resize(PIC_RESIZE, antialias=True),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
data = data_transforms(img_rgb)
data.unsqueeze_(0)
c_m = Core().compile_model(model=onnx_file_path, device_name='GPU')
res_onnx = c_m([data])
output_layer = c_m.output(0)
res_onnx_i = res_onnx[output_layer]
return res_onnx_i.argmax()
if __name__ == '__main__':
pic_path = r"1716795604217.png"
onnx_path = "sr_scene.onnx"
class_list = ["setting", "map", "playing"]
index = predict(onnx_path, pic_path)
print(class_list[index])