[STM32] 车辆颜色识别

[复制链接]
1799|6
 楼主| 一路向北lm 发表于 2018-7-16 08:23 | 显示全部楼层 |阅读模式
本帖最后由 一路向北lm 于 2018-7-16 08:29 编辑

转载博客 https://blog.csdn.net/cesc_treze ... 61935?from=timeline 仅供大家参考学习

识别目标包括:

1.车牌号

2.车辆颜色

3.车辆品牌,具体到如奥迪A6之类

4.驾驶人是否系安全带

5.是否贴有年检标志

实验平台

开发平台使用Caffe,参考了博客小咸鱼_的实现(pretrained CaffeNet+finetune),本文主要在其基础上补充一些实现的细节。训练好的模型已经分享到百度云。 链接: https://pan.baidu.com/s/1eRSDFlo 密码: ahyx

1. 训练数据准备

在caffe/examples下建立目录car_color,并在其下面建立几个子目录:

car_color/data:存放训练和验证数据

car_color/models:存放prototxt和训练得到的模型

car_color/pre_train_models:存放预训练的CaffeNet

data目录

首先新建文件color_map.txt,存放待分类的颜色,这里我们划分了8种颜色

  1. black
  2. white
  3. red
  4. blue
  5. green
  6. gray
  7. yellow
  8. brown

对每种颜色划分train和valid数据:

  1. # ratio for train and valid data
  2. train_ratio = 0.8
  3. val_ratio = 0.2

  4. if not os.path.exists('train/' + color):
  5.     os.makedirs('train/' + color)
  6. if not os.path.exists('val/' + color):
  7.     os.makedirs('val/' + color)

  8. num = len(os.listdir('raw_data/' + color))
  9. print("Folder {} has {} images.".format(color, num))

  10. for file in os.listdir('raw_data/' + color):
  11.     oldfile = 'raw_data/{}/{}'.format(color, file)
  12.     if train_num > 0:
  13.         newfile = 'train/{}'.format(file)
  14.         shutil.copyfile(oldfile, newfile)
  15.         train_num -= 1
  16.     elif val_num > 0:
  17.         newfile = 'val/{}'.format(file)
  18.         shutil.copyfile(oldfile, newfile)
  19.         val_num -= 1
  20.     else:
  21.         pass

创建train.txt和val.txt,用于lmdb数据库的生成:

  1. prefix = '/your/path/caffe/examples/car_color/data/'
  2. def make_dict(syn_file):
  3.     i = 0
  4.     with open(syn_file, 'r') as df:
  5.         for kv in [d.strip().split(' ') for d in df]:
  6.             dict[kv[0]] = i; i = i + 1
  7. dict = {}
  8. make_dict('color_map.txt')

  9. # list file directories
  10. command = 'cd %s%s; find -name *.jpg | cut -d ''/'' -f2-3 >train.txt' % (prefix, train)
  11. system(command)
  12. command = 'cd %s%s; find -name *.jpg | cut -d ''/'' -f2-3 >val.txt' % (prefix, val)
  13. system(command)

  14. # add label
  15. train_in = open(prefix + train + 'train.txt')
  16. val_in = open(prefix + val + 'val.txt')
  17. train_out = open(prefix + 'train.txt', 'w')
  18. val_out = open(prefix + 'val.txt', 'w')
  19. for d in train_in:
  20.     train_out.write(d[0:len(d)-1] + ' ' + str(dict[d.strip().split('/')[0]]) + '\n')
  21. for d in val_in:
  22.     val_out.write(d[0:len(d)-1] + ' ' + str(dict[d.strip().split('/')[0]]) + '\n')

生成的train.txt形如:

  1. blue/016_011_00011.jpg 3
  2. brown/001_001_00005.jpg 7
  3. brown/002_002_00001.jpg 7
  4. brown/002_002_00003.jpg 7
  5. brown/003_001_00002.jpg 7
  6. brown/006_001_00003.jpg 7
  7. ...

models目录和pre_train_models目录
1. 从models/bvlc_reference_caffenet下拷贝train_val.prototxt、deploy.prototxt和solver.prototxt这三个文件到examples/ car_color/models,并修改对应的路径。
2. 修改train_val.prototxt中fc8的名称和输出层节点数。
3. 下载预训练的CaffeModel至目录examples/car_color/pre_train_models。
篇幅问题不做赘述,具体可以参考小咸鱼_的博客。

生成lmdb数据库和图像均值

  1. ./examples/car_color/create_imagenet.sh
  2. ./examples/car_color/make_imagenet_mean.sh

2. 开始训练
  1. ./build/tools/caffe train -solver examples/car_color/models/solver.prototxt -weights examples/car_color/pre_train_models/bvlc_reference_caffenet.caffemodel
这里有必要把初始学习率设低一些,否则很难收敛。我用的是0.001。
最终实验时每个类别的训练和评估分别用了50/10张图片。
训练速度比我想象的要更快,50个Iter后Valid accuracy就到了72.5%,150个Iter后基本稳定在95%。
3. 实验结果
  1. I0518 11:12:40.746090 12784 solver.cpp:341] Iteration 1000, Testing net (#0)
  2. I0518 11:12:40.938202 12784 solver.cpp:409]     Test net output #0: accuracy = 0.975
  3. I0518 11:12:40.938266 12784 solver.cpp:409]     Test net output #1: loss = 0.0606646 (* 1 = 0.0606646 loss)
  4. I0518 11:12:40.938277 12784 solver.cpp:326] Optimization Done.
  5. I0518 11:12:40.938285 12784 caffe.cpp:215] Optimization Done.



 楼主| 一路向北lm 发表于 2018-7-16 08:30 | 显示全部楼层
仅供参考,
henangongda123 发表于 2018-7-16 18:14 | 显示全部楼层
车辆停到指定位置识别,有方案吗?
 楼主| 一路向北lm 发表于 2018-7-16 20:48 | 显示全部楼层
henangongda123 发表于 2018-7-16 18:14
车辆停到指定位置识别,有方案吗?

打算做个关于这方面的项目。
henangongda123 发表于 2018-7-16 22:30 | 显示全部楼层
一路向北lm 发表于 2018-7-16 20:48
打算做个关于这方面的项目。

我就在搞,方案还没确定
123987 发表于 2018-7-17 16:58 | 显示全部楼层
颜色识别也要训练?搞不懂。另外不知道你看过一个小品没有。冯巩背个包代替安全带,请问你怎么识别驾驶员是系的安全带还是包带?车辆品牌识别。乖乖,高大上哦!呵呵!
 楼主| 一路向北lm 发表于 2018-7-17 20:28 | 显示全部楼层
123987 发表于 2018-7-17 16:58
颜色识别也要训练?搞不懂。另外不知道你看过一个小品没有。冯巩背个包代替安全带,请问你怎么识别驾驶员是 ...

好无奈啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

293

主题

3837

帖子

81

粉丝
快速回复 在线客服 返回列表 返回顶部