Skip to content

Instantly share code, notes, and snippets.

@nateraw
Created June 2, 2021 19:15
Show Gist options
  • Save nateraw/e494b96ca754e2ae71473b1aa9d30c69 to your computer and use it in GitHub Desktop.
Save nateraw/e494b96ca754e2ae71473b1aa9d30c69 to your computer and use it in GitHub Desktop.
Boilerplate for preparing cats and dogs dataset. Removes corrupted images.
import pickle
from pathlib import Path
import tensorflow as tf
root = Path('./PetImages')
num_skipped = 0
examples = []
for folder_name in ("Cat", "Dog"):
for fpath in (root / folder_name).glob('*'):
with fpath.open('rb') as f:
is_jfif = tf.compat.as_bytes('JFIF') in f.peek(10)
if is_jfif:
examples.append({'img_bytes': f.read(), 'labels': folder_name.lower()})
continue
num_skipped+=1
fpath.unlink()
print("Deleted %d images" % num_skipped)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment