thomasw21 commited on
Commit
3e03859
·
1 Parent(s): 09b5b06

Being able to seriablize

Browse files
Files changed (1) hide show
  1. pmd.py +72 -13
pmd.py CHANGED
@@ -20,6 +20,8 @@ from abc import abstractmethod
20
  from hashlib import md5
21
  from pathlib import Path
22
  from typing import Any, Dict, List, Optional
 
 
23
  from urllib.parse import unquote_plus
24
 
25
  # TODO: @thomasw21
@@ -53,6 +55,13 @@ _FEATURES = datasets.Features(
53
  )
54
 
55
 
 
 
 
 
 
 
 
56
  class BaseLoader:
57
  def __init__(
58
  self,
@@ -149,7 +158,8 @@ class COCOloader(BaseLoaderWithDLManager):
149
  def generate_gen_kwargs(self, dl_manager):
150
  annotation_file = (
151
  Path(dl_manager.download_and_extract(self._ANNOTATION_URL))
152
- / "annotations" / f"captions_{self._SPLIT_MAP[self.split]}.json"
 
153
  )
154
  image_folder = Path(
155
  dl_manager.download_and_extract(self._IMAGES_URLS[self.split])
@@ -184,7 +194,12 @@ class COCOloader(BaseLoaderWithDLManager):
184
  "texts": [annotation["caption"]],
185
  "source": self.source,
186
  "meta": json.dumps(
187
- {"image_metadata": image_metadata, "annotation": annotation}
 
 
 
 
 
188
  ),
189
  }
190
 
@@ -206,7 +221,11 @@ class SBULoader(DatasetsLoader):
206
  "image": None,
207
  "texts": [row["caption"]],
208
  "source": self.source,
209
- "meta": json.dumps(meta),
 
 
 
 
210
  }
211
  ]
212
 
@@ -239,7 +258,11 @@ class LocalizedNarrativesOpenImagesLoader(BaseLoaderWithDLManager):
239
  "image": None,
240
  "texts": [annotation["caption"]],
241
  "source": self.source,
242
- "meta": json.dumps(annotation),
 
 
 
 
243
  }
244
 
245
 
@@ -283,7 +306,11 @@ class LocalizedNarrativesCOCOLoader(BaseLoaderWithDLManager):
283
  "image": str(image_path.absolute()),
284
  "texts": [annotation["caption"]],
285
  "source": self.source,
286
- "meta": json.dumps(annotation),
 
 
 
 
287
  }
288
 
289
 
@@ -341,7 +368,11 @@ class LocalizedNarrativesFlickr30kLoader(BaseLoaderWithDLManager):
341
  "image": str(image_path.absolute()),
342
  "texts": [annotation["caption"]],
343
  "source": self.source,
344
- "meta": json.dumps(annotation),
 
 
 
 
345
  }
346
 
347
 
@@ -386,7 +417,11 @@ class LocalizedNarrativesADE20kLoader(BaseLoaderWithDLManager):
386
  "image": str(image_path.absolute()),
387
  "texts": [annotation["caption"]],
388
  "source": self.source,
389
- "meta": json.dumps(annotation),
 
 
 
 
390
  }
391
 
392
 
@@ -415,7 +450,11 @@ class VisualGenomeLoader(DatasetsLoader):
415
  ),
416
  "texts": [region["phrase"]],
417
  "source": self.source,
418
- "meta": json.dumps(meta),
 
 
 
 
419
  }
420
  for region in row["regions"]
421
  ]
@@ -447,7 +486,11 @@ class WITLoader(DatasetsLoader):
447
  ]
448
  ],
449
  "source": self.source,
450
- "meta": json.dumps(meta),
 
 
 
 
451
  }
452
  ]
453
 
@@ -469,7 +512,11 @@ class ConceptualCaptions(DatasetsLoader):
469
  "image": None,
470
  "texts": [row["caption"]],
471
  "source": self.source,
472
- "meta": json.dumps(meta),
 
 
 
 
473
  }
474
  ]
475
 
@@ -491,7 +538,11 @@ class Conceptual12MLoader(DatasetsLoader):
491
  "image": None,
492
  "texts": [row["caption"]],
493
  "source": self.source,
494
- "meta": json.dumps(meta),
 
 
 
 
495
  }
496
  ]
497
 
@@ -514,7 +565,11 @@ class RedCapsLoader(DatasetsLoader):
514
  # TODO @thomasw21
515
  "texts": [row["raw_caption"]],
516
  "source": self.source,
517
- "meta": json.dumps(meta),
 
 
 
 
518
  }
519
  ]
520
 
