Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
APS360Project/definitions_v3.py at master · itazap/APS360Project · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
itazap
APS360Project
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
APS360Project
/
definitions_v3.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
257 lines (198 loc) · 8.25 KB
master
Breadcrumbs
APS360Project
/
definitions_v3.py
Copy path
Top
File metadata and controls
Code
Blame
257 lines (198 loc) · 8.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 12 12:53:53 2019
@author: ita
"""
#----------------------Imports------------------------------
import
numpy
as
np
import
torch
import
torch
.
nn
as
nn
import
torch
.
nn
.
functional
as
F
from
torch
.
autograd
import
Variable
import
torch
.
utils
.
data
as
data
import
torchvision
from
torchvision
import
transforms
from
torchvision
import
*
import
torch
import
math
import
numpy
as
np
import
matplotlib
.
pyplot
as
plt
import
time
as
t
import
torch
.
optim
as
optim
from
scipy
import
ndimage
as
ndimage
from
PIL
import
Image
,
ImageOps
#--------------------Data Loading and Splitting ---------------------------------
def
get_data_loader
(
batch_size
):
train_path
=
'trainData'
val_path
=
'valData'
#test_path = 'testData'
transform
=
transforms
.
Compose
([
transforms
.
ToTensor
(),
transforms
.
Normalize
((
0.5
,
0.5
,
0.5
), (
0.5
,
0.5
,
0.5
))])
trainSet
=
torchvision
.
datasets
.
ImageFolder
(
root
=
train_path
,
transform
=
transform
)
train_data_loader
=
torch
.
utils
.
data
.
DataLoader
(
trainSet
,
batch_size
=
batch_size
,
shuffle
=
True
)
valSet
=
torchvision
.
datasets
.
ImageFolder
(
root
=
val_path
,
transform
=
transform
)
val_data_loader
=
torch
.
utils
.
data
.
DataLoader
(
valSet
,
batch_size
=
batch_size
,
shuffle
=
True
)
# testSet = torchvision.datasets.ImageFolder(root=test_path, transform=transform)
# test_data_loader = torch.utils.data.DataLoader(testSet, batch_size=batch_size, shuffle=True)
return
train_data_loader
,
val_data_loader
#,test_data_loader
#--------------------Base Model----------------------------------------------------
class
BaseModel
(
nn
.
Module
):
def
__init__
(
self
,
input_size
):
super
(
BaseModel
,
self
).
__init__
()
self
.
name
=
"Base"
self
.
input_size
=
((
input_size
-
2
)
/
2
)
self
.
conv1
=
nn
.
Conv2d
(
1
,
5
,
3
)
self
.
conv2
=
nn
.
Conv2d
(
5
,
7
,
5
)
self
.
pool
=
nn
.
MaxPool2d
(
2
,
2
)
self
.
fc1
=
nn
.
Linear
(
int
(
7
*
147
*
147
),
1000
)
self
.
fc2
=
nn
.
Linear
(
1000
,
2
)
def
forward
(
self
,
x
):
x
=
self
.
pool
(
F
.
relu
(
self
.
conv1
(
x
)))
x
=
self
.
pool
(
F
.
relu
(
self
.
conv2
(
x
)))
x
=
x
.
view
(
-
1
,
int
(
7
*
147
*
147
) )
x
=
self
.
fc1
(
x
)
x
=
self
.
fc2
(
x
)
x
=
x
.
squeeze
(
1
)
# Flatten to [batch_size]
return
x
#-------------------Filter (HP)----------------------------------------
def
HPFilter
(
img
):
weights
=
torch
.
tensor
([[[
-
1.
,
2.
,
-
2.
,
2.
,
-
1.
],
[
2.
,
-
6.
,
8.
,
-
6.
,
2.
],
[
-
2.
,
8.
,
-
12.
,
8.
,
-
2.
],
[
2.
,
-
6.
,
8.
,
-
6.
,
2.
],
[
-
1.
,
2.
,
-
2.
,
2.
,
1.
]],
[[
-
1.
,
2.
,
-
2.
,
2.
,
-
1.
],
[
2.
,
-
6.
,
8.
,
-
6.
,
2.
],
[
-
2.
,
8.
,
-
12.
,
8.
,
-
2.
],
[
2.
,
-
6.
,
8.
,
-
6.
,
2.
],
[
-
1.
,
2.
,
-
2.
,
2.
,
1.
]],
[[
-
1.
,
2.
,
-
2.
,
2.
,
-
1.
],
[
2.
,
-
6.
,
8.
,
-
6.
,
2.
],
[
-
2.
,
8.
,
-
12.
,
8.
,
-
2.
],
[
2.
,
-
6.
,
8.
,
-
6.
,
2.
],
[
-
1.
,
2.
,
-
2.
,
2.
,
1.
]]])
weights
=
1
/
12
*
weights
weights
=
weights
.
unsqueeze
(
dim
=
0
).
cuda
()
filteredimgs
=
F
.
conv2d
(
img
,
weights
,
padding
=
2
).
cuda
()
return
filteredimgs
def
HPFilter2
(
img
):
weights
=
torch
.
tensor
([[
-
1.
,
2.
,
-
2.
,
2.
,
-
1.
],
[
2.
,
-
6.
,
8.
,
-
6.
,
2.
],
[
-
2.
,
8.
,
-
12.
,
8.
,
-
2.
],
[
2.
,
-
6.
,
8.
,
-
6.
,
2.
],
[
-
1.
,
2.
,
-
2.
,
2.
,
1.
]])
filteredimgs
=
[]
for
im
in
img
:
im
=
np
.
transpose
(
im
,[
1
,
2
,
0
])
im
=
im
/
2
+
0.5
im
=
im
.
squeeze
()
result
=
ndimage
.
convolve
(
im
,
np
.
atleast_3d
(
weights
))
result
=
torch
.
from_numpy
(
result
)
result
=
np
.
transpose
(
result
,[
2
,
0
,
1
])
filteredimgs
.
append
(
result
)
filteredimgs
=
torch
.
stack
(
filteredimgs
)
return
filteredimgs
.
cuda
()
def
LPFilter
(
img
):
weights2_m1
=
np
.
array
([[
0.
,
0.
,
0.
,
0.
,
0.
],
[
0.
,
-
1.
,
2.
,
-
1.
,
0.
],
[
0.
,
2.
,
-
4.
,
2.
,
0
],
[
0.
,
-
1.
,
2.
,
-
1.
,
0.
],
[
0.
,
0.
,
0.
,
0.
,
0.
]])
weights2_m1
=
1
/
4
*
weights2_m1
weights2_m2
=
np
.
array
([[
-
1.
,
2.
,
-
2.
,
2.
,
-
1
],
[
2.
,
-
6.
,
8.
,
-
6.
,
2.
],
[
-
2.
,
8.
,
-
12.
,
8.
,
-
2.
],
[
2.
,
-
6.
,
8.
,
-
6.
,
2.
],
[
-
1.
,
2.
,
-
2.
,
2.
,
-
1
],])
weights2_m2
=
1
/
12
*
weights2_m2
weights2_m3
=
np
.
array
([[
0.
,
0.
,
0.
,
0.
,
0.
],
[
0.
,
0.
,
0.
,
0.
,
0.
],
[
0.
,
1.
,
-
2.
,
1.
,
0
],
[
0.
,
0.
,
0.
,
0.
,
0.
],
[
0.
,
0.
,
0.
,
0.
,
0.
]])
weights2_m3
=
1
/
2
*
weights2_m3
weights
=
np
.
dot
(
weights2_m1
,
weights2_m2
,
weights2_m3
)
weights
=
torch
.
from_numpy
(
weights
)
weights3
=
[
weights
,
weights
,
weights
]
weights3
=
torch
.
stack
(
weights3
).
float
()
weights3
=
weights3
.
unsqueeze
(
dim
=
0
).
cuda
()
filteredimgs
=
F
.
conv2d
(
img
,
weights3
,
padding
=
2
).
cuda
()
return
filteredimgs
#-------------------Train Loop (Ft. Get Accuracy & Plotting)----------------------------------------
def
get_accuracy
(
mdl
,
set_
,
batch_size
):
mdl
.
cuda
()
batch_size
=
16
label_
=
[
0
]
*
(
batch_size
*
2
)
for
i
in
range
(
batch_size
,
batch_size
*
2
):
label_
[
i
]
=
1
label
=
torch
.
tensor
(
label_
).
cuda
()
trainSet_
,
valSet_
=
get_data_loader
(
batch_size
)
if
set_
==
"train"
:
data_
=
trainSet_
elif
set_
==
"val"
:
data_
=
valSet_
correct
=
0
total
=
0
for
img
,
batch
in
data_
:
img
,
batch
=
img
.
cuda
(),
batch
.
cuda
()
if
(
len
(
batch
)
==
batch_size
):
b
=
torch
.
split
(
img
,
600
,
dim
=
3
)
img
=
torch
.
cat
(
b
,
0
)
filteredimgs
=
LPFilter
(
img
)
output
=
mdl
(
filteredimgs
).
cuda
()
# output = mdl(img).cuda()
pred
=
output
.
max
(
1
,
keepdim
=
True
)[
1
]
# get the index of the max log-probability
correct
+=
pred
.
eq
(
label
.
view_as
(
pred
)).
sum
().
item
()
#compute how many predictions were correct
total
+=
img
.
shape
[
0
]
#get the total ammount of predictions
return
correct
/
total
def
train
(
mdl
,
epochs
=
20
,
batch_size
=
32
,
learning_rate
=
0.0001
):
mdl
.
cuda
()
criterion
=
nn
.
CrossEntropyLoss
()
optimizer
=
optim
.
SGD
(
mdl
.
parameters
(),
lr
=
learning_rate
,
momentum
=
0.9
)
trainSet
,
valSet
=
get_data_loader
(
batch_size
)
train_acc
,
val_acc
=
[], []
n
=
0
# the number of iterations
label_
=
[
0
]
*
(
batch_size
*
2
)
for
i
in
range
(
batch_size
,
batch_size
*
2
):
label_
[
i
]
=
1
label
=
torch
.
tensor
(
label_
).
cuda
()
print
(
"--------------Starting--------------"
)
for
epoch
in
range
(
epochs
):
# loop over the dataset multiple times
t1
=
t
.
time
()
itera
=
0
filteredimg
=
[]
for
img
,
batch
in
iter
(
trainSet
):
if
(
len
(
batch
)
!=
batch_size
):
break
img
,
batch
=
img
.
cuda
(),
batch
.
cuda
()
b
=
torch
.
split
(
img
,
600
,
dim
=
3
)
img
=
torch
.
cat
(
b
,
0
)
# print(label)
itera
+=
batch_size
*
2
filteredimgs
=
LPFilter
(
img
).
cuda
()
out
=
mdl
(
filteredimgs
).
cuda
()
loss
=
criterion
(
out
,
label
)
loss
.
backward
()
optimizer
.
step
()
optimizer
.
zero_grad
()
# print(itera)
# Calculate the statistics
train_acc
.
append
(
get_accuracy
(
mdl
,
"train"
,
batch_size
))
val_acc
.
append
(
get_accuracy
(
mdl
,
"val"
,
batch_size
))
# compute validation accuracy
n
+=
1
print
(
"Epoch"
,
n
,
"Done in:"
,
t
.
time
()
-
t1
,
"With Training Accuracy:"
,
train_acc
[
-
1
],
"And Validation Accuracy:"
,
val_acc
[
-
1
])
# Save the current model (checkpoint) to a file
model_path
=
"model_{0}_bs{1}_lr{2}_epoch{3}"
.
format
(
mdl
.
name
,
batch_size
,
learning_rate
,
epoch
)
torch
.
save
(
mdl
.
state_dict
(),
model_path
)
iterations
=
list
(
range
(
1
,
epochs
+
1
))
print
(
"--------------Finished--------------"
)
return
iterations
,
train_acc
,
val_acc
def
plot
(
iterations
,
train_acc
,
val_acc
):
plt
.
title
(
"Training Curve"
)
plt
.
plot
(
iterations
,
train_acc
,
label
=
"Train"
)
plt
.
plot
(
iterations
,
val_acc
,
label
=
"Validation"
)
plt
.
xlabel
(
"Epochs"
)
plt
.
ylabel
(
"Training Accuracy"
)
plt
.
legend
(
loc
=
'best'
)
plt
.
show
()
print
(
"Final Training Accuracy: {}"
.
format
(
train_acc
[
-
1
]))
print
(
"Final Validation Accuracy: {}"
.
format
(
val_acc
[
-
1
]))
You can’t perform that action at this time.