Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame] | 1 | # SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | """ |
| 16 | Utility functions related to data and models that are common to all the model conditioning examples. |
| 17 | """ |
| 18 | import tensorflow as tf |
| 19 | import numpy as np |
| 20 | |
| 21 | |
| 22 | def get_data(): |
| 23 | """Downloads and returns the pre-processed data and labels for training and testing. |
| 24 | |
| 25 | Returns: |
| 26 | Tuple of: (train data, train labels, test data, test labels) |
| 27 | """ |
| 28 | |
| 29 | # To save time we use the MNIST dataset for this example. |
| 30 | (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() |
| 31 | |
| 32 | # Convolution operations require data to have 4 dimensions. |
| 33 | # We divide by 255 to help training and cast to float32 for TensorFlow. |
| 34 | x_train = (x_train[..., np.newaxis] / 255.0).astype(np.float32) |
| 35 | x_test = (x_test[..., np.newaxis] / 255.0).astype(np.float32) |
| 36 | |
| 37 | return x_train, y_train, x_test, y_test |
| 38 | |
| 39 | |
| 40 | def create_model(): |
| 41 | """Create and returns a simple Keras model for training MNIST. |
| 42 | |
| 43 | We will use a simple convolutional neural network for this example, |
| 44 | but the model optimization methods employed should be compatible with a |
| 45 | wide variety of CNN architectures such as Mobilenet and Inception etc. |
| 46 | |
| 47 | Returns: |
| 48 | Uncompiled Keras model. |
| 49 | """ |
| 50 | |
| 51 | keras_model = tf.keras.models.Sequential([ |
| 52 | tf.keras.layers.Conv2D(32, 3, padding='same', input_shape=(28, 28, 1), activation=tf.nn.relu), |
| 53 | tf.keras.layers.Conv2D(32, 3, padding='same', activation=tf.nn.relu), |
| 54 | tf.keras.layers.MaxPool2D(), |
| 55 | tf.keras.layers.Conv2D(32, 3, padding='same', activation=tf.nn.relu), |
| 56 | tf.keras.layers.MaxPool2D(), |
| 57 | tf.keras.layers.Flatten(), |
| 58 | tf.keras.layers.Dense(units=10, activation=tf.nn.softmax) |
| 59 | ]) |
| 60 | |
| 61 | return keras_model |