@@ -43,7 +43,7 @@ import zipfile
43
43
为方便快速上手,我们提供了上述数据集的小规模采样“train_valid_test_tiny.zip”。如果你要使用上述Kaggle比赛的完整数据集,还需要把下面` demo ` 变量改为` False ` 。
44
44
45
45
``` {.python .input n=1}
46
- # 如果使用下载的 Kaggle 比赛的完整数据集,把下面改为 False。
46
+ # 如果使用下载的Kaggle比赛的完整数据集,把demo变量改为False
47
47
demo = True
48
48
data_dir = '../data/kaggle_dog'
49
49
if demo:
@@ -61,10 +61,10 @@ for f in zipfiles:
61
61
62
62
``` {.python .input}
63
63
def reorg_train_valid(data_dir, train_dir, input_dir, valid_ratio, idx_label):
64
- # 训练集中数量最少一类的狗的样本数。
64
+ # 训练集中数量最少一类的狗的样本数
65
65
min_n_train_per_label = (
66
66
collections.Counter(idx_label.values()).most_common()[:-2:-1][0][1])
67
- # 验证集中每类狗的样本数。
67
+ # 验证集中每类狗的样本数
68
68
n_valid_per_label = math.floor(min_n_train_per_label * valid_ratio)
69
69
label_count = {}
70
70
for train_file in os.listdir(os.path.join(data_dir, train_dir)):
@@ -89,14 +89,14 @@ def reorg_train_valid(data_dir, train_dir, input_dir, valid_ratio, idx_label):
89
89
``` {.python .input n=2}
90
90
def reorg_dog_data(data_dir, label_file, train_dir, test_dir, input_dir,
91
91
valid_ratio):
92
- # 读取训练数据标签。
92
+ # 读取训练数据标签
93
93
with open(os.path.join(data_dir, label_file), 'r') as f:
94
- # 跳过文件头行(栏名称)。
94
+ # 跳过文件头行(栏名称)
95
95
lines = f.readlines()[1:]
96
96
tokens = [l.rstrip().split(',') for l in lines]
97
97
idx_label = dict(((idx, label) for idx, label in tokens))
98
98
reorg_train_valid(data_dir, train_dir, input_dir, valid_ratio, idx_label)
99
- # 整理测试集。
99
+ # 整理测试集
100
100
d2l.mkdir_if_not_exist([data_dir, input_dir, 'test', 'unknown'])
101
101
for test_file in os.listdir(os.path.join(data_dir, test_dir)):
102
102
shutil.copy(os.path.join(data_dir, test_dir, test_file),
@@ -107,8 +107,8 @@ def reorg_dog_data(data_dir, label_file, train_dir, test_dir, input_dir,
107
107
108
108
``` {.python .input n=3}
109
109
if demo:
110
- # 注意:此处使用小数据集并将批量大小相应设小。使用 Kaggle 比赛的完整数据集时可设批量大
111
- # 小为较大整数。
110
+ # 注意:此处使用小数据集并将批量大小相应设小。使用Kaggle比赛的完整数据集时可设批量大小
111
+ # 为较大整数
112
112
input_dir, batch_size = 'train_valid_test_tiny', 1
113
113
else:
114
114
label_file, train_dir, test_dir = 'labels.csv', 'train', 'test'
@@ -123,18 +123,18 @@ else:
123
123
124
124
``` {.python .input n=4}
125
125
transform_train = gdata.vision.transforms.Compose([
126
- # 随机对图像裁剪出面积为原图像面积 0.08 到 1 倍之间、且高和宽之比在 3/4 和 4/3 之间
127
- # 的图像,再放缩为高和宽均为 224 像素的新图像。
126
+ # 随机对图像裁剪出面积为原图像面积0.08到1倍之间、且高和宽之比在3/4和4/3之间的图像,再
127
+ # 放缩为高和宽均为224像素的新图像
128
128
gdata.vision.transforms.RandomResizedCrop(224, scale=(0.08, 1.0),
129
129
ratio=(3.0/4.0, 4.0/3.0)),
130
130
gdata.vision.transforms.RandomFlipLeftRight(),
131
- # 随机变化亮度、对比度和饱和度。
131
+ # 随机变化亮度、对比度和饱和度
132
132
gdata.vision.transforms.RandomColorJitter(brightness=0.4, contrast=0.4,
133
133
saturation=0.4),
134
- # 随机加噪音。
134
+ # 随机加噪声
135
135
gdata.vision.transforms.RandomLighting(0.1),
136
136
gdata.vision.transforms.ToTensor(),
137
- # 对图像的每个通道做标准化。
137
+ # 对图像的每个通道做标准化
138
138
gdata.vision.transforms.Normalize([0.485, 0.456, 0.406],
139
139
[0.229, 0.224, 0.225])])
140
140
```
@@ -144,7 +144,7 @@ transform_train = gdata.vision.transforms.Compose([
144
144
``` {.python .input}
145
145
transform_test = gdata.vision.transforms.Compose([
146
146
gdata.vision.transforms.Resize(256),
147
- # 将图像中央的高和宽均为 224 的正方形区域裁剪出来。
147
+ # 将图像中央的高和宽均为224的正方形区域裁剪出来
148
148
gdata.vision.transforms.CenterCrop(224),
149
149
gdata.vision.transforms.ToTensor(),
150
150
gdata.vision.transforms.Normalize([0.485, 0.456, 0.406],
@@ -188,14 +188,14 @@ test_iter = gdata.DataLoader(test_ds.transform_first(transform_test),
188
188
``` {.python .input n=6}
189
189
def get_net(ctx):
190
190
finetune_net = model_zoo.vision.resnet34_v2(pretrained=True)
191
- # 定义新的输出网络。
191
+ # 定义新的输出网络
192
192
finetune_net.output_new = nn.HybridSequential(prefix='')
193
193
finetune_net.output_new.add(nn.Dense(256, activation='relu'))
194
- # 120 是输出的类别数。
194
+ # 120是输出的类别个数
195
195
finetune_net.output_new.add(nn.Dense(120))
196
- # 初始化输出网络。
196
+ # 初始化输出网络
197
197
finetune_net.output_new.initialize(init.Xavier(), ctx=ctx)
198
- # 把模型参数分配到即将用于计算的 CPU 或 GPU 上。
198
+ # 把模型参数分配到内存或显存上
199
199
finetune_net.collect_params().reset_ctx(ctx)
200
200
return finetune_net
201
201
```
@@ -223,7 +223,7 @@ def evaluate_loss(data_iter, net, ctx):
223
223
``` {.python .input n=7}
224
224
def train(net, train_iter, valid_iter, num_epochs, lr, wd, ctx, lr_period,
225
225
lr_decay):
226
- # 只训练我们定义的小规模输出网络。
226
+ # 只训练自定义的小规模输出网络
227
227
trainer = gluon.Trainer(net.output_new.collect_params(), 'sgd',
228
228
{'learning_rate': lr, 'momentum': 0.9, 'wd': wd})
229
229
for epoch in range(num_epochs):
0 commit comments