<jupyter_start><jupyter_text>Fashion MNIST with `tf.keras` from ScratchThis example demonstrates the workflow to create, train, and validate a TensorFlow `tf.keras` model, save it to HDF5 `.h5` model and convert it to Core ML `.mlmodel` format using the `tfcoreml` converter. For moreexamples, refer `test_tf_2x.py` file. Note: - This notebook was tested with following dependencies:```tensorflow==2.0.0coremltools==3.1tfcoreml==1.1```- Models from TensorFlow 2.0+ is supported only for `minimum_ios_deployment_target>='13'`.You can also use `coremltools.converters.tensorflow.convert()` instead of `tfcoreml.convert()` to convert your model.<jupyter_code>import tensorflow as tf
import numpy as np
import tfcoreml

print(tf.__version__)
# prepare fashion_mnist dataset
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

train_images = train_images / 255.0
test_images = test_images / 255.0
# create a simple model using tf.keras
keras_model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])
# training and evaludate keras model
keras_model.compile(optimizer='adam',
                    loss='sparse_categorical_crossentropy',
                    metrics=['accuracy'])

keras_model.fit(train_images, train_labels, epochs=10)
test_loss, test_acc = keras_model.evaluate(test_images, test_labels, verbose=2)

print('\nTest accuracy:', test_acc)
# save the tf.keras model as .h5 model file
model_file = './mnist_fashion_model.h5'
keras_model.save(model_file)

!ls mnist_fashion_model.h5
# convert this model to Core ML format
model = tfcoreml.convert(tf_model_path=model_file,
                         input_name_shape_dict={'flatten_input': (1, 28, 28)},
                         output_feature_names=['Identity'],
                         minimum_ios_deployment_target='13')
model.save('./mnist_fashion_model.mlmodel')

!ls mnist_fashion_model.mlmodel
# run predictions with fake image as an input
fake_image = np.random.rand(1, 28, 28)

keras_predictions = keras_model.predict(fake_image)
print(keras_predictions[:10])

coreml_predictions = model.predict({'flatten_input': fake_image})['Identity']
print(coreml_predictions[:10])

assert(np.allclose(keras_predictions, coreml_predictions))<jupyter_output>[[1.5719648e-09 1.7905072e-09 5.9817944e-07 8.1820750e-10 9.6943937e-09
  5.0254831e-20 1.5249961e-07 6.2053448e-17 9.9999928e-01 1.0400648e-15]]
[[1.57196778e-09 1.79050730e-09 5.98181146e-07 8.18209001e-10
  9.69441238e-09 5.02548314e-20 1.52499751e-07 6.20534484e-17
  9.99999285e-01 1.04006487e-15]]
{'fraction_non_alphanumeric': 0.11544117647058824, 'fraction_numerical': 0.09669117647058824, 'mean_word_length': 4.839055793991417, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '68409', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1131, 'n_tokens_neox': 958, 'n_words': 200, 'path': 'examples/neural_network_inference/tensorflow_converter/Tensorflow_2/tf_keras_fashion_mnist.ipynb', 'repo_name': 'Gerzer/coremltools', 'size': '2720'}