<jupyter_start><jupyter_text>ResNet50

Call tensorflow.application library

 Load data<jupyter_code>SEED = 2021
import os
os.environ['PYTHONHASHSEED'] = str(SEED)

import tensorflow as tf
from tensorflow.keras import datasets, layers, models

import matplotlib.pyplot as plt
import random
import numpy

random.seed(SEED)
numpy.random.seed(SEED)
tf.random.set_seed(SEED)

(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0

print(tf.test.gpu_device_name())
tf.config.list_physical_devices('GPU')<jupyter_output>/device:GPU:0<jupyter_text>Preveiw photos<jupyter_code>class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
               'dog', 'frog', 'horse', 'ship', 'truck']

plt.figure(figsize=(10, 10))
for i in range(25):
    plt.subplot(5, 5, i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i])
    # The CIFAR labels happen to be arrays,
    # which is why you need the extra index
    plt.xlabel(class_names[train_labels[i][0]], color='red', fontsize='large')
plt.show()<jupyter_output><empty_output><jupyter_text>Build Model<jupyter_code>def ResNet50(input_shape):
    input = layers.Input(shape=input_shape)
    x = tf.keras.applications.resnet50.ResNet50(weights=None, input_shape=input_shape, classes=10, classifier_activation=None)(input)

    return models.Model(input, x)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code># The image size in CIFAR10 is 32*32 RGB
m = ResNet50((32, 32, 3), 10)
m.summary()

m.compile(optimizer='adam',
          loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
          metrics=['accuracy'])
history = m.fit(train_images, train_labels, batch_size=100, epochs=100,
        validation_data=(test_images, test_labels))<jupyter_output>Epoch 1/100
500/500 [==============================] - 41s 66ms/step - loss: 1.9509 - accuracy: 0.3684 - val_loss: 2.3480 - val_accuracy: 0.2264
Epoch 2/100
500/500 [==============================] - 31s 62ms/step - loss: 1.6740 - accuracy: 0.4482 - val_loss: 1145.4738 - val_accuracy: 0.0912
Epoch 3/100
500/500 [==============================] - 32s 65ms/step - loss: 1.7352 - accuracy: 0.4276 - val_loss: 1.5347 - val_accuracy: 0.4394
Epoch 4/100
500/500 [==============================] - 33s 65ms/step - loss: 1.5886 - accuracy: 0.4863 - val_loss: 1.5721 - val_accuracy: 0.4428
Epoch 5/100
500/500 [==============================] - 33s 65ms/step - loss: 1.5477 - accuracy: 0.4909 - val_loss: 1.6303 - val_accuracy: 0.4564
Epoch 6/100
500/500 [==============================] - 32s 63ms/step - loss: 1.4089 - accuracy: 0.5522 - val_loss: 8.4916 - val_accuracy: 0.3990
Epoch 7/100
500/500 [==============================] - 32s 63ms/step - loss: 1.4840 - accuracy: 0.5187 - val_loss: 6.6646 - val[...]<jupyter_text>Evaluate the model<jupyter_code>plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0, 1])
plt.legend(loc='lower right')
plt.plot(history.history['loss'], label='loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0, 10])
plt.legend(loc='upper right')
print(max(history.history['val_accuracy']))<jupyter_output>0.7483000159263611
{'fraction_non_alphanumeric': 0.1998289623717218, 'fraction_numerical': 0.09007981755986318, 'mean_word_length': 4.877721943048576, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '169991', 'license': 'MIT', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1281, 'n_words': 244, 'path': 'ResNet/ResNet50_CIFAR10_tf.ipynb', 'repo_name': 'liao2000/ML-Notebook', 'size': '3508'}