Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
animegan2-pytorch
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
git
animegan2-pytorch
Commits
b5bc801b
Commit
b5bc801b
authored
Nov 29, 2021
by
bryandlee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor: Remove cv2 depedency
parent
f11e0567
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
21 deletions
+23
-21
test.py
test.py
+23
-21
No files found.
test.py
View file @
b5bc801b
import
os
import
argparse
import
torch
import
cv2
from
PIL
import
Image
import
numpy
as
np
import
os
import
torch
from
torchvision.transforms.functional
import
to_tensor
,
to_pil_image
from
model
import
Generator
torch
.
backends
.
cudnn
.
enabled
=
False
torch
.
backends
.
cudnn
.
benchmark
=
False
torch
.
backends
.
cudnn
.
deterministic
=
True
def
load_image
(
image_path
,
x32
=
False
):
img
=
cv2
.
imread
(
image_path
)
.
astype
(
np
.
float32
)
img
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_BGR2RGB
)
h
,
w
=
img
.
shape
[:
2
]
img
=
Image
.
open
(
image_path
)
.
convert
(
"RGB"
)
if
x32
:
# resize image to multiple of 32s
if
x32
:
def
to_32s
(
x
):
return
256
if
x
<
256
else
x
-
x
%
32
img
=
cv2
.
resize
(
img
,
(
to_32s
(
w
),
to_32s
(
h
)))
return
256
if
x
<
256
else
x
-
x
%
32
w
,
h
=
img
.
size
img
=
img
.
resize
((
to_32s
(
w
),
to_32s
(
h
)))
img
=
torch
.
from_numpy
(
img
)
img
=
img
/
127.5
-
1.0
return
img
...
...
@@ -43,22 +44,22 @@ def test(args):
image
=
load_image
(
os
.
path
.
join
(
args
.
input_dir
,
image_name
),
args
.
x32
)
with
torch
.
no_grad
():
i
nput
=
image
.
permute
(
2
,
0
,
1
)
.
unsqueeze
(
0
)
.
to
(
device
)
out
=
net
(
i
nput
,
args
.
upsample_align
)
.
squeeze
(
0
)
.
permute
(
1
,
2
,
0
)
.
cpu
()
.
numpy
()
out
=
(
out
+
1
)
*
127
.5
out
=
np
.
clip
(
out
,
0
,
255
)
.
astype
(
np
.
uint8
)
cv2
.
imwrite
(
os
.
path
.
join
(
args
.
output_dir
,
image_name
),
cv2
.
cvtColor
(
out
,
cv2
.
COLOR_BGR2RGB
))
i
mage
=
to_tensor
(
image
)
.
unsqueeze
(
0
)
*
2
-
1
out
=
net
(
i
mage
.
to
(
device
),
args
.
upsample_align
)
.
cpu
()
out
=
out
.
squeeze
(
0
)
.
clip
(
-
1
,
1
)
*
0.5
+
0
.5
out
=
to_pil_image
(
out
)
out
.
save
(
os
.
path
.
join
(
args
.
output_dir
,
image_name
))
print
(
f
"image saved: {image_name}"
)
if
__name__
==
'__main__'
:
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
'--checkpoint'
,
type
=
str
,
default
=
'./
pytorch_generator_P
aprika.pt'
,
default
=
'./
weights/p
aprika.pt'
,
)
parser
.
add_argument
(
'--input_dir'
,
...
...
@@ -79,12 +80,13 @@ if __name__ == '__main__':
'--upsample_align'
,
type
=
bool
,
default
=
False
,
help
=
"Align corners in decoder upsampling layers"
)
parser
.
add_argument
(
'--x32'
,
action
=
"store_true"
,
help
=
"Resize images to multiple of 32"
)
args
=
parser
.
parse_args
()
test
(
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