-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_classification.py
83 lines (68 loc) · 2.96 KB
/
train_classification.py
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
from astrosite_dataset import ClassificationAstrositeDataset, TrackingAstrositeDataset, SpectrogramDataset
from classification_network import LeNet5
import os
import torch
import sys
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
dataset_path = '../filtered_dataset/recordings'
assert os.path.isdir(dataset_path)
target_list = ['50574', '47851', '37951', '39533', '43751', '32711', '27831', '45465',
'46826', '42942'] #, '42741', '41471', '43873', '40982', '41725', '43874',
#'27711', '40892', '50005', '44637']
train_dataset = SpectrogramDataset(dataset_path, split=target_list, test=False)
test_dataset = SpectrogramDataset(dataset_path, split=target_list, test=True)
# Device will determine whether to run the training on GPU or CPU.
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Using ", device)
batch_size = 80
num_classes = len(target_list)
net = LeNet5(num_classes).to(device)
model_filename = "./networks/classification_easy.pt"
learning_rate = 0.001
num_epochs = 10
cost = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)
print(train_dataset[0][0].shape)
print(len(train_dataset))
train_loader = torch.utils.data.DataLoader(dataset = train_dataset,
batch_size = batch_size,
shuffle = True,
num_workers = 4)
test_loader = torch.utils.data.DataLoader(dataset = test_dataset,
batch_size = batch_size,
shuffle = True,
num_workers = 4)
total_step_train = len(train_loader)
total_step_test = len(test_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = images.to(device)
labels = labels.to(device)
#Forward pass
outputs = net(images)
loss = cost(outputs, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 10 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step_train, loss.item()))
sys.stdout.flush()
for j, (images, labels) in enumerate(test_loader):
images = images.to(device)
labels = labels.to(device)
#Forward pass
outputs = net(images)
loss = cost(outputs, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 10 == 0:
print('Test: epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, j+1, total_step_test, loss.item()))
sys.stdout.flush()
torch.save(net.state_dict(), model_filename)