<jupyter_start><jupyter_text>Using colab as the basic hardware platform, So we need to change the colab tensorflow version to 2.1.0 and mount the Google Drive. =============================================== 使用colab作为基础硬件平台，因此我们需要将colab tensorflow版本更改为2.1.0并安装谷歌网盘驱动器。<jupyter_code>import sys
sys.path[0] = '/tensorflow-2.1.0/python3.6'
from google.colab import drive
drive.mount('/drive')
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, Sequential, losses, optimizers, datasets
from tensorflow.keras.utils import to_categorical
tf.__version__<jupyter_output><empty_output><jupyter_text>Read cifar100 data from keras.datasets  =============================================== 从keras.datasets 中读取cifar100 数据<jupyter_code>(x, y), (x_test, y_test) = datasets.cifar100.load_data()
print(x.shape,y.shape,x_test.shape,y_test.shape)
print(x.dtype,y.dtype)<jupyter_output><empty_output><jupyter_text>Preprocess the data type and values. Normalize x and embedding y to one_hot format and reshape the y dimension to tf accessable dimension. =============================================== 预处理数据类型和值。将x标准化并将y转换到one_hot格式，并将y维度重塑为tf可访问维度。<jupyter_code>def preprocess(x, y):
    x = tf.cast(x, dtype=tf.float32) / 255.
    y = tf.cast(to_categorical(tf.squeeze(tf.cast(y, dtype=tf.int32), axis=1), num_classes=100), dtype=tf.int32)
    return x,y
class VGG16(keras.Model):
    def __init__(self):
        super(VGG16, self).__init__()
        self.VGG16 = Sequential([
            layers.Conv2D(64, kernel_size=[3,3], padding="same", activation=tf.nn.relu),
            layers.BatchNormalization(),
            layers.Dropout(0.3),
            layers.Conv2D(64, kernel_size=[3,3], padding="same", activation=tf.nn.relu),
            layers.BatchNormalization(),
            layers.Dropout(rate=0.5),
            layers.MaxPool2D(pool_size=[2,2], strides=2, padding="same"),

            layers.Conv2D(128, kernel_size=[3,3], padding="same", activation=tf.nn.relu),
            layers.BatchNormalization(),
            layers.Dropout(0.4),
            layers.Conv2D(128, kernel_size=[3,3], padding="same", activation=tf.nn.relu),
            layers.BatchNormalization(),
            layers.MaxPool2D(pool_size=[2,2], strides=2, padding="same"),

            layers.Conv2D(256, kernel_size=[3,3], padding="same", activation=tf.nn.relu),
            layers.BatchNormalization(),
            layers.Dropout(0.4),
            layers.Conv2D(256, kernel_size=[3,3], padding="same", activation=tf.nn.relu),
            layers.BatchNormalization(),
            layers.Dropout(0.4),
            layers.Conv2D(256, kernel_size=[3,3], padding="same", activation=tf.nn.relu),
            layers.BatchNormalization(),
            layers.MaxPool2D(pool_size=[2,2], strides=2, padding="same"),

            layers.Conv2D(512, kernel_size=[3,3], padding="same", activation=tf.nn.relu),
            layers.BatchNormalization(),
            layers.Dropout(0.4),
            layers.Conv2D(512, kernel_size=[3,3], padding="same", activation=tf.nn.relu),
            layers.BatchNormalization(),
            layers.Dropout(0.4),
            layers.Conv2D(512, kernel_size=[3,3], padding="same", activation=tf.nn.relu),
            layers.BatchNormalization(),
            layers.MaxPool2D(pool_size=[2,2], strides=2, padding="same"),
            
            layers.Conv2D(512, kernel_size=[3,3], padding="same", activation=tf.nn.relu),
            layers.BatchNormalization(),
            layers.Dropout(0.4),
            layers.Conv2D(512, kernel_size=[3,3], padding="same", activation=tf.nn.relu),
            layers.BatchNormalization(),
            layers.Dropout(0.4),
            layers.Conv2D(512, kernel_size=[3,3], padding="same", activation=tf.nn.relu),
            layers.BatchNormalization(),
            layers.MaxPool2D(pool_size=[2,2], strides=2, padding="same"),     

            layers.Flatten(),
            layers.Dense(4096, activation=tf.nn.relu),
            layers.Dropout(rate=0.5),
            layers.Dense(4096, activation=tf.nn.relu),
            layers.Dropout(rate=0.5),
            layers.Dense(100, activation=tf.nn.softmax)
        ])
        
    def call(self, inputs, training=None):
        x = inputs
        prediction = self.VGG16(x)
        return prediction
def main(x, y, x_test, y_test):
    epochs = 1000
    model = VGG16()
    model.build(input_shape=(None, 32, 32, 3))
    model.summary()
    save_best = keras.callbacks.ModelCheckpoint('/drive/My Drive/Github/CNN/VGG16_best_model.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min')
    early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', verbose=1, min_delta=0, patience=100, mode='auto')
    callbacks_list = [early_stop, save_best]
    model.compile(optimizer=optimizers.Adam(),
                 loss=losses.categorical_crossentropy,
                 metrics=['accuracy'])
    x, y = preprocess(x, y)
    x_test, y_test = preprocess(x_test, y_test)
    history = model.fit(x=x, y=y, epochs=epochs, batch_size=512, validation_data=(x_test, y_test), verbose=1, callbacks=callbacks_list)
    return history
history = main(x, y, x_test, y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16324178989821395, 'fraction_numerical': 0.03744958709429614, 'mean_word_length': 3.409822184589331, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '301809', 'license': 'MIT', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1754, 'n_words': 277, 'path': 'Image Classification/VGG16.ipynb', 'repo_name': 'Steven-XSX/Multiple-neural-network-structures-implemented-by-Tensorflow2', 'size': '5207'}