Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A multi-functional library for full-stack Deep Learning. Simplifies Model Building, API development, and Model Deployment.

License

NotificationsYou must be signed in to change notification settings

aniketmaurya/chitra

Repository files navigation

CodeFactorCoverageGitHub issuesDocumentation Status

What is chitra?

chitra (चित्र) is a multi-functional library for full-stack Deep Learning. It simplifies Model Building, API development, and Model Deployment.

Components

arch

Load Image from Internet url, filepath ornumpy 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:

🚘 Implementation Roadmap

  • One click deployment toserverless platform.

If you have more use case pleaseraise 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

DownloadsDownloadsGitHub License

Using pip (recommended)

  1. Minimum installationpip install -U chitra

  2. Full Installationpip install -U 'chitra[all]'

  3. Install for Trainingpip install -U 'chitra[nn]'

  4. Install for Servingpip install -U 'chitra[serve]'

From source

pip install git+https://github.com/aniketmaurya/chitra@master

Or,

git clone https://github.com/aniketmaurya/chitra.gitcd chitrapip install .

🧑‍💻 Usage

Loading data for image classification

Chitradataloader anddatagenerator modules for loading data.dataloader is a minimal dataloader thatreturnstf.data.Dataset object.datagenerator provides flexibility to users on how they want to load and manipulatethe data.

importnumpyasnpimportchitrafromchitra.dataloaderimportClfimportmatplotlib.pyplotaspltclf_dl=Clf()data=clf_dl.from_folder(cat_dog_path,target_shape=(224,224))clf_dl.show_batch(8,figsize=(8,8))

Show Batch

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 theTiny 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 thefolder1, now we can just update theimage file generator andrest of the functionality will remain same.

Dataset also support progressive resizing of images.

Updating component

fromchitra.datageneratorimportDatasetds=Dataset(data_path)# it will load the folders and NOT imagesds.filenames[:3]
Output
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']
defload_files(path):returnglob(f'{path}/*/images/*')defget_label(path):returnpath.split('/')[-3]ds.update_component('get_filenames',load_files)ds.filenames[:3]
Output
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 generatorforimg,labelinds.generator():print('first call to generator:',img.shape)break# seconds call to generatorforimg,labelinds.generator():print('seconds call to generator:',img.shape)break# third call to generatorforimg,labelinds.generator():print('third call to generator:',img.shape)break
Output
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 atf.data dataloader was never as easy as this one liner. It converts the Python generatorintotf.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()foreindl.take(1):print(e[0].shape)foreindl.take(1):print(e[0].shape)foreindl.take(1):print(e[0].shape)
Output
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 fromtf.keras.Model, it contains everything that is required for training. It exposestrainer.cyclic_fit method which trains the model using Cyclic Learning rate discoveredbyLeslie Smith.

fromchitra.trainerimportTrainer,create_cnnfromchitra.datageneratorimportDatasetds=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),)
Training Loop... cyclic learning rate already set!
Epoch 1/51/1 [==============================] - 0s 14ms/step - loss: 6.4702 - binary_accuracy: 0.2500Epoch 2/5Returning the last set size which is: (224, 224)1/1 [==============================] - 0s 965us/step - loss: 5.9033 - binary_accuracy: 0.5000Epoch 3/5Returning the last set size which is: (224, 224)1/1 [==============================] - 0s 977us/step - loss: 5.9233 - binary_accuracy: 0.5000Epoch 4/5Returning the last set size which is: (224, 224)1/1 [==============================] - 0s 979us/step - loss: 2.1408 - binary_accuracy: 0.7500Epoch 5/5Returning 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 visualizewhat the Network is learning.trainer module has InterpretModel class which creates GradCam and GradCam++visualization with almost no additional code.

fromchitra.trainerimportInterpretModeltrainer=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: 282Egyptian Mau

png

🎨 Data Visualization

Image annotation

Bounding Box creation is based on top ofimgaug library.

fromchitra.imageimportChitraimportmatplotlib.pyplotaspltbbox= [70,25,190,210]label='Dog'image=Chitra(image_path,bboxes=bbox,labels=label)plt.imshow(image.draw_boxes())

png

SeePlay with Images for detailedexample!

🚀 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 provideschitra.serve.GradioApp for building Interactive UI app for ML/DL modelsandchitra.serve.API for building Rest API endpoint.

fromchitra.serveimportcreate_apifromchitra.trainerimportcreate_cnnmodel=create_cnn('mobilenetv2',num_classes=2)create_api(model,run=True,api_type='image-classification')
API Docs Preview

Preview Model Server

SeeExample Section for detailedexplanation!

🛠 Utility

Limit GPU memory or enable dynamic GPU memory growth for Tensorflow.

fromchitra.utility.tf_utilsimportlimit_gpu,gpu_dynamic_mem_growth# limit the amount of GPU required for your traininglimit_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 theContributingGuidelines 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 fullContributor Covenant Code of Conduct

Acknowledgement

chitra is built with help of awesome libraries likeTensorflow 2.x,imgaug,FastAPI andGradio.


[8]ページ先頭

©2009-2025 Movatter.jp