|
import torch |
|
import torch.nn as nn |
|
import torch.nn.functional as F |
|
import torch.optim as optim |
|
import torchvision |
|
import torchvision.transforms as transforms |
|
import scipy |
|
|
|
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
learning_rate = 0.005 |
|
batch_size = 128 |
|
hidden_size = 300 |
|
num_classes = 10 |
|
num_epochs = 550 |
|
|
|
|
|
|
|
transform = transforms.Compose( |
|
[transforms.Resize(64), transforms.CenterCrop(64), transforms.ToTensor()] |
|
) |
|
|
|
train_dataset = torchvision.datasets.STL10(root='./dataSTL10', split="train", transform=transform, download=True) |
|
test_dataset = torchvision.datasets.STL10(root='./dataSTL10', split="test", transform=transform, download=True) |
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True) |
|
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False) |
|
|
|
|
|
|
|
class CNN(nn.Module): |
|
def __init__(self): |
|
super(CNN, self).__init__() |
|
self.conv1 = nn.Conv2d(3, 32, 5) |
|
self.conv2 = nn.Conv2d(32, 64, 5) |
|
|
|
self.fc1 = nn.Linear(64 * 13 * 13, 128) |
|
self.fc2 = nn.Linear(128, 64) |
|
self.fc3 = nn.Linear(64, num_classes) |
|
|
|
def forward(self, x): |
|
x = F.max_pool2d(F.relu(self.conv1(x)), (2,2)) |
|
x = F.max_pool2d(F.relu(self.conv2(x)), 2) |
|
x = x.view(-1, self.num_flat_features(x)) |
|
x = F.relu(self.fc1(x)) |
|
x = F.relu(self.fc2(x)) |
|
x = self.fc3(x) |
|
return x |
|
|
|
def num_flat_features(self, x): |
|
size = x.size()[1:] |
|
num_features = 1 |
|
for s in size: |
|
num_features *= s |
|
return num_features |
|
|
|
cnn = CNN().to(device) |
|
|
|
criterion = nn.CrossEntropyLoss() |
|
optimizer = optim.Adam(cnn.parameters(), lr=learning_rate) |
|
|
|
|
|
|
|
for epoch in range(num_epochs): |
|
for i, (images, labels) in enumerate(train_loader): |
|
images = images.to(device) |
|
labels = labels.to(device) |
|
|
|
out = cnn(images) |
|
loss = criterion(out, labels) |
|
|
|
optimizer.zero_grad() |
|
loss.backward() |
|
optimizer.step() |
|
|
|
if(i+1) % 1 == 0: |
|
print(f'epoch: {epoch+1}/{num_epochs} step: {i+1}, loss: loss: {loss.item():.4f}') |
|
|
|
|
|
with torch.no_grad(): |
|
n_correct = 0 |
|
n_samples = 0 |
|
for images, labels in test_loader: |
|
images = images.to(device) |
|
labels = labels.to(device) |
|
outputs = cnn(images) |
|
|
|
_, predicted = torch.max(outputs.data, 1) |
|
n_samples += labels.size(0) |
|
n_correct += (predicted == labels).sum().item() |
|
|
|
acc = 100.0 * n_correct / n_samples |
|
print(f'Accuracy of the network on the {n_samples} test images: {acc} %') |
|
|
|
|
|
|
|
torch.save(cnn.state_dict(), "cnn_model.pth") |
|
|
|
|