<jupyter_start><jupyter_text>Retraining Inception model for CIFAR-10 images--------------------------------------In this script, we download the CIFAR-10 images and transform them in the Inception Retraining Format. The end purpose of the files is for re-training the Google Inception tensorflow model to work on the CIFAR-10.We start by loading the necessary libraries.<jupyter_code>import tensorflow as tf
from tensorflow import keras

from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.applications.inception_v3 import preprocess_input, decode_predictions<jupyter_output><empty_output><jupyter_text>Next, set the parameters. - `batch_size`: this is how many cifar examples to train on in one batch. - `buffer_size`: this is how many cifar examples to shuffle randomly on a buffer<jupyter_code># Set dataset parameters
batch_size = 32
buffer_size= 1000<jupyter_output><empty_output><jupyter_text>Download the CIFAR-10 dataset using `tf.keras.datasets` API.CIFAR is composed of 50k train and 10k test images that are 32x32.<jupyter_code>(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()

objects = ['airplane', 'automobile', 'bird', 'cat', 'deer',
           'dog', 'frog', 'horse', 'ship', 'truck']<jupyter_output><empty_output><jupyter_text>Initialize the data pipeline using `tf.data.DataSet` for train and test datasets.InceptionV3 is pretrained on ImageNet dataset, so our CIFAR-10 images must match the format of these images:- The width and height expected should be no smaller than 75, so we will resize our images to 75x75 spatial size.- The images should be normalized, so we will apply the inception preprocessing task (`preprocess_input` method) on each image.<jupyter_code>dataset_train = tf.data.Dataset.from_tensor_slices((x_train, y_train))
dataset_test = tf.data.Dataset.from_tensor_slices((x_test, y_test))

def preprocess_cifar10(img, label):
    img = tf.cast(img, tf.float32)
    img = tf.image.resize(img, (75, 75))
    return tf.keras.applications.inception_v3.preprocess_input(img) , label

dataset_train_processed = dataset_train.shuffle(buffer_size).batch(batch_size).map(preprocess_cifar10)
dataset_test_processed = dataset_test.batch(batch_size).map(preprocess_cifar10)<jupyter_output><empty_output><jupyter_text>Now, we will create our own model based on the Inception V3 model.We will load the Inception V3 model using the `tensorflow.keras.applications` API. This API contains pre-trained deep learning models that can be used for prediction, feature extraction and fine-tuning.Then, we will load the weights without the classification head.<jupyter_code>inception_model = InceptionV3(
    include_top=False,
    weights="imagenet",
    input_shape=(75,75,3)
)<jupyter_output><empty_output><jupyter_text>We build our own model on top of the Inception V3 model by adding a classifier with 3 fully-connected layers.<jupyter_code>x = inception_model.output
x= keras.layers.GlobalAveragePooling2D()(x)
x = keras.layers.Dense(1024, activation="relu")(x)
x = keras.layers.Dense(128, activation="relu")(x)
output = keras.layers.Dense(10, activation="softmax")(x)

model=keras.Model(inputs=inception_model.input, outputs = output)

for inception_layer in inception_model.layers:
    inception_layer.trainable= False


model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])<jupyter_output><empty_output><jupyter_text>Display the model summary<jupyter_code>model.summary()
model.fit(x=dataset_train_processed , 
          validation_data=dataset_test_processed)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10091235830799004, 'fraction_numerical': 0.019076582803428256, 'mean_word_length': 6.052631578947368, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '350918', 'license': 'MIT', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1125, 'n_words': 319, 'path': 'ch8/04_Retraining_Current_Architectures/04_download_cifar10.ipynb', 'repo_name': 'AbdelrahimKoura/Machine-Learning-Using-TensorFlow-Cookbook', 'size': '3617'}