davanstrien HF Staff commited on
Commit
cc71572
Β·
1 Parent(s): 189463a

Refactor output schema creation to define features before processing dataset

Browse files
Files changed (1) hide show
  1. detect-objects.py +13 -15
detect-objects.py CHANGED
@@ -328,7 +328,18 @@ def main():
328
  logger.error("Ensure the model exists and you have access permissions")
329
  sys.exit(1)
330
 
331
- # Process dataset
 
 
 
 
 
 
 
 
 
 
 
332
  logger.info("πŸ” Processing images...")
333
  processed_dataset = dataset.map(
334
  lambda batch: process_batch(
@@ -342,23 +353,10 @@ def main():
342
  ),
343
  batched=True,
344
  batch_size=args.batch_size,
 
345
  desc="Detecting objects",
346
  )
347
 
348
- # Create dynamic features with ClassLabel
349
- logger.info("πŸ“Š Creating output schema...")
350
- new_features = processed_dataset.features.copy()
351
- new_features["objects"] = Sequence(
352
- {
353
- "bbox": Sequence(Value("float32"), length=4),
354
- "category": ClassLabel(names=[class_name]),
355
- "score": Value("float32"),
356
- }
357
- )
358
-
359
- # Cast to proper types
360
- processed_dataset = processed_dataset.cast(new_features)
361
-
362
  # Calculate statistics
363
  total_detections = sum(len(objs) for objs in processed_dataset["objects"])
364
  images_with_detections = sum(len(objs) > 0 for objs in processed_dataset["objects"])
 
328
  logger.error("Ensure the model exists and you have access permissions")
329
  sys.exit(1)
330
 
331
+ # Define output schema before processing
332
+ logger.info("πŸ“Š Creating output schema...")
333
+ new_features = dataset.features.copy()
334
+ new_features["objects"] = Sequence(
335
+ {
336
+ "bbox": Sequence(Value("float32"), length=4),
337
+ "category": ClassLabel(names=[class_name]),
338
+ "score": Value("float32"),
339
+ }
340
+ )
341
+
342
+ # Process dataset with explicit output features
343
  logger.info("πŸ” Processing images...")
344
  processed_dataset = dataset.map(
345
  lambda batch: process_batch(
 
353
  ),
354
  batched=True,
355
  batch_size=args.batch_size,
356
+ features=new_features,
357
  desc="Detecting objects",
358
  )
359
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
360
  # Calculate statistics
361
  total_detections = sum(len(objs) for objs in processed_dataset["objects"])
362
  images_with_detections = sum(len(objs) > 0 for objs in processed_dataset["objects"])