quick_start_en.md 4.0 KB
Newer Older
1 2
Setting up First Visualization
==============================
D
daminglu 已提交
3 4 5 6 7 8

VisualDL is a deep learning visualization tool. It can be used to visualize intermediate and final results for training.
Currently, VisualDL supports visualization features as follows:

- Scalar: plot of trends, can be used to show error trends during training.
- Image: image visualization, can be used to show intermediate images from CNN.
9
- Audio: can be used to play input audio samples or generated audio samples.
D
daminglu 已提交
10 11
- Histogram: can be used to show parameter distribution and trend.
- Graph: can be used to visualize model structure.
12
- High dimensional: can be used to visualize embeddings projection.
D
daminglu 已提交
13

T
Thuan Nguyen 已提交
14 15
VisualDL provides both Python SDK and C++ SDK in nature. It can support various frameworks.
Users can retrieve visualization data by simply adding a few lines of code using Pythong SDK.
D
daminglu 已提交
16 17 18 19 20 21 22 23 24 25 26 27
In addition, users can also have a deep integration by using the C++ SDK at a lower level.

## A Simple Python Demo on Scalar
For simplicity, we first try to use Python SDK.

The first step of using VisualDL is to create a `LogWriter' that can store visualization data.


```python
from VisualDL import LogWriter
from random import random

J
Jeff Wang 已提交
28
logw = LogWriter("./random_log", sync_cycle=10000)
D
daminglu 已提交
29 30
```

T
Thuan Nguyen 已提交
31
The first parameter points to a folder; the second parameter `sync_cycle` specifies out of how memory operations should be
J
Jeff Wang 已提交
32
store the data into hard drive.
J
Jeff Wang 已提交
33 34

### sync_cycle
J
Jeff Wang 已提交
35
Writing is a heavy operation. Setting `sync_cycle` might slow down your training.
J
Jeff Wang 已提交
36
A good starting point is to set the `sync_cycle` to be at least twice the amount of data point your would like to capture.
D
daminglu 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49

There are different modes for model training, such as training, validating and testing. All these correspond to `mode' in VisualDL.
We can use the following pattern to specify mode:


```python
with logw.mode("train") as logger:
    pass
```

Next we create a `Scalar` component. Each component needs a tag. A tag can be a string of any length.
For example, `layer/classification/error`.

W
WANG Lei 已提交
50
```python
D
daminglu 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
# create scalars in mode train and test.
with logw.mode('train') as logger:
    scalar0 = logger.scalar("scratch/scalar")

with logw.mode('test') as logger:
    scalar1 = logger.scalar("scratch/scalar")

# add scalar records.
for step in range(200):
    scalar0.add_record(step, step * 1. / 200)
    scalar1.add_record(step, 1. - step * 1. / 200)
```

The example above randomly generated some logs. Next we can open the board page:

```
67
visualdl --logdir ./random_log --port 8080
D
daminglu 已提交
68 69 70 71 72
```

Point your browser to `http://0.0.0.0:8080`, you can see the scalar as follows:

<p align="center">
T
Thuan Nguyen 已提交
73
<img src="https://raw.githubusercontent.com/PaddlePaddle/VisualDL/develop/docs/images/scratch_scalar.png"/>
D
daminglu 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
</p>

## Scalar Demo in C++
VisualDL's C++ SDK is very similar to its Python SDK. The Python demo above can be writen in C++ as follows:

```c++
  const auto dir = "./randomlog";
  LogWriter logwriter(dir, 30);
  auto logger = logwriter.AsMode("train");

  components::Scalar<float> scalar0(writer.AddTablet("scalar0"));
  components::Scalar<float> scalar1(writer.AddTablet("scalar1"));

  for (int step = 0; step < 200; step++) {
    scalar0.AddRecord(step, step * 1. / 200);
    scalar1.AddRecord(step, 1. - step * 1. / 200);
  }
```

## Visualization Based on ONNX Model Structure
T
Thuan Nguyen 已提交
94
VisualDL supports the visualization for the format in [ONNX](https://github.com/onnx/onnx).
D
daminglu 已提交
95 96 97
Currently, ONNX supports format conversion among various deep learning frameworks such as `MXNet`, `PyTorch`, `Caffe2`, `Caffe`.

```
98
visualdl --logdir somedir --model_pb <path_to_onnx_model>
D
daminglu 已提交
99 100 101 102 103
```

For example, for the MNIST dataset, Graph component can render model graph as below:

<p align=center>
T
Thuan Nguyen 已提交
104
<img width="70%" src="https://raw.githubusercontent.com/PaddlePaddle/VisualDL/develop/demo/mxnet/mxnet_graph.gif" />
D
daminglu 已提交
105
</p>
J
Jeff Wang 已提交
106 107 108 109 110

Please consult [ONNX tutorials](https://github.com/onnx/tutorials) on how to export the ONNX format model.

The VisualDL Graphing system uses `GraphViz` to visualize the ONNX model. To enable the VisualDL Graph feature,
please install [GraphViz](https://www.graphviz.org/download/).