Skip to content

Commit

Permalink
[docs]: update docs html
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhikangNiu committed Oct 2, 2023
1 parent f9b4438 commit fa1e035
Show file tree
Hide file tree
Showing 122 changed files with 14,568 additions and 577 deletions.
Binary file added docs/_images/CPU&NPU.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/DDP.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/DP.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/LSTM-arch.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/LSTM-gate_f.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/LSTM-gate_m.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/LSTM-gate_o.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/LSTM-update.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/MLU.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/acc_wandb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/backends.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/block1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/dadiannao.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/diannao.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/expansion1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/shidiannao2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/shortcut1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/torchaudio_feature_extractions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/wandb_api_keys.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/wandb_config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_images/wandb_sys.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
181 changes: 181 additions & 0 deletions docs/_sources/第七章/7.4 使用wandb可视化训练过程.md.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# 7.4 使用wandb可视化训练过程

在上一节中,我们使用了Tensorboard可视化训练过程,但是Tensorboard对数据的保存仅限于本地,也很难分析超参数不同对实验的影响。wandb的出现很好的解决了这些问题,因此在本章节中,我们将对wandb进行简要介绍。
wandb是Weights & Biases的缩写,它能够自动记录模型训练过程中的超参数和输出指标,然后可视化和比较结果,并快速与其他人共享结果。目前它能够和Jupyter、TensorFlow、Pytorch、Keras、Scikit、fast.ai、LightGBM、XGBoost一起结合使用。

经过本节的学习,你将收获:

- wandb的安装
- wandb的使用
- demo演示

## 7.4.1 wandb的安装

wandb的安装非常简单,我们只需要使用pip安装即可。

