热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Bye-byeMobileNet.HelloEfficientNet!

Sotheresultsareclear.IfmodelsizeandinferencetimeisapriorityoveraccuracythenonlyuseMobileNetV2,otherwisetheEfficientNetLitefamilyshouldbeyourgo-tomodelandespeciallyEfficientNetLite-0canactasadirectrepl

Opinion

Bye-bye MobileNet. Hello EfficientNet!

MobileNet was the go-to model for an app and edge deployment. Now it is dethroned by the family of EfficientNet Lite models.

Vardan Agarwal

Follow

Jun 1 ·6min read

Bye-bye MobileNet. Hello EfficientNet!

Photo by Luke Tanis on Unsplash

How to run complex deep learning models on mobiles and edge devices with a limitation of processing power and memory with good speed? Create a model with a backbone of MobileNetV2, convert it to Tensorflow Lite, and you are done. Now it is challenged by EfficientNet Lite. Read on to learn about the need for EfficientNet-Lite from EfficientNet, how to create EfficientNet Lite models and we will also compare these models to see who reigns supreme. If you want to learn about the architecture of EfficientNet in-depth, you can read the article below.

Complete Architectural Details of all EfficientNet Models

Let’s dive deep into the architectural details of all the different EfficientNet Models and find out how they differ…

towardsdatascience.com

Before I start, Yes I have shamelessly stolen the title from Rhea Moutafis hit article Bye-bye Python. Hello Julia!

Drawbacks of EfficientNet models in Tensorflow Lite → Need of EfficientNet Lite

Recently, I had written an article comparing EfficientNet against other pre-trained models like MobileNetV2, Inception, and Xception and I thought of converting those saved models to their Tensorflow Lite counterparts to see how their inference time stacks against each other.

EfficientNet should be the goto pre-trained model or…

Compare the time and accuracy of different pre-trained models and finally create an ensemble to boost results.

towardsdatascience.com

It was all fine till comparing the model sizes but after that, I was in for a shock.

Bye-bye MobileNet. Hello EfficientNet!
Comparing Model Sizes

To calculate the inference time I loaded and processed an image and made predictions on it for a hundred times and took its average.

Bye-bye MobileNet. Hello EfficientNet!
Comparing Model Inference times

The inference time for MobileNet dropped for the Tensorflow Lite model as expected but it increased for the EfficientNet models!!! Tensorflow Lite should make the models smaller and decrease inference time! So why is this happening and how to solve this? This is cleared up in EfficientNet Lite which has the following changes according to this article :

  • Removed squeeze-and-excitation networks since they are not well supported
  • Replaced all swish activations with RELU6, which significantly improved the quality of post-training quantization
  • Fixed the stem and head while scaling models up to reduce the size and computations of scaled models

MobileNet VS EfficientNet Lite in Tensorflow Lite

These models will be created with model maker which as stated in its tutorial

Model Maker library simplifies the process of adapting and converting a TensorFlow neural-network model to particular input data when deploying this model for on-device ML applications.

It sure does this as and creating and it supports MobileNetV2, ResNet50, and the first five models of EfficientNet Lite family. You can also you different models from the Tensorflow Hub using ImageModelSpec or create or own custom model and export ModelSpec to Tensorflow Hub and then use ImageModelSpec .

In this article, I will only be combining MobileNetV2 with EfficientNet Lite 0 to 4. The data used would be the flower dataset with labels of daisy, dandelion, roses, sunflowers, and tulips which will be divided into 80, 10, 10% for training, validation, and testing respectively.

We will need Tensorflow examples for this which can be pip installed using:

!pip install -q git+https://github.com/tensorflow/examples.git#egg=tensorflow-examples[model_maker]

Then we make our required imports which are the model specs, image classifier, data loader for image classifier, TensorFlow, NumPy, and Matplotlib.

from tensorflow_examples.lite.model_maker.core.data_util.image_dataloader import ImageClassifierDataLoader
from tensorflow_examples.lite.model_maker.core.task import image_classifier
from tensorflow_examples.lite.model_maker.core.task.model_spec import (mobilenet_v2_spec,
        efficientnet_lite0_spec,
        efficientnet_lite1_spec,
        efficientnet_lite2_spec,
        efficientnet_lite3_spec,
        efficientnet_lite4_spec)
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

Then we load the data, split it into the required categories, and use the ImageClassifierDataLoader class to make it ready for the image classifier. The from_folder method can be used and it assumes that images of different classes are present in different subfolders in the main folder with the name of the subfolder being the class name. Make sure the images have a PNG or JPG extension as only they are supported.

image_path = tf.keras.utils.get_file('flower_photos',
'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz', untar=True)data = ImageClassifierDataLoader.from_folder(image_path)
train_data, rest_data = data.split(0.8)
validation_data, test_data = rest_data.split(0.5)

