Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
visual-object-recognition
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Rolf Sievert
visual-object-recognition
Commits
7a79b40c
Commit
7a79b40c
authored
5 years ago
by
Rolf Sievert
Browse files
Options
Downloads
Patches
Plain Diff
Added validation
parent
2b45fcf5
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
python/main.py
+87
-45
87 additions, 45 deletions
python/main.py
python/test.py
+2
-2
2 additions, 2 deletions
python/test.py
python/train.py
+29
-3
29 additions, 3 deletions
python/train.py
with
118 additions
and
50 deletions
python/main.py
+
87
−
45
View file @
7a79b40c
import
os
import
os
import
glob
import
glob
from
math
import
sqrt
from
math
import
sqrt
...
@@ -13,6 +12,10 @@ from train import train
...
@@ -13,6 +12,10 @@ from train import train
from
test
import
test
from
test
import
test
import
numpy
as
np
import
numpy
as
np
from
constants
import
MEAN_MAP
,
STD_MAP
from
constants
import
MEAN_MAP
,
STD_MAP
from
torch.utils.data.sampler
import
SubsetRandomSampler
# Set local working directory
os
.
chdir
(
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
)))
##################################
##################################
## RESULTS ##
## RESULTS ##
...
@@ -59,6 +62,8 @@ LEARNING_RATE = 0.001 # DEAFULT IS 0.001
...
@@ -59,6 +62,8 @@ LEARNING_RATE = 0.001 # DEAFULT IS 0.001
## PREPARE DATA ##
## PREPARE DATA ##
##################################
##################################
NUM_EPOCHS
=
30
TRAIN_LOGGING
=
False
CHANNELS
=
1
if
SPACE
==
"
L
"
else
3
CHANNELS
=
1
if
SPACE
==
"
L
"
else
3
MEAN
=
MEAN_MAP
[
SPACE
]
MEAN
=
MEAN_MAP
[
SPACE
]
STD
=
MEAN_MAP
[
SPACE
]
STD
=
MEAN_MAP
[
SPACE
]
...
@@ -72,61 +77,98 @@ transform_comp = transforms.Compose([
...
@@ -72,61 +77,98 @@ transform_comp = transforms.Compose([
transforms
.
Normalize
(
MEAN
,
STD
),
transforms
.
Normalize
(
MEAN
,
STD
),
])
])
trainset
=
torchvision
.
datasets
.
CIFAR10
(
root
=
'
./data
'
,
NUM_WORKERS
=
2
train
=
True
,
# If running on windows, change to 0 workers (does not work otherwise)
if
os
.
name
==
'
nt
'
:
NUM_WORKERS
=
0
def
sampleSplit
(
dataset
,
split
):
"""
Splits indexes of dataset.
"""
dataset_size
=
len
(
dataset
)
indices
=
list
(
range
(
dataset_size
))
split
=
int
(
np
.
floor
(
split
*
dataset_size
))
# Shuffle indices
np
.
random
.
seed
(
123
)
np
.
random
.
shuffle
(
indices
)
train_indices
,
val_indices
=
indices
[:
split
],
indices
[
split
:]
# Creating PT data samplers and loaders:
train_sampler
=
SubsetRandomSampler
(
train_indices
)
valid_sampler
=
SubsetRandomSampler
(
val_indices
)
return
train_sampler
,
valid_sampler
if
__name__
==
'
__main__
'
:
trainvalset
=
torchvision
.
datasets
.
CIFAR10
(
root
=
'
./data
'
,
train
=
True
,
download
=
True
,
transform
=
transform_comp
)
# Get random indices for train and validation data
train_sampler
,
val_sampler
=
sampleSplit
(
trainvalset
,
0.75
)
trainloader
=
torch
.
utils
.
data
.
DataLoader
(
trainvalset
,
batch_size
=
256
,
sampler
=
train_sampler
,
num_workers
=
NUM_WORKERS
)
valloader
=
torch
.
utils
.
data
.
DataLoader
(
trainvalset
,
batch_size
=
256
,
sampler
=
val_sampler
,
num_workers
=
NUM_WORKERS
)
testset
=
torchvision
.
datasets
.
CIFAR10
(
root
=
'
./data
'
,
train
=
False
,
download
=
True
,
download
=
True
,
transform
=
transform_comp
)
transform
=
transform_comp
)
train
loader
=
torch
.
utils
.
data
.
DataLoader
(
t
rain
set
,
test
loader
=
torch
.
utils
.
data
.
DataLoader
(
t
est
set
,
batch_size
=
256
,
batch_size
=
128
,
shuffle
=
Tru
e
,
shuffle
=
Fals
e
,
num_workers
=
2
)
num_workers
=
NUM_WORKERS
)
testset
=
torchvision
.
datasets
.
CIFAR10
(
root
=
'
./data
'
,
##################################
train
=
False
,
## PREPARE NETWORK ##
download
=
True
,
##################################
transform
=
transform_comp
)
testloader
=
torch
.
utils
.
data
.
DataLoader
(
testset
,
use_cuda
=
torch
.
cuda
.
is_available
()
batch_size
=
128
,
if
use_cuda
:
shuffle
=
False
,
print
(
"
Cuda support enabled.
"
)
num_workers
=
2
)
else
:
##################################
print
(
"
Cuda support DISABLED.
"
)
## PREPARE NETWORK ##
##################################
use_cuda
=
torch
.
cuda
.
is_available
()
classes
=
(
'
plane
'
,
'
car
'
,
'
bird
'
,
'
cat
'
,
'
deer
'
,
'
dog
'
,
'
frog
'
,
'
horse
'
,
'
ship
'
,
'
truck
'
)
classes
=
(
'
plane
'
,
'
car
'
,
'
bird
'
,
'
cat
'
,
'
deer
'
,
'
dog
'
,
'
frog
'
,
'
horse
'
,
#model = cvlNet()
'
ship
'
,
'
truck
'
)
model
=
goalNet
(
CHANNELS
)
#model = cvlNet()
if
use_cuda
:
model
=
goalNet
(
CHANNELS
)
model
.
cuda
()
cudnn
.
benchmark
=
True
if
use_cuda
:
objective
=
nn
.
CrossEntropyLoss
()
model
.
cuda
()
cudnn
.
benchmark
=
True
objective
=
nn
.
CrossEntropyLoss
(
)
optimizer
=
torch
.
optim
.
Adam
(
model
.
parameters
(),
lr
=
LEARNING_RATE
)
optimizer
=
torch
.
optim
.
Adam
(
model
.
parameters
(),
lr
=
LEARNING_RATE
)
##################################
## TRAIN NETWORK ##
##################################
##################################
model
,
loss_log
,
acc_log
=
train
(
model
,
## TRAIN NETWORK ##
trainloader
,
##################################
valloader
,
optimizer
,
start_epoch
=
1
objective
,
num_epochs
=
50
use_cuda
,
model
,
loss_log
,
acc_log
=
train
(
model
,
start_epoch
=
1
,
trainloader
,
num_epochs
=
NUM_EPOCHS
,
optimizer
,
log
=
TRAIN_LOGGING
)
objective
,
use_cuda
,
start_epoch
,
num_epochs
=
num_epochs
)
##################################
##################################
## TEST NETWORK ##
## TEST NETWORK ##
##################################
##################################
test_acc
=
test
(
model
,
testloader
,
use_cuda
)
test_acc
=
test
(
model
,
testloader
,
use_cuda
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
python/test.py
+
2
−
2
View file @
7a79b40c
...
@@ -5,7 +5,6 @@ from torch.autograd import Variable
...
@@ -5,7 +5,6 @@ from torch.autograd import Variable
def
test
(
model
,
testloader
,
use_cuda
):
def
test
(
model
,
testloader
,
use_cuda
):
correct
=
0
correct
=
0
total
=
0
total
=
0
for
idx
,
(
inputs
,
targets
)
in
enumerate
(
testloader
):
for
idx
,
(
inputs
,
targets
)
in
enumerate
(
testloader
):
...
@@ -14,7 +13,8 @@ def test(model, testloader, use_cuda):
...
@@ -14,7 +13,8 @@ def test(model, testloader, use_cuda):
if
use_cuda
:
if
use_cuda
:
inputs
,
targets
=
inputs
.
cuda
(),
targets
.
cuda
()
inputs
,
targets
=
inputs
.
cuda
(),
targets
.
cuda
()
inputs
=
torch
.
tensor
(
inputs
,
requires_grad
=
False
)
#inputs = torch.tensor(inputs, requires_grad=False)
inputs
=
inputs
.
clone
().
detach
().
requires_grad_
(
False
)
# Feed-forward the network
# Feed-forward the network
outputs
=
model
(
inputs
)
outputs
=
model
(
inputs
)
...
...
This diff is collapsed.
Click to expand it.
python/train.py
+
29
−
3
View file @
7a79b40c
...
@@ -11,7 +11,28 @@ import os
...
@@ -11,7 +11,28 @@ import os
#- Backpropagate the loss until the first layer.
#- Backpropagate the loss until the first layer.
#- Update the weights for different convolution layers based on the gradients.
#- Update the weights for different convolution layers based on the gradients.
def
train
(
model
,
trainloader
,
optimizer
,
objective
,
use_cuda
,
start_epoch
,
num_epochs
):
def
validate
(
model
,
valloader
,
use_cuda
,
epoch
):
correct
=
0
total
=
0
for
idx
,
(
inputs
,
targets
)
in
enumerate
(
valloader
):
# Move data to GPU if CUDA is available
if
use_cuda
:
inputs
,
targets
=
inputs
.
cuda
(),
targets
.
cuda
()
#inputs = torch.tensor(inputs, requires_grad=False)
inputs
=
inputs
.
clone
().
detach
().
requires_grad_
(
False
)
# Feed-forward the network
outputs
=
model
(
inputs
)
_
,
predicted
=
torch
.
max
(
outputs
.
data
,
1
)
total
+=
targets
.
size
(
0
)
correct
+=
(
predicted
==
targets
).
sum
().
item
()
print
(
'
Validation accuracy after epoch %d: %d %%
'
%
(
epoch
,
100
*
correct
/
total
))
def
train
(
model
,
trainloader
,
valloader
,
optimizer
,
objective
,
use_cuda
,
start_epoch
,
num_epochs
,
log
=
True
):
loss_log
=
[]
loss_log
=
[]
acc_log
=
[]
acc_log
=
[]
...
@@ -33,7 +54,8 @@ def train(model, trainloader, optimizer, objective, use_cuda, start_epoch, num_e
...
@@ -33,7 +54,8 @@ def train(model, trainloader, optimizer, objective, use_cuda, start_epoch, num_e
if
use_cuda
:
if
use_cuda
:
inputs
,
targets
=
inputs
.
cuda
(),
targets
.
cuda
()
inputs
,
targets
=
inputs
.
cuda
(),
targets
.
cuda
()
inputs
,
targets
=
torch
.
tensor
(
inputs
,
requires_grad
=
True
),
torch
.
tensor
(
targets
).
long
()
#inputs, targets = torch.tensor(inputs, requires_grad=True), torch.tensor(targets).long()
inputs
,
targets
=
inputs
.
clone
().
detach
().
requires_grad_
(
True
),
targets
.
clone
().
detach
()
# Clear the gradients of all variables
# Clear the gradients of all variables
optimizer
.
zero_grad
()
optimizer
.
zero_grad
()
...
@@ -60,7 +82,8 @@ def train(model, trainloader, optimizer, objective, use_cuda, start_epoch, num_e
...
@@ -60,7 +82,8 @@ def train(model, trainloader, optimizer, objective, use_cuda, start_epoch, num_e
total
+=
targets
.
size
(
0
)
total
+=
targets
.
size
(
0
)
correct
+=
(
predicted
==
targets
).
sum
().
item
()
correct
+=
(
predicted
==
targets
).
sum
().
item
()
print
(
'
Loss: {:.8f} | Acc: {:.2f}% ({}/{})
'
.
format
((
train_loss
/
(
batch_idx
+
1
)),
100.
*
correct
/
total
,
correct
,
total
))
if
log
:
print
(
'
Loss: {:.8f} | Acc: {:.2f}% ({}/{})
'
.
format
((
train_loss
/
(
batch_idx
+
1
)),
100.
*
correct
/
total
,
correct
,
total
))
loss_log
.
append
((
train_loss
/
(
batch_idx
+
1
)))
loss_log
.
append
((
train_loss
/
(
batch_idx
+
1
)))
acc_log
.
append
(
100.
*
correct
/
total
)
acc_log
.
append
(
100.
*
correct
/
total
)
...
@@ -76,6 +99,9 @@ def train(model, trainloader, optimizer, objective, use_cuda, start_epoch, num_e
...
@@ -76,6 +99,9 @@ def train(model, trainloader, optimizer, objective, use_cuda, start_epoch, num_e
file_path
=
'
./checkpoints/checkpoint_{}.ckpt
'
.
format
(
epoch
)
file_path
=
'
./checkpoints/checkpoint_{}.ckpt
'
.
format
(
epoch
)
torch
.
save
(
state
,
file_path
)
torch
.
save
(
state
,
file_path
)
# Run and print validation
validate
(
model
,
valloader
,
use_cuda
,
epoch
)
# Save the final model
# Save the final model
torch
.
save
(
model
.
state_dict
(),
'
./models/cvlnet_trained_cifar10_final
'
)
torch
.
save
(
model
.
state_dict
(),
'
./models/cvlnet_trained_cifar10_final
'
)
print
(
'
Training Finished!
'
)
print
(
'
Training Finished!
'
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment