This file contains 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 | |
(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]. |
This file contains 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 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]) |
This file contains 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
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'] |
This file contains 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
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(), |
This file contains 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
model.fit(train_dataset, epochs=5, validation_data=val_dataset) | |
model.evaluate(test_dataset, verbose=2) | |
# Output | |
# loss: 1.0985 - accuracy: 0.6060 |
This file contains 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
# 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) |
This file contains 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 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. |
This file contains 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
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' | |
) |
This file contains 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
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 |
This file contains 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
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 |
OlderNewer