<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Avoid training-serving skew using TensorFlow Transform
In this notebook, we show how to use tf.transform to carry out preprocessing efficiently,
but save the preprocessing operations so that they are automatically applied during inference.
Enable GPU and set up helper functions
This notebook and pretty much every other notebook in this repository
will run faster if you are using a GPU.
On Colab
Step1:  Run Beam pipeline locally
Step2:  Display preprocessing data
Note that the files contain already preprocessed (scaled, resized images),
so we can simply read the data and display it.
Step3:  Train the model
Step4:  Predictions
For serving, we will write a serving function that calls the transform model
followed by our actual model.
We will look at serving functions in Chapter 7. For now, we show how to explicitly
call the transform function followed by model.predict()
<ASSISTANT_TASK:>
Python Code:
import tensorflow as tf
print(tf.version.VERSION)
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
  raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_name))
Explanation: Avoid training-serving skew using TensorFlow Transform
In this notebook, we show how to use tf.transform to carry out preprocessing efficiently,
but save the preprocessing operations so that they are automatically applied during inference.
Enable GPU and set up helper functions
This notebook and pretty much every other notebook in this repository
will run faster if you are using a GPU.
On Colab:
- Navigate to Edit→Notebook Settings
- Select GPU from the Hardware Accelerator drop-down
On Cloud AI Platform Notebooks:
- Navigate to https://console.cloud.google.com/ai-platform/notebooks
- Create an instance with a GPU or select your instance and add a GPU
Next, we'll confirm that we can connect to the GPU with tensorflow:
End of explanation
!cat run_dataflow.sh
!./run_dataflow.sh > /dev/null 2>&1
!ls -l flower_tftransform/
Explanation: Run Beam pipeline locally
End of explanation
import matplotlib.pylab as plt
import numpy as np
import tensorflow as tf
IMG_HEIGHT = 448
IMG_WIDTH = 448
IMG_CHANNELS = 3
CLASS_NAMES = 'daisy dandelion roses sunflowers tulips'.split()
ds = tf.data.experimental.make_batched_features_dataset(
    './flower_tftransform/train-00000-of-00016.gz',
    batch_size=5,
    features = {
        'image': tf.io.FixedLenFeature([IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS], tf.float32),
        'label': tf.io.FixedLenFeature([], tf.string),
        'label_int': tf.io.FixedLenFeature([], tf.int64)
    },
    reader=lambda filenames: tf.data.TFRecordDataset(filenames, compression_type='GZIP')
)
for feats in ds.take(1):
    print(feats['image'].shape)
f, ax = plt.subplots(1, 5, figsize=(15,15))
for feats in ds.take(1):
    for idx in range(5): # batchsize
        ax[idx].imshow((feats['image'][idx].numpy()));
        ax[idx].set_title(feats['label'][idx].numpy())
        ax[idx].axis('off')
Explanation: Display preprocessing data
Note that the files contain already preprocessed (scaled, resized images),
so we can simply read the data and display it.
End of explanation
# Helper functions
def training_plot(metrics, history):
  f, ax = plt.subplots(1, len(metrics), figsize=(5*len(metrics), 5))
  for idx, metric in enumerate(metrics):
    ax[idx].plot(history.history[metric], ls='dashed')
    ax[idx].set_xlabel("Epochs")
    ax[idx].set_ylabel(metric)
    ax[idx].plot(history.history['val_' + metric]);
    ax[idx].legend([metric, 'val_' + metric])
import tensorflow_hub as hub
import os
# Load compressed models from tensorflow_hub
os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'    
def create_preproc_dataset(pattern, batch_size):
    return tf.data.experimental.make_batched_features_dataset(
        pattern,
        batch_size=batch_size,
        features = {
            'image': tf.io.FixedLenFeature([IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS], tf.float32),
            'label': tf.io.FixedLenFeature([], tf.string),
            'label_int': tf.io.FixedLenFeature([], tf.int64)
        },
        reader=lambda filenames: tf.data.TFRecordDataset(filenames, compression_type='GZIP'),
        num_epochs=1
    ).map(
        lambda x: (x['image'], x['label_int'])
    )
# parameterize to the values in the previous cell
# WARNING! training on a small subset dataset (note top_dir)
def train_and_evaluate(top_dir='./flower_tftransform',
                       batch_size = 32,
                       lrate = 0.001,
                       l1 = 0.,
                       l2 = 0.,
                       num_hidden = 16):
    regularizer = tf.keras.regularizers.l1_l2(l1, l2)
    
    train_dataset = create_preproc_dataset(os.path.join(top_dir, 'train-*'), batch_size)
    eval_dataset = create_preproc_dataset(os.path.join(top_dir, 'valid-*'), batch_size)
    layers = [
      tf.keras.layers.experimental.preprocessing.CenterCrop(
          height=IMG_HEIGHT//2, width=IMG_WIDTH//2,
          input_shape=(IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS),
      ),
      hub.KerasLayer(
          "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4", 
          trainable=False,
          name='mobilenet_embedding'),
      tf.keras.layers.Dense(num_hidden,
                            kernel_regularizer=regularizer, 
                            activation=tf.keras.activations.relu,
                            name='dense_hidden'),
      tf.keras.layers.Dense(len(CLASS_NAMES), 
                            kernel_regularizer=regularizer,
                            activation='softmax',
                            name='flower_prob')
    ]
    model = tf.keras.Sequential(layers, name='flower_classification')
    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=lrate),
                loss=tf.keras.losses.SparseCategoricalCrossentropy(
                    from_logits=False),
                metrics=['accuracy'])
    print(model.summary())
    history = model.fit(train_dataset, validation_data=eval_dataset, epochs=3)
    training_plot(['loss', 'accuracy'], history)
    return model
model = train_and_evaluate()
Explanation: Train the model
End of explanation
!saved_model_cli show --all --dir ./flower_tftransform/tft/transform_fn
# get some files to do inference on.
filenames = [
    'gs://practical-ml-vision-book/flowers_5_jpeg/flower_photos/dandelion/9818247_e2eac18894.jpg',
    'gs://practical-ml-vision-book/flowers_5_jpeg/flower_photos/dandelion/9853885425_4a82356f1d_m.jpg',
    'gs://practical-ml-vision-book/flowers_5_jpeg/flower_photos/dandelion/98992760_53ed1d26a9.jpg',
    'gs://practical-ml-vision-book/flowers_5_jpeg/flower_photos/dandelion/9939430464_5f5861ebab.jpg',
    'gs://practical-ml-vision-book/flowers_5_jpeg/flower_photos/dandelion/9965757055_ff01b5ee6f_n.jpg'
]
img_bytes = [
    tf.io.read_file(filename) for filename in filenames
]
label = [
    'n/a' for filename in filenames
] # not used in inference
label_int = [
    -1 for filename in filenames
] # not used in inference
# by calling the preproc function, we get images of the right size & crop
preproc = tf.keras.models.load_model('./flower_tftransform/tft/transform_fn').signatures['transform_signature']
preprocessed = preproc(img_bytes=tf.convert_to_tensor(img_bytes), 
                       label=tf.convert_to_tensor(label, dtype=tf.string), 
                       label_int=tf.convert_to_tensor(label_int, dtype=tf.int64))
# then we call model.predict() and take the argmx of the result
pred_label_index = tf.math.argmax(model.predict(preprocessed)).numpy()
print(pred_label_index)
Explanation: Predictions
For serving, we will write a serving function that calls the transform model
followed by our actual model.
We will look at serving functions in Chapter 7. For now, we show how to explicitly
call the transform function followed by model.predict()
End of explanation
<END_TASK>