Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
d2l-zh
提交
3dc6dedf
D
d2l-zh
项目概览
OpenDocCN
/
d2l-zh
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
d2l-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3dc6dedf
编写于
11月 25, 2017
作者:
M
muli
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
tiny fix for fcn
上级
d97b0091
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
19 addition
and
19 deletion
+19
-19
chapter_computer-vision/fcn.md
chapter_computer-vision/fcn.md
+19
-19
未找到文件。
chapter_computer-vision/fcn.md
浏览文件 @
3dc6dedf
...
...
@@ -17,7 +17,7 @@
```
{.python .input n=1}
import os
import tarfile
import tarfile
from mxnet import gluon
data_root = '../data'
...
...
@@ -65,7 +65,7 @@ train_images, train_labels = read_images()
imgs = []
for i in range(3):
imgs += [train_images[i], train_labels[i]]
utils.show_images(imgs, nrows=3, ncols=2, figsize=(12,8))
[im.shape for im in imgs]
```
...
...
@@ -82,9 +82,9 @@ def rand_crop(data, label, height, width):
imgs = []
for _ in range(3):
imgs += rand_crop(train_images[0], train_labels[0],
imgs += rand_crop(train_images[0], train_labels[0],
200, 300)
utils.show_images(imgs, nrows=3, ncols=2, figsize=(12,8))
```
...
...
@@ -96,7 +96,7 @@ classes = ['background','aeroplane','bicycle','bird','boat',
'dog','horse','motorbike','person','potted plant',
'sheep','sofa','train','tv/monitor']
# RGB color for each class
colormap = [[0,0,0],[128,0,0],[0,128,0], [128,128,0], [0,0,128],
colormap = [[0,0,0],[128,0,0],[0,128,0], [128,128,0], [0,0,128],
[128,0,128],[0,128,128],[128,128,128],[64,0,0],[192,0,0],
[64,128,0],[192,128,0],[64,0,128],[192,0,128],
[64,128,128],[192,128,128],[0,64,0],[128,64,0],
...
...
@@ -112,9 +112,9 @@ import numpy as np
from mxnet import nd
cm2lbl = np.zeros(256**3)
for i,cm in enumerate(colormap):
for i,cm in enumerate(colormap):
cm2lbl[(cm[0]*256+cm[1])*256+cm[2]] = i
def image2label(im):
data = im.astype('int32').asnumpy()
idx = (data[:,:,0]*256+data[:,:,1])*256+data[:,:,2]
...
...
@@ -142,12 +142,12 @@ def normalize_image(data):
return (data.astype('float32') / 255 - rgb_mean) / rgb_std
class VOCSegDataset(gluon.data.Dataset):
def _filter(self, images):
return [im for im in images if (
im.shape[0] >= self.crop_size[0] and
im.shape[1] >= self.crop_size[1])]
im.shape[1] >= self.crop_size[1])]
def __init__(self, train, crop_size):
self.crop_size = crop_size
data, label = read_images(train=train)
...
...
@@ -155,15 +155,15 @@ class VOCSegDataset(gluon.data.Dataset):
self.data = [normalize_image(im) for im in data]
self.label = self._filter(label)
print('Read '+str(len(self.data))+' examples')
def __getitem__(self, idx):
data, label = rand_crop(
self.data[idx], self.label[idx],
*self.crop_size)
data = data.transpose((2,0,1))
data = data.transpose((2,0,1))
label = image2label(label)
return data, label
def __len__(self):
return len(self.data)
```
...
...
@@ -244,7 +244,7 @@ pretrained_net = models.resnet18_v2(pretrained=True)
net = nn.HybridSequential()
for layer in pretrained_net.features[:-2]:
net.add(layer)
x = nd.random.uniform(shape=(1,3,*input_shape))
print('Input:', x.shape)
print('Output:', net(x).shape)
...
...
@@ -259,7 +259,7 @@ with net.name_scope():
net.add(
nn.Conv2D(num_classes, kernel_size=1),
nn.Conv2DTranspose(num_classes, kernel_size=64, padding=16,strides=32)
)
)
```
## 训练
...
...
@@ -316,8 +316,8 @@ plt.show()
from mxnet import init
conv_trans = net[-1]
conv_trans.initialize(init=init.
Xavier
())
net[-2].initialize(init=init.
Zero
())
conv_trans.initialize(init=init.
Zero
())
net[-2].initialize(init=init.
Xavier
())
x = nd.zeros((batch_size, 3, *input_shape))
net(x)
...
...
@@ -334,7 +334,7 @@ import sys
sys.path.append('..')
import utils
loss = gluon.loss.SoftmaxCrossEntropyLoss(axis=1)
loss = gluon.loss.SoftmaxCrossEntropyLoss(axis=1)
ctx = utils.try_all_gpus()
net.collect_params().reset_ctx(ctx)
...
...
@@ -375,7 +375,7 @@ for i in range(n):
x = test_images[i]
pred = label2image(predict(x))
imgs += [x, pred, test_labels[i]]
utils.show_images(imgs, nrows=n, ncols=3, figsize=(6,10))
```
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录