onnx RyzenAI image-classification dataset:imagenet-1k arxiv:1905.11946 license:apache-2.0 region:us
---
license: apache-2.0
tags:
- RyzenAI
- image-classification
- onnx
datasets:
- imagenet-1k
---

# EfficientNet

The EfficientNet model was proposed in [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](https://arxiv.org/abs/1905.11946) by Mingxing Tan and Quoc V. Le. 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. The specific version of EfficientNet here is EfficientNet-ES (EdgeTPU-Small).

We develop a modified version that could be supported by [AMD Ryzen AI](https://ryzenai.docs.amd.com/en/latest/).

## Model description

The abstract from the paper is the following:

*Convolutional Neural Networks (ConvNets) are commonly developed at a fixed resource budget, and then scaled up for better accuracy if more resources are available. In this paper, we systematically study model scaling and identify that carefully balancing network depth, width, and resolution can lead to better performance. Based on this observation, we propose a new scaling method that uniformly scales all dimensions of depth/width/resolution using a simple yet highly effective compound coefficient. We demonstrate the effectiveness of this method on scaling up MobileNets and ResNet. To go even further, we use neural architecture search to design a new baseline network and scale it up to obtain a family of models, called EfficientNets, which achieve much better accuracy and efficiency than previous ConvNets. In particular, our EfficientNet-B7 achieves state-of-the-art 84.3% top-1 accuracy on ImageNet, while being 8.4x smaller and 6.1x faster on inference than the best existing ConvNet. Our EfficientNets also transfer well and achieve state-of-the-art accuracy on CIFAR-100 (91.7%), Flowers (98.8%), and 3 other transfer learning datasets, with an order of magnitude fewer parameters.*

The original code can be found [here](https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet).

## Intended uses & limitations

You can use the raw model for image classification. See the [model hub](https://huggingface.co/models?sort=trending&search=efficientnet) to look for fine-tuned versions on a task that interests you.

## How to use

### Installation

1. Follow [Ryzen AI Installation](https://ryzenai.docs.amd.com/en/latest/inst.html) to prepare the environment for Ryzen AI.

2. Run the following script to install pre-requisites for this model.

   ```shell
   pip install -r requirements.txt 
   ```

### Test & Evaluation

- Inference one image (Image Classification):

  ```python
  import onnxruntime
  import argparse
  from PIL import Image 
  import torchvision.transforms as transforms 

  parser = argparse.ArgumentParser()
  parser.add_argument('--onnx_path', type=str, default="EfficientNet_int.onnx", required=False)
  parser.add_argument('--image_path', type=str, required=True)

  args = parser.parse_args()

  def read_image():
    # Read a PIL image 
    image = Image.open(args.image_path)
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    
    transform = transforms.Compose([
      transforms.ToTensor(),
      transforms.Resize((224, 224)),
      normalize,
    ])
    img_tensor = transform(image).unsqueeze(0)
    return img_tensor.numpy()


  def main():
    so = onnxruntime.SessionOptions()
    ort_session = onnxruntime.InferenceSession(
      args.onnx_path, so, providers=['CUDAExecutionProvider'])
    ort_inputs = {
      "WrapModel::input_0": read_image()
    }
    output = ort_session.run(None, ort_inputs)[0]
    print("class id =", output[0].argmax())


  if __name__ == "__main__":
    main()
  ```

- Evaluate ImageNet validation dataset(50,000 images), using `eval_onnx.py` .

  ```shell
  python eval_onnx.py --onnx_model EfficientNet_int.onnx --ipu --provider_config Path\To\vaip_config.json --data_dir /Path/To/Your/Dataset
  ```

### Performance

​	Dataset: ImageNet validation dataset (50,000 images).

|       Metric        | Accuracy on IPU  |
| :-----------------: | :-------------: |
| top1& top5 accuracy | 77.72% / 93.78% |

## Citation

```bibtex
@article{EfficientNet,
 author       = {Mingxing Tan and Quoc V. Le},
  title       = {Searching for MobileNetV3},
  year        = {2019},
  url         = {https://arxiv.org/abs/1905.11946},
}
```