Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
96d2ce91
Unverified
Commit
96d2ce91
authored
Oct 27, 2023
by
Nicolas Hug
Committed by
GitHub
Oct 27, 2023
Browse files
Fix quantized references (#8073)
parent
a63f98ad
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
13 deletions
+21
-13
references/classification/README.md
references/classification/README.md
+7
-7
references/classification/train_quantization.py
references/classification/train_quantization.py
+14
-6
No files found.
references/classification/README.md
View file @
96d2ce91
...
@@ -289,10 +289,10 @@ For all post training quantized models, the settings are:
...
@@ -289,10 +289,10 @@ For all post training quantized models, the settings are:
2.
num_workers: 16
2.
num_workers: 16
3.
batch_size: 32
3.
batch_size: 32
4.
eval_batch_size: 128
4.
eval_batch_size: 128
5.
backend: 'fbgemm'
5.
q
backend: 'fbgemm'
```
```
python train_quantization.py --device='cpu' --post-training-quantize --backend='fbgemm' --model='$MODEL'
python train_quantization.py --device='cpu' --post-training-quantize --
q
backend='fbgemm' --model='$MODEL'
```
```
Here
`$MODEL`
is one of
`googlenet`
,
`inception_v3`
,
`resnet18`
,
`resnet50`
,
`resnext101_32x8d`
,
`shufflenet_v2_x0_5`
and
`shufflenet_v2_x1_0`
.
Here
`$MODEL`
is one of
`googlenet`
,
`inception_v3`
,
`resnet18`
,
`resnet50`
,
`resnext101_32x8d`
,
`shufflenet_v2_x0_5`
and
`shufflenet_v2_x1_0`
.
...
@@ -301,12 +301,12 @@ Here `$MODEL` is one of `googlenet`, `inception_v3`, `resnet18`, `resnet50`, `re
...
@@ -301,12 +301,12 @@ Here `$MODEL` is one of `googlenet`, `inception_v3`, `resnet18`, `resnet50`, `re
Here are commands that we use to quantize the
`shufflenet_v2_x1_5`
and
`shufflenet_v2_x2_0`
models.
Here are commands that we use to quantize the
`shufflenet_v2_x1_5`
and
`shufflenet_v2_x2_0`
models.
```
```
# For shufflenet_v2_x1_5
# For shufflenet_v2_x1_5
python train_quantization.py --device='cpu' --post-training-quantize --backend='fbgemm' \
python train_quantization.py --device='cpu' --post-training-quantize --
q
backend='fbgemm' \
--model=shufflenet_v2_x1_5 --weights="ShuffleNet_V2_X1_5_Weights.IMAGENET1K_V1" \
--model=shufflenet_v2_x1_5 --weights="ShuffleNet_V2_X1_5_Weights.IMAGENET1K_V1" \
--train-crop-size 176 --val-resize-size 232 --data-path /datasets01_ontap/imagenet_full_size/061417/
--train-crop-size 176 --val-resize-size 232 --data-path /datasets01_ontap/imagenet_full_size/061417/
# For shufflenet_v2_x2_0
# For shufflenet_v2_x2_0
python train_quantization.py --device='cpu' --post-training-quantize --backend='fbgemm' \
python train_quantization.py --device='cpu' --post-training-quantize --
q
backend='fbgemm' \
--model=shufflenet_v2_x2_0 --weights="ShuffleNet_V2_X2_0_Weights.IMAGENET1K_V1" \
--model=shufflenet_v2_x2_0 --weights="ShuffleNet_V2_X2_0_Weights.IMAGENET1K_V1" \
--train-crop-size 176 --val-resize-size 232 --data-path /datasets01_ontap/imagenet_full_size/061417/
--train-crop-size 176 --val-resize-size 232 --data-path /datasets01_ontap/imagenet_full_size/061417/
```
```
...
@@ -317,7 +317,7 @@ For Mobilenet-v2, the model was trained with quantization aware training, the se
...
@@ -317,7 +317,7 @@ For Mobilenet-v2, the model was trained with quantization aware training, the se
1.
num_workers: 16
1.
num_workers: 16
2.
batch_size: 32
2.
batch_size: 32
3.
eval_batch_size: 128
3.
eval_batch_size: 128
4.
backend: 'qnnpack'
4.
q
backend: 'qnnpack'
5.
learning-rate: 0.0001
5.
learning-rate: 0.0001
6.
num_epochs: 90
6.
num_epochs: 90
7.
num_observer_update_epochs:4
7.
num_observer_update_epochs:4
...
@@ -339,7 +339,7 @@ For Mobilenet-v3 Large, the model was trained with quantization aware training,
...
@@ -339,7 +339,7 @@ For Mobilenet-v3 Large, the model was trained with quantization aware training,
1.
num_workers: 16
1.
num_workers: 16
2.
batch_size: 32
2.
batch_size: 32
3.
eval_batch_size: 128
3.
eval_batch_size: 128
4.
backend: 'qnnpack'
4.
q
backend: 'qnnpack'
5.
learning-rate: 0.001
5.
learning-rate: 0.001
6.
num_epochs: 90
6.
num_epochs: 90
7.
num_observer_update_epochs:4
7.
num_observer_update_epochs:4
...
@@ -359,7 +359,7 @@ For post training quant, device is set to CPU. For training, the device is set t
...
@@ -359,7 +359,7 @@ For post training quant, device is set to CPU. For training, the device is set t
### Command to evaluate quantized models using the pre-trained weights:
### Command to evaluate quantized models using the pre-trained weights:
```
```
python train_quantization.py --device='cpu' --test-only --backend='<backend>' --model='<model_name>'
python train_quantization.py --device='cpu' --test-only --
q
backend='<
q
backend>' --model='<model_name>'
```
```
For inception_v3 you need to pass the following extra parameters:
For inception_v3 you need to pass the following extra parameters:
...
...
references/classification/train_quantization.py
View file @
96d2ce91
...
@@ -23,9 +23,9 @@ def main(args):
...
@@ -23,9 +23,9 @@ def main(args):
raise
RuntimeError
(
"Post training quantization example should not be performed on distributed mode"
)
raise
RuntimeError
(
"Post training quantization example should not be performed on distributed mode"
)
# Set backend engine to ensure that quantized model runs on the correct kernels
# Set backend engine to ensure that quantized model runs on the correct kernels
if
args
.
backend
not
in
torch
.
backends
.
quantized
.
supported_engines
:
if
args
.
q
backend
not
in
torch
.
backends
.
quantized
.
supported_engines
:
raise
RuntimeError
(
"Quantized backend not supported: "
+
str
(
args
.
backend
))
raise
RuntimeError
(
"Quantized backend not supported: "
+
str
(
args
.
q
backend
))
torch
.
backends
.
quantized
.
engine
=
args
.
backend
torch
.
backends
.
quantized
.
engine
=
args
.
q
backend
device
=
torch
.
device
(
args
.
device
)
device
=
torch
.
device
(
args
.
device
)
torch
.
backends
.
cudnn
.
benchmark
=
True
torch
.
backends
.
cudnn
.
benchmark
=
True
...
@@ -55,7 +55,7 @@ def main(args):
...
@@ -55,7 +55,7 @@ def main(args):
if
not
(
args
.
test_only
or
args
.
post_training_quantize
):
if
not
(
args
.
test_only
or
args
.
post_training_quantize
):
model
.
fuse_model
(
is_qat
=
True
)
model
.
fuse_model
(
is_qat
=
True
)
model
.
qconfig
=
torch
.
ao
.
quantization
.
get_default_qat_qconfig
(
args
.
backend
)
model
.
qconfig
=
torch
.
ao
.
quantization
.
get_default_qat_qconfig
(
args
.
q
backend
)
torch
.
ao
.
quantization
.
prepare_qat
(
model
,
inplace
=
True
)
torch
.
ao
.
quantization
.
prepare_qat
(
model
,
inplace
=
True
)
if
args
.
distributed
and
args
.
sync_bn
:
if
args
.
distributed
and
args
.
sync_bn
:
...
@@ -89,7 +89,7 @@ def main(args):
...
@@ -89,7 +89,7 @@ def main(args):
)
)
model
.
eval
()
model
.
eval
()
model
.
fuse_model
(
is_qat
=
False
)
model
.
fuse_model
(
is_qat
=
False
)
model
.
qconfig
=
torch
.
ao
.
quantization
.
get_default_qconfig
(
args
.
backend
)
model
.
qconfig
=
torch
.
ao
.
quantization
.
get_default_qconfig
(
args
.
q
backend
)
torch
.
ao
.
quantization
.
prepare
(
model
,
inplace
=
True
)
torch
.
ao
.
quantization
.
prepare
(
model
,
inplace
=
True
)
# Calibrate first
# Calibrate first
print
(
"Calibrating"
)
print
(
"Calibrating"
)
...
@@ -161,7 +161,7 @@ def get_args_parser(add_help=True):
...
@@ -161,7 +161,7 @@ def get_args_parser(add_help=True):
parser
.
add_argument
(
"--data-path"
,
default
=
"/datasets01/imagenet_full_size/061417/"
,
type
=
str
,
help
=
"dataset path"
)
parser
.
add_argument
(
"--data-path"
,
default
=
"/datasets01/imagenet_full_size/061417/"
,
type
=
str
,
help
=
"dataset path"
)
parser
.
add_argument
(
"--model"
,
default
=
"mobilenet_v2"
,
type
=
str
,
help
=
"model name"
)
parser
.
add_argument
(
"--model"
,
default
=
"mobilenet_v2"
,
type
=
str
,
help
=
"model name"
)
parser
.
add_argument
(
"--backend"
,
default
=
"qnnpack"
,
type
=
str
,
help
=
"fbgemm or qnnpack"
)
parser
.
add_argument
(
"--
q
backend"
,
default
=
"qnnpack"
,
type
=
str
,
help
=
"
Quantized backend:
fbgemm or qnnpack"
)
parser
.
add_argument
(
"--device"
,
default
=
"cuda"
,
type
=
str
,
help
=
"device (Use cuda or cpu Default: cuda)"
)
parser
.
add_argument
(
"--device"
,
default
=
"cuda"
,
type
=
str
,
help
=
"device (Use cuda or cpu Default: cuda)"
)
parser
.
add_argument
(
parser
.
add_argument
(
...
@@ -257,9 +257,17 @@ def get_args_parser(add_help=True):
...
@@ -257,9 +257,17 @@ def get_args_parser(add_help=True):
parser
.
add_argument
(
"--clip-grad-norm"
,
default
=
None
,
type
=
float
,
help
=
"the maximum gradient norm (default None)"
)
parser
.
add_argument
(
"--clip-grad-norm"
,
default
=
None
,
type
=
float
,
help
=
"the maximum gradient norm (default None)"
)
parser
.
add_argument
(
"--weights"
,
default
=
None
,
type
=
str
,
help
=
"the weights enum name to load"
)
parser
.
add_argument
(
"--weights"
,
default
=
None
,
type
=
str
,
help
=
"the weights enum name to load"
)
parser
.
add_argument
(
"--backend"
,
default
=
"PIL"
,
type
=
str
.
lower
,
help
=
"PIL or tensor - case insensitive"
)
parser
.
add_argument
(
"--use-v2"
,
action
=
"store_true"
,
help
=
"Use V2 transforms"
)
return
parser
return
parser
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
args
=
get_args_parser
().
parse_args
()
args
=
get_args_parser
().
parse_args
()
if
args
.
backend
in
(
"fbgemm"
,
"qnnpack"
):
raise
ValueError
(
"The --backend parameter has been re-purposed to specify the backend of the transforms (PIL or Tensor) "
"instead of the quantized backend. Please use the --qbackend parameter to specify the quantized backend."
)
main
(
args
)
main
(
args
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment