• Stars
    star
    102
  • Rank 333,585 (Top 7 %)
  • Language
    Jupyter Notebook
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Data Reading Blocks for Python



Framework agnostic, faster data reading for DeepLearning.

A native extension for Python built with C++ and pybind11.

Installation • Why? • What is the performance gain? • Tutorial • License

PyPI version

DataReadingBlocks for Python is a python module that provides collection of C++ backed data reading primitives. It targets deep-learning needs, but it is framework agnostic.

Installation

Available as pypi package:

$ pip install dareblopy

To build from sources refer to wiki page.

Why?

Development initially started to speedup reading from ZIP archives, reduce copying data, increase time of GIL being released to improve concurrency.

But why reading from ZIP archive? Reading a ton of small files (which is often the case) can be slow, specially if the drive is network attached, e.g. with NFS. However, the bottle neck here is hardly the disk speed, but the overhead of filesystem, name-lookup, creating file descriptors, and additional network usage if NFS is used.

If, all the small files are agglomerated into larger file (or several large files), that improves performance substantially. This is exactly the reason behind TFRecords in TensorFlow:

To read data efficiently it can be helpful to serialize your data and store it in a set of files (100-200MB each) that can each be read linearly. This is especially true if the data is being streamed over a network. This can also be useful for caching any data-preprocessing.

The downside of TFRecords is that it's TensorFlow only.

A much simpler, yet still effective solution is to store data in ZIP archive with zero compression. However, using zipfile package from standard library can be slow, since it is implemented purely in Python and in certain cases can cause unnecessary data copying.

That's precisely the reason behind development of DareBlopy. In addition to that it also has such features as:

  • Readying JPEG images directly to numpy arrays (from ZIP and from filesystem), to reduce memory usage and unnecessary data copies.
  • Two JPEG backends selectable at run-time: libjpeg and libjpeg-turbo. Both backends are embedded into DareBlopy and do not depend on any system package.
  • Reading of TFRecords (not all features are support though) without dependency on TensorFlow that enables usage of datasets stored as TFRecords with ML frameworks other than TensorFlow, e.g. Pytorch.
  • Random yielders, iterators and, dataloaders to simplify doing DataLearning with TFRecords with other ML frameworks.
  • No dependency on system packages. You install it from pip - it works.
  • Support for compressed ZIP archives, including LZ4 compression.
  • Virtual filesystem. Allows mounting of zip archives.

What is the performance gain?

Well, it depends a lot on a particular use-case. Let's consider several. All details of the benchmarks you can find in run_benchmark.py. You can also run it on your machine and compare results to the ones reported here.

Reading files to bytes

Python's bytes object can be a bit nasty. Generally speaking, you can not return from C/C++ land data as a bytes object without making a data copy. That's because memory for bytes object must be allocated as one chunk for both, the header and data itself. In DareBlopy this extra copy is eliminated, you can find details here.

In this test scenario, we read 200 files, each of which ~30kb. Reading is done from local filesystem and from a ZIP archive.

Reading files using DareBlopy is faster even when read from filesystem, but when read from ZIP it provides substantial improvement.

Reading JPEGs to numpy's ndarray

This is where DareBlopy's feature of direct readying to numpy array is demonstrated. When the file is read, it is decompressed directly to a preallocated numpy array, and all of that happens on C++ land while GIL is released.

Note: here PIL v.7.0.0 is used, on Ubuntu 18. In my installation, it does not use libjpeg-turbo.

It this case, difference between ZIP/filesystem is quite insignificant, but things change dramatically if filesystem is streamed over a network:

Reading TFRecords

DareBlopy can read TensorFlow records. This functionality was developed in the first place for reading FFHQ dataset from TFRecords.

It introduces alias to string type: uint8, which allows to return directly numpy array if the shape is known beforehand.

For example, code like:

        features = {
            'data': db.FixedLenFeature([], db.string)
        }

Can be replaced with:

        features = {
            'data': db.FixedLenFeature([3, 32, 32], db.uint8)
        }

This decoding to numpy array comes at zero cost, which is demonstrated below:

Tutorial

Import DareBlopy

import dareblopy as db
from IPython.display import Image, display
import PIL.Image

Open zip archive:

archive = db.open_zip_archive("test_utils/test_image_archive.zip")

Read image to bytes and display:

b = archive.open_as_bytes('0.jpg')
Image(b)

jpeg

Alternatively, read image to numpy:

img = archive.read_jpg_as_numpy('0.jpg')
img.shape
(256, 256, 3)
display(PIL.Image.fromarray(img))

png

For more advanced usage please refer to:

License

Apache License 2.0

More Repositories

1

ALAE

[CVPR2020] Adversarial Latent Autoencoders
Python
3,511
star
2

bimpy

imgui for python
C++
202
star
3

GPND

Generative Probabilistic Novelty Detection with Adversarial Autoencoders
Python
130
star
4

VAE

Example of vanilla VAE for face image generation at resolution 128x128 using pytorch.
Python
67
star
5

KeplerOrbits

C++
57
star
6

ShaderBoiler

Aimed to eliminate preprocessor hell in shaders and kernels.
C++
40
star
7

StyleGAN_Blobless

Original stylegan without blobs (without retraining).
Python
38
star
8

StyleGANCpp

Unofficial implementation of StyleGAN's generator
C++
33
star
9

MinimalFem

C++
30
star
10

EnvMapTooL

C
30
star
11

tensor4

tensor4 - pytorch to C++ convertor using lightweight templated tensor library
C++
28
star
12

SimpleText

One header library for rendering text via OpenGL API
C++
20
star
13

StyleGan

Unofficial Pytorch implementation of Style GAN paper
Python
17
star
14

dlutils

Python
6
star
15

ProceduralSky_bgfx

C++
5
star
16

ProceduralSky

C++
5
star
17

fsal

File System Abstraction Layer
C++
5
star
18

TinyFEM

C++
4
star
19

hashranking

Fast procedures for hamming distance computation, ranking, mAP computation. Fore deep-learning research in hashing and retrieval.
Python
4
star
20

GetUUID

The GetUUID is a simple tool written in python that prints UUID and platform information for given iOS bundle or dSYM.
Python
4
star
21

DeepLearningServerSetup

My notes on setting up a server for Deep-Learning
2
star
22

Emul86

C++
2
star
23

CPP-prototyping-template

C++ prototyping template with imgui and other libs
C++
2
star
24

kmeans

Simple, one header drop in templated library from kmeans
C++
2
star
25

BroadPhase

The BroadPhase is a simple example of 2d physics engine.
C++
2
star
26

bfio

One-header library for defining writing and reading procedures of C/C++ objects to/from specific binary formats.
C++
1
star
27

PicoRender

C++
1
star
28

CUDA_tutorial1

Simple example of usage CUDA with CMake
Cuda
1
star
29

SWCParser

Reader/Writer for SWC files
C++
1
star
30

nonius

Nonius benchmark-framework fork
C++
1
star