forked from Lornatang/SRGAN-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
101 lines (81 loc) · 3.57 KB
/
inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright 2022 Dakewe Biotech Corporation. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import argparse
import os
import cv2
import torch
from torch import nn
import imgproc
import model
from utils import load_state_dict
model_names = sorted(
name for name in model.__dict__ if
name.islower() and not name.startswith("__") and callable(model.__dict__[name]))
def choice_device(device_type: str) -> torch.device:
# Select model processing equipment type
if device_type == "cuda":
device = torch.device("cuda", 0)
else:
device = torch.device("cpu")
return device
def build_model(model_arch_name: str, device: torch.device) -> nn.Module:
# Initialize the super-resolution model
sr_model = model.__dict__[model_arch_name](in_channels=3,
out_channels=3,
channels=64,
num_rcb=16)
sr_model = sr_model.to(device=device)
return sr_model
def main(args):
device = choice_device(args.device_type)
# Initialize the model
sr_model = build_model(args.model_arch_name, device)
print(f"Build `{args.model_arch_name}` model successfully.")
# Load model weights
sr_model = load_state_dict(sr_model, args.model_weights_path)
print(f"Load `{args.model_arch_name}` model weights `{os.path.abspath(args.model_weights_path)}` successfully.")
# Start the verification mode of the model.
sr_model.eval()
lr_tensor = imgproc.preprocess_one_image(args.inputs_path, device)
# Use the model to generate super-resolved images
with torch.no_grad():
sr_tensor = sr_model(lr_tensor)
# Save image
sr_image = imgproc.tensor_to_image(sr_tensor, False, False)
sr_image = cv2.cvtColor(sr_image, cv2.COLOR_RGB2BGR)
cv2.imwrite(args.output_path, sr_image)
print(f"SR image save to `{args.output_path}`")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Using the model generator super-resolution images.")
parser.add_argument("--model_arch_name",
type=str,
default="srresnet_x4")
parser.add_argument("--inputs_path",
type=str,
default="./figure/comic_lr.png",
help="Low-resolution image path.")
parser.add_argument("--output_path",
type=str,
default="./figure/comic_sr.png",
help="Super-resolution image path.")
parser.add_argument("--model_weights_path",
type=str,
default="./results/pretrained_models/SRGAN_x4-ImageNet-8c4a7569.pth.tar",
help="Model weights file path.")
parser.add_argument("--device_type",
type=str,
default="cpu",
choices=["cpu", "cuda"])
args = parser.parse_args()
main(args)