将yolov2-tiny模型部署到前端

1、下载YAD2K

YAD2K是一个转换器,将darknet的权重(weights)和配置(cfg)文件转换为keras的模型文件(.h5)。
安装步骤:
具体安装步骤这里有介绍

# clone文件,再进入cd到文件夹
git clone https://github.com/allanzelener/yad2k.git
cd yad2k

2、模型转换

① 需要修改一些bug,将yad2k.py中,第83行buffer = weights_file.read(16) 改为buffer = weights_file.read(20);


从网址https://pjreddie.com/media/files/yolov2-tiny.weights下载yolov2-tiny的权重文件yolov2-tiny.weights;
从网址https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov2-tiny.cfg下载yolov2-tiny的配置文件yolov2-tiny.cfg;

③ 将这两个文件都放置在yad2k的根目录下,在根目录下打开终端,输入:

python yad2k.py yolov2-tiny.cfg yolov2-tiny.weights model_data/yolov2-tiny.h5

将darknet的模型文件转换为keras的模型文件;

⑤ 转换完之后记得运行检验一下

python test_yolo.py model_data/yolov2-tiny.h5

如果能够输出一些预测数据,则说明没有问题
如:

Using TensorFlow backend.
2019-10-15 17:04:09.704439: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2019-10-15 17:04:09.945269: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433] Found device 0 with properties:
name: GeForce GTX 1060 6GB major: 6 minor: 1 memoryClockRate(GHz): 1.7085
pciBusID: 0000:01:00.0
totalMemory: 6.00GiB freeMemory: 4.96GiB
2019-10-15 17:04:09.952606: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0
2019-10-15 17:04:10.295898: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-10-15 17:04:10.299511: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990]      0
2019-10-15 17:04:10.301350: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0:   N
2019-10-15 17:04:10.303334: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4706 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060 6GB, pci bus id: 0000:01:00.0, compute capability: 6.1)
WARNING:tensorflow:From D:\anaconda\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
D:\anaconda\lib\site-packages\keras\engine\saving.py:292: UserWarning: No training configuration found in save file: the model was *not* compiled. Compile it manually.
  warnings.warn('No training configuration found in save file: '
model_data/yolov2-tiny.h5 model, anchors, and classes loaded.
Found 6 boxes for dog.jpg
person 0.34 (66, 66) (97, 94)
car 0.40 (500, 101) (716, 176)
motorbike 0.44 (64, 78) (100, 120)
dog 0.44 (128, 212) (389, 531)
car 0.62 (455, 85) (669, 166)
bicycle 0.85 (45, 93) (596, 482)
Found 1 boxes for eagle.jpg
bird 0.87 (57, 127) (608, 465)
Found 2 boxes for giraffe.jpg
giraffe 0.62 (145, 0) (445, 416)
zebra 0.75 (262, 274) (418, 444)
Found 4 boxes for horses.jpg
horse 0.46 (224, 187) (437, 374)
horse 0.58 (7, 183) (171, 255)
horse 0.87 (421, 208) (589, 352)
horse 0.87 (9, 187) (292, 400)
Found 3 boxes for person.jpg
dog 0.66 (66, 258) (203, 349)
horse 0.75 (420, 133) (597, 335)
person 0.86 (184, 100) (277, 337)
Found 0 boxes for scream.jpg

3、将.h5模型文件转换为tfjs模型文件

首先需要有tensorflowjs API,可以pip install tensorflowjs安装,建议使用虚拟环境。

ensorflowjs_converter --input_format keras \			# keras的固定格式
                       model_data/yolov2-tiny.h5 \		# .h5所在的路径
                       tfjs_model_data					# 转换后的模型保存路劲

具体转换方法可参考官网介绍

4、前端部署

在html文件中导入tfjs:

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"> </script>

调用:

<script>
    const url = "./tfjs_model_data/model.json";     	// 模型路径
    async function func() {
        const model = await tf.loadLayersModel(url);    // 加载模型
        console.log(model);
        result = model.predict(img);          			// 模型预测
    }

    func();
</script>

5、遇到的问题及解决办法

FileNotFoundError: [Errno 2] No such file or directory: 'model_data/yolo_anchors.txt’
解决办法:
将model_data里面的yolov2_tiny_anchors.txt改为yolo_anchors.txt即可

PermissionError: [Errno 13] Permission denied: 'images\out’
解决办法:
将test_yolo.py的第118行前面加上 if os.path.isfile(os.path.join(test_path, image_file)): ,后面的代码相应对齐(119~190行)

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值