-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCNN.py
More file actions
60 lines (54 loc) · 2.05 KB
/
CNN.py
File metadata and controls
60 lines (54 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import numpy as np
import tensorflow as tf
import logging
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
# Configure logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
def load_data():
"""
Loads and preprocesses the MNIST dataset.
Returns:
tuple: Training and test datasets.
"""
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape((x_train.shape[0], 28, 28, 1)).astype("float32") / 255
x_test = x_test.reshape((x_test.shape[0], 28, 28, 1)).astype("float32") / 255
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
return (x_train, y_train), (x_test, y_test)
def build_model():
"""
Builds and compiles a Convolutional Neural Network (CNN) model.
Returns:
model (Sequential): Compiled CNN model.
"""
model = Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(64, kernel_size=(3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
return model
def train_and_evaluate(model, x_train, y_train, x_test, y_test):
"""
Trains and evaluates the CNN model.
"""
model.fit(x_train, y_train, epochs=10, batch_size=128, validation_split=0.2)
test_loss, test_acc = model.evaluate(x_test, y_test)
logging.info(f"Test accuracy: {test_acc:.4f}")
def main():
"""
Main function to load data, build, train, and evaluate the CNN model.
"""
(x_train, y_train), (x_test, y_test) = load_data()
model = build_model()
train_and_evaluate(model, x_train, y_train, x_test, y_test)
if __name__ == "__main__":
main()