@@ -663,7 +718,11 @@ class YFCC100MLoader(BaseLoaderWithDLManager):
663
  "image": None,
664
  "texts": [text],
665
  "source": self.source,
666
- "meta": json.dumps(annotation),
 
 
 
 
667
  }
668
  cursor.close()
669
 
 
20
  from hashlib import md5
21
  from pathlib import Path
22
  from typing import Any, Dict, List, Optional
23
+
24
+ from datetime import datetime
25
  from urllib.parse import unquote_plus
26
 
27
  # TODO: @thomasw21
 
55
  )
56
 
57
 
58
+ def json_serializer(o):
59
+ if isinstance(o, datetime):
60
+ return str(o)
61
+
62
+ raise TypeError(f"Object of type {o.__class__.__name__} is not JSON serializable")
63
+
64
+
65
  class BaseLoader:
66
  def __init__(
67
  self,
 
158
  def generate_gen_kwargs(self, dl_manager):
159
  annotation_file = (
160
  Path(dl_manager.download_and_extract(self._ANNOTATION_URL))
161
+ / "annotations"
162
+ / f"captions_{self._SPLIT_MAP[self.split]}.json"
163
  )
164
  image_folder = Path(
165
  dl_manager.download_and_extract(self._IMAGES_URLS[self.split])
 
194
  "texts": [annotation["caption"]],
195
  "source": self.source,
196
  "meta": json.dumps(
197
+ {
198
+ "image_metadata": image_metadata,
199
+ "annotation": annotation,
200
+ },
201
+ default=json_serializer,
202
+ indent=2,
203
  ),
204
  }
205
 
 
221
  "image": None,
222
  "texts": [row["caption"]],
223
  "source": self.source,
224
+ "meta": json.dumps(
225
+ meta,
226
+ default=json_serializer,
227
+ indent=2,
228
+ ),
229
  }
230
  ]
231
 
 
258
  "image": None,
259
  "texts": [annotation["caption"]],
260
  "source": self.source,
261
+ "meta": json.dumps(
262
+ annotation,
263
+ default=json_serializer,
264
+ indent=2,
265
+ ),
266
  }
267
 
268
 
 
306
  "image": str(image_path.absolute()),
307
  "texts": [annotation["caption"]],
308
  "source": self.source,
309
+ "meta": json.dumps(
310
+ annotation,
311
+ default=json_serializer,
312
+ indent=2,
313
+ ),
314
  }
315
 
316
 
 
368
  "image": str(image_path.absolute()),
369
  "texts": [annotation["caption"]],
370
  "source": self.source,
371
+ "meta": json.dumps(
372
+ annotation,
373
+ default=json_serializer,
374
+ indent=2,
375
+ ),
376
  }
377
 
378
 
 
417
  "image": str(image_path.absolute()),
418
  "texts": [annotation["caption"]],
419
  "source": self.source,
420
+ "meta": json.dumps(
421
+ annotation,
422
+ default=json_serializer,
423
+ indent=2,
424
+ ),
425
  }
426
 
427
 
 
450
  ),
451
  "texts": [region["phrase"]],
452
  "source": self.source,
453
+ "meta": json.dumps(
454
+ meta,
455
+ default=json_serializer,
456
+ indent=2,
457
+ ),
458
  }
459
  for region in row["regions"]
460
  ]
 
486
  ]
487
  ],
488
  "source": self.source,
489
+ "meta": json.dumps(
490
+ meta,
491
+ default=json_serializer,
492
+ indent=2,
493
+ ),
494
  }
495
  ]
496
 
 
512
  "image": None,
513
  "texts": [row["caption"]],
514
  "source": self.source,
515
+ "meta": json.dumps(
516
+ meta,
517
+ default=json_serializer,
518
+ indent=2,
519
+ ),
520
  }
521
  ]
522
 
 
538
  "image": None,
539
  "texts": [row["caption"]],
540
  "source": self.source,
541
+ "meta": json.dumps(
542
+ meta,
543
+ default=json_serializer,
544
+ indent=2,
545
+ ),
546
  }
547
  ]
548
 
 
565
  # TODO @thomasw21
566
  "texts": [row["raw_caption"]],
567
  "source": self.source,
568
+ "meta": json.dumps(
569
+ meta,
570
+ default=json_serializer,
571
+ indent=2,
572
+ ),
573
  }
574
  ]
575
 
 
718
  "image": None,
719
  "texts": [text],
720
  "source": self.source,
721
+ "meta": json.dumps(
722
+ annotation,
723
+ default=json_serializer,
724
+ indent=2,
725
+ ),
726
  }
727
  cursor.close()
728