Let's take a look at our data

Bye-bye MobileNet. Hello EfficientNet!

Dataset

Creating models with model-maker is one-liner now.

model = image_classifier.create(train_data, model_spec=model_spec, epochs=epochs, validation_data=validation_data)

Specify whichever model spec you want like for MobileNetV2 it is mobilenet_v2_spec or for EfficientNet Lite-2 it is efficientnet_lite2_spec as stated in the imports. EfficientNet Lite-0 is the default one if no one is specified. I trained each for 15 epochs and here are the results.

Bye-bye MobileNet. Hello EfficientNet!

Training and Validation accuracy and loss for all models

Surprisingly, EfficientNet Lite-4 performed badly on both the testing and training sets but this might just mean that it needs more training epochs. The worst performance on the validation set was of MobileNet and the other EfficientNet models were close to each other with EfficientNet Lite-2 and EfficientNet Lite-3 sharing the spoils with the highest accuracy.

To convert these models and save them as Tensorflow Lite files write

model.export(export_dir='.')

This saves a label.txt and a model.tflite file. Using these models is done like the normal tflite models by creating an interpreter.

# Read TensorFlow Lite model from TensorFlow Lite file.
with tf.io.gfile.GFile('model.tflite', 'rb') as f:
  model_cOntent= f.read()

# Read label names from label file.
with tf.io.gfile.GFile('labels.txt', 'r') as f:
  label_names = f.read().split('\n')

# Initialze TensorFlow Lite inpterpreter.
interpreter = tf.lite.Interpreter(model_cOntent=model_content)
interpreter.allocate_tensors()
input_index = interpreter.get_input_details()[0]['index']
output = interpreter.tensor(interpreter.get_output_details()[0]["index"])

# Run predictions on each test image data and calculate accuracy.
accurate_count = 0
for i, (image, label) in enumerate(test_data.dataset):
    # Pre-processing should remain the same. Currently, just normalize each pixel value and resize image according to the model's specification.
    image, _ = model.preprocess(image, label)
    # Add batch dimension and convert to float32 to match with the model's input
    # data format.
    image = tf.expand_dims(image, 0).numpy()

    # Run inference.
    interpreter.set_tensor(input_index, image)
    interpreter.invoke()

    # Post-processing: remove batch dimension and find the label with highest
    # probability.
    predict_label = np.argmax(output()[0])
    # Get label name with label index.
    predict_label_name = label_names[predict_label]

    accurate_count += (predict_label == label.numpy())

accuracy = accurate_count * 1.0 / test_data.size
print('TensorFlow Lite model accuracy = %.3f' % accuracy)

The size of the models, the testing accuracies, and the inference time which was again a mean of 100 times were noted down.

Bye-bye MobileNet. Hello EfficientNet!
Results

So the results are clear. If model size and inference time is a priority over accuracy then only use MobileNetV2, otherwise the EfficientNet Lite family should be your go-to model and especially EfficientNet Lite-0 can act as a direct replacement to MobileNetV2.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 我们


