训练需装 chitra[nn],服务需装 chitra[serve],完整功能用 chitra[all];训练与推理依赖 TensorFlow/Keras;边界框可视化基于 imgaug 库。
chitra
aniketmauryaApache-2.0活跃维护一站式深度学习全栈库,把模型搭建、FastAPI 接口开发和模型部署串起来,还带 GradCAM 可解释性分析,省去自己拼装各环节的麻烦。
A multi-functional library for full-stack Deep Learning. Simplifies Model Building, API development, and Model Deployment.
pip install -U chitraREADME 未说明具体显存要求;提供 limit_gpu 与 gpu_dynamic_mem_growth 工具来限制或动态增长 TensorFlow 的 GPU 显存。
一站式深度学习库,简化模型构建、API 开发与部署
一站式深度学习全栈库,把模型搭建、FastAPI 接口开发和模型部署串起来,还带 GradCAM 可解释性分析,省去自己拼装各环节的麻烦。
核心能力
- 用 Trainer 类基于 tf.keras 做循环学习率训练
- 用 GradCAM/GradCAM++ 零额外代码解释模型
- 用 Chitra 类在图片上绘制边界框做可视化
- 用 create_api 为任意框架模型生成 REST API 或 Gradio UI
- 支持渐进式图像缩放与 tf.data 快速数据加载
容易踩的坑
- 0.2.1.dev0pre-release2022-01-09 · 2022-01-09 09:31 UTC
查看更新说明
What's Changed
- Refactor CI by @aniketmaurya in https://github.com/gradsflow/chitra/pull/142
- fix imgaug dependencies by @aniketmaurya in https://github.com/gradsflow/chitra/pull/143
- Update docs by @aniketmaurya in https://github.com/gradsflow/chitra/pull/144
- fix docs by @aniketmaurya in https://github.com/gradsflow/chitra/pull/145
- Format code with black by @deepsource-autofix in https://github.com/gradsflow/chitra/pull/146
- add batchify by @aniketmaurya in https://github.com/gradsflow/chitra/pull/147
- Pip compile docs/requirements.txt by @aniketmaurya in https://github.com/gradsflow/chitra/pull/149
- [Snyk] Security upgrade mistune from 0.8.4 to 2.0.1 by @aniketmaurya in https://github.com/gradsflow/chitra/pull/150
- add default extensions by @aniketmaurya in https://github.com/gradsflow/chitra/pull/153
- fixes by @aniketmaurya in https://github.com/gradsflow/chitra/pull/154
- revert fixes by @aniketmaurya in https://github.com/gradsflow/chitra/pull/155
New Contributors
- @deepsource-autofix made their first contribution in https://github.com/gradsflow/chitra/pull/146
Full Changelog: https://github.com/gradsflow/chitra/compare/v0.2.0...0.2.1.dev0
- v0.2.0最新正式版2021-11-26 · 2021-11-26 17:10 UTC
What's Changed
- Config file for pyup.io by @pyup-bot in https://github.com/aniketmaurya/chitra/pull/30
- [ImgBot] Optimize images by @imgbot in https://github.com/aniketmaurya/chitra/pull/28
- Create CODE_OF_CONDUCT.md by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/31
- update serving example by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/32
- docs: fix image url & python style by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/33
- Bugfix/datagenerator by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/44
- update sonar badges by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/45
- Update docs by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/47
- [ImgBot] Optimize images by @imgbot in https://github.com/aniketmaurya/chitra/pull/48
- Update docs by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/49
- [ImgBot] Optimize images by @imgbot in https://github.com/aniketmaurya/chitra/pull/50
- [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/aniketmaurya/chitra/pull/51
- Update CONTRIBUTING.md by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/53
- update chitra description by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/56
- update docs with new Version 📖 by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/57
- Docs/style by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/59
- add model serving to documentation by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/60
- add cache image func by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/61
- setup testing CI by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/62
- Fix 'Duplicate Code' issue in chitra\dataloader.py by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/64
- Add pep8speaks by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/65
- remove codacy by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/66
- refactor code & test by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/67
- fix sonar coverage by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/68
- replace / with internal keyword by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/70
- quality/reduce code smell ✨ by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/73
- increase coverage 🚀 by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/74
- increase coverage 🚀 by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/75
- deepsource auto fixes 🤖 by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/79
- visualize confusion matrix by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/81
- [ImgBot] Optimize images by @imgbot in https://github.com/aniketmaurya/chitra/pull/82
- update docs by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/83
- fix hanging test by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/84
- Migrate to black by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/86
- resize bounding box with image.resize() by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/87
- 📝document image-bbox resize by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/88
- add dataprocess pipeline and class based model serving by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/89
- [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/aniketmaurya/chitra/pull/90
- [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/aniketmaurya/chitra/pull/91
- fix requirement install by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/92
- refactor API & Docs by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/93
- docs fixes by @aniketmaurya in https://github.com/aniketmaurya/chitra/pull/94
- Integrate Gr
- v0.1.1Auto Build Docker Image for Model Server2021-08-07 · 2021-08-07 14:05 UTC
查看更新说明
chitraCLI can build docker image for any kind of Machine Learning or Deep Learning Model.chitra builder create [OPTIONS] Options: --path TEXT [default: ./] --port TEXT --tag TEXT --help Show this message and exit. - 0.1.0Release 0.1.02021-07-24 · 2021-07-24 11:45 UTC
查看更新说明
🎉 🎉 Chitra now supports Model Serving and Image Data Loading with Bounding Boxes. You can easily create UI app or Rest API for Any Machine Learning or Deep Learning Model (Framework Agnostic).
Auto Dockerization and Serverless deployment is in roadmap 🚘.
📚 Get started with the Documentation https://chitra.readthedocs.io/en/latest/
- 0.1.0b4Model Serverpre-release2021-07-22 · 2021-07-22 02:56 UTC
查看更新说明
🎉 GradioApp integration 🚀 Increase Code Coverage ✨ Data Processing refactor
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
chitra
What is chitra?
chitra (चित्र) is a multi-functional library for full-stack Deep Learning. It simplifies Model Building, API development, and Model Deployment.
Components
Load Image from Internet url, filepath or numpy array and plot Bounding Boxes on the images easily.
Model Training and Explainable AI.
Easily create UI for Machine Learning models or Rest API backend that can be deployed for serving ML Models in Production.
📌 Highlights:
- [New] Auto Dockerization of Models 🐳
- [New] Framework Agnostic Model Serving & Interactive UI prototype app ✨🌟
- [New] Data Visualization, Bounding Box Visualization 🐶🎨
- Model interpretation using GradCAM/GradCAM++ with no extra code 🔥
- Faster data loading without any boilerplate 🤺
- Progressive resizing of images 🎨
- Rapid experiments with different models using
chitra.trainermodule 🚀
🚘 Implementation Roadmap
- One click deployment to
serverlessplatform.
If you have more use case please raise an issue/PR with the feature you want. If you want to contribute, feel free to raise a PR. It doesn't need to be perfect. We will help you get there.
📀 Installation
Using pip (recommended)
-
Minimum installation
pip install -U chitra -
Full Installation
pip install -U 'chitra[all]' -
Install for Training
pip install -U 'chitra[nn]' -
Install for Serving
pip install -U 'chitra[serve]'
From source
pip install git+https://github.com/aniketmaurya/chitra@master
Or,
git clone https://github.com/aniketmaurya/chitra.git
cd chitra
pip install .
🧑💻 Usage
Loading data for image classification
Chitra dataloader and datagenerator modules for loading data. dataloader is a minimal dataloader that
returns tf.data.Dataset object. datagenerator provides flexibility to users on how they want to load and manipulate
the data.
import numpy as np
import chitra
from chitra.dataloader import Clf
import matplotlib.pyplot as plt
clf_dl = Clf()
data = clf_dl.from_folder(cat_dog_path, target_shape=(224, 224))
clf_dl.show_batch(8, figsize=(8, 8))

Image datagenerator
Dataset class provides the flexibility to load image dataset by updating components of the class.
Components of Dataset class are:
- image file generator
- resizer
- label generator
- image loader
These components can be updated with custom function by the user according to their dataset structure. For example the Tiny Imagenet dataset is organized as-
train_folder/
.....folder1/
.....file.txt
.....folder2/
.....image1.jpg
.....image2.jpg
.
.
.
......imageN.jpg
The inbuilt file generator search for images on the folder1, now we can just update the image file generator and
rest of the functionality will remain same.
Dataset also support progressive resizing of images.
Updating component
from chitra.datagenerator import Dataset
ds = Dataset(data_path)
# it will load the folders and NOT images
ds.filenames[:3]
No item present in the image size list
['/Users/aniket/Pictures/data/tiny-imagenet-200/train/n02795169/n02795169_boxes.txt',
'/Users/aniket/Pictures/data/tiny-imagenet-200/train/n02795169/images',
'/Users/aniket/Pictures/data/tiny-imagenet-200/train/n02769748/images']
def load_files(path):
return glob(f'{path}/*/images/*')
def get_label(path):
return path.split('/')[-3]
ds.update_component('get_filenames', load_files)
ds.filenames[:3]
get_filenames updated with <function load_files at 0x7fad6916d0e0>
No item present in the image size list
['/Users/aniket/Pictures/data/tiny-imagenet-200/train/n02795169/images/n02795169_369.JPEG',
'/Users/aniket/Pictures/data/tiny-imagenet-200/train/n02795169/images/n02795169_386.JPEG',
'/Users/aniket/Pictures/data/tiny-imagenet-200/train/n02795169/images/n02795169_105.JPEG']
Progressive resizing
It is the technique to sequentially resize all the images while training the CNNs on smaller to bigger image sizes. Progressive Resizing is described briefly in his terrific fastai course, “Practical Deep Learning for Coders”. A great way to use this technique is to train a model with smaller image size say 64x64, then use the weights of this model to train another model on images of size 128x128 and so on. Each larger-scale model incorporates the previous smaller-scale model layers and weights in its architecture. ~KDnuggets
image_sz_list = [(28, 28), (32, 32), (64, 64)]
ds = Dataset(data_path, image_size=image_sz_list)
ds.update_component('get_filenames', load_files)
ds.update_component('get_label', get_label)
# first call to generator
for img, label in ds.generator():
print('first call to generator:', img.shape)
break
# seconds call to generator
for img, label in ds.generator():
print('seconds call to generator:', img.shape)
break
# third call to generator
for img, label in ds.generator():
print('third call to generator:', img.shape)
break
get_filenames updated with <function load_files at 0x7fad6916d0e0>
get_label updated with <function get_label at 0x7fad6916d8c0>
first call to generator: (28, 28, 3)
seconds call to generator: (32, 32, 3)
third call to generator: (64, 64, 3)
tf.data support
Creating a tf.data dataloader was never as easy as this one liner. It converts the Python generator
into tf.data.Dataset for a faster data loading, prefetching, caching and everything provided by tf.data.
image_sz_list = [(28, 28), (32, 32), (64, 64)]
ds = Dataset(data_path, image_size=image_sz_list)
ds.update_component('get_filenames', load_files)
ds.update_component('get_label', get_label)
dl = ds.get_tf_dataset()
for e in dl.take(1):
print(e[0].shape)
for e in dl.take(1):
print(e[0].shape)
for e in dl.take(1):
print(e[0].shape)
get_filenames updated with <function load_files at 0x7fad6916d0e0>
get_label updated with <detn get_label at 0x7fad6916d8c0>
(28, 28, 3)
(32, 32, 3)
(64, 64, 3)
Trainer
The Trainer class inherits from tf.keras.Model, it contains everything that is required for training. It exposes
trainer.cyclic_fit method which trains the model using Cyclic Learning rate discovered
by Leslie Smith.
from chitra.trainer import Trainer, create_cnn
from chitra.datagenerator import Dataset
ds = Dataset(cat_dog_path, image_size=(224, 224))
model = create_cnn('mobilenetv2', num_classes=2, name='Cat_Dog_Model')
trainer = Trainer(ds, model)
# trainer.summary()
trainer.compile2(batch_size=8,
optimizer=tf.keras.optimizers.SGD(1e-3, momentum=0.9, nesterov=True),
lr_range=(1e-6, 1e-3),
loss='binary_crossentropy',
metrics=['binary_accuracy'])
trainer.cyclic_fit(epochs=5,
batch_size=8,
lr_range=(0.00001, 0.0001),
)
Epoch 1/5
1/1 [==============================] - 0s 14ms/step - loss: 6.4702 - binary_accuracy: 0.2500
Epoch 2/5
Returning the last set size which is: (224, 224)
1/1 [==============================] - 0s 965us/step - loss: 5.9033 - binary_accuracy: 0.5000
Epoch 3/5
Returning the last set size which is: (224, 224)
1/1 [==============================] - 0s 977us/step - loss: 5.9233 - binary_accuracy: 0.5000
Epoch 4/5
Returning the last set size which is: (224, 224)
1/1 [==============================] - 0s 979us/step - loss: 2.1408 - binary_accuracy: 0.7500
Epoch 5/5
Returning the last set size which is: (224, 224)
1/1 [==============================] - 0s 982us/step - loss: 1.9062 - binary_accuracy: 0.8750
<tensorflow.python.keras.callbacks.History at 0x7f8b1c3f2410>
✨ Model Interpretability
It is important to understand what is going inside the model. Techniques like GradCam and Saliency Maps can visualize
what the Network is learning. trainer module has InterpretModel class which creates GradCam and GradCam++
visualization with almost no additional code.
from chitra.trainer import InterpretModel
trainer = Trainer(ds, create_cnn('mobilenetv2', num_classes=1000, keras_applications=False))
model_interpret = InterpretModel(True, trainer)
image = ds[1][0].numpy().astype('uint8')
image = Image.fromarray(image)
model_interpret(image)
print(IMAGENET_LABELS[285])
Returning the last set size which is: (224, 224)
index: 282
Egyptian Mau

🎨 Data Visualization
Image annotation
Bounding Box creation is based on top of imgaug library.
from chitra.image import Chitra
import matplotlib.pyplot as plt
bbox = [70, 25, 190, 210]
label = 'Dog'
image = Chitra(image_path, bboxes=bbox, labels=label)
plt.imshow(image.draw_boxes())

See Play with Images for detailed example!
🚀 Model Serving (Framework Agnostic)
Chitra can Create Rest API or Interactive UI app for Any Learning Model -
ML, DL, Image Classification, NLP, Tensorflow, PyTorch or SKLearn.
It provides chitra.serve.GradioApp for building Interactive UI app for ML/DL models
and chitra.serve.API for building Rest API endpoint.
from chitra.serve import create_api
from chitra.trainer import create_cnn
model = create_cnn('mobilenetv2', num_classes=2)
create_api(model, run=True, api_type='image-classification')

See Example Section for detailed explanation!
🛠 Utility
Limit GPU memory or enable dynamic GPU memory growth for Tensorflow.
from chitra.utility.tf_utils import limit_gpu, gpu_dynamic_mem_growth
# limit the amount of GPU required for your training
limit_gpu(gpu_id=0, memory_limit=1024 * 2)
No GPU:0 found in your system!
gpu_dynamic_mem_growth()
No GPU found on the machine!
🤗 Contribute
Contributions of any kind are welcome. Please check the Contributing Guidelines before contributing.
Code Of Conduct
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
Read full Contributor Covenant Code of Conduct
Acknowledgement
chitra is built with help of awesome libraries like Tensorflow 2.x, imgaug, FastAPI and Gradio.