Skip to content

Commit

Permalink
gluonbook -> import d2lzh as d2l
Browse files Browse the repository at this point in the history
  • Loading branch information
astonzhang committed Jan 3, 2019
1 parent c86bbc2 commit f0dac92
Show file tree
Hide file tree
Showing 57 changed files with 364 additions and 355 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `gluonbook`包索引
# `d2lzh`包索引


函数、类等名称:定义所在章节
Expand Down
2 changes: 1 addition & 1 deletion chapter_appendix/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
aws
buy-gpu
how-to-contribute
gluonbook
d2lzh
```
2 changes: 1 addition & 1 deletion chapter_computational-performance/async-computation.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ c
为了演示异步计算的性能,我们先实现一个简单的计时类。

```{.python .input}
class Benchmark(): # 本类已保存在 gluonbook 包中方便以后使用。
class Benchmark(): # 本类已保存在 d2lzh 包中方便以后使用。
def __init__(self, prefix=None):
self.prefix = prefix + ' ' if prefix else ''
Expand Down
14 changes: 7 additions & 7 deletions chapter_computational-performance/auto-parallelism.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ MXNet后端会自动构建计算图。通过计算图,系统可以知道所有
首先导入本节中实验所需的包或模块。注意,我们需要至少一个GPU才能运行本节实验。

```{.python .input}
import gluonbook as gb
import d2lzh as d2l
import mxnet as mx
from mxnet import nd
```
Expand Down Expand Up @@ -35,19 +35,19 @@ run(x_cpu) # 预热开始。
run(x_gpu)
nd.waitall() # 预热结束。
with gb.Benchmark('Run on CPU.'):
with d2l.Benchmark('Run on CPU.'):
run(x_cpu)
nd.waitall()
with gb.Benchmark('Then run on GPU.'):
with d2l.Benchmark('Then run on GPU.'):
run(x_gpu)
nd.waitall()
```

我们去掉`run(x_cpu)``run(x_gpu)`两个计算任务之间的`nd.waitall()`,希望系统能自动并行这两个任务。

```{.python .input}
with gb.Benchmark('Run on both CPU and GPU in parallel.'):
with d2l.Benchmark('Run on both CPU and GPU in parallel.'):
run(x_cpu)
run(x_gpu)
nd.waitall()
Expand All @@ -64,19 +64,19 @@ with gb.Benchmark('Run on both CPU and GPU in parallel.'):
def copy_to_cpu(x):
return [y.copyto(mx.cpu()) for y in x]
with gb.Benchmark('Run on GPU.'):
with d2l.Benchmark('Run on GPU.'):
y = run(x_gpu)
nd.waitall()
with gb.Benchmark('Then copy to CPU.'):
with d2l.Benchmark('Then copy to CPU.'):
copy_to_cpu(y)
nd.waitall()
```

我们去掉计算和通讯之间的`waitall`函数,打印这两个任务完成的总时间。

```{.python .input}
with gb.Benchmark('Run and copy in parallel.'):
with d2l.Benchmark('Run and copy in parallel.'):
y = run(x_gpu)
copy_to_cpu(y)
nd.waitall()
Expand Down
12 changes: 6 additions & 6 deletions chapter_computational-performance/multiple-gpus-gluon.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
首先导入本节实验所需的包或模块。运行本节中的程序需要至少两块GPU。

