<jupyter_start><jupyter_text>Data Download and Prepare<jupyter_code>import tensorflow_datasets as tfds

dataset, info = tfds.load("tf_flowers", as_supervised=True, with_info=True)
n_classes = info.features["label"].num_classes
dataset_size = info.splits["train"].num_examples
test_set_raw, valid_set_raw, train_set_raw = tfds.load(
    "tf_flowers",
    split=["train[:10%]", "train[10%:25%]", "train[25%:]"],
    as_supervised=True)<jupyter_output><empty_output><jupyter_text>Preprocessing steps<jupyter_code>def preprocess(image, label):
    resized_image = tf.image.resize(image, [224, 224])
    final_image = keras.applications.xception.preprocess_input(resized_image)
    return final_image, label
def central_crop(image):
    shape = tf.shape(image)
    min_dim = tf.reduce_min([shape[0], shape[1]])
    top_crop = (shape[0] - min_dim) // 4
    bottom_crop = shape[0] - top_crop
    left_crop = (shape[1] - min_dim) // 4
    right_crop = shape[1] - left_crop
    return image[top_crop:bottom_crop, left_crop:right_crop]

def random_crop(image):
    shape = tf.shape(image)
    min_dim = tf.reduce_min([shape[0], shape[1]]) * 90 // 100
    return tf.image.random_crop(image, [min_dim, min_dim, 3])

def preprocess(image, label, randomize=False):
    if randomize:
        cropped_image = random_crop(image)
        cropped_image = tf.image.random_flip_left_right(cropped_image)
    else:
        cropped_image = central_crop(image)
    resized_image = tf.image.resize(cropped_image, [224, 224])
    final_image = keras.applications.xception.preprocess_input(resized_image)
    return final_image, label

batch_size = 32
train_set = train_set_raw.shuffle(1000).repeat()
train_set = train_set.map(preprocess).batch(batch_size).prefetch(1)
valid_set = valid_set_raw.map(preprocess).batch(batch_size).prefetch(1)
test_set = test_set_raw.map(preprocess).batch(batch_size).prefetch(1)<jupyter_output><empty_output><jupyter_text>Transfer Learning and Training  Xception model<jupyter_code>base_model = keras.applications.xception.Xception(weights="imagenet",
                                                  include_top=False)
avg = keras.layers.GlobalAveragePooling2D()(base_model.output)
output = keras.layers.Dense(n_classes, activation="softmax")(avg)
model = keras.models.Model(inputs=base_model.input, outputs=output)
for layer in base_model.layers:
    layer.trainable = True

optimizer = keras.optimizers.SGD(learning_rate=0.01, momentum=0.9,
                                 nesterov=True, decay=0.001)
model.compile(loss="sparse_categorical_crossentropy", optimizer=optimizer,
              metrics=["accuracy"])
history = model.fit(train_set,
                    steps_per_epoch=int(0.75 * dataset_size / batch_size),
                    validation_data=valid_set,
                    validation_steps=int(0.15 * dataset_size / batch_size),
                    epochs=10)<jupyter_output>Epoch 1/10
86/86 [==============================] - 153s 2s/step - loss: 0.5667 - accuracy: 0.7980 - val_loss: 0.3153 - val_accuracy: 0.8860
Epoch 2/10
86/86 [==============================] - 113s 1s/step - loss: 0.1138 - accuracy: 0.9677 - val_loss: 0.2117 - val_accuracy: 0.9154
Epoch 3/10
86/86 [==============================] - 113s 1s/step - loss: 0.0426 - accuracy: 0.9909 - val_loss: 0.2042 - val_accuracy: 0.9283
Epoch 4/10
86/86 [==============================] - 113s 1s/step - loss: 0.0196 - accuracy: 0.9967 - val_loss: 0.2051 - val_accuracy: 0.9338
Epoch 5/10
86/86 [==============================] - 113s 1s/step - loss: 0.0142 - accuracy: 0.9971 - val_loss: 0.2173 - val_accuracy: 0.9246
Epoch 6/10
86/86 [==============================] - 113s 1s/step - loss: 0.0107 - accuracy: 0.9982 - val_loss: 0.2370 - val_accuracy: 0.9210
Epoch 7/10
86/86 [==============================] - 114s 1s/step - loss: 0.0088 - accuracy: 0.9989 - val_loss: 0.2381 - val_accuracy: 0.9210
Epoch 8/10
86[...]<jupyter_text>Visualize layers of transfered model<jupyter_code>for index, layer in enumerate(base_model.layers):
    print(index, layer.name)<jupyter_output>0 input_2
