-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnn.py
More file actions
386 lines (332 loc) · 14.5 KB
/
Copy pathcnn.py
File metadata and controls
386 lines (332 loc) · 14.5 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import numpy as np
import matplotlib.pyplot as plt
import os
import pickle
import json
import cv2
from activations import ReLU, Softmax
from convolution import Conv2D
from maxpooling import MaxPool2D
from fullyconnected import Flatten, Dense
from configurations import NUM_CLASSES, IMG_SIZE, CLASSES
class CNN:
def __init__(self):
self.layers = [
# Architecture as specified
Conv2D(filters=32, kernel_size=3, in_channels=3, padding=1),
ReLU(),
MaxPool2D(),
Flatten(),
Dense(input_size=16*16*32, output_size=128), # Adjusted for 32x32 input after pooling
ReLU(),
Dense(input_size=128, output_size=NUM_CLASSES),
Softmax()
]
self.epoch_count = 0
self.best_val_acc = 0.0
self.best_epoch = 0
def forward(self, X):
# Preprocessing steps
# X = self._preprocess_images(X)
for layer in self.layers:
X = layer.forward(X)
return X
def backward(self, y_true, learning_rate):
d_out = y_true
for layer in reversed(self.layers):
if isinstance(layer, (Dense, Conv2D)):
d_out = layer.backward(d_out, learning_rate)
else:
d_out = layer.backward(d_out)
return d_out
def train(self, X_train, y_train, X_val, y_val, epochs=10, batch_size=32,
learning_rate=0.001, checkpoint_dir=None, checkpoint_freq=1,
resume_from=None):
"""
Enhanced training with checkpointing and best model saving
"""
# Initialize or resume training
if resume_from:
self.load_checkpoint(resume_from)
print(f"Resuming training from epoch {self.epoch_count}")
train_loss_history = []
val_loss_history = []
train_acc_history = []
val_acc_history = []
# Create checkpoint directory if needed
if checkpoint_dir and not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
print("\ntraining started ............\n")
for epoch in range(epochs):
current_epoch = self.epoch_count + 1
indices = np.arange(X_train.shape[0])
np.random.shuffle(indices)
X_train = X_train[indices]
y_train = y_train[indices]
epoch_loss = 0
correct = 0
total = 0
# Training loop
for i in range(0, X_train.shape[0], batch_size):
X_batch = X_train[i:i+batch_size]
y_batch = y_train[i:i+batch_size]
output = self.forward(X_batch)
loss = -np.mean(np.log(output[np.arange(len(y_batch)), y_batch] + 1e-10))
preds = np.argmax(output, axis=1)
batch_correct = np.sum(preds == y_batch)
epoch_loss += loss * len(X_batch)
correct += batch_correct
total += len(X_batch)
y_onehot = np.eye(NUM_CLASSES)[y_batch]
self.backward(y_onehot, learning_rate)
# Validation
val_output = self.forward(X_val)
val_loss = -np.mean(np.log(val_output[np.arange(len(y_val)), y_val] + 1e-10))
val_preds = np.argmax(val_output, axis=1)
val_acc = np.mean(val_preds == y_val)
# Record metrics
train_loss = epoch_loss / total
train_acc = correct / total
train_loss_history.append(train_loss)
val_loss_history.append(val_loss)
train_acc_history.append(train_acc)
val_acc_history.append(val_acc)
print("-----------------------------------------------------------------------------------")
print(f"Epoch {current_epoch}: "
f"Train Loss: {train_loss:.4f}, Train Acc: {train_acc:.4f} | "
f"Val Loss: {val_loss:.4f}, Val Acc: {val_acc:.4f}")
# Save best model
if val_acc > self.best_val_acc:
self.best_val_acc = val_acc
self.best_epoch = current_epoch
if checkpoint_dir:
best_model_path = os.path.join(checkpoint_dir, "best_model.ckpt")
self.save_checkpoint(best_model_path, current_epoch,
train_loss_history, val_loss_history,
train_acc_history, val_acc_history)
print(f"New best model saved with val_acc {val_acc:.4f}")
# Save checkpoint if needed
if checkpoint_dir and (current_epoch % checkpoint_freq == 0 or epoch == epochs-1):
checkpoint_path = os.path.join(checkpoint_dir, f"epoch_{current_epoch}.ckpt")
self.save_checkpoint(checkpoint_path, current_epoch,
train_loss_history, val_loss_history,
train_acc_history, val_acc_history)
print(f"Saved checkpoint to {checkpoint_path}")
self.epoch_count += 1
# Final report
print(f"\nTraining completed. Best validation accuracy: {self.best_val_acc:.4f} at epoch {self.best_epoch}")
# Plot results
if checkpoint_dir:
self.plot_training_history_from_dir(checkpoint_dir)
else:
self.plot_training_history(train_loss_history, val_loss_history,
train_acc_history, val_acc_history)
# [Keep all other methods (save_checkpoint, load_checkpoint, evaluate, etc.) the same]
# Only the architecture and preprocessing steps have changed
def save_checkpoint(self, filepath, epoch, train_loss_hist, val_loss_hist,
train_acc_hist, val_acc_hist):
"""Save complete training state"""
model_data = {
'epoch': epoch,
'best_val_acc': self.best_val_acc,
'best_epoch': self.best_epoch,
'train_loss_history': train_loss_hist,
'val_loss_history': val_loss_hist,
'train_acc_history': train_acc_hist,
'val_acc_history': val_acc_hist,
'layers': []
}
for layer in self.layers:
if isinstance(layer, (Conv2D, Dense)):
layer_data = {
'type': type(layer).__name__,
'weights': layer.weights,
'bias': layer.bias
}
if isinstance(layer, Conv2D):
layer_data.update({
'filters': layer.filters,
'kernel_size': layer.kernel_size,
'in_channels': layer.in_channels,
'stride': layer.stride,
'padding': layer.padding
})
else: # Dense
layer_data.update({
'input_size': layer.weights.shape[0],
'output_size': layer.weights.shape[1]
})
model_data['layers'].append(layer_data)
else:
model_data['layers'].append({'type': type(layer).__name__})
with open(filepath, 'wb') as f:
pickle.dump(model_data, f)
def load_checkpoint(self, filepath):
"""Load complete training state"""
with open(filepath, 'rb') as f:
model_data = pickle.load(f)
self.epoch_count = model_data['epoch']
self.best_val_acc = model_data.get('best_val_acc', 0.0)
self.best_epoch = model_data.get('best_epoch', 0)
new_layers = []
for layer_data in model_data['layers']:
if layer_data['type'] == 'Conv2D':
layer = Conv2D(
filters=layer_data['filters'],
kernel_size=layer_data['kernel_size'],
in_channels=layer_data['in_channels'],
stride=layer_data['stride'],
padding=layer_data['padding']
)
layer.weights = layer_data['weights']
layer.bias = layer_data['bias']
new_layers.append(layer)
elif layer_data['type'] == 'Dense':
layer = Dense(
input_size=layer_data['input_size'],
output_size=layer_data['output_size']
)
layer.weights = layer_data['weights']
layer.bias = layer_data['bias']
new_layers.append(layer)
elif layer_data['type'] == 'ReLU':
new_layers.append(ReLU())
elif layer_data['type'] == 'MaxPool2D':
new_layers.append(MaxPool2D())
elif layer_data['type'] == 'Flatten':
new_layers.append(Flatten())
elif layer_data['type'] == 'Softmax':
new_layers.append(Softmax())
self.layers = new_layers
print(f"Loaded checkpoint from epoch {self.epoch_count}")
# [Keep all other methods the same as in your original code]
def plot_training_history(self, train_loss, val_loss, train_acc, val_acc):
"""Plot training history"""
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(train_loss, label='Train Loss')
plt.plot(val_loss, label='Val Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(train_acc, label='Train Accuracy')
plt.plot(val_acc, label='Val Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
def plot_training_history_from_dir(self, checkpoint_dir):
"""Plot training history by reading checkpoints from a directory"""
train_loss, val_loss, train_acc, val_acc = [], [], [], []
# Get all checkpoint files and sort by epoch number
checkpoint_files = sorted(
[f for f in os.listdir(checkpoint_dir) if f.endswith('.ckpt') and not f.endswith('best_model.ckpt')],
key=lambda x: int(x.split('_')[1].split('.')[0]) # Extract epoch number from "epoch_X.ckpt"
)
print(checkpoint_files)
for file in checkpoint_files:
file_path = os.path.join(checkpoint_dir, file)
try:
with open(file_path, 'rb') as f:
data = pickle.load(f)
train_loss.append(data['train_loss_history'][-1]) # Get last value for this epoch
print(data['train_loss_history'][-1])
val_loss.append(data['val_loss_history'][-1])
train_acc.append(data['train_acc_history'][-1])
val_acc.append(data['val_acc_history'][-1])
print(f"Loaded checkpoint from {file_path}")
except Exception as e:
print(f"Error loading {file_path}: {str(e)}")
continue
if not train_loss:
print("No valid checkpoint data found to plot!")
return
# Plotting
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(train_loss, label='Train Loss')
plt.plot(val_loss, label='Val Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Loss Over Epochs')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(train_acc, label='Train Accuracy')
plt.plot(val_acc, label='Val Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.title('Accuracy Over Epochs')
plt.legend()
plt.tight_layout()
plt.show()
def evaluate(self, X_test, y_test):
output = self.forward(X_test)
preds = np.argmax(output, axis=1)
acc = np.mean(preds == y_test)
print(f"Test Accuracy: {acc:.4f}")
return acc
# @staticmethod
def load_model(self, filepath):
# """Load a model from a file"""
# with open(filepath, 'rb') as f:
# model_data = pickle.load(f)
# model = CNN()
# new_layers = []
# for layer_data in model_data['layers']:
# if layer_data['type'] == 'Conv2D':
# layer = Conv2D(
# filters=layer_data['filters'],
# kernel_size=layer_data['kernel_size'],
# in_channels=layer_data['in_channels'],
# stride=layer_data['stride'],
# padding=layer_data['padding']
# )
# layer.weights = layer_data['weights']
# layer.bias = layer_data['bias']
# new_layers.append(layer)
# elif layer_data['type'] == 'Dense':
# layer = Dense(
# input_size=layer_data['input_size'],
# output_size=layer_data['output_size']
# )
# layer.weights = layer_data['weights']
# layer.bias = layer_data['bias']
# new_layers.append(layer)
# elif layer_data['type'] == 'ReLU':
# new_layers.append(ReLU())
# elif layer_data['type'] == 'MaxPool2D':
# new_layers.append(MaxPool2D())
# elif layer_data['type'] == 'Flatten':
# new_layers.append(Flatten())
# elif layer_data['type'] == 'Softmax':
# new_layers.append(Softmax())
# model.layers = new_layers
self.load_checkpoint(filepath)
print(f"Model loaded from {filepath}")
# return model
def predict(self, image_path, show_image=True):
"""Predict class for a single image"""
try:
img = cv2.imread(image_path)
if img is None:
print(f"Error: Could not read image from {image_path}")
return None
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
if img.shape != (IMG_SIZE, IMG_SIZE, 3):
print(f"Error: Image shape {img.shape} is not (64, 64, 3)")
return None
X = np.array([img], dtype=np.float32) / 255.0
output = self.forward(X)
pred_class_idx = np.argmax(output, axis=1)[0]
pred_class = CLASSES[pred_class_idx]
confidence = output[0][pred_class_idx]
if show_image:
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title(f"Predicted: {pred_class} ({confidence:.2f})")
plt.axis('off')
plt.show()
return pred_class, confidence
except Exception as e:
print(f"Error during prediction: {str(e)}")
return None