```{.python .input n=1}
import gluonbook as gb
import d2lzh as d2l
import mxnet as mx
from mxnet import autograd, gluon, init, nd
from mxnet.gluon import loss as gloss, nn, utils as gutils
Expand All @@ -17,15 +17,15 @@ import time
我们使用ResNet-18来作为本节的样例模型。由于本节的输入图像使用原尺寸(未放大),这里的模型构造与[“残差网络(ResNet)”](../chapter_convolutional-neural-networks/resnet.md)一节中的ResNet-18构造稍有不同。这里的模型在一开始使用了较小的卷积核、步幅和填充,并去掉了最大池化层。

```{.python .input n=2}
def resnet18(num_classes): # 本函数已保存在 gluonbook 包中方便以后使用。
def resnet18(num_classes): # 本函数已保存在 d2lzh 包中方便以后使用。
def resnet_block(num_channels, num_residuals, first_block=False):
blk = nn.Sequential()
for i in range(num_residuals):
if i == 0 and not first_block:
blk.add(gb.Residual(
blk.add(d2l.Residual(
num_channels, use_1x1conv=True, strides=2))
else:
blk.add(gb.Residual(num_channels))
blk.add(d2l.Residual(num_channels))
return blk
net = nn.Sequential()
Expand Down Expand Up @@ -75,7 +75,7 @@ weight.data(ctx[0])[0], weight.data(ctx[1])[0]

```{.python .input n=7}
def train(num_gpus, batch_size, lr):
train_iter, test_iter = gb.load_data_fashion_mnist(batch_size)
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
ctx = [mx.gpu(i) for i in range(num_gpus)]
print('running on:', ctx)
net.initialize(init=init.Normal(sigma=0.01), ctx=ctx, force_reinit=True)
Expand All @@ -95,7 +95,7 @@ def train(num_gpus, batch_size, lr):
trainer.step(batch_size)
nd.waitall()
train_time = time.time() - start
test_acc = gb.evaluate_accuracy(test_iter, net, ctx[0])
test_acc = d2l.evaluate_accuracy(test_iter, net, ctx[0])
print('epoch %d, time: %.1f sec, test acc %.2f' % (
epoch + 1, train_time, test_acc))
```
Expand Down
8 changes: 4 additions & 4 deletions chapter_computational-performance/multiple-gpus.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
为了从零开始实现多GPU训练中的数据并行,让我们先导入需要的包或模块。

```{.python .input n=2}
import gluonbook as gb
import d2lzh as d2l
import mxnet as mx
from mxnet import autograd, nd
from mxnet.gluon import loss as gloss
Expand Down Expand Up @@ -143,7 +143,7 @@ def train_batch(X, y, gpu_params, ctx, lr):
for i in range(len(gpu_params[0])):
allreduce([gpu_params[c][i].grad for c in range(len(ctx))])
for param in gpu_params: # 在各个 GPU 上分别更新模型参数。
gb.sgd(param, lr, X.shape[0]) # 这里使用了完整批量大小。
d2l.sgd(param, lr, X.shape[0]) # 这里使用了完整批量大小。
```

## 训练函数
Expand All @@ -152,7 +152,7 @@ def train_batch(X, y, gpu_params, ctx, lr):

```{.python .input n=11}
def train(num_gpus, batch_size, lr):
train_iter, test_iter = gb.load_data_fashion_mnist(batch_size)
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
ctx = [mx.gpu(i) for i in range(num_gpus)]
print('running on:', ctx)
# 将模型参数复制到 num_gpus 个 GPU 上。
Expand All @@ -168,7 +168,7 @@ def train(num_gpus, batch_size, lr):
def net(x): # 在 GPU 0 上验证模型。
return lenet(x, gpu_params[0])
test_acc = gb.evaluate_accuracy(test_iter, net, ctx[0])
test_acc = d2l.evaluate_accuracy(test_iter, net, ctx[0])
print('epoch %d, time: %.1f sec, test acc: %.2f'
% (epoch + 1, train_time, test_acc))
```
Expand Down
16 changes: 8 additions & 8 deletions chapter_computer-vision/anchor.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

```{.python .input n=1}
%matplotlib inline
import gluonbook as gb
import d2lzh as d2l
from mxnet import contrib, gluon, image, nd
import numpy as np
np.set_printoptions(2)
Expand Down Expand Up @@ -44,7 +44,7 @@ boxes[250, 250, 0, :]
为了描绘图像中以某个像素为中心的所有锚框,我们先定义`show_bboxes`函数以便在图像上画出多个边界框。

```{.python .input n=4}
# 本函数已保存在 gluonbook 包中方便以后使用。
# 本函数已保存在 d2lzh 包中方便以后使用。
def show_bboxes(axes, bboxes, labels=None, colors=None):
def _make_list(obj, default_values=None):
if obj is None:
Expand All @@ -57,7 +57,7 @@ def show_bboxes(axes, bboxes, labels=None, colors=None):
colors = _make_list(colors, ['b', 'g', 'r', 'm', 'c'])
for i, bbox in enumerate(bboxes):
color = colors[i % len(colors)]
rect = gb.bbox_to_rect(bbox.asnumpy(), color)
rect = d2l.bbox_to_rect(bbox.asnumpy(), color)
axes.add_patch(rect)
if labels and len(labels) > i:
text_color = 'k' if color == 'w' else 'w'
Expand All @@ -69,9 +69,9 @@ def show_bboxes(axes, bboxes, labels=None, colors=None):
刚刚我们看到,变量`boxes`中$x$和$y$轴的坐标值分别已除以图像的宽和高。在绘图时,我们需要恢复锚框的原始坐标值,并因此定义了变量`bbox_scale`。现在,我们可以画出图像中以(250,250)为中心的所有锚框了。可以看到,大小为0.75且宽高比为1的蓝色锚框较好地覆盖了图像中的狗。

```{.python .input n=5}
gb.set_figsize()
d2l.set_figsize()
bbox_scale = nd.array((w, h, w, h))
fig = gb.plt.imshow(img)
fig = d2l.plt.imshow(img)
show_bboxes(fig.axes, boxes[250, 250, :, :] * bbox_scale,
['s=0.75, r=1', 's=0.5, r=1', 's=0.25, r=1', 's=0.75, r=2',
's=0.75, r=0.5'])
Expand Down Expand Up @@ -131,7 +131,7 @@ anchors = nd.array([[0, 0.1, 0.2, 0.3], [0.15, 0.2, 0.4, 0.4],
[0.63, 0.05, 0.88, 0.98], [0.66, 0.45, 0.8, 0.8],
[0.57, 0.3, 0.92, 0.9]])
fig = gb.plt.imshow(img)
fig = d2l.plt.imshow(img)
show_bboxes(fig.axes, ground_truth[:, 1:] * bbox_scale, ['dog', 'cat'], 'k')
show_bboxes(fig.axes, anchors * bbox_scale, ['0', '1', '2', '3', '4']);
```
Expand Down Expand Up @@ -187,7 +187,7 @@ cls_probs = nd.array([[0] * 4, # 背景的预测概率。
在图像上打印预测边界框和它们的置信度。

```{.python .input n=12}
fig = gb.plt.imshow(img)
fig = d2l.plt.imshow(img)
show_bboxes(fig.axes, anchors * bbox_scale,
['dog=0.9', 'dog=0.8', 'dog=0.7', 'cat=0.9'])
```
Expand All @@ -204,7 +204,7 @@ output
我们移除掉类别为-1的预测边界框,并可视化非极大值抑制保留的结果。

```{.python .input n=14}
fig = gb.plt.imshow(img)
fig = d2l.plt.imshow(img)
for i in output[0].asnumpy():
if i[0] == -1:
continue
Expand Down
12 changes: 6 additions & 6 deletions chapter_computer-vision/bounding-box.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

```{.python .input}
%matplotlib inline
import gluonbook as gb
import d2lzh as d2l
from mxnet import image
```

下面加载本节将使用的示例图像。可以看到图像左边是一只狗,右边是一只猫。它们是这张图像里的两个主要目标。

```{.python .input}
gb.set_figsize()
d2l.set_figsize()
img = image.imread('../img/catdog.jpg').asnumpy()
gb.plt.imshow(img); # 加分号只显示图。
d2l.plt.imshow(img); # 加分号只显示图。
```

## 边界框
Expand All @@ -32,18 +32,18 @@ dog_bbox, cat_bbox = [60, 45, 378, 516], [400, 112, 655, 493]
我们可以在图中将边界框画出来,以检查其是否准确。画之前,我们定义一个辅助函数`bbox_to_rect`。它将边界框表示成matplotlib的边界框格式。

```{.python .input n=3}
def bbox_to_rect(bbox, color): # 本函数已保存在 gluonbook 包中方便以后使用。
def bbox_to_rect(bbox, color): # 本函数已保存在 d2lzh 包中方便以后使用。
# 将边界框(左上 x、左上 y,右下 x,右下 y)格式转换成 matplotlib 格式:
# ((左上 x,左上 y),宽,高)。
return gb.plt.Rectangle(
return d2l.plt.Rectangle(
xy=(bbox[0], bbox[1]), width=bbox[2]-bbox[0], height=bbox[3]-bbox[1],
fill=False, edgecolor=color, linewidth=2)
```

我们将边界框加载在图像上,可以看到目标的主要轮廓基本在框内。

```{.python .input}
fig = gb.plt.imshow(img)
fig = d2l.plt.imshow(img)
fig.axes.add_patch(bbox_to_rect(dog_bbox, 'blue'))
fig.axes.add_patch(bbox_to_rect(cat_bbox, 'red'));
```
Expand Down
26 changes: 13 additions & 13 deletions chapter_computer-vision/fcn.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

```{.python .input n=2}
%matplotlib inline
import gluonbook as gb
import d2lzh as d2l
from mxnet import gluon, image, init, nd
from mxnet.gluon import data as gdata, loss as gloss, model_zoo, nn
import numpy as np
Expand Down Expand Up @@ -136,11 +136,11 @@ out_img = Y[0].transpose((1, 2, 0))
可以看到,转置卷积层将图像的高和宽分别放大2倍。值得一提的是,除了坐标刻度不同,双线性插值放大的图像和[“目标检测和边界框”](bounding-box.md)一节中打印出的原图看上去没什么两样。

```{.python .input}
gb.set_figsize()
d2l.set_figsize()
print('input image shape:', img.shape)
gb.plt.imshow(img.asnumpy());
d2l.plt.imshow(img.asnumpy());
print('output image shape:', out_img.shape)
gb.plt.imshow(out_img.asnumpy());
d2l.plt.imshow(out_img.asnumpy());
```

在全卷积网络中,我们将转置卷积层初始化为双线性插值的上采样。对于$1\times 1$卷积层,我们采用Xavier随机初始化。
Expand All @@ -157,16 +157,16 @@ net[-2].initialize(init=init.Xavier())

```{.python .input n=13}
crop_size, batch_size, colormap2label = (320, 480), 32, nd.zeros(256**3)
for i, cm in enumerate(gb.VOC_COLORMAP):
for i, cm in enumerate(d2l.VOC_COLORMAP):
colormap2label[(cm[0] * 256 + cm[1]) * 256 + cm[2]] = i
voc_dir = gb.download_voc_pascal(data_dir='../data')
voc_dir = d2l.download_voc_pascal(data_dir='../data')
num_workers = 0 if sys.platform.startswith('win32') else 4
train_iter = gdata.DataLoader(
gb.VOCSegDataset(True, crop_size, voc_dir, colormap2label), batch_size,
d2l.VOCSegDataset(True, crop_size, voc_dir, colormap2label), batch_size,
shuffle=True, last_batch='discard', num_workers=num_workers)
test_iter = gdata.DataLoader(
gb.VOCSegDataset(False, crop_size, voc_dir, colormap2label), batch_size,
d2l.VOCSegDataset(False, crop_size, voc_dir, colormap2label), batch_size,
last_batch='discard', num_workers=num_workers)
```

Expand All @@ -175,12 +175,12 @@ test_iter = gdata.DataLoader(
现在我们可以开始训练模型了。这里的损失函数和准确率计算与图像分类中的并没有本质上的不同。因为我们使用转置卷积层的通道来预测像素的类别,所以在`SoftmaxCrossEntropyLoss`里指定了`axis=1`(通道维)选项。此外,模型基于每个像素的预测类别是否正确来计算准确率。

```{.python .input n=12}
ctx = gb.try_all_gpus()
ctx = d2l.try_all_gpus()
loss = gloss.SoftmaxCrossEntropyLoss(axis=1)
net.collect_params().reset_ctx(ctx)
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.1,
'wd': 1e-3})
gb.train(train_iter, test_iter, net, loss, trainer, ctx, num_epochs=5)
d2l.train(train_iter, test_iter, net, loss, trainer, ctx, num_epochs=5)
```

## 预测
Expand All @@ -199,7 +199,7 @@ def predict(img):

```{.python .input n=14}
def label2image(pred):
colormap = nd.array(gb.VOC_COLORMAP, ctx=ctx[0], dtype='uint8')
colormap = nd.array(d2l.VOC_COLORMAP, ctx=ctx[0], dtype='uint8')
X = pred.astype('int32')
return colormap[X, :]
```
Expand All @@ -209,14 +209,14 @@ def label2image(pred):
为了简单起见,我们只读取几张较大的测试图像,并从图像的左上角开始截取形状为$320\times480$的区域:只有该区域用来预测。对于输入图像,我们先打印截取的区域,再打印预测结果,最后打印标注的类别。

```{.python .input n=15}
test_images, test_labels = gb.read_voc_images(is_train=False)
test_images, test_labels = d2l.read_voc_images(is_train=False)
n, imgs = 4, []
for i in range(n):
crop_rect = (0, 0, 480, 320)
X = image.fixed_crop(test_images[i], *crop_rect)
pred = label2image(predict(X))
imgs += [X, pred, image.fixed_crop(test_labels[i], *crop_rect)]
gb.show_images(imgs[::3] + imgs[1::3] + imgs[2::3], 3, n);
d2l.show_images(imgs[::3] + imgs[1::3] + imgs[2::3], 3, n);
```

## 小结
Expand Down
8 changes: 4 additions & 4 deletions chapter_computer-vision/fine-tuning.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

```{.python .input n=1}
%matplotlib inline
import gluonbook as gb
import d2lzh as d2l
from mxnet import gluon, init, nd
from mxnet.gluon import data as gdata, loss as gloss, model_zoo
from mxnet.gluon import utils as gutils
Expand Down Expand Up @@ -64,7 +64,7 @@ test_imgs = gdata.vision.ImageFolderDataset(
```{.python .input n=4}
hotdogs = [train_imgs[i][0] for i in range(8)]
not_hotdogs = [train_imgs[-i - 1][0] for i in range(8)]
gb.show_images(hotdogs + not_hotdogs, 2, 8, scale=1.4);
d2l.show_images(hotdogs + not_hotdogs, 2, 8, scale=1.4);
```

在训练时,我们先从图像中裁剪出随机大小和随机高宽比的一块随机区域,然后将该区域缩放为高和宽均为224像素的输入。测试时,我们将图像的高和宽均缩放为256像素,然后从中裁剪出高和宽均为224像素的中心区域作为输入。此外,我们对RGB(红、绿、蓝)三个颜色通道的数值做标准化:每个数值减去该通道所有数值的平均值,再除以该通道所有数值的标准差作为输出。
Expand Down Expand Up @@ -121,13 +121,13 @@ def train_fine_tuning(net, learning_rate, batch_size=128, num_epochs=5):
train_imgs.transform_first(train_augs), batch_size, shuffle=True)
test_iter = gdata.DataLoader(
test_imgs.transform_first(test_augs), batch_size)
ctx = gb.try_all_gpus()
ctx = d2l.try_all_gpus()
net.collect_params().reset_ctx(ctx)
net.hybridize()
loss = gloss.SoftmaxCrossEntropyLoss()
trainer = gluon.Trainer(net.collect_params(), 'sgd', {
'learning_rate': learning_rate, 'wd': 0.001})
gb.train(train_iter, test_iter, net, loss, trainer, ctx, num_epochs)
d2l.train(train_iter, test_iter, net, loss, trainer, ctx, num_epochs)
```

我们将`Trainer`实例中的学习率设的小一点,例如0.01,以便微调预训练得到的模型参数。根据前面的设置,我们将以10倍的学习率从头训练目标模型的输出层参数。
Expand Down
Loading

0 comments on commit f0dac92

Please sign in to comment.