Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. import types
  2. import numpy as np
  3. import collections
  4. import pandas as pd
  5.  
  6. from random import shuffle
  7.  
  8. import nvidia.dali.ops as ops
  9. import nvidia.dali.types as types
  10. from nvidia.dali.pipeline import Pipeline
  11.  
  12. class ExternalInputIterator(object):
  13. def __init__(self, batch_size, data_file, image_dir):
  14. self.images_dir = image_dir
  15. self.batch_size = batch_size
  16. self.data_file = data_file
  17. with open(self.data_file, 'r') as f:
  18. self.files = [line.rstrip() for line in f if line is not '']
  19. shuffle(self.files)
  20.  
  21. def __iter__(self):
  22. self.i = 0
  23. self.n = len(self.files)
  24. return self
  25.  
  26. def __next__(self):
  27. batch = []
  28. labels = []
  29. for _ in range(self.batch_size):
  30. # *label reads multiple labels
  31. jpeg_filename, *label = self.files[self.i].split(' ')
  32. f = open(image_dir + jpeg_filename, 'rb')
  33. batch.append(np.frombuffer(f.read(), dtype = np.uint8))
  34. labels.append(np.array(label, dtype = np.uint8))
  35. self.i = (self.i + 1) % self.n
  36. return (batch, labels)
  37.  
  38. next = __next__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement