Code Monkey home page Code Monkey logo

kerasgen's Introduction

kerasgen

Latest PyPI version DOI

A Keras/Tensorflow compatible image data generator for creating balanced batches. This datagenerator is compatible with TripletLoss as it guarantees the existence of postive pairs in every batch.

Installation

pip install kerasgen

Usage

from kerasgen.balanced_image_dataset import balanced_image_dataset_from_directory

train_ds = balanced_image_dataset_from_directory(
    directory, num_classes_per_batch=2,
    num_images_per_class=5, image_size=(256, 256),
    validation_split=0.2, subset='training', seed=555,
    safe_triplet=True)

val_ds = balanced_image_dataset_from_directory(
    directory, num_classes_per_batch=2,
    num_images_per_class=5, image_size=(256, 256),
    validation_split=0.2, subset='validation', seed=555,
    safe_triplet=True)

Generates a balanced per batch tf.data.Dataset from image files in a directory.

Your directory structure should be:

main_directory/
...class_a/
......a_image_1.jpg
......a_image_2.jpg
...class_b/
......b_image_1.jpg
......b_image_2.jpg

Behind the scenes, this module creates a different dataset for every class and by using weighted random sampling, some random classes (num_classes_per_batch) are drawn and a specific number of images is selected from every choosen class (num_images_per_class) as long as there are enough samples from this class.

If there is no enough samples remaining from the choosen class, it is skipped and another class is choosen (This behaviour can be disabled and we indefinitely repeat the classes datasets)

Setting safe_triplet to False (Default) makes sure that every image is seen exactly one time per epoch but it does not guarantee a fixed num_classes_per_batch or num_images_per_class in later batches.

Setting safe_triplet to True does not guarantee that every epoch will include all different samples from the dataset. But as sampling is weighted per class, every epoch will include a very high percentage of the dataset and should approach 100% as dataset size increases. This however guarantee that both num_classes_per_batch and num_images_per_class are fixed for all batches including later ones.

If you are to use this generator with TripletLoss, your should either:

  • Set safe_triplet to True
  • Keep safe_triplet default False value but be careful with choosing the batch_size so you do not end up with a last batch containing a single class (or a single sample)

Batch size is calculated by multiplying num_classes_per_batch and num_images_per_class.

Requirements

  • Tensorflow >= 2.9
  • Numpy >= 1.19

Compatible with

Versions

  • v0.1.3: TF>= 2.9
  • v0.1.2: TF>= 2.8
  • v0.1.1: TF>= 2.7

Licence

MIT

Authors

KerasGen was written by Mahmoud Bahaa

If you use this software, please cite it using the metadata from this CITATION.cff

kerasgen's People

Contributors

ma7555 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

minhhnh

kerasgen's Issues

kerasgen fails to import with latest version of keras and tensorflow

Describe the bug
Importing kerasgen as from kerasgen import balanced_image_dataset fails with ImportError on the latest version of tensorflow (2.9.1).

To Reproduce
Steps to reproduce the behavior:

  1. mkdir /tmp/repro && cd /tmp/repro
  2. python -m virtualenv .venv && source .venv/bin/activate
  3. python -m pip install tensorflow
  4. python -c 'from kerasgen import balanced_image_dataset'

Observed

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/repro/.venv/lib/python3.8/site-packages/kerasgen/balanced_image_dataset.py", line 8, in <module>
    from keras.preprocessing import dataset_utils
ImportError: cannot import name 'dataset_utils' from 'keras.preprocessing' (/home/azureuser/asru/tmp/.venv/lib/python3.8/site-packages/keras/preprocessing/__init__.py)

Expected behavior
kerasgen imports module successfully.

Preprocessing images

Is it possible to pass a preprocessing function to the batch generator? I want to normalize images during the training

No balanced split

Hi! After training with balanced_image_dataset_from_directory , I have tried to test the validation dataset that your app made.
What I get from it is the following:

Found 161866 files belonging to 9198 classes.
Using 129493 files for training.
Found 161866 files belonging to 9198 classes.
Using 32373 files for validation.

I thought that every class would be used in validation database, but what I get using:
x = np.concatenate([x for x, y in val_ds], axis=0)
y = np.concatenate([y for x, y in val_ds], axis=0)

The variable y has just 4500 unique classes. Where are the ones missing of those 9198 classes? It's not about not having enough photos because the class 3 (for example, one missing in validation dataset), it has more than 10 photos. The parameters of my balanced_image_dataset are these ones:

val_ds = balanced_image_dataset_from_directory(
    directory, num_classes_per_batch=64,
    num_images_per_class=4, image_size=(160, 160),
    seed=6, validation_split=0.2, subset='validation',
    safe_triplet=True)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.