<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Introduction
Welcome to the Petals to the Metal competition! In this competition, you’re challenged to build a machine learning model that identifies the type of flowers in a dataset of images.
In this tutorial notebook, you'll learn how to build an image classifier in Keras and train it on a Tensor Processing Unit (TPU). Then, in the following exercise, you'll create your own notebook and make a submission to the competition. At the end, you'll have a complete project you can build off of with ideas of your own.
<blockquote style="margin-right
Step1:  Distribution Strategy
A TPU has eight different cores and each of these cores acts as its own accelerator. (A TPU is sort of like having eight GPUs in one machine.) We tell TensorFlow how to make use of all these cores at once through a distribution strategy. Run the following cell to create the distribution strategy that we'll later apply to our model.
Step2:  TensorFlow will distribute training among the eight TPU cores by creating eight different replicas of your model. 
Loading the Competition Data
When used with TPUs, datasets are often serialized into TFRecords. This is a format convenient for distributing data to each of the TPUs cores.
We've included functions in the petal_helper script that will load the TFRecords for you and create a data pipeline you can use with your model. There is one function for each split.
Step3:  These are tf.data.Dataset objects. You can think about a dataset in TensorFlow as a stream of data records. Check out this guide for more on working with the tf.data API.
Define Model
Now we're ready to create a neural network for classifying images! We'll use what's known as transfer learning. With transfer learning, you reuse part of a pretrained model to get a head-start on a new dataset.
For this tutorial, we'll to use a model called VGG16 pretrained on ImageNet). Later, you might want to experiment with other models included with Keras. (Xception wouldn't be a bad choice.)
The distribution strategy we created earlier contains a context manager, strategy.scope. This context manager tells TensorFlow how to divide the work of training among the eight TPU cores. When using TensorFlow with a TPU, it's important to define your model in a strategy.scope() context.
Step4:  Training
And now we're ready to train the model! After defining a few parameters, we're good to go.
Step5:  This next cell shows how the loss and metrics progressed during training. Thankfully, it converges!
Step6:  Predictions
Once you're satisfied with everything, you're ready to make predictions on the test set.
Step7:  We'll generate a file submission.csv. This file is what you'll submit to get your score on the leaderboard.
<ASSISTANT_TASK:>
Python Code:
from petal_helper import *
import tensorflow as tf
Explanation: Introduction
Welcome to the Petals to the Metal competition! In this competition, you’re challenged to build a machine learning model that identifies the type of flowers in a dataset of images.
In this tutorial notebook, you'll learn how to build an image classifier in Keras and train it on a Tensor Processing Unit (TPU). Then, in the following exercise, you'll create your own notebook and make a submission to the competition. At the end, you'll have a complete project you can build off of with ideas of your own.
<blockquote style="margin-right:auto; margin-left:auto; background-color: #ebf9ff; padding: 1em; margin:24px;">
    <strong>Get Started Now!</strong><br>
You don't need to understand everything in this notebook before starting the exercise. If you like, go ahead and <b><a href="#$NEXT_NOTEBOOK_URL$">open the exercise</a></b> and follow the instructions at the beginning to enter the competition. Then, return to this tutorial for a walkthrough of your starter project.
</blockquote>
Load the Helper Functions
Attached to the notebook is a utility script called petal_helper. It contains a number of helper functions related to data loading and visualization. The following cell will import them into your notebook session. We'll also import TensorFlow, which we'll use to create our model.
End of explanation
# Detect TPU, return appropriate distribution strategy
try:
    tpu = tf.distribute.cluster_resolver.TPUClusterResolver() 
    print('Running on TPU ', tpu.master())
except ValueError:
    tpu = None
if tpu:
    tf.config.experimental_connect_to_cluster(tpu)
    tf.tpu.experimental.initialize_tpu_system(tpu)
    strategy = tf.distribute.experimental.TPUStrategy(tpu)
else:
    strategy = tf.distribute.get_strategy() 
