Image Classification Code
Image Classification Code
import tensorflow as tf
# Data augmentation
datagen = ImageDataGenerator(
rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True
datagen.fit(X_train)
def lr_schedule(epoch):
initial_lr = 0.001
if epoch > 5:
return initial_lr
lr_scheduler = LearningRateScheduler(lr_schedule)
model = keras.Sequential([
layers.BatchNormalization(),
layers.BatchNormalization(),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.BatchNormalization(),
layers.BatchNormalization(),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Flatten(),
layers.Dense(128, activation="relu"),
layers.BatchNormalization(),
layers.Dropout(0.5),
])
model.compile(
optimizer="adam",
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=["accuracy"]
history = model.fit(
epochs=20,
validation_data=(X_test, y_test),
callbacks=[early_stopping, lr_scheduler]
model.save("cifar10_model.h5")
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history["accuracy"], label="Train")
plt.plot(history.history["val_accuracy"], label="Validation")
plt.legend()
plt.title("Accuracy")
plt.subplot(1, 2, 2)
plt.plot(history.history["loss"], label="Train")
plt.plot(history.history["val_loss"], label="Validation")
plt.legend()
plt.title("Loss")
plt.show()