1 block1_conv1
2 block1_conv1_bn
3 block1_conv1_act
4 block1_conv2
5 block1_conv2_bn
6 block1_conv2_act
7 block2_sepconv1
8 block2_sepconv1_bn
9 block2_sepconv2_act
10 block2_sepconv2
11 block2_sepconv2_bn
12 conv2d_4
13 block2_pool
14 batch_normalization_4
15 add_12
16 block3_sepconv1_act
17 block3_sepconv1
18 block3_sepconv1_bn
19 block3_sepconv2_act
20 block3_sepconv2
21 block3_sepconv2_bn
22 conv2d_5
23 block3_pool
24 batch_normalization_5
25 add_13
26 block4_sepconv1_act
27 block4_sepconv1
28 block4_sepconv1_bn
29 block4_sepconv2_act
30 block4_sepconv2
31 block4_sepconv2_bn
32 conv2d_6
33 block4_pool
34 batch_normalization_6
35 add_14
36 block5_sepconv1_act
37 block5_sepconv1
38 block5_sepconv1_bn
39 block5_sepconv2_act
40 block5_sepconv2
41 block5_sepconv2_bn
42 block5_sepconv3_act
43 block5_sepconv3
44 block5_sepconv3_bn
45 add_15
46 block6_sepconv1_act
47 block6_sepconv1
48 block6_sepconv1_bn
49 block6_sepconv2_act
50 block6_sepconv2
51 block6_sepconv2_bn
52 block6_se[...]<jupyter_text>EffcientNet B0 model<jupyter_code>base_model = keras.applications.EfficientNetB0(weights="imagenet",
                                                  include_top=False)
avg = keras.layers.GlobalAveragePooling2D()(base_model.output)
output = keras.layers.Dense(n_classes, activation="softmax")(avg)
model = keras.models.Model(inputs=base_model.input, outputs=output)
for layer in base_model.layers:
    layer.trainable = True

optimizer = keras.optimizers.SGD(learning_rate=0.01, momentum=0.9,
                                 nesterov=True, decay=0.001)
model.compile(loss="sparse_categorical_crossentropy", optimizer=optimizer,
              metrics=["accuracy"])
history = model.fit(train_set,
                    steps_per_epoch=int(0.75 * dataset_size / batch_size),
                    validation_data=valid_set,
                    validation_steps=int(0.15 * dataset_size / batch_size),
                    epochs=10)<jupyter_output><empty_output><jupyter_text>Visualize layers of transfered model<jupyter_code>for index, layer in enumerate(base_model.layers):
    print(index, layer.name)<jupyter_output><empty_output><jupyter_text>ResNet 101 model<jupyter_code>base_model = keras.applications.ResNet101(weights="imagenet",
                                                  include_top=False)
avg = keras.layers.GlobalAveragePooling2D()(base_model.output)
output = keras.layers.Dense(n_classes, activation="softmax")(avg)
model = keras.models.Model(inputs=base_model.input, outputs=output)
for layer in base_model.layers:
    layer.trainable = True

optimizer = keras.optimizers.SGD(learning_rate=0.01, momentum=0.9,
                                 nesterov=True, decay=0.001)
model.compile(loss="sparse_categorical_crossentropy", optimizer=optimizer,
              metrics=["accuracy"])
history = model.fit(train_set,
                    steps_per_epoch=int(0.75 * dataset_size / batch_size),
                    validation_data=valid_set,
                    validation_steps=int(0.15 * dataset_size / batch_size),
                    epochs=10)<jupyter_output><empty_output><jupyter_text>Visualize layers of transfered model<jupyter_code>for index, layer in enumerate(base_model.layers):
    print(index, layer.name)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12891796129735622, 'fraction_numerical': 0.07127282638321068, 'mean_word_length': 4.110724233983287, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '271510', 'license': 'MIT', 'n_tokens_mistral': 2846, 'n_tokens_neox': 2567, 'n_words': 446, 'path': 'Lab10.ipynb', 'repo_name': 'anton-selitskiy/The-Art-of-ML', 'size': '7338'}