2727data_queue = Queue (16 * 2000 )
2828error_queue = Queue (16 * 2000 )
2929
30-
3130def fetch_single_image (image_url , timeout = None , retries = 0 ):
3231 for _ in range (retries + 1 ):
3332 try :
@@ -46,11 +45,13 @@ def fetch_single_image(image_url, timeout=None, retries=0):
4645def map_sample (
4746 url , caption ,
4847 image_shape = (256 , 256 ),
48+ timeout = 15 ,
49+ retries = 3 ,
4950 upscale_interpolation = cv2 .INTER_LANCZOS4 ,
5051 downscale_interpolation = cv2 .INTER_AREA ,
5152):
5253 try :
53- image = fetch_single_image (url , timeout = 15 , retries = 3 ) # Assuming fetch_single_image is defined elsewhere
54+ image = fetch_single_image (url , timeout = timeout , retries = retries ) # Assuming fetch_single_image is defined elsewhere
5455 if image is None :
5556 return
5657
@@ -84,15 +85,24 @@ def map_sample(
8485 "original_width" : original_width ,
8586 })
8687 except Exception as e :
88+ print (f"Error in map_sample: { str (e )} " )
8789 error_queue .put ({
8890 "url" : url ,
8991 "caption" : caption ,
9092 "error" : str (e )
9193 })
92-
93- def map_batch (batch , num_threads = 256 , image_shape = (256 , 256 ), timeout = None , retries = 0 ):
94- with ThreadPoolExecutor (max_workers = num_threads ) as executor :
95- executor .map (map_sample , batch ["url" ], batch ['caption' ], image_shape = image_shape , timeout = timeout , retries = retries )
94+
95+ def map_batch (batch , num_threads = 256 , image_shape = (256 , 256 ), timeout = 15 , retries = 3 ):
96+ try :
97+ map_sample_fn = partial (map_sample , image_shape = image_shape , timeout = timeout , retries = retries )
98+ with ThreadPoolExecutor (max_workers = num_threads ) as executor :
99+ executor .map (map_sample_fn , batch ["url" ], batch ['caption' ])
100+ except Exception as e :
101+ print (f"Error in map_batch: { str (e )} " )
102+ error_queue .put ({
103+ "batch" : batch ,
104+ "error" : str (e )
105+ })
96106
97107def parallel_image_loader (dataset : Dataset , num_workers : int = 8 , image_shape = (256 , 256 ), num_threads = 256 ):
98108 map_batch_fn = partial (map_batch , num_threads = num_threads , image_shape = image_shape )
@@ -102,8 +112,10 @@ def parallel_image_loader(dataset: Dataset, num_workers: int = 8, image_shape=(2
102112 iteration = 0
103113 while True :
104114 # Repeat forever
105- dataset = dataset .shuffle (seed = iteration )
115+ print (f"Shuffling dataset with seed { iteration } " )
116+ # dataset = dataset.shuffle(seed=iteration)
106117 shards = [dataset [i * shard_len :(i + 1 )* shard_len ] for i in range (num_workers )]
118+ print (f"mapping { len (shards )} shards" )
107119 pool .map (map_batch_fn , shards )
108120 iteration += 1
109121
@@ -205,4 +217,3 @@ def __next__(self):
205217
206218 def __len__ (self ):
207219 return len (self .dataset )
208-
0 commit comments