Skip to main content

Image utility library for Deep Learning

Project description

chitra

chitra

What is chitra?

chitra (चित्र) is an image utility library for Deep Learning tasks. (It is not image-processing library)

chitra reduces image data loading boilerplates for classification and object-detection.

It can also generate bounding-boxes from the annotated dataset.

If you have more use cases please raise an issue with the feature you want.

Installation

Using pip (recommended)

pip install -U chitra

From source

git clone https://github.com/aniketmaurya/chitra.git
cd chitra
pip install -e .

Usage

Loading data for image classification

import numpy as np
import tensorflow as tf
import chitra
from chitra.dataloader import Clf, show_batch
import matplotlib.pyplot as plt
path = '/Users/aniket/Pictures/data/train'

clf_dl = Clf()
data = clf_dl.from_folder(path, target_shape=(224, 224))

clf_dl.show_batch(8, figsize=(8,8))
for e in data.take(1):
    image = e[0].numpy().astype('uint8')
    label = e[1].numpy()
plt.imshow(image)
plt.show()

png

Visualization

Image annotation

Thanks to fizyr keras-retinanet.

from chitra.visualization import draw_annotations

labels = np.array([label])
bbox = np.array([[30, 50, 170, 190]])
label_to_name = lambda x: 'Cat' if x==0 else 'Dog'
draw_annotations(image, ({'bboxes': bbox, 'labels':labels,}), label_to_name=label_to_name)
plt.imshow(image)
plt.show()

png

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.

from chitra.datagenerator import Dataset
from glob import glob
ds = Dataset('/data/aniket/tiny-imagenet/data/tiny-imagenet-200/train')
# it will load the folders and NOT images
ds.filenames[:3]
No item present in the image size list





['/data/aniket/tiny-imagenet/data/tiny-imagenet-200/train/n03584254',
 '/data/aniket/tiny-imagenet/data/tiny-imagenet-200/train/n02403003',
 '/data/aniket/tiny-imagenet/data/tiny-imagenet-200/train/n02056570']
def new_image_fileloader(path): return glob(f'{path}/*/images/*')

ds.update_component('get_filenames', new_image_fileloader)
ds.filenames[:3]
get_filenames updated with <function new_image_fileloader at 0x7fd1dc18fdd0>





['/data/aniket/tiny-imagenet/data/tiny-imagenet-200/train/n03584254/images/n03584254_251.JPEG',
 '/data/aniket/tiny-imagenet/data/tiny-imagenet-200/train/n03584254/images/n03584254_348.JPEG',
 '/data/aniket/tiny-imagenet/data/tiny-imagenet-200/train/n03584254/images/n03584254_465.JPEG']

Progressive resizing

image_sz_list = [(28, 28), (32, 32), (64, 64)]

ds = Dataset('/data/aniket/tiny-imagenet/data/tiny-imagenet-200/train', image_size=image_sz_list)
ds.update_component('get_filenames', new_image_fileloader)

# 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 new_image_fileloader at 0x7fd1dc18fdd0>
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/aniket/tiny-imagenet/data/tiny-imagenet-200/train', image_size=image_sz_list)
ds.update_component('get_filenames', new_image_fileloader)

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 new_image_fileloader at 0x7fd1dc18fdd0>
(32, 32, 3)
(64, 64, 3)
Returning the last set size which is: (64, 64)
(64, 64, 3)

Utils

from chitra.utils import limit_gpu

# limit the amount of GPU required for your training
limit_gpu(gpu_id=0, memory_limit=1024*2)
1 Physical GPUs, 1 Logical GPUs

Contributing

Contributions of any kind are welcome. Please check the Contributing Guidelines before contributing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chitra-0.0.16.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

chitra-0.0.16-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file chitra-0.0.16.tar.gz.

File metadata

  • Download URL: chitra-0.0.16.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.9

File hashes

Hashes for chitra-0.0.16.tar.gz
Algorithm Hash digest
SHA256 d5659ce05bb2b4666901090424f1bb77b18585edfc8a6293e412b95460fc2ff3
MD5 08b43b693c983f7a3c9f898895c9f873
BLAKE2b-256 ca7b4f22ef60e78577eac5f4de4635eae158b3234019f74d5f2cc593f9098a1b

See more details on using hashes here.

Provenance

File details

Details for the file chitra-0.0.16-py3-none-any.whl.

File metadata

  • Download URL: chitra-0.0.16-py3-none-any.whl
  • Upload date:
  • Size: 16.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.9

File hashes

Hashes for chitra-0.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 b8961b9738e193d14a4f996adf5dcde54a1f427908cd96f703a8ff0360f5ddee
MD5 1bc8a0100ce083d1049e25a9adb86f92
BLAKE2b-256 37d58b23f04b7b80e8ff12d94f13ead53841e32d2a5f92650f23ddcb91458df8

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page