Repository navigation
Expand file tree
/
Copy pathimageprocessingpartb.py
More file actions
251 lines (199 loc) · 8.42 KB
/
Copy pathimageprocessingpartb.py
File metadata and controls
251 lines (199 loc) · 8.42 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
# -*- coding: utf-8 -*-
"""Untitled5.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1n5VEhHtwd393zTKDNIG-BzWPqVXQPWwh
"""
import torch
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
# Load the MNIST dataset
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
trainset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform)
testset = torchvision.datasets.MNIST(root='./data', train=False, download=True, transform=transform)
# Visualize a sample from each class
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
classes = [str(i) for i in range(10)]
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
# get some random training images
dataiter = iter(trainloader)
images, labels = next(dataiter)
# show images
imshow(torchvision.utils.make_grid(images[:10]))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(10)))
from sklearn.model_selection import train_test_split
train_data, val_data = train_test_split(trainset, test_size=0.2, random_state=42)
trainloader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True)
valloader = torch.utils.data.DataLoader(val_data, batch_size=64, shuffle=False)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False)
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F # Import F for functional operations
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
self.fc1 = nn.Linear(64 * 7 * 7, 128)
self.fc2 = nn.Linear(128, 10)
self.dropout = nn.Dropout(0.25)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 64 * 7 * 7)
x = F.relu(self.fc1(x))
x = self.dropout(x)
x = self.fc2(x)
return x
# Instantiate the model, define the loss function and the optimizer
model = CNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
def train_model(model, trainloader, valloader, criterion, optimizer, num_epochs=25):
train_losses, val_losses = [], []
best_val_loss = float('inf')
best_model_wts = None
for epoch in range(num_epochs):
model.train()
running_loss = 0.0
for inputs, labels in trainloader:
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0)
epoch_loss = running_loss / len(trainloader.dataset)
train_losses.append(epoch_loss)
model.eval()
val_loss = 0.0
with torch.no_grad():
for inputs, labels in valloader:
outputs = model(inputs)
loss = criterion(outputs, labels)
val_loss += loss.item() * inputs.size(0)
epoch_val_loss = val_loss / len(valloader.dataset)
val_losses.append(epoch_val_loss)
if epoch_val_loss < best_val_loss:
best_val_loss = epoch_val_loss
best_model_wts = model.state_dict()
print(f'Epoch {epoch+1}/{num_epochs}, Training Loss: {epoch_loss:.4f}, Validation Loss: {epoch_val_loss:.4f}')
model.load_state_dict(best_model_wts)
return model, train_losses, val_losses
model, train_losses, val_losses = train_model(model, trainloader, valloader, criterion, optimizer, num_epochs=25)
# Plot training and validation loss
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 5))
plt.plot(train_losses, label='Training Loss')
plt.plot(val_losses, label='Validation Loss')
plt.legend()
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Training and Validation Loss')
plt.show()
from sklearn.metrics import confusion_matrix, accuracy_score
import seaborn as sns
def evaluate_model(model, testloader):
model.eval()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in testloader:
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
all_preds.extend(preds.numpy())
all_labels.extend(labels.numpy())
accuracy = accuracy_score(all_labels, all_preds)
cm = confusion_matrix(all_labels, all_preds)
return accuracy, cm
accuracy, cm = evaluate_model(model, testloader)
print(f'Test Accuracy: {accuracy:.4f}')
# Plot confusion matrix
plt.figure(figsize=(10, 7))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=classes, yticklabels=classes)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()
percentages = [5, 10, 50, 100]
for pct in percentages:
subset_size = int(len(train_data) * pct / 100)
subset, _ = torch.utils.data.random_split(train_data, [subset_size, len(train_data) - subset_size])
trainloader = torch.utils.data.DataLoader(subset, batch_size=64, shuffle=True)
model = CNN()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
model, train_losses, val_losses = train_model(model, trainloader, valloader, criterion, optimizer, num_epochs=25)
accuracy, cm = evaluate_model(model, testloader)
print(f'Training with {pct}% of data, Test Accuracy: {accuracy:.4f}')
plt.figure(figsize=(10, 5))
plt.plot(train_losses, label='Training Loss')
plt.plot(val_losses, label='Validation Loss')
plt.legend()
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title(f'Training and Validation Loss ({pct}% of data)')
plt.show()
from skimage.feature import hog
# Function to extract HoG features
def extract_hog_features(images):
hog_features = []
for img in images:
feature = hog(img.numpy().squeeze(), pixels_per_cell=(4, 4), cells_per_block=(2, 2), visualize=False)
hog_features.append(feature)
return np.array(hog_features)
# Extract features for train and test sets
train_features = extract_hog_features([img for img, _ in trainset])
test_features = extract_hog_features([img for img, _ in testset])
train_labels = [label for _, label in trainset]
test_labels = [label for _, label in testset]
from sklearn.model_selection import train_test_split
train_features, val_features, train_labels, val_labels = train_test_split(
train_features, train_labels, test_size=0.2, random_state=42
)
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, confusion_matrix
import seaborn as sns
# Train a classifier (e.g., SVM)
classifier = SVC(kernel='linear')
classifier.fit(train_features, train_labels)
# Evaluate the classifier on the validation set
val_predictions = classifier.predict(val_features)
val_accuracy = accuracy_score(val_labels, val_predictions)
print(f'Validation Accuracy: {val_accuracy:.4f}')
# Evaluate the classifier on the test set
test_predictions = classifier.predict(test_features)
test_accuracy = accuracy_score(test_labels, test_predictions)
print(f'Test Accuracy: {test_accuracy:.4f}')
# Confusion matrix
cm = confusion_matrix(test_labels, test_predictions)
plt.figure(figsize=(10, 7))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=classes, yticklabels=classes)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix with HoG Features')
plt.show()
percentages = [5, 10, 50, 100]
for pct in percentages:
subset_size = int(len(train_features) * pct / 100)
subset_features = train_features[:subset_size]
subset_labels = train_labels[:subset_size]
classifier = SVC(kernel='linear')
classifier.fit(subset_features, subset_labels)
test_predictions = classifier.predict(test_features)
test_accuracy = accuracy_score(test_labels, test_predictions)
print(f'Training with {pct}% of data, Test Accuracy: {test_accuracy:.4f}')
cm = confusion_matrix(test_labels, test_predictions)
plt.figure(figsize=(10, 7))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=classes, yticklabels=classes)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title(f'Confusion Matrix with HoG Features ({pct}% of data)')
plt.show()