Skip to content

Instantly share code, notes, and snippets.

@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].
@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_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']
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(),
model.fit(train_dataset, epochs=5, validation_data=val_dataset)
model.evaluate(test_dataset, verbose=2)
# Output
# loss: 1.0985 - accuracy: 0.6060
@eileen-code4fun
eileen-code4fun / cifar10_model_cloud.py
Created May 26, 2021 03:20
CIFAR10 Model on Cloud
# A distributed strategy to take advantage of available hardward.
# No-op otherwise.
mirrored_strategy = tf.distribute.MirroredStrategy()
with mirrored_strategy.scope():
model = create_model()
# Restore from the latest checkpoint if available.
latest_ckpt = tf.train.latest_checkpoint(GCS_PATH_FOR_CHECKPOINTS)
if latest_ckpt:
model.load_weights(latest_ckpt)
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--epochs', dest='epochs', type=int, default=5)
args = parser.parse_args()
# Refer to the argument as args.epochs.
@eileen-code4fun
eileen-code4fun / cifar10_setup.py
Last active May 28, 2021 00:49
CIFAR10 Setup
from setuptools import find_packages
from setuptools import setup
setup(
name='trainer',
version='0.1',
packages=find_packages(),
include_package_data=True,
description='An E2E Tutorial of Vertex AI'
)
cloud_model = tf.keras.models.load_model(GCS_PATH_FOR_SAVED_MODEL)
# Compile the model to make sure it uses the correct accuracy metric.
cloud_model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])
cloud_model.evaluate(test_dataset, verbose=2)
# Output loss: 0.917 - accuracy: 0.6620
workerPoolSpecs:
machineSpec:
# Machines and GPUs: https://cloud.google.com/vertex-ai/docs/training/configure-compute#specifying_gpus
machineType: n1-standard-4
acceleratorType: NVIDIA_TESLA_V100
acceleratorCount: 2
replicaCount: 1
pythonPackageSpec:
# Executors: https://cloud.google.com/vertex-ai/docs/training/pre-built-containers
executorImageUri: us-docker.pkg.dev/vertex-ai/training/tf-gpu.2-3:latest