推荐阅读
  • Ihavetwomethodsofgeneratingmdistinctrandomnumbersintherange[0..n-1]我有两种方法在范围[0.n-1]中生 ... [详细]
  • php更新数据库字段的函数是,php更新数据库字段的函数是 ... [详细]
  • 在Linux系统中,网络配置是至关重要的任务之一。本文详细解析了Firewalld和Netfilter机制,并探讨了iptables的应用。通过使用`ip addr show`命令来查看网卡IP地址(需要安装`iproute`包),当网卡未分配IP地址或处于关闭状态时,可以通过`ip link set`命令进行配置和激活。此外,文章还介绍了如何利用Firewalld和iptables实现网络流量控制和安全策略管理,为系统管理员提供了实用的操作指南。 ... [详细]
  • 开机自启动的几种方式
    0x01快速自启动目录快速启动目录自启动方式源于Windows中的一个目录,这个目录一般叫启动或者Startup。位于该目录下的PE文件会在开机后进行自启动 ... [详细]
  • 本文详细介绍了 InfluxDB、collectd 和 Grafana 的安装与配置流程。首先,按照启动顺序依次安装并配置 InfluxDB、collectd 和 Grafana。InfluxDB 作为时序数据库,用于存储时间序列数据;collectd 负责数据的采集与传输;Grafana 则用于数据的可视化展示。文中提供了 collectd 的官方文档链接,便于用户参考和进一步了解其配置选项。通过本指南,读者可以轻松搭建一个高效的数据监控系统。 ... [详细]
  • PTArchiver工作原理详解与应用分析
    PTArchiver工作原理及其应用分析本文详细解析了PTArchiver的工作机制,探讨了其在数据归档和管理中的应用。PTArchiver通过高效的压缩算法和灵活的存储策略,实现了对大规模数据的高效管理和长期保存。文章还介绍了其在企业级数据备份、历史数据迁移等场景中的实际应用案例,为用户提供了实用的操作建议和技术支持。 ... [详细]
  • ### 优化后的摘要本文对 HDU ACM 1073 题目进行了详细解析,该题属于基础字符串处理范畴。通过分析题目要求,我们可以发现这是一道较为简单的题目。代码实现中使用了 C++ 语言,并定义了一个常量 `N` 用于字符串长度的限制。主要操作包括字符串的输入、处理和输出,具体步骤涉及字符数组的初始化和字符串的逆序操作。通过对该题目的深入探讨,读者可以更好地理解字符串处理的基本方法和技巧。 ... [详细]
  • 为了确保iOS应用能够安全地访问网站数据,本文介绍了如何在Nginx服务器上轻松配置CertBot以实现SSL证书的自动化管理。通过这一过程,可以确保应用始终使用HTTPS协议,从而提升数据传输的安全性和可靠性。文章详细阐述了配置步骤和常见问题的解决方法,帮助读者快速上手并成功部署SSL证书。 ... [详细]
  • 深入解析Android 4.4中的Fence机制及其应用
    在Android 4.4中,Fence机制是处理缓冲区交换和同步问题的关键技术。该机制广泛应用于生产者-消费者模式中,确保了不同组件之间高效、安全的数据传输。通过深入解析Fence机制的工作原理和应用场景,本文探讨了其在系统性能优化和资源管理中的重要作用。 ... [详细]
  • 在Cisco IOS XR系统中,存在提供服务的服务器和使用这些服务的客户端。本文深入探讨了进程与线程状态转换机制,分析了其在系统性能优化中的关键作用,并提出了改进措施,以提高系统的响应速度和资源利用率。通过详细研究状态转换的各个环节,本文为开发人员和系统管理员提供了实用的指导,旨在提升整体系统效率和稳定性。 ... [详细]
  • Python 伦理黑客技术:深入探讨后门攻击(第三部分)
    在《Python 伦理黑客技术:深入探讨后门攻击(第三部分)》中,作者详细分析了后门攻击中的Socket问题。由于TCP协议基于流,难以确定消息批次的结束点,这给后门攻击的实现带来了挑战。为了解决这一问题,文章提出了一系列有效的技术方案,包括使用特定的分隔符和长度前缀,以确保数据包的准确传输和解析。这些方法不仅提高了攻击的隐蔽性和可靠性,还为安全研究人员提供了宝贵的参考。 ... [详细]
  • MATLAB字典学习工具箱SPAMS:稀疏与字典学习的详细介绍、配置及应用实例
    SPAMS(Sparse Modeling Software)是一个强大的开源优化工具箱,专为解决多种稀疏估计问题而设计。该工具箱基于MATLAB,提供了丰富的算法和函数,适用于字典学习、信号处理和机器学习等领域。本文将详细介绍SPAMS的配置方法、核心功能及其在实际应用中的典型案例,帮助用户更好地理解和使用这一工具箱。 ... [详细]
  • 优化后的标题:深入探讨网关安全:将微服务升级为OAuth2资源服务器的最佳实践
    本文深入探讨了如何将微服务升级为OAuth2资源服务器,以订单服务为例,详细介绍了在POM文件中添加 `spring-cloud-starter-oauth2` 依赖,并配置Spring Security以实现对微服务的保护。通过这一过程,不仅增强了系统的安全性,还提高了资源访问的可控性和灵活性。文章还讨论了最佳实践,包括如何配置OAuth2客户端和资源服务器,以及如何处理常见的安全问题和错误。 ... [详细]
  • 本文详细介绍了批处理技术的基本概念及其在实际应用中的重要性。首先,对简单的批处理内部命令进行了概述,重点讲解了Echo命令的功能,包括如何打开或关闭回显功能以及显示消息。如果没有指定任何参数,Echo命令会显示当前的回显设置。此外,文章还探讨了批处理技术在自动化任务执行、系统管理等领域的广泛应用,为读者提供了丰富的实践案例和技术指导。 ... [详细]
  • QT框架中事件循环机制及事件分发类详解
    在QT框架中,QCoreApplication类作为事件循环的核心组件,为应用程序提供了基础的事件处理机制。该类继承自QObject,负责管理和调度各种事件,确保程序能够响应用户操作和其他系统事件。通过事件循环,QCoreApplication实现了高效的事件分发和处理,使得应用程序能够保持流畅的运行状态。此外,QCoreApplication还提供了多种方法和信号槽机制,方便开发者进行事件的定制和扩展。 ... [详细]
author-avatar
holy190
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有