print("REPLICAS: ", strategy.num_replicas_in_sync)
Explanation: Distribution Strategy
A TPU has eight different cores and each of these cores acts as its own accelerator. (A TPU is sort of like having eight GPUs in one machine.) We tell TensorFlow how to make use of all these cores at once through a distribution strategy. Run the following cell to create the distribution strategy that we'll later apply to our model.
End of explanation
ds_train = get_training_dataset()
ds_valid = get_validation_dataset()
ds_test = get_test_dataset()
print("Training:", ds_train)
print ("Validation:", ds_valid)
print("Test:", ds_test)
Explanation: TensorFlow will distribute training among the eight TPU cores by creating eight different replicas of your model. 
Loading the Competition Data
When used with TPUs, datasets are often serialized into TFRecords. This is a format convenient for distributing data to each of the TPUs cores.
We've included functions in the petal_helper script that will load the TFRecords for you and create a data pipeline you can use with your model. There is one function for each split.
End of explanation
with strategy.scope():
    pretrained_model = tf.keras.applications.VGG16(
        weights='imagenet',
        include_top=False ,
        input_shape=[*IMAGE_SIZE, 3]
    )
    pretrained_model.trainable = False
    
    model = tf.keras.Sequential([
        # To a base pretrained on ImageNet to extract features from images...
        pretrained_model,
        # ... attach a new head to act as a classifier.
        tf.keras.layers.GlobalAveragePooling2D(),
        tf.keras.layers.Dense(len(CLASSES), activation='softmax')
    ])
    model.compile(
        optimizer='adam',
        loss = 'sparse_categorical_crossentropy',
        metrics=['sparse_categorical_accuracy'],
    )
model.summary()
Explanation: These are tf.data.Dataset objects. You can think about a dataset in TensorFlow as a stream of data records. Check out this guide for more on working with the tf.data API.
Define Model
Now we're ready to create a neural network for classifying images! We'll use what's known as transfer learning. With transfer learning, you reuse part of a pretrained model to get a head-start on a new dataset.
For this tutorial, we'll to use a model called VGG16 pretrained on ImageNet). Later, you might want to experiment with other models included with Keras. (Xception wouldn't be a bad choice.)
The distribution strategy we created earlier contains a context manager, strategy.scope. This context manager tells TensorFlow how to divide the work of training among the eight TPU cores. When using TensorFlow with a TPU, it's important to define your model in a strategy.scope() context.
End of explanation
# Define the batch size. This will be 16 with TPU off and 128 (=16*8) with TPU on
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
# Define training epochs
EPOCHS = 12
STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
history = model.fit(
    ds_train,
    validation_data=ds_valid,
    epochs=EPOCHS,
    steps_per_epoch=STEPS_PER_EPOCH,
)
Explanation: Training
And now we're ready to train the model! After defining a few parameters, we're good to go.
End of explanation
display_training_curves(
    history.history['loss'],
    history.history['val_loss'],
    'loss',
    211,
)
display_training_curves(
    history.history['sparse_categorical_accuracy'],
    history.history['val_sparse_categorical_accuracy'],
    'accuracy',
    212,
)
Explanation: This next cell shows how the loss and metrics progressed during training. Thankfully, it converges!
End of explanation
test_ds = get_test_dataset(ordered=True)
print('Computing predictions...')
test_images_ds = test_ds.map(lambda image, idnum: image)
probabilities = model.predict(test_images_ds)
predictions = np.argmax(probabilities, axis=-1)
print(predictions)
Explanation: Predictions
Once you're satisfied with everything, you're ready to make predictions on the test set.
End of explanation
print('Generating submission.csv file...')
# Get image ids from test set and convert to unicode
test_ids_ds = test_ds.map(lambda image, idnum: idnum).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES))).numpy().astype('U')
# Write the submission file
np.savetxt(
    'submission.csv',
    np.rec.fromarrays([test_ids, predictions]),
    fmt=['%s', '%d'],
    delimiter=',',
    header='id,label',
    comments='',
)
# Look at the first few predictions
!head submission.csv
Explanation: We'll generate a file submission.csv. This file is what you'll submit to get your score on the leaderboard.
End of explanation
<END_TASK>