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
a0867fac
Unverified
Commit
a0867fac
authored
Mar 14, 2022
by
Vasilis Vryniotis
Committed by
GitHub
Mar 14, 2022
Browse files
Adding RandomShortestSize transform (#5610)
parent
bb79470a
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
1 deletion
+37
-1
references/detection/transforms.py
references/detection/transforms.py
+37
-1
No files found.
references/detection/transforms.py
View file @
a0867fac
from
typing
import
List
,
Tuple
,
Dict
,
Optional
from
typing
import
List
,
Tuple
,
Dict
,
Optional
,
Union
import
torch
import
torch
import
torchvision
import
torchvision
...
@@ -401,3 +401,39 @@ class FixedSizeCrop(nn.Module):
...
@@ -401,3 +401,39 @@ class FixedSizeCrop(nn.Module):
img
,
target
=
self
.
_pad
(
img
,
target
,
[
0
,
0
,
pad_right
,
pad_bottom
])
img
,
target
=
self
.
_pad
(
img
,
target
,
[
0
,
0
,
pad_right
,
pad_bottom
])
return
img
,
target
return
img
,
target
class
RandomShortestSize
(
nn
.
Module
):
def
__init__
(
self
,
min_size
:
Union
[
List
[
int
],
Tuple
[
int
],
int
],
max_size
:
int
,
interpolation
:
InterpolationMode
=
InterpolationMode
.
BILINEAR
,
):
super
().
__init__
()
self
.
min_size
=
[
min_size
]
if
isinstance
(
min_size
,
int
)
else
list
(
min_size
)
self
.
max_size
=
max_size
self
.
interpolation
=
interpolation
def
forward
(
self
,
image
:
Tensor
,
target
:
Optional
[
Dict
[
str
,
Tensor
]]
=
None
)
->
Tuple
[
Tensor
,
Optional
[
Dict
[
str
,
Tensor
]]]:
_
,
orig_height
,
orig_width
=
F
.
get_dimensions
(
image
)
min_size
=
self
.
min_size
[
torch
.
randint
(
len
(
self
.
min_size
),
(
1
,)).
item
()]
r
=
min
(
min_size
/
min
(
orig_height
,
orig_width
),
self
.
max_size
/
max
(
orig_height
,
orig_width
))
new_width
=
int
(
orig_width
*
r
)
new_height
=
int
(
orig_height
*
r
)
image
=
F
.
resize
(
image
,
[
new_height
,
new_width
],
interpolation
=
self
.
interpolation
)
if
target
is
not
None
:
target
[
"boxes"
][:,
0
::
2
]
*=
new_width
/
orig_width
target
[
"boxes"
][:,
1
::
2
]
*=
new_height
/
orig_height
if
"masks"
in
target
:
target
[
"masks"
]
=
F
.
resize
(
target
[
"masks"
],
[
new_height
,
new_width
],
interpolation
=
InterpolationMode
.
NEAREST
)
return
image
,
target
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