```python
pip install wandb
```
安装完成后,我们需要在[官网](https://wandb.ai/)注册一个账号并复制下自己的API keys,然后在本地使用下面的命令登录。

```python
wandb login
```
这时,我们会看到下面的界面,只需要粘贴你的API keys即可。
![](./figures/wandb_api_keys.png)

## 7.4.2 wandb的使用

wandb的使用也非常简单,只需要在代码中添加几行代码即可。

```python
import wandb
wandb.init(project='my-project', entity='my-name')
```

这里的project和entity是你在wandb上创建的项目名称和用户名,如果你还没有创建项目,可以参考[官方文档](https://docs.wandb.ai/quickstart)。

## 7.4.3 demo演示

下面我们使用一个CIFAR10的图像分类demo来演示wandb的使用。


```python

import random # to set the python random seed
import numpy # to set the numpy random seed
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
from torchvision.models import resnet18
import warnings
warnings.filterwarnings('ignore')
```
使用wandb的第一步是初始化wandb,这里我们使用wandb.init()函数来初始化wandb,其中project是你在wandb上创建的项目名称,name是你的实验名称。
```python
# 初始化wandb
import wandb
wandb.init(project="thorough-pytorch",
name="wandb_demo",)
```
使用wandb的第二步是设置超参数,这里我们使用wandb.config来设置超参数,这样我们就可以在wandb的界面上看到超参数的变化。wandb.config的使用方法和字典类似,我们可以使用config.key的方式来设置超参数。

```python
# 超参数设置
config = wandb.config # config的初始化
config.batch_size = 64
config.test_batch_size = 10
config.epochs = 5
config.lr = 0.01
config.momentum = 0.1
config.use_cuda = True
config.seed = 2043
config.log_interval = 10

# 设置随机数
def set_seed(seed):
random.seed(config.seed)
torch.manual_seed(config.seed)
numpy.random.seed(config.seed)

```
第三步是构建训练和测试的pipeline,这里我们使用pytorch的CIFAR10数据集和resnet18来构建训练和测试的pipeline。
```python
def train(model, device, train_loader, optimizer):
model.train()

for batch_id, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
criterion = nn.CrossEntropyLoss()
loss = criterion(output, target)
loss.backward()
optimizer.step()

# wandb.log用来记录一些日志(accuracy,loss and epoch), 便于随时查看网路的性能
def test(model, device, test_loader, classes):
model.eval()
test_loss = 0
correct = 0
example_images = []

with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
criterion = nn.CrossEntropyLoss()
test_loss += criterion(output, target).item()
pred = output.max(1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()
example_images.append(wandb.Image(
data[0], caption="Pred:{} Truth:{}".format(classes[pred[0].item()], classes[target[0]])))

# 使用wandb.log 记录你想记录的指标
wandb.log({
"Examples": example_images,
"Test Accuracy": 100. * correct / len(test_loader.dataset),
"Test Loss": test_loss
})

wandb.watch_called = False


def main():
use_cuda = config.use_cuda and torch.cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}

# 设置随机数
set_seed(config.seed)
torch.backends.cudnn.deterministic = True

# 数据预处理
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

# 加载数据
train_loader = DataLoader(datasets.CIFAR10(
root='dataset',
train=True,
download=True,
transform=transform
), batch_size=config.batch_size, shuffle=True, **kwargs)

test_loader = DataLoader(datasets.CIFAR10(
root='dataset',
train=False,
download=True,
transform=transform
), batch_size=config.batch_size, shuffle=False, **kwargs)

classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

model = resnet18(pretrained=True).to(device)
optimizer = optim.SGD(model.parameters(), lr=config.lr, momentum=config.momentum)

wandb.watch(model, log="all")
for epoch in range(1, config.epochs + 1):
train(model, device, train_loader, optimizer)
test(model, device, test_loader, classes)

# 本地和云端模型保存
torch.save(model.state_dict(), 'model.pth')
wandb.save('model.pth')


if __name__ == '__main__':
main()

```
当我们运行完上面的代码后,我们就可以在wandb的界面上看到我们的训练结果了和系统的性能指标。同时,我们还可以在setting里面设置训练完给我们发送邮件,这样我们就可以在训练完之后及时的查看训练结果了。
![](./figures/acc_wandb.png)
![](./figures/wandb_sys.png)
![](./figures/wandb_config.png)

我们可以发现,使用wandb可以很方便的记录我们的训练结果,除此之外,wandb还为我们提供了很多的功能,比如:模型的超参数搜索,模型的版本控制,模型的部署等等。这些功能都可以帮助我们更好的管理我们的模型,更好的进行模型的迭代和优化。这些功能我们在后面的更新中会进行介绍。
1 change: 1 addition & 0 deletions docs/_sources/第七章/index.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
7.1 可视化网络结构
7.2 CNN卷积层可视化
7.3 使用TensorBoard可视化训练过程
7.4 使用wandb可视化训练过程
```
2 changes: 1 addition & 1 deletion docs/_sources/第三章/3.2 基本配置.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ os.environ['CUDA_VISIBLE_DEVICES'] = '0,1' # 指明调用的GPU为0,1号
# 方案二:使用“device”,后续对要使用GPU的变量用.to(device)即可
device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu") # 指明调用的GPU为1号
```
```


当然还会有一些其他模块或用户自定义模块会用到的参数,有需要也可以在一开始进行设置。
61 changes: 34 additions & 27 deletions docs/_sources/第三章/3.3 数据读入.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,56 @@ val_data = datasets.ImageFolder(val_path, transform=data_transform)

这里使用了PyTorch自带的ImageFolder类的用于读取按一定结构存储的图片数据(path对应图片存放的目录,目录下包含若干子目录,每个子目录对应属于同一个类的图片)。

其中data_transform可以对图像进行一定的变换,如翻转、裁剪等操作,可自己定义。这里我们会在下一章通过实战加以介绍
其中`data_transform`可以对图像进行一定的变换,如翻转、裁剪等操作,可自己定义。这里我们会在下一章通过实战加以介绍并在[notebook](https://github.com/datawhalechina/thorough-pytorch/tree/main/notebook/%E7%AC%AC%E5%85%AB%E7%AB%A0%20PyTorch%E7%94%9F%E6%80%81%E7%AE%80%E4%BB%8B)中做了示例代码

这里另外给出一个例子,其中图片存放在一个文件夹,另外有一个csv文件给出了图片名称对应的标签。这种情况下需要自己来定义Dataset类:
这里我们给出一个自己定制Dataset的例子

```python

import os
import pandas as pd
from torchvision.io import read_image

class MyDataset(Dataset):
def __init__(self, data_dir, info_csv, image_list, transform=None):
def __init__(self, annotations_file, img_dir, transform=None, target_transform=None):
"""
Args:
data_dir: path to image directory.
info_csv: path to the csv file containing image indexes
with corresponding labels.
image_list: path to the txt file contains image names to training/validation set
transform: optional transform to be applied on a sample.
annotations_file (string): Path to the csv file with annotations.
img_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied
on a sample.
target_transform (callable, optional): Optional transform to be applied
on the target.
"""
label_info = pd.read_csv(info_csv)
image_file = open(image_list).readlines()
self.data_dir = data_dir
self.image_file = image_file
self.label_info = label_info
self.img_labels = pd.read_csv(annotations_file)
self.img_dir = img_dir
self.transform = transform
self.target_transform = target_transform

def __len__(self):
return len(self.img_labels)

def __getitem__(self, index):
def __getitem__(self, idx):
"""
Args:
index: the index of item
Returns:
image and its labels
idx (int): Index
"""
image_name = self.image_file[index].strip('\n')
raw_label = self.label_info.loc[self.label_info['Image_index'] == image_name]
label = raw_label.iloc[:,0]
image_name = os.path.join(self.data_dir, image_name)
image = Image.open(image_name).convert('RGB')
if self.transform is not None:
img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0])
image = read_image(img_path)
label = self.img_labels.iloc[idx, 1]
if self.transform:
image = self.transform(image)
if self.target_transform:
label = self.target_transform(label)
return image, label

