Skip to content

Instantly share code, notes, and snippets.

import tensorflow as tf
import tensorflow_datasets as tfds
ds = tfds.load('cifar10', as_supervised=True)
def csvfy(dataset, filename):
with open(filename, 'w') as f:
for image, label in dataset:
f.write('{},'.format(label.numpy()))
f.write(','.join(str(x) for x in list(image.numpy().reshape(-1))))
api_client.create_schedule_from_job_spec(
job_spec_path='e2e-tutorial-automl-pipeline-schedule.json',
# Start at the first minute of every hour.
schedule='1 * * * *'
)
@eileen-code4fun
eileen-code4fun / cifar10_pl_launch.py
Last active June 2, 2021 22:41
CIFAR10 PL Launch
from kfp.v2 import compiler
from google.cloud.aiplatform import AIPlatformClient
compiler.Compiler().compile(pipeline_func=e2e_tutorial_pipeline, package_path='e2e-tutorial-automl-pipeline.json')
api_client = AIPlatformClient(project_id='PROJECT_ID', region='us-central1')
api_client.create_run_from_job_spec(
job_spec_path='e2e-tutorial-automl-pipeline.json'
)
from kfp import dsl
from google_cloud_pipeline_components import aiplatform
@dsl.pipeline(
name='e2e-tutorial-automl-pipeline',
description='A simple automl training',
pipeline_root='GCS_PATH_FOR_PIPELINE'
)
def e2e_tutorial_pipeline():
job = aiplatform.AutoMLImageTrainingJobRunOp(
{
"displayName": "e2e-tutorial-automl",
"inputDataConfig": {
"datasetId": "DATASET_ID",
"filterSplit": {
"trainingFilter": "labels.aiplatform.googleapis.com/ml_use=training",
"validationFilter": "labels.aiplatform.googleapis.com/ml_use=validation",
"testFilter": "labels.aiplatform.googleapis.com/ml_use=test"
}
},
@eileen-code4fun
eileen-code4fun / cifar10_automl.py
Last active May 31, 2021 14:13
CIFAR10 AutoML
import tensorflow as tf
import tensorflow_datasets as tfds
cifar10 = tfds.load('cifar10', as_supervised=True)
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
def save_images_write_metadata(dataset, purpose, mf):
i = 1
for image, label in dataset:
fn = '{}_{}.jpeg'.format(purpose, i)
import base64
import io
attr_map = tf.io.decode_jpeg(base64.b64decode(BASE64_JPEG_DATA))
plt.subplot(1, 2, 1)
plt.xticks([])
plt.yticks([])
plt.title(class_names[test_labels[0][0]])
plt.imshow(test_images[0])
plt.xticks([])
{
"inputs": {
"image": {
"inputTensorName": "conv2d_input",
"modality": "image"
}
},
"outputs": {
"explanation": {
"outputTensorName": "dense_1"
import json
data = {
"instances": [
(test_images[0]/255.0).tolist()
]
}
print(json.dumps(data), file=open('prediction_request.json', 'w'))
@eileen-code4fun
eileen-code4fun / cifar10_ol_pred.py
Last active May 28, 2021 17:54
CIFAR10 OL Pred
import tensorflow as tf
from tensorflow.keras import datasets
import matplotlib.pyplot as plt
_, (test_images, test_labels) = datasets.cifar10.load_data()
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
plt.xticks([])
plt.yticks([])
plt.title(class_names[test_labels[0][0]])