Skip to content

Instantly share code, notes, and snippets.

model.fit(train_dataset, epochs=5, validation_data=val_dataset)
model.evaluate(test_dataset, verbose=2)
# Output
# loss: 1.0985 - accuracy: 0.6060
from tensorflow.keras import layers, models, losses
def create_model():
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D(2, 2),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D(2, 2),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.Flatten(),
@eileen-code4fun
eileen-code4fun / cifar10_dataset.py
Created May 26, 2021 02:52
Input Datasets for CIFAR10
def extract(example):
data = tf.io.parse_example(
example,
# Schema of the example.
{
'image': tf.io.FixedLenFeature(shape=(32, 32, 3), dtype=tf.float32),
'label': tf.io.FixedLenFeature(shape=(), dtype=tf.int64)
}
)
return data['image'], data['label']
@eileen-code4fun
eileen-code4fun / cifar10_data_display.py
Created May 26, 2021 02:39
CIFAR10 Data Verification
import matplotlib.pyplot as plt
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
dataset = tf.data.TFRecordDataset([GCS_PATH_FOR_DATA + 'train.tfrecord'])
plt.figure(figsize=(10, 10))
for i, example in enumerate(dataset.take(16)):
data = tf.train.Example()
data.ParseFromString(example.numpy())
image = tf.constant(data.features.feature['image'].float_list.value, shape=[32, 32, 3])
@eileen-code4fun
eileen-code4fun / cifar10_data.py
Last active November 23, 2021 14:00
CIFAR10 Data Preparation
import tensorflow as tf
(train_images, train_labels), (test_images, test_labels) = tf.keras.dataset.cifar10.load_data()
def preprocess(filename, images, labels):
with tf.io.TFRecordWriter(filename) as writer:
for image, label in zip(images, labels):
# Encode the image and label in tf.train.Example.
feature = {
# Normalize the image to range [0, 1].