Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
V
ViToolkit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package 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
Wu, Jiantao (PG/R - Comp Sci & Elec Eng)
ViToolkit
Commits
bed04831
Commit
bed04831
authored
1 year ago
by
gent
Browse files
Options
Downloads
Patches
Plain Diff
Add DynamicResolution class and enable dynamic resolution option
parent
813e9fb5
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
vitookit/datasets/ffcv_transform.py
+41
-3
41 additions, 3 deletions
vitookit/datasets/ffcv_transform.py
vitookit/evaluation/eval_cls_ffcv.py
+11
-1
11 additions, 1 deletion
vitookit/evaluation/eval_cls_ffcv.py
with
52 additions
and
4 deletions
vitookit/datasets/ffcv_transform.py
+
41
−
3
View file @
bed04831
...
@@ -35,9 +35,49 @@ from torch import nn
...
@@ -35,9 +35,49 @@ from torch import nn
IMAGENET_MEAN
=
np
.
array
([
0.485
,
0.456
,
0.406
])
*
255
IMAGENET_MEAN
=
np
.
array
([
0.485
,
0.456
,
0.406
])
*
255
IMAGENET_STD
=
np
.
array
([
0.229
,
0.224
,
0.225
])
*
255
IMAGENET_STD
=
np
.
array
([
0.229
,
0.224
,
0.225
])
*
255
@gin.configurable
class
DynamicResolution
:
def
__init__
(
self
,
start_ramp
=
gin
.
REQUIRED
,
end_ramp
=
gin
.
REQUIRED
,
min_res
=
160
,
max_res
=
224
,
step
=
32
):
self
.
start_ramp
=
start_ramp
self
.
end_ramp
=
end_ramp
self
.
min_res
=
min_res
self
.
max_res
=
max_res
self
.
step
=
step
assert
min_res
<=
max_res
def
get_resolution
(
self
,
epoch
):
if
epoch
<=
self
.
start_ramp
:
return
self
.
min_res
if
epoch
>=
self
.
end_ramp
:
return
self
.
max_res
# otherwise, linearly interpolate to the nearest multiple of 32
interp
=
np
.
interp
([
epoch
],
[
self
.
start_ramp
,
self
.
end_ramp
],
[
self
.
min_res
,
self
.
max_res
])
final_res
=
int
(
np
.
round
(
interp
[
0
]
/
self
.
step
))
*
self
.
step
return
final_res
def
__call__
(
self
,
loader
,
epoch
,
is_ffcv
=
False
):
img_size
=
self
.
get_resolution
(
epoch
)
print
(
"
resolution = %s
"
%
str
(
img_size
))
if
is_ffcv
:
pipeline
=
loader
.
pipeline_specs
[
'
image
'
]
if
pipeline
.
decoder
.
output_size
[
0
]
!=
img_size
:
k
=
img_size
/
self
.
max_res
pipeline
.
decoder
.
scale
=
(
0.2
*
k
,
1
)
pipeline
.
decoder
.
output_size
=
(
img_size
,
img_size
)
loader
.
generate_code
()
else
:
decoder
=
loader
.
dataset
.
transforms
.
transform
.
transforms
[
0
]
k
=
img_size
/
self
.
max_res
decoder
.
size
=
(
img_size
,
img_size
)
decoder
.
scale
=
(
0.2
*
k
,
1
)
@gin.configurable
@gin.configurable
def
SimplePipeline
(
img_size
=
224
,
scale
=
(
0.2
,
1
),
ratio
=
(
3.0
/
4.0
,
4.0
/
3.0
)
,
blur
=
False
):
def
SimplePipeline
(
img_size
=
224
,
scale
=
(
0.2
,
1
),
ratio
=
(
3.0
/
4.0
,
4.0
/
3.0
)):
image_pipeline
=
[
image_pipeline
=
[
RandomResizedCropRGBImageDecoder
((
img_size
,
img_size
),
scale
=
scale
,
ratio
=
ratio
),
RandomResizedCropRGBImageDecoder
((
img_size
,
img_size
),
scale
=
scale
,
ratio
=
ratio
),
RandomHorizontalFlip
(),
RandomHorizontalFlip
(),
...
@@ -46,8 +86,6 @@ def SimplePipeline(img_size=224,scale=(0.2,1), ratio=(3.0/4.0, 4.0/3.0),blur=Fal
...
@@ -46,8 +86,6 @@ def SimplePipeline(img_size=224,scale=(0.2,1), ratio=(3.0/4.0, 4.0/3.0),blur=Fal
ToDevice
(
torch
.
device
(
'
cuda
'
)),
ToDevice
(
torch
.
device
(
'
cuda
'
)),
ToTorchImage
(),
ToTorchImage
(),
]
]
if
blur
:
image_pipeline
.
append
(
transforms
.
GaussianBlur
(
3
))
label_pipeline
=
[
IntDecoder
(),
ToTensor
(),
ToDevice
(
torch
.
device
(
'
cuda
'
))]
label_pipeline
=
[
IntDecoder
(),
ToTensor
(),
ToDevice
(
torch
.
device
(
'
cuda
'
))]
# Pipeline for each data field
# Pipeline for each data field
pipelines
=
{
pipelines
=
{
...
...
This diff is collapsed.
Click to expand it.
vitookit/evaluation/eval_cls_ffcv.py
+
11
−
1
View file @
bed04831
...
@@ -51,6 +51,7 @@ def get_args_parser():
...
@@ -51,6 +51,7 @@ def get_args_parser():
help
=
'
Accumulate gradient iterations (for increasing the effective batch size under memory constraints)
'
)
help
=
'
Accumulate gradient iterations (for increasing the effective batch size under memory constraints)
'
)
parser
.
add_argument
(
'
--epochs
'
,
default
=
100
,
type
=
int
)
parser
.
add_argument
(
'
--epochs
'
,
default
=
100
,
type
=
int
)
parser
.
add_argument
(
'
--ckpt_freq
'
,
default
=
5
,
type
=
int
)
parser
.
add_argument
(
'
--ckpt_freq
'
,
default
=
5
,
type
=
int
)
parser
.
add_argument
(
"
--dynamic_resolution
"
,
default
=
False
,
action
=
"
store_true
"
,
help
=
"
Use dynamic resolution.
"
)
# Model parameters
# Model parameters
parser
.
add_argument
(
"
--compile
"
,
action
=
'
store_true
'
,
default
=
False
,
help
=
"
compile model with PyTorch 2.0
"
)
parser
.
add_argument
(
"
--compile
"
,
action
=
'
store_true
'
,
default
=
False
,
help
=
"
compile model with PyTorch 2.0
"
)
...
@@ -354,6 +355,14 @@ def main(args):
...
@@ -354,6 +355,14 @@ def main(args):
exit
(
0
)
exit
(
0
)
print
(
f
"
Start training for
{
args
.
epochs
}
epochs from
{
args
.
start_epoch
}
"
)
print
(
f
"
Start training for
{
args
.
epochs
}
epochs from
{
args
.
start_epoch
}
"
)
if
args
.
dynamic_resolution
:
import
torch._dynamo
torch
.
_dynamo
.
config
.
suppress_errors
=
True
dres
=
DynamicResolution
()
else
:
dres
=
None
start_time
=
time
.
time
()
start_time
=
time
.
time
()
max_accuracy
=
0.0
max_accuracy
=
0.0
if
args
.
output_dir
and
misc
.
is_main_process
():
if
args
.
output_dir
and
misc
.
is_main_process
():
...
@@ -364,7 +373,8 @@ def main(args):
...
@@ -364,7 +373,8 @@ def main(args):
pass
pass
for
epoch
in
range
(
args
.
start_epoch
,
args
.
epochs
):
for
epoch
in
range
(
args
.
start_epoch
,
args
.
epochs
):
if
dres
:
dres
(
data_loader_train
,
epoch
,
True
)
train_stats
=
train_one_epoch
(
train_stats
=
train_one_epoch
(
model
,
criterion
,
data_loader_train
,
model
,
criterion
,
data_loader_train
,
optimizer
,
device
,
epoch
,
loss_scaler
,
lr_scheduler
,
optimizer
,
device
,
epoch
,
loss_scaler
,
lr_scheduler
,
...
...
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