keras-efficientnet

A Keras implementation of EfficientNet


License
MIT
Install
pip install keras-efficientnet==0.1.3

Documentation

A Keras implementation of EfficientNet

EfficientNets

[1] Mingxing Tan and Quoc V. Le. EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks. ICML 2019. Arxiv link: https://arxiv.org/abs/1905.11946.

1. About EfficientNet Models

EfficientNets are a family of image classification models, which achieve state-of-the-art accuracy, yet being an order-of-magnitude smaller and faster than previous models.

We develop EfficientNets based on AutoML and Compound Scaling. In particular, we first use AutoML Mobile framework to develop a mobile-size baseline network, named as EfficientNet-B0; Then, we use the compound scaling method to scale up this baseline to obtain EfficientNet-B1 to B7.

EfficientNets achieve state-of-the-art accuracy on ImageNet with an order of magnitude better efficiency:

  • In high-accuracy regime, our EfficientNet-B7 achieves state-of-the-art 84.4% top-1 / 97.1% top-5 accuracy on ImageNet with 66M parameters and 37B FLOPS, being 8.4x smaller and 6.1x faster on CPU inference than previous best Gpipe.

  • In middle-accuracy regime, our EfficientNet-B1 is 7.6x smaller and 5.7x faster on CPU inference than ResNet-152, with similar ImageNet accuracy.

  • Compared with the widely used ResNet-50, our EfficientNet-B4 improves the top-1 accuracy from 76.3% of ResNet-50 to 82.6% (+6.3%), under similar FLOPS constraint.

2. Get the weights

In order to get the TF official weights.

  • Create a Colab file in your Google Drive.
  • Mount the drive with
from google.colab import drive
drive.mount('/content/drive/')
  • Run this command in your created Colab

!gsutil cp -r gs://cloud-tpu-checkpoints/efficientnet "/content/drive/My Drive/effnets/"

  • Download the effnets folder from your drive and extract somewhere.

  • Extract the weights from each layer to a directory by.

from extract_weights import extract_tensors_from_checkpoint_file

extract_tensors_from_checkpoint_file('efficientnet-b0/model.ckpt-109400')  # change this line to your extracted directory
  • Each TF weights directory should be like.
best_eval.txt
checkpoint
model.ckpt-12345.data-00000-of-00001
model.ckpt-12345.index
model.ckpt-12345.meta

Use model.ckpt-12345 in this case.

  • Create the hdf5 weights by run the load_weights.py, make sure to change WEIGHTS_DIR and model_name first.

  • The example of testing a panda image is in main.py

Credit

Layers and utils from

https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet

load_weights and extract_weights implemented from

https://github.com/yuyang-huang/keras-inception-resnet-v2