-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrecognizer_complex_cnn.py
More file actions
99 lines (81 loc) · 3.02 KB
/
recognizer_complex_cnn.py
File metadata and controls
99 lines (81 loc) · 3.02 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import numpy as np
import pandas as pd
from tensorflow.keras import layers, models, regularizers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping
from sklearn.model_selection import train_test_split
# Load the training and test data
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')
# Separate labels and features
y_df = train[['label']]
x_df = train.drop(['label'], axis=1)
# Convert to numpy arrays
y = y_df.to_numpy().flatten()
X = x_df.to_numpy()
# Normalize the images to the range [0, 1]
X = X / 255.0
# Reshape the images to include the channel dimension
X = X.reshape(X.shape[0], 28, 28, 1)
# Split the data into training and validation sets
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=42)
# Data augmentation
datagen = ImageDataGenerator(
rotation_range=15,
width_shift_range=0.15,
height_shift_range=0.15,
zoom_range=0.2,
shear_range=0.15
)
datagen.fit(X_train)
# Modify the model with additional layers and regularization
model = models.Sequential([
layers.Input(shape=(28, 28, 1)),
layers.Conv2D(32, (3, 3), activation='relu', padding='same'),
layers.BatchNormalization(),
layers.Conv2D(32, (3, 3), activation='relu', padding='same'),
layers.MaxPooling2D((2, 2)),
layers.Dropout(0.3),
layers.Conv2D(64, (3, 3), activation='relu', padding='same'),
layers.BatchNormalization(),
layers.Conv2D(64, (3, 3), activation='relu', padding='same'),
layers.MaxPooling2D((2, 2)),
layers.Dropout(0.3),
layers.Conv2D(128, (3, 3), activation='relu', padding='same'),
layers.BatchNormalization(),
layers.Conv2D(128, (3, 3), activation='relu', padding='same'),
layers.MaxPooling2D((2, 2)),
layers.Dropout(0.4),
layers.Flatten(),
layers.Dense(256, activation='relu', kernel_regularizer=regularizers.l2(0.001)),
layers.BatchNormalization(),
layers.Dropout(0.5),
layers.Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Callbacks for learning rate adjustment and early stopping
callbacks = [
ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, verbose=1),
EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
]
# Train the model with callbacks
model.fit(datagen.flow(X_train, y_train, batch_size=64),
epochs=50,
validation_data=(X_val, y_val),
callbacks=callbacks)
# Save the model
model.save('digit_recognition_model.keras')
# Prepare the test data
test = test.to_numpy()
test = test / 255.0
test = test.reshape(test.shape[0], 28, 28, 1)
# Predict the labels for the test data
predictions = model.predict(test)
predicted_labels = np.argmax(predictions, axis=1)
# Save the predictions to a CSV file
submission = pd.DataFrame({
'ImageId': range(1, len(predicted_labels) + 1),
'Label': predicted_labels
})
submission.to_csv('submission.csv', index=False)