-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn_basic.py
124 lines (101 loc) · 3.87 KB
/
cnn_basic.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
from torch.autograd import Variable
bp_train_dataset = bpdata_train(csv_file='/home/jeyamariajose/Projects/dl/bp_train.csv',
root_dir='/home/jeyamariajose/Projects/dl/data/train')
bp_test_dataset = bpdata_test(csv_file='/home/jeyamariajose/Projects/dl/bp_test.csv',
root_dir='/home/jeyamariajose/Projects/dl/data/test/')
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# Hyper parameers
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, 128,kernel_size=2, stride=1, padding=2),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))
self.layer2 = nn.Sequential(
nn.Conv2d(128, 256, kernel_size=2, stride=1, padding=2),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))
self.fc = nn.Linear(129024, 1)
nn.init.xavier_uniform_(self.fc.weight)
#nn.init.xavier_uniform_(self.layer1.weight)
#nn.init.xavier_uniform_(self.layer2.weight)
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)
for epoch in range(num_epochs):
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 = Variable(label.float())
#print(label)
data = Variable(torch.tensor(data).float())
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')
model.eval()
with torch.no_grad():
correct = 0
total = 0
for i,(data,label) in enumerate(test_loader):
label = label.float()
#print(label)
data = torch.tensor(data).float()
data = data.unsqueeze(0)
#print(data.shape)
outputs = model(data)
outputs = outputs[0]
#print(outputs,label)
outputs = outputs.numpy()
label = label.numpy()
#print('Testing MAE : {} '.format(metrics.mean_absolute_error(label, outputs)))