# [译] PyTorch 实例：用 ResNet 进行交通标志分类

【导读】 本文是机器学习工程师 Pavel Surmenok 撰写的一篇技术博客，用 Pytorch 实现 ResNet 网络，并用德国交通标志识别基准数据集进行实验。文中分别介绍了数据集、实验方法、代码、准备工作，并对图像增强、学习率、模型微调、误差分析等步骤进行详细介绍。文章中给出了 GitHub 代码，本文是一篇学习 PyTorch ResNet 的很好的实例教程。

### ResNet for Traffic Sign Classification With PyTorch

http://benchmark.ini.rub.de/?section=gtsrb&subsection=dataset

https://github.com/surmenok/GTSRB/blob/master/german-traffic-signs.ipynb

https://github.com/surmenok/GTSRB

www.fast.ai/2017/11/13/validation-sets/

```arch = resnet34
learn = ConvLearner.pretrained(arch, data, precompute=False)
```

```sz = 96

# Look at examples of image augmentation
def get_augs():
x,_ = next(iter(data.aug_dl))
return data.trn_ds.denorm(x)[1]

aug_tfms = [RandomRotate(20), RandomLighting(0.8, 0.8)]
tfms = tfms_from_model(arch, sz, aug_tfms=aug_tfms, max_zoom=1.2)
data = ImageClassifierData.from_paths(path, tfms=tfms, test_name='test')

ims = np.stack([get_augs() for i in range(6)])
plots(ims, rows=2)```

https://towardsdatascience.com/estimating-optimal-learning-rate-for-a-deep-neural-network-ce32f2556ce0

```def plot_loss_change(sched, sma=1, n_skip=20, y_lim=(-0.01, 0.01)):
"""
Plots rate of change of the loss function.
Parameters:
sched - learning rate scheduler, an instance of LR_Finder class.
sma - number of batches for simple moving average to smooth out the curve.
n_skip - number of batches to skip on the left.
y_lim - limits for the y axis.
"""
derivatives = [0] * (sma + 1)
for i in range(1 + sma, len(learn.sched.lrs)):
derivative = (learn.sched.losses[i] - learn.sched.losses[i - sma]) / sma
derivatives.append(derivative)

plt.ylabel("d/loss")
plt.xlabel("learning rate (log scale)")
plt.plot(learn.sched.lrs[n_skip:], derivatives[n_skip:])
plt.xscale('log')
plt.ylim(y_lim)

learn.lr_find()```

```wd = 5e-4
learn.fit(0.01, 1, wds=wd)
```

```learn.unfreeze()
learn.fit(0.01, 3, wds=wd)```

`learn.fit(lr, 4, cycle_len=1, cycle_mult=2, wds=wd)`

```log_preds,y = learn.predict_with_targs()
preds = np.exp(log_preds)
pred_labels = np.argmax(preds, axis=1)

results = ImageModelResults(data.val_ds, log_preds)

results.plot_most_incorrect(1)```

```log_preds,_ = learn.TTA(n_aug=20, is_test=True)
preds = np.mean(np.exp(log_preds),0)
accuracy_np(preds, y_true)```

2011年IJCNN竞赛排行榜排名：

•  CNN与ÁlvaroArcos-García等人的3个空间变换器 99.71％

• DanCireşan等人的 CNN 。99.46％

•  基于颜色斑点的COSFIRE过滤器 ，用于由Baris Gecer进行物体识别，98.97％

fast.ai最新版本的“深入学习编码器”课程：

course.fast.ai

GitHub：

https://github.com/surmenok/GTSRB

fastai：

https://github.com/fastai/fastai

CNN with 3 spatial transformers：

Committee of CNNs：

https://www.sciencedirect.com/science/article/pii/S0893608012000524?via%3Dihub

Color-blob-based COSFIRE blters for object recognition：

dx.doi.org/10.1016/j.imavis.2016.10.006

https://towardsdatascience.com/resnet-for-traffic-sign-classification-with-pytorch-5883a97bbaa3