Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
89 changes: 89 additions & 0 deletions machine_learning/Cnn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import tensorflow as tf

Check failure on line 1 in machine_learning/Cnn.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

machine_learning/Cnn.py:1:1: N999 Invalid module name: 'Cnn'
from tensorflow.keras import layers, models
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np

Check failure on line 5 in machine_learning/Cnn.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

machine_learning/Cnn.py:5:17: F401 `numpy` imported but unused
import matplotlib.pyplot as plt

# Load and preprocess dataset (using MNIST dataset as an example)

Check failure on line 8 in machine_learning/Cnn.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

machine_learning/Cnn.py:1:1: I001 Import block is un-sorted or un-formatted
mnist = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# Reshape and normalize the data
train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255

# Data Augmentation
datagen = ImageDataGenerator(
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1
)
datagen.fit(train_images)

# Model building
model = models.Sequential()

# Adding Convolutional layers
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Conv2D(64, (3, 3), activation='relu'))

# Flattening and adding Dense layers
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dropout(0.5)) # Dropout to prevent overfitting

# Output layer
model.add(layers.Dense(10, activation='softmax'))

# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

# Early stopping to avoid overfitting
early_stopping = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)

Check failure on line 51 in machine_learning/Cnn.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

machine_learning/Cnn.py:51:89: E501 Line too long (89 > 88)

# Train the model with data augmentation and early stopping
history = model.fit(
datagen.flow(train_images, train_labels, batch_size=32),
epochs=10,
validation_data=(test_images, test_labels),
callbacks=[early_stopping]
)

# Plotting training and validation accuracy/loss
def plot_history(history):
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(len(acc))

plt.figure(figsize=(12, 6))

Check failure on line 71 in machine_learning/Cnn.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

machine_learning/Cnn.py:71:1: W293 Blank line contains whitespace
# Plot accuracy
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

# Plot loss
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')

plt.show()

# Call the plot function to visualize training progress
plot_history(history)
Loading