Last active
June 14, 2021 14:38
-
-
Save eileen-code4fun/8334a21f5fdd68d2ecb78c1a843a2dcd to your computer and use it in GitHub Desktop.
CIFAR10 Encode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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