Skip to content

Instantly share code, notes, and snippets.

@pbamotra
Created July 3, 2019 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbamotra/cc01e1deff1382bebd89d641b01ea8ab to your computer and use it in GitHub Desktop.
Save pbamotra/cc01e1deff1382bebd89d641b01ea8ab to your computer and use it in GitHub Desktop.
DALI Post-1.5
import types
import numpy as np
import collections
import pandas as pd
from random import shuffle
import nvidia.dali.ops as ops
import nvidia.dali.types as types
from nvidia.dali.pipeline import Pipeline
class ExternalInputIterator(object):
def __init__(self, batch_size, data_file, image_dir):
self.images_dir = image_dir
self.batch_size = batch_size
self.data_file = data_file
with open(self.data_file, 'r') as f:
self.files = [line.rstrip() for line in f if line is not '']
shuffle(self.files)
def __iter__(self):
self.i = 0
self.n = len(self.files)
return self
def __next__(self):
batch = []
labels = []
for _ in range(self.batch_size):
# *label reads multiple labels
jpeg_filename, *label = self.files[self.i].split(' ')
f = open(image_dir + jpeg_filename, 'rb')
batch.append(np.frombuffer(f.read(), dtype = np.uint8))
labels.append(np.array(label, dtype = np.uint8))
self.i = (self.i + 1) % self.n
return (batch, labels)
next = __next__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment