Skip to content

Instantly share code, notes, and snippets.

@eileen-code4fun
Last active June 14, 2021 14:38
Show Gist options
  • Save eileen-code4fun/8334a21f5fdd68d2ecb78c1a843a2dcd to your computer and use it in GitHub Desktop.
Save eileen-code4fun/8334a21f5fdd68d2ecb78c1a843a2dcd to your computer and use it in GitHub Desktop.
CIFAR10 Encode
import tensorflow as tf
def normalize(image, label):
return tf.cast(image, dtype=tf.float32) / 255.0, label
train = ds['train'].map(normalize).batch(32)
test = ds['test'].map(normalize).batch(32)
encoding_model = tf.keras.Model(inputs=model.input, outputs=model.get_layer('flatten').output)
def csv_dump2(dataset, filename):
with open(filename, 'w') as f:
for images, labels in dataset:
encodings = encoding_model.predict(images)
labels = labels.numpy()
for i in range(len(labels)):
f.write('{},'.format(labels[i]))
f.write(','.join(str(x) for x in list(encodings[i])))
f.write('\n')
csv_dump2(train, 'train_encoded.csv')
csv_dump2(test, 'test_encoded.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment