-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwendy_test.py
201 lines (152 loc) · 5.35 KB
/
wendy_test.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
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
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
import os
#from torchsummary import summary
import pandas as pd
import numpy as np
from skimage.io import imread, imsave
from tqdm import tqdm
import re
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from skimage.transform import rotate
from skimage.util import random_noise
from skimage.filters import gaussian
from scipy import ndimage
import torch
from torch.autograd import Variable
from torch.nn import Linear, ReLU, CrossEntropyLoss, Sequential, Conv2d, MaxPool2d, Module, Softmax, BatchNorm2d, Dropout
from torch.optim import Adam, SGD
train_data=pd.read_csv("train.csv")
test_data=pd.read_csv("test.csv")
print(train_data.columns)
label_0=os.listdir("0_img")
label_1=os.listdir("1_img")
label_2=os.listdir("2_img")
more1=len(label_0)-len(label_1)
more2=len(label_0)-len(label_2)
train_data.head()
train_img = []
for example_path in tqdm(train_data['example_path']):
image_path = example_path
img = imread(image_path)
img = img/255
train_img.append(img)
train_x = np.array(train_img)
train_y = train_data['label'].values
train_x.shape, train_y.shape
train_x, val_x, train_y, val_y = train_test_split(train_x, train_y, test_size = 0.2, random_state = 13, stratify=train_y)
(train_x.shape, train_y.shape), (val_x.shape, val_y.shape)
# When you have all the agumented images
# you need to modify the input final_train_data
final_train_data = []
final_target_train = []
len(final_target_train), len(final_train_data)
final_train = np.array(final_train_data)
final_target_train = np.array(final_target_train)
# converting training images into torch format
final_train = final_train.reshape(7405, 3, 224, 224)
final_train = torch.from_numpy(final_train)
final_train = final_train.float()
# converting the target into torch format
final_target_train = final_target_train.astype(int)
final_target_train = torch.from_numpy(final_target_train)
# converting validation images into torch format
val_x = val_x.reshape(165, 3, 224, 224)
val_x = torch.from_numpy(val_x)
val_x = val_x.float()
# converting the target into torch format
val_y = val_y.astype(int)
val_y = torch.from_numpy(val_y)
torch.manual_seed(0)
class Net(Module):
def __init__(self):
super(Net, self).__init__()
self.cnn_layers = Sequential(
# Defining a 2D convolution layer
Conv2d(3, 32, kernel_size=3, stride=1, padding=1),
ReLU(inplace=True),
# adding batch normalization
BatchNorm2d(32),
MaxPool2d(kernel_size=2, stride=2),
# adding dropout
Dropout(p=0.25),
# Defining another 2D convolution layer
Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
ReLU(inplace=True),
# adding batch normalization
BatchNorm2d(64),
MaxPool2d(kernel_size=2, stride=2),
# adding dropout
Dropout(p=0.25),
# Defining another 2D convolution layer
Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
ReLU(inplace=True),
# adding batch normalization
BatchNorm2d(128),
MaxPool2d(kernel_size=2, stride=2),
# adding dropout
Dropout(p=0.25),
# Defining another 2D convolution layer
Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
ReLU(inplace=True),
# adding batch normalization
BatchNorm2d(128),
MaxPool2d(kernel_size=2, stride=2),
# adding dropout
Dropout(p=0.25),
)
self.linear_layers = Sequential(
Linear(128 * 14 * 14, 512),
ReLU(inplace=True),
Dropout(),
Linear(512, 256),
ReLU(inplace=True),
Dropout(),
Linear(256,10),
ReLU(inplace=True),
Dropout(),
Linear(10,2)
)
# Defining the forward pass
def forward(self, x):
x = self.cnn_layers(x)
x = x.view(x.size(0), -1)
x = self.linear_layers(x)
return x
# defining the model
model = Net()
# defining the optimizer
optimizer = Adam(model.parameters(), lr=0.000075)
# defining the loss function
criterion = CrossEntropyLoss()
# checking if GPU is available
if torch.cuda.is_available():
model = model.cuda()
criterion = criterion.cuda()
print(model)
torch.manual_seed(0)
# batch size of the model
batch_size = 64
# number of epochs to train the model
n_epochs = 20
for epoch in range(1, n_epochs+1):
train_loss = 0.0
permutation = torch.randperm(final_train.size()[0])
training_loss = []
for i in tqdm(range(0,final_train.size()[0], batch_size)):
indices = permutation[i:i+batch_size]
batch_x, batch_y = final_train[indices], final_target_train[indices]
if torch.cuda.is_available():
batch_x, batch_y = batch_x.cuda(), batch_y.cuda()
optimizer.zero_grad()
outputs = model(batch_x)
loss = criterion(outputs,batch_y)
training_loss.append(loss.item())
loss.backward()
optimizer.step()
training_loss = np.average(training_loss)
print('epoch: \t', epoch, '\t training loss: \t', training_loss)
torch.save(model, 'model.pt')