Are you interested in computer vision and looking for a user-friendly library to work with image models in Keras? Look no further than Keras Image Models (KIMM)! KIMM is a fantastic collection of image models, blocks, and layers written in Keras 3, making it easy for you to perform various computer vision tasks with state-of-the-art models and pre-trained weights.
What is KIMM?
KIMM stands for Keras Image Models and is an open-source library developed by Hongyu Chiu. It aims to provide a wide range of image models in a user-friendly manner, making it accessible to both beginners and experienced practitioners in the field of computer vision.
Key Features of KIMM:
- Model zoo: KIMM offers a comprehensive model zoo with almost all models coming with pre-trained weights on the ImageNet dataset. This means you can quickly get started with these models without having to train them from scratch.
- Easy model export: KIMM provides APIs to export models to
.tflite
and.onnx
formats, making it convenient to deploy your models on different platforms. - Reparameterization support: KIMM supports the reparameterization technique, allowing you to optimize your models for better performance and efficiency.
- Feature extraction: With KIMM, you can easily extract features from your models, enabling you to use them for various downstream tasks like image classification, object detection, and more.
Installation
Installing KIMM is a breeze! Simply run the following command in your terminal or command prompt:
# Installation
pip install keras kimm -U
Make sure you have Keras installed as well, as KIMM depends on it.
Usage
KIMM provides a simple and intuitive API for working with image models. Here are some key functions and their usage:
kimm.list_models
: Lists the available models in KIMM.kimm.models.*.available_feature_keys
: Returns the available feature keys for a specific model.kimm.models.*()
: Initializes a specific model with optional arguments.kimm.models.*(..., feature_extractor=True, feature_keys=...)
: Enables feature extraction for a specific model.kimm.utils.get_reparameterized_model
: Reparameterizes a model for optimization.kimm.export.export_tflite
: Exports a model to the.tflite
format.kimm.export.export_onnx
: Exports a model to the.onnx
format.
Here’s a simple example of how you can use KIMM to initialize a MobileOneS0 model with pre-trained ImageNet weights and perform feature extraction:
import keras
import kimm
import numpy as np
# List available models
print(kimm.list_models("mobileone", weights="imagenet"))
# ['MobileOneS0', 'MobileOneS1', 'MobileOneS2', 'MobileOneS3']
# Initialize model with pretrained ImageNet weights
x = keras.random.uniform([1, 224, 224, 3])
model = kimm.models.MobileOneS0()
y = model.predict(x)
print(y.shape)
# (1, 1000)
# List available feature keys of the model class
print(kimm.models.MobileOneS0.available_feature_keys)
# ['STEM_S2', 'BLOCK0_S4', 'BLOCK1_S8', 'BLOCK2_S16', 'BLOCK3_S32']
# Enable feature extraction by setting `feature_extractor=True`
model = kimm.models.MobileOneS0(
feature_extractor=True, feature_keys=["BLOCK2_S16", "BLOCK3_S32"]
)
features = model.predict(x)
for feature_name, feature in features.items():
print(feature_name, feature.shape)
# BLOCK2_S16 (1, 14, 14, 256)
# BLOCK3_S32 (1, 7, 7, 1024)
# TOP (1, 1000)
Practical examples
To help you get started with KIMM, the library provides several practical examples in the form of Colab notebooks. These examples cover various tasks and demonstrate how to use KIMM effectively. Here are a few noteworthy examples:
- Image classification with ImageNet weights:
- Colab Notebook: https://colab.research.google.com/drive/14WxYgVjlwCIO9MwqPYW-dskbTL2UHsVN?usp=sharing
- This example showcases how to use KIMM to perform image classification using a pre-trained model on the ImageNet dataset. It demonstrates the simplicity and effectiveness of using KIMM for classification tasks.
- Fine-Tuning on Cats vs. Dogs dataset:
- Colab Notebook: https://colab.research.google.com/drive/1IbqfqG2NKEOKvBOznIPT1kjOdVPfThmd?usp=sharing
- This example demonstrates how to fine-tune an image classification model using KIMM on the Cats vs. Dogs dataset. It showcases the process of transfer learning and how KIMM can be used to achieve excellent results with minimal effort.
- Grad-CAM visualization:
- Colab Notebook: https://colab.research.google.com/drive/1h25VmsYDOLL6BNbRPEVOh1arIgcEoHu6?usp=sharing
- This example illustrates how to use KIMM to generate Grad-CAM visualizations, which highlight the regions of an image that contribute most to a specific class prediction. It provides insights into the model’s decision-making process and can be valuable for interpretability and debugging.
Model zoo
KIMM offers a wide range of image models in its model zoo, covering various architectures and tasks. Some of the notable models include:
- ConvMixer
- ConvNeXt
- DenseNet
- EfficientNet
- EfficientNetLite
- EfficientNetV2
- GhostNet
- GhostNetV2
- HGNet
- HGNetV2
- InceptionNeXt
- InceptionV3
- LCNet
- MobileNetV2
- MobileNetV3
- MobileOne
- MobileViT
- MobileViTV2
- RegNet
- RepVGG
- ResNet
- TinyNet
- VGG
- ViT
- Xception
Each model comes with pre-trained weights on the ImageNet dataset, making it easy to use them for transfer learning or as feature extractors.
Conclusion
Keras Image Models (KIMM) is a powerful and user-friendly library that simplifies working with image models in Keras. With its extensive model zoo, pre-trained weights, and feature extraction capabilities, KIMM empowers you to tackle various computer vision tasks with ease. Whether you are a beginner or an experienced practitioner, KIMM provides a seamless experience for leveraging state-of-the-art models in your projects.
To get started with KIMM, simply install it via pip and explore the provided examples and documentation. The library’s GitHub repository at https://github.com/james77777778/keras-image-models serves as a valuable resource, containing the source code, documentation, and issue tracker. You can also find KIMM on PyPI at https://pypi.org/project/kimm/ for easy installation.