Skip to content

Commit

Permalink
[PaddlePaddle] fix d2l.py bugs (d2l-ai#1181)
Browse files Browse the repository at this point in the history
* update d2l

* Fix the bugs of d2l

* remove multi gpus paddle tab

Co-authored-by: cheungdaven <cheungdaven@gmail.com>
  • Loading branch information
吴高升 and cheungdaven committed Jul 30, 2022
1 parent 9be898b commit 8c8d4da
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 536 deletions.
2 changes: 1 addition & 1 deletion chapter_computational-performance/multiple-gpus-concise.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ train(num_gpus=2, batch_size=512, lr=0.2)
```

```{.python .input}
#@tab pytorch, paddle
#@tab pytorch
train(net, num_gpus=2, batch_size=512, lr=0.2)
```

Expand Down
2 changes: 1 addition & 1 deletion chapter_computational-performance/multiple-gpus.md
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ train(num_gpus=1, batch_size=256, lr=0.2)
尽管如此,让我们看看Fashion-MNIST数据集上会发生什么。

```{.python .input}
#@tab all
#@tab mxnet, pytorch
train(num_gpus=2, batch_size=256, lr=0.2)
```

Expand Down
29 changes: 16 additions & 13 deletions chapter_computer-vision/semantic-segmentation-and-dataset.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ train_features, train_labels = read_voc_images(voc_dir, True)
#@tab paddle
#@save
def read_voc_images(voc_dir, is_train=True):
"""Defined in :numref:`sec_semantic_segmentation`"""
"""读取所有VOC图像并标注
Defined in :numref:`sec_semantic_segmentation`"""
txt_fname = os.path.join(voc_dir, 'ImageSets', 'Segmentation',
Expand All @@ -120,11 +121,11 @@ def read_voc_images(voc_dir, is_train=True):
images = f.read().split()
features, labels = [], []
for i, fname in enumerate(images):
features.append(paddle.to_tensor(paddle.vision.image.image_load(os.path.join(
voc_dir, 'JPEGImages', f'{fname}.jpg'), backend='cv2')[..., ::-1], dtype=paddle.float32).transpose(
features.append(paddle.vision.image.image_load(os.path.join(
voc_dir, 'JPEGImages', f'{fname}.jpg'), backend='cv2')[..., ::-1].transpose(
[2, 0, 1]))
labels.append(paddle.to_tensor(paddle.vision.image.image_load(os.path.join(
voc_dir, 'SegmentationClass', f'{fname}.png'), backend='cv2')[..., ::-1], dtype=paddle.float32).transpose(
labels.append(paddle.vision.image.image_load(os.path.join(
voc_dir, 'SegmentationClass', f'{fname}.png'), backend='cv2')[..., ::-1].transpose(
[2, 0, 1]))
return features, labels
Expand Down Expand Up @@ -152,7 +153,7 @@ d2l.show_images(imgs, 2, n);
#@tab paddle
n = 5
imgs = train_features[0:n] + train_labels[0:n]
imgs = [img.transpose([1, 2, 0]).astype(paddle.uint8) for img in imgs]
imgs = [img.transpose([1, 2, 0]) for img in imgs]
d2l.show_images(imgs, 2, n);
```

Expand Down Expand Up @@ -230,7 +231,7 @@ def voc_colormap2label():
def voc_label_indices(colormap, colormap2label):
"""将VOC标签中的RGB值映射到它们的类别索引"""
colormap = colormap.transpose([1, 2, 0]).astype('int32')#[281,500,3]
colormap = colormap.transpose([1, 2, 0]).astype('int32')
idx = ((colormap[:, :, 0] * 256 + colormap[:, :, 1]) * 256
+ colormap[:, :, 2])
return colormap2label[idx]
Expand Down Expand Up @@ -306,9 +307,9 @@ d2l.show_images(imgs[::2] + imgs[1::2], 2, n);
#@tab paddle
imgs = []
for _ in range(n):
imgs += voc_rand_crop(train_features[0], train_labels[0], 200, 300)
imgs = [img.transpose([1, 2, 0]).astype(paddle.uint8) for img in imgs]
imgs += voc_rand_crop(train_features[0].transpose([1, 2, 0]), train_labels[0].transpose([1, 2, 0]), 200, 300)
imgs = [img for img in imgs]
d2l.show_images(imgs[::2] + imgs[1::2], 2, n);
```

Expand Down Expand Up @@ -390,7 +391,8 @@ class VOCSegDataset(torch.utils.data.Dataset):
#@tab paddle
#@save
class VOCSegDataset(paddle.io.Dataset):
"""一个用于加载VOC数据集的自定义数据集"""
"""一个用于加载VOC数据集的自定义数据集
Defined in :numref:`sec_semantic_segmentation`"""
def __init__(self, is_train, crop_size, voc_dir):
self.transform = paddle.vision.transforms.Normalize(
Expand All @@ -404,17 +406,18 @@ class VOCSegDataset(paddle.io.Dataset):
print('read ' + str(len(self.features)) + ' examples')
def normalize_image(self, img):
return self.transform(img.astype(paddle.float32) / 255)
return self.transform(img.astype("float32") / 255)
def filter(self, imgs):
return [img for img in imgs if (
img.shape[1] >= self.crop_size[0] and
img.shape[2] >= self.crop_size[1])]
def __getitem__(self, idx):
feature, label = voc_rand_crop(self.features[idx], self.labels[idx],
feature = paddle.to_tensor(self.features[idx],dtype='float32')
label = paddle.to_tensor(self.labels[idx],dtype='float32')
feature, label = voc_rand_crop(feature,label,
*self.crop_size)
return (feature, voc_label_indices(label, self.colormap2label))
def __len__(self):
Expand Down
1 change: 1 addition & 0 deletions chapter_convolutional-neural-networks/lenet.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def evaluate_accuracy_gpu(net, data_iter, device=None): #@save
net.eval() # 设置为评估模式
if not device:
device = next(iter(net.parameters())).place
paddle.set_device("gpu:{}".format(str(device)[-2]))
# 正确预测的数量,总预测的数量
metric = d2l.Accumulator(2)
with paddle.no_grad():
Expand Down
2 changes: 1 addition & 1 deletion chapter_linear-networks/image-classification-dataset.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ batch_size = 256
def get_dataloader_workers(): #@save
"""使用4个进程来读取数据"""
return 0 if not sys.platform.startswith('linux') else 4
return 0
train_iter = paddle.io.DataLoader(dataset=mnist_train,
batch_size=batch_size,
Expand Down
1 change: 1 addition & 0 deletions chapter_preface/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ import paddle
from paddle import nn
from paddle.nn import functional as F
from paddle.vision import transforms
import paddle.vision as paddlevision
from PIL import Image
```

Expand Down
4 changes: 1 addition & 3 deletions chapter_recurrent-modern/seq2seq.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ from d2l import paddle as d2l
import math
import paddle
from paddle import nn
import warnings
warnings.filterwarnings("ignore")
```

## 编码器
Expand Down Expand Up @@ -828,7 +826,7 @@ def train_seq2seq(net, data_iter, lr, num_epochs, tgt_vocab, device):
X, X_valid_len, Y, Y_valid_len = [paddle.to_tensor(x, place=device) for x in batch]
bos = paddle.to_tensor([tgt_vocab['<bos>']] * Y.shape[0]).reshape([-1, 1])
dec_input = paddle.concat([bos, Y[:, :-1]], 1) # 强制教学
Y_hat, _ = net(X, dec_input, X_valid_len)
Y_hat, _ = net(X, dec_input, X_valid_len.squeeze())
l = loss(Y_hat, Y, Y_valid_len.squeeze())
l.backward() # 损失函数的标量进行“反向传播”
d2l.grad_clipping(net, 1)
Expand Down
2 changes: 1 addition & 1 deletion d2l/mxnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ def show_list_len_pair_hist(legend, xlabel, ylabel, xlist, ylist):
for patch in patches[1].patches:
patch.set_hatch('/')
d2l.plt.legend(legend)

def truncate_pad(line, num_steps, padding_token):
"""截断或填充文本序列
Expand Down
Loading

0 comments on commit 8c8d4da

Please sign in to comment.