def __len__(self):
return len(self.image_file)
```

其中,我们的标签类似于以下的形式:
```csv
image1.jpg, 0
image2.jpg, 1
......
image9.jpg, 9
```
构建好Dataset后,就可以使用DataLoader来按批次读入数据了,实现代码如下:

```python
Expand Down
6 changes: 1 addition & 5 deletions docs/_sources/第三章/3.4 模型构建.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,6 @@ class Conv2D(nn.Module):
import torch
from torch import nn

# 定义一个函数来计算卷积层。它对输入和输出做相应的升维和降维
import torch
from torch import nn

# 定义一个函数来计算卷积层。它对输入和输出做相应的升维和降维
def comp_conv2d(conv2d, X):
# (1, 1)代表批量大小和通道数
Expand Down Expand Up @@ -432,4 +428,4 @@ AlexNet(
(6): Linear(in_features=4096, out_features=10, bias=True)
)
)
```
```
42 changes: 16 additions & 26 deletions docs/_sources/第三章/3.5 模型初始化.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ tensor([[0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000,
## 初始化函数的封装
人们常常将各种初始化方法定义为一个`initialize_weights()`的函数并在模型初始后进行使用。
```python
def initialize_weights(self):
for m in self.modules():
def initialize_weights(model):
for m in model.modules():
# 判断是否属于Conv2d
if isinstance(m, nn.Conv2d):
torch.nn.init.xavier_normal_(m.weight.data)
torch.nn.init.zeros_(m.weight.data)
# 判断是否有偏置
if m.bias is not None:
torch.nn.init.constant_(m.bias.data,0.3)
Expand Down Expand Up @@ -125,31 +125,21 @@ class MLP(nn.Module):
return self.output(o)

mlp = MLP()
print(list(mlp.parameters()))
print(mlp.hidden.weight.data)
print("-------初始化-------")

initialize_weights(mlp)
print(list(mlp.parameters()))
mlp.apply(initialize_weights)
# 或者initialize_weights(mlp)
print(mlp.hidden.weight.data)
```
```python
[Parameter containing:
tensor([[[[ 0.2103, -0.1679, 0.1757],
[-0.0647, -0.0136, -0.0410],
[ 0.1371, -0.1738, -0.0850]]]], requires_grad=True), Parameter containing:
tensor([0.2507], requires_grad=True), Parameter containing:
tensor([[ 0.2790, -0.1247, 0.2762, 0.1149, -0.2121, -0.3022, -0.1859, 0.2983,
-0.0757, -0.2868]], requires_grad=True), Parameter containing:
tensor([-0.0905], requires_grad=True)]
tensor([[[[ 0.3069, -0.1865, 0.0182],
[ 0.2475, 0.3330, 0.1352],
[-0.0247, -0.0786, 0.1278]]]])
"-------初始化-------"
[Parameter containing:
tensor([[[[-0.3196, -0.0204, -0.5784],
[ 0.2660, 0.2242, -0.4198],
[-0.0952, 0.6033, -0.8108]]]], requires_grad=True),
Parameter containing:
tensor([0.3000], requires_grad=True),
Parameter containing:
tensor([[ 0.7542, 0.5796, 2.2963, -0.1814, -0.9627, 1.9044, 0.4763, 1.2077,
0.8583, 1.9494]], requires_grad=True),
Parameter containing:
tensor([0.], requires_grad=True)]
```
tensor([[[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]]])
```
**注意:**
我们在初始化时,最好不要将模型的参数初始化为0,因为这样会导致梯度消失,从而影响模型的训练效果。因此,我们在初始化时,可以使用其他初始化方法或者将模型初始化为一个很小的值,如0.01,0.1等。
14 changes: 13 additions & 1 deletion docs/_sources/第三章/3.7 训练与评估.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def train(epoch):
data, label = data.cuda(), label.cuda()
optimizer.zero_grad()
output = model(data)
loss = criterion(label, output)
loss = criterion(output, label)
loss.backward()
optimizer.step()
train_loss += loss.item()*data.size(0)
Expand All @@ -101,3 +101,15 @@ def val(epoch):
val_loss = val_loss/len(val_loader.dataset)
print('Epoch: {} \tTraining Loss: {:.6f}'.format(epoch, val_loss))
```
对于图像分类任务,我们还可以使用sklearn.metrics中的classification_report函数来计算模型的准确率、召回率、F1值等指标,如下所示:

```python
from sklearn.metrics import classification_report
"""
将下方代码的labels和preds替换为模型预测出来的所有label和preds,
target_names替换为类别名称,
既可得到模型的分类报告
"""
print(classification_report(labels.cpu(), preds.cpu(), target_names=class_names))
```
除此之外,我们还可以使用`torcheval`或`torchmetric`来对模型进行评估。
Loading

0 comments on commit fa1e035

Please sign in to comment.