> ## Content Index
> Fetch the complete content index at: https://yinguobing.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# EfficientDet Runner
- URL: https://yinguobing.com/blog/efficientdet-runner/
- Published: 2021-03-03T09:43:09.000Z
- Updated: 2021-03-03T13:02:46.000Z
- Description: 执行EfficientDet模型推演的最小代码模块。
- Author: 尹国冰
- Tags: 深度学习, 物体检测, TensorFlow

EfficientDet是Google提出来的一种物体检测方案，并且提供了基于TensorFlow的开源实现。地址在这里：

[google/automlGoogle Brain AutoML. Contribute to google/automl development by creating an account on GitHub.![](https://github.githubassets.com/favicons/favicon.svg)GitHubgoogle![](https://avatars.githubusercontent.com/u/1342004?s=400&v=4)](https://github.com/google/automl/tree/master/efficientdet?ref=yinguobing.com)

该实现整体上比较完整，提供了训练、测试与推演的全部代码。但是如果你只需要推演部分的话，就显得有些冗余。

因此我开源了EfficientDet Runner，提供了执行EfficientDet模型推演的最小代码模块。地址如下：

[yinguobing/efficientdet-runner执行EfficientDet模型推演的最小代码模块. Contribute to yinguobing/efficientdet-runner development by creating an account on GitHub.![](https://github.githubassets.com/favicons/favicon.svg)GitHubyinguobing![](https://repository-images.githubusercontent.com/343608911/5d040980-7c47-11eb-88f4-5570ad09665b)](https://github.com/yinguobing/efficientdet-runner?ref=yinguobing.com)

检测的结果可以参见这段视频：

[EfficientDet物体检测Demo\_哔哩哔哩 (゜-゜)つロ 干杯\~-bilibili%E5%9F%BA%E4%BA%8EEfficientDet%E7%9A%84%E7%89%A9%E4%BD%93%E6%A3%80%E6%B5%8BDEMO%E3%80%82%E6%A8%A1%E5%9E%8B%E6%9D%A5%E8%87%AA%E5%AE%98%E6%96%B9%E9%87%8A%E5%87%BA%E7%9A%84%20EfficientDet-d0%E3%80%82%0A%0A%E4%BD%A0%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8%E5%AE%98%E6%96%B9%E4%BB%A3%E7%A0%81%E8%8E%B7%E5%8F%9…![](http://i1.hdslb.com/bfs/archive/dc502cdca9fd0f8475eb957153c39fc66451b11b.jpg@57w_57h_1c.png)真诚友善硬核的流浪AI![](http://i1.hdslb.com/bfs/archive/dc502cdca9fd0f8475eb957153c39fc66451b11b.jpg)](https://www.bilibili.com/video/BV1av411h73n/?ref=yinguobing.com)

以下内容为代码使用方法。

### 准备工作

最小代码意味着你需要以 `SavedModel` 格式存储模型。请在官方实现中完成模型转换工作。

#### 获取官方源代码

```bash
git clone https://github.com/google/automl.git
```

#### 下载模型Checkpoint

EfficientDet包含多个不同规模的实现。这里以D0为例，模型名称为 efficientdet-d0。从官网下载checkpoin文件。

```bash
wget https://storage.googleapis.com/cloud-tpu-checkpoints/efficientdet/coco/efficientdet-d0.tar.gz 
tar xf efficientdet-d0.tar.gz
```

模型checkpoint被解压缩到 efficientdet-d0 文件夹。

#### 转换Checkpoint以支持TensorFlow 2

使用 keras 目录中的 inspector.py，将checkpoint转换为TensorFlow 2支持的格式，并存储到 ckpt-tf2 文件夹。

```bash
python3 -m keras.inspector --mode=dry --model_name=efficientdet-d0 --model_dir=efficientdet-d0 --export_ckpt=ckpt-tf2/efficientdet-d0
```

#### 导出为 SavedModel 格式

以推演为主要目的，SavedModel 格式可以在没有源代码的情况下执行，更加方便。

```bash
python3 -m keras.inspector --mode=export --model_name=efficientdet-d0 \ --model_dir=ckpt-tf2 --saved_model_dir=saved_model
```

该命令会同时保存一份冻结后的模型文件，以便有需要的情况下使用。

### 安装

获取本项目的代码。

```bash
git clone https://github.com/yinguobing/efficientdet-runner.git
```

### 运行

实现视频检测的示例文件 demp.py，记得将导出的模型文件夹存储在 saved\_model 目录下。

```bash
python3 demo.py --video=input.mov
```