-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn_train.py
103 lines (84 loc) · 3.24 KB
/
cnn_train.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from custom_data import bpdata_train,bpdata_test
import numpy as np
from sklearn.metrics import mean_squared_error, r2_score
from sklearn import metrics
bp_train_dataset = bpdata_train(csv_file='/home/jeyamariajose/Projects/dl/bp_train_new.csv',
root_dir='/home/jeyamariajose/Projects/dl/data/cleaned/train')
bp_test_dataset = bpdata_test(csv_file='/home/jeyamariajose/Projects/dl/bp_test_new.csv',
root_dir='/home/jeyamariajose/Projects/dl/data/cleaned/test/')
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# Hyper parameters
num_epochs = 20
num_classes = 1
batch_size = 1
learning_rate = 0.001
train_loader = torch.utils.data.DataLoader(dataset=bp_train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=bp_test_dataset,
batch_size=batch_size,
shuffle=False)
class ConvNet(nn.Module):
def __init__(self, num_classes=1):
super(ConvNet, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 16,kernel_size=1, stride=1, padding=2),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(kernel_size=1, stride=2))
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=1, stride=1, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=1, stride=2))
self.fc = nn.Linear(32384, 1)
def forward(self, x):
out = self.layer1(x)
#print(out.shape)
out = self.layer2(out)
#print(out.shape)
out = out.reshape(out.size(0), -1)
#print(out.shape)
out = self.fc(out)
return out
model = ConvNet(num_classes).to(device)
min_loss = 1000
# Loss and optimizer
criterion = torch.nn.MSELoss(size_average = False)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train the model
total_step = len(train_loader)
print(model)
for epoch in range(num_epochs):
break
for i,(data,label) in enumerate(train_loader):
#print(i)
#print(data)
# data = data.to(device)
# label = label.to(device)
# Forward pass
# data = np.array(data))
label = label.float()
#print(label)
data = torch.tensor(data).float()
data = data.unsqueeze(0)
data = data.unsqueeze(0)
#print(data.shape)
outputs = model(data)
outputs = outputs[0]
loss = criterion(outputs, label)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
torch.save(model.state_dict(), 'model%d.ckpt'%epoch)
if loss<min_loss:
min_loss = loss
torch.save(model.state_dict(), 'model.ckpt')