How to Implement ShuffleNet in TensorFlow

Nov 4, 2020 | Data Science

Welcome to the world of efficient Convolutional Neural Networks (CNNs) tailored for mobile devices! Today, we’re diving into how to implement ShuffleNet using TensorFlow, a remarkable architecture that minimizes computational demands while maintaining performance. Let’s explore this topic step by step.

What is ShuffleNet?

ShuffleNet is a CNN architecture specifically designed for mobile devices with limited computing power. It’s a lightweight alternative that outperforms Google MobileNet, achieving better results with fewer floating point operations (FLOPs). You can find the in-depth research in the original paper: ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices.

Understanding the ShuffleNet Unit

This image illustrates the primary components of the ShuffleNet unit, which includes innovative techniques such as group convolutions and channel shuffling.

Key Concepts

Group Convolutions

One of the standout features of ShuffleNet is the group convolution operator. However, it is important to note that the TensorFlow backend does not support this operator natively. Therefore, it’s essential to implement the group convolution using graph operations instead. For additional information on this implementation, you can check the discussion here: Support Channel groups in convolutional layers #10482.

Channel Shuffling

Channel shuffling is vital for better channel interaction. You can achieve channel shuffling by following these steps:

  • Reshape the input tensor from (N, H, W, C) into (N, H, W, G, C).
  • Perform a matrix transpose operation on the dimensions (G, C).
  • Reshape the tensor back into (N, H, W, C).

Where:

  • N: Batch size
  • H: Feature map height
  • W: Feature map width
  • C: Number of channels
  • G: Number of groups

Note that the number of channels should be divisible by the number of groups.

Usage

Main Dependencies

Before running the model, ensure you have the following Python packages installed:

  • Python 3 or above
  • TensorFlow 1.3.0
  • NumPy 1.13.1
  • TQDM 4.15.0
  • easydict 1.7
  • Matplotlib 2.0.2

Training and Testing

  1. Prepare your dataset and modify the data_loader.py in the DataLoader class’s load_data() method.
  2. Adjust the configtest.json file to fit your needs.

To run your implementation, use:

python main.py --config configtest.json

Results

The model has successfully overfitted the TinyImageNet-200 dataset referenced in CS231n – Convolutional Neural Networks for Visual Recognition, and work is underway for training on the ImageNet dataset.

Benchmarking Performance

The original paper achieved around 140 MFLOPs. With the implemented group convolution operator in TensorFlow, a remarkable 270 MFLOPs have been attained. Since the paper counts multiplications and additions as one unit, this translates into achieving the proposed performance level effectively.

Calculating FLOPs in TensorFlow

To calculate the FLOPs, ensure your batch size is set to 1, and execute the following command once your model is loaded:

tf.profiler.profile(        tf.get_default_graph(),        options=tf.profiler.ProfileOptionBuilder.float_operation(), cmd=scope)

Troubleshooting

If you encounter issues during the implementation, consider the following:

  • Check TensorFlow version compatibility with your machine’s architecture.
  • Ensure all dependencies are correctly installed and in their required versions.
  • Refer to error messages for hints on what may be going wrong, and feel free to seek help in community forums.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Final Thoughts

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Updates

As of now, both inference and training features are functioning properly. Keep an eye out for further progress!

License

This project is licensed under the Apache License 2.0, so feel free to review the LICENSE file for details.

Acknowledgments

Special thanks to my colleagues who supported me during this project, particularly Momen Abdelrazek and Mohamed Zahran.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox