Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement