VGG ARCHITECTURE EX -10
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras import datasets, layers, models, losses
(x_train, y_train), (x_test, y_test)=tf.keras.datasets.mnist.load_data()
x_train = tf.pad(x_train, [[0, 0], [2,2], [2,2]])/255
x_test = tf.pad(x_test, [[0, 0], [2,2], [2,2]])/255
x_train = tf.expand_dims(x_train, axis=3, name=None)
x_test = tf.expand_dims(x_test, axis=3, name=None)
x_val = x_train[-2000:,:,:,:]
y_val = y_train[-2000:]
x_train = x_train[:-2000,:,:,:]
y_train = y_train[:-2000]
model = models.Sequential([
layers.experimental.preprocessing.Resizing(224, 224,
interpolation="bilinear",
input_shape=x_train.shape[1:]),
layers.Conv2D(64, 3, strides=1, padding='same'),
layers.Activation('relu'),
layers.Conv2D(64, 3, strides=1, padding='same'),
layers.Activation('relu'),
layers.MaxPooling2D(2, strides=2),
layers.Conv2D(128, 3, strides=1, padding='same'),
layers.Activation('relu'),
layers.Conv2D(128, 3, strides=1, padding='same'),
layers.Activation('relu'),
layers.MaxPooling2D(2, strides=2),
layers.Conv2D(256, 3, strides=1, padding='same'),
layers.Activation('relu'),
layers.Conv2D(256, 1, strides=1, padding='same'),
layers.Activation('relu'),
layers.MaxPooling2D(2, strides=2),
layers.Conv2D(512, 3, strides=1, padding='same'),
layers.Activation('relu'),
layers.Conv2D(512, 3, strides=1, padding='same'),
layers.Activation('relu'),
layers.Conv2D(512, 1, strides=1, padding='same'),
layers.Activation('relu'),
layers.MaxPooling2D(2, strides=2),
layers.Conv2D(512, 3, strides=1, padding='same'),
layers.Activation('relu'),
layers.Conv2D(512, 3, strides=1, padding='same'),
layers.Activation('relu'),
layers.Conv2D(512, 1, strides=1, padding='same'),
layers.Activation('relu'),
layers.MaxPooling2D(2, strides=2),
layers.Flatten(),
layers.Dense(4096, activation='relu'),
layers.Dropout(0.5),
layers.Dense(4096, activation='relu'),
layers.Dropout(0.5),
layers.Dense(10, activation='softmax')
])
model.summary()
model.compile(optimizer='adam',
loss=losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
history = model.fit(x_train, y_train,
batch_size=64,
epochs=20,
validation_data=(x_val, y_val))
fig, axs = plt.subplots(2, 1, figsize=(15,15))
axs[0].plot(history.history['loss'])
axs[0].plot(history.history['val_loss'])
axs[0].title.set_text('Training Loss vs Validation Loss')
axs[0].legend(['Train', 'Val'])
axs[1].plot(history.history['accuracy'])
axs[1].plot(history.history['val_accuracy'])
axs[1].title.set_text('Training Accuracy vs Validation Accuracy')
axs[1].legend(['Train', 'Val'])
results = model.evaluate(x_test, y_test)
print("Loss = {}, Accuray = {}".format(results[0], results[1]))
Comments
Post a Comment