FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
DeepLearning/Image_segmentation/DeepLabV3/predict.py at master · Overmind7/DeepLearning · GitHub
Overmind7
/
DeepLearning
Public
forked from
KKKSQJ/DeepLearning
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
DeepLearning
/
Image_segmentation
/
DeepLabV3
/
predict.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
172 lines (145 loc) · 6.42 KB
Breadcrumbs
DeepLearning
/
Image_segmentation
/
DeepLabV3
/
predict.py
Copy path
File metadata and controls
172 lines (145 loc) · 6.42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import
glob
import
json
import
logging
import
os
.
path
import
time
import
timeit
from
pathlib
import
Path
import
numpy
as
np
import
argparse
import
yaml
import
shutil
from
tqdm
import
tqdm
from
PIL
import
Image
import
matplotlib
.
pyplot
as
plt
import
torch
import
torch
.
nn
.
functional
as
F
from
torch
.
utils
.
data
import
DataLoader
from
torchvision
import
transforms
from
models
.
network
import
build_model
from
utils
.
label
import
label2rgb
IMG_FORMATS
=
[
'bmp'
,
'jpg'
,
'jpeg'
,
'png'
,
'tif'
,
'tiff'
,
'dng'
,
'webp'
,
'mpo'
]
def
time_sync
():
# pytorch-accurate time
if
torch
.
cuda
.
is_available
():
torch
.
cuda
.
synchronize
()
return
time
.
time
()
@
torch
.
no_grad
()
def
run
(
cfg
=
'config/example.yaml'
,
# 配置文件,主要用于读取模型配置
weights
=
'best_model.pth'
,
# 模型路径
source
=
'./data/test'
,
# 测试数据路径,可以是文件夹,可以是单张图片
use_cuda
=
True
,
# 是否使用cuda
view_img
=
False
,
# 是否可视化测试图片
save_mask
=
True
,
# 是否将保存mask
save_viz
=
True
,
# 是否保存融合图
palette_path
=
"palette.json"
,
project
=
'result'
# 结果输出路径
):
if
isinstance
(
cfg
,
dict
):
config
=
cfg
else
:
import
yaml
yaml_file
=
Path
(
cfg
).
name
with
open
(
cfg
)
as
f
:
config
=
yaml
.
safe_load
(
f
)
device
=
torch
.
device
(
"cuda"
if
torch
.
cuda
.
is_available
()
and
use_cuda
else
"cpu"
)
if
save_mask
:
os
.
makedirs
(
project
+
"/mask"
,
exist_ok
=
True
)
if
save_viz
:
os
.
makedirs
(
project
+
"/viz"
,
exist_ok
=
True
)
# Load model
assert
os
.
path
.
exists
(
weights
),
"model path: {} does not exists"
.
format
(
weights
)
num_classes
=
config
[
"train"
][
"num_classes"
]
+
1
model
=
build_model
(
config
,
num_classes
,
pretrain
=
False
)
checkpoint
=
torch
.
load
(
weights
,
map_location
=
'cpu'
)[
"model"
]
model
.
load_state_dict
(
checkpoint
,
strict
=
True
)
model
.
eval
().
to
(
device
)
# 调色板,用于给mask上色
with
open
(
palette_path
,
"rb"
)
as
f
:
pallette_dict
=
json
.
load
(
f
)
pallette
=
[]
for
v
in
pallette_dict
.
values
():
pallette
+=
v
# Run once
y
=
model
(
torch
.
rand
(
1
,
3
,
480
,
480
).
to
(
device
))
# 转成torch script
# 如果libtorch版本太低的话,模型版本也不能太高,需要进行转换
# checkpoint = torch.load(weights, map_location='cpu')["model"]
# torch.save(checkpoint, "libtorch.pth", _use_new_zipfile_serialization=False)
# trace_module = torch.jit.trace(model, torch.rand(1, 3, 224, 224).to(device))
# print(trace_module.code) # 查看模型结构
# output = trace_module(torch.ones(1, 3, 224, 224).to(device)) # 测试
# print(output)
# trace_module.save('model_gpu.pt') # 模型保存
# script_module = torch.jit.script(model)
# print(script_module.code)
# # output = script_module(torch.rand(1, 1, 224, 224))
# script_module.save('model.pt')
# Data transform
data_transform
=
transforms
.
Compose
([
transforms
.
Resize
(
1024
),
transforms
.
ToTensor
(),
transforms
.
Normalize
(
mean
=
(
0.485
,
0.456
,
0.406
),
std
=
(
0.229
,
0.224
,
0.225
))
])
# Load img
assert
os
.
path
.
exists
(
source
),
"data source: {} does not exists"
.
format
(
source
)
if
os
.
path
.
isdir
(
source
):
files
=
sorted
(
glob
.
glob
(
os
.
path
.
join
(
source
,
'*.*'
)))
elif
os
.
path
.
isfile
(
source
):
# img = Image.open(source)
# if img.mode != 'RGB':
# img = img.convert('RGB')
files
=
[
source
]
else
:
raise
Exception
(
f'ERROR:
{
source
}
does not exist'
)
images
=
[
x
for
x
in
files
if
x
.
split
(
'.'
)[
-
1
].
lower
()
in
IMG_FORMATS
]
images
=
tqdm
(
images
)
for
img_path
in
images
:
full_img
=
Image
.
open
(
img_path
).
convert
(
"RGB"
)
img
=
data_transform
(
full_img
)
img
=
torch
.
unsqueeze
(
img
,
dim
=
0
)
t1
=
time_sync
()
output
=
model
(
img
.
to
(
device
))
t2
=
time_sync
()
print
(
"inference time: {}"
.
format
(
t2
-
t1
))
prediction
=
output
[
'out'
].
argmax
(
1
).
squeeze
(
0
)
prediction
=
prediction
.
to
(
"cpu"
).
numpy
().
astype
(
np
.
uint8
)
file_name
=
img_path
.
split
(
os
.
sep
)[
-
1
][:
-
4
]
mask
=
Image
.
fromarray
(
prediction
)
mask
.
putpalette
(
pallette
)
mask
=
mask
.
resize
((
full_img
.
size
[
0
],
full_img
.
size
[
1
]),
resample
=
Image
.
NEAREST
)
viz
=
label2rgb
(
np
.
asarray
(
mask
),
np
.
asarray
(
full_img
),
font_size
=
15
,
loc
=
"rb"
,
colormap
=
np
.
asarray
(
list
(
pallette_dict
.
values
())))
if
view_img
:
fig
,
ax
=
plt
.
subplots
(
1
,
3
)
ax
[
0
].
set_title
(
'Input image'
)
ax
[
0
].
imshow
(
full_img
)
ax
[
1
].
set_title
(
f'Output mask'
)
ax
[
1
].
imshow
(
mask
)
ax
[
2
].
set_title
(
f'Vis'
)
ax
[
2
].
imshow
(
viz
)
plt
.
xticks
([]),
plt
.
yticks
([])
plt
.
show
()
if
save_mask
:
mask
.
save
(
os
.
path
.
join
(
project
+
"/mask"
,
"{}.png"
.
format
(
file_name
)))
if
save_viz
:
viz
=
Image
.
fromarray
(
viz
)
viz
.
save
(
os
.
path
.
join
(
project
+
"/viz"
,
"{}.png"
.
format
(
file_name
)))
if
__name__
==
'__main__'
:
parser
=
argparse
.
ArgumentParser
(
description
=
'Predict masks from input images'
)
parser
.
add_argument
(
'--cfg'
,
type
=
str
,
default
=
'config/example.yaml'
,
help
=
'experiment configure file name'
)
parser
.
add_argument
(
'--weights'
,
'-w'
,
default
=
'best.pth'
,
metavar
=
'FILE'
,
help
=
'Specify the file in which the model is stored'
)
parser
.
add_argument
(
'--source'
,
'-i'
,
default
=
'data/test'
,
help
=
'Filenames of input images/dir'
)
parser
.
add_argument
(
'--use_cuda'
,
default
=
True
,
action
=
'store_true'
,
help
=
'Use cuda to predict'
)
parser
.
add_argument
(
'--view_img'
,
'-v'
,
action
=
'store_true'
,
default
=
False
,
help
=
'Visualize the images as they are processed'
)
parser
.
add_argument
(
'--save-mask'
,
'-sm'
,
action
=
'store_true'
,
default
=
True
,
help
=
'Whether save the output masks'
)
parser
.
add_argument
(
'--save-viz'
,
'-sv'
,
action
=
'store_true'
,
default
=
True
,
help
=
'Whether save the output vizs'
)
parser
.
add_argument
(
'--palette_path'
,
default
=
'palette.json'
,
help
=
'Plot Mask by different color'
)
parser
.
add_argument
(
'--project'
,
'-p'
,
metavar
=
'OUTPUT'
,
default
=
'result'
,
help
=
'Output of img path'
)
opt
=
parser
.
parse_args
()
run
(
**
vars
(
opt
))
Back
|
FazBrowse Home
|
New Git URL