Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
人
人工智能系统实战第三期
Overview
Overview
Details
Activity
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
yy
人工智能系统实战第三期
Commits
6cdc2b99
Commit
6cdc2b99
authored
Jan 24, 2024
by
前钰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
6b8af4fa
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
0 deletions
+67
-0
test.py
人工智能系统实战第三期/实战代码/计算机视觉/CNN/test.py
+67
-0
No files found.
人工智能系统实战第三期/实战代码/计算机视觉/CNN/test.py
0 → 100644
View file @
6cdc2b99
import
matplotlib.pyplot
as
plt
import
matplotlib.pyplot
as
plt
import
os
import
json
import
torch.nn
as
nn
from
network.AlexNet
import
AlexNet
import
torch
from
PIL
import
Image
from
torchvision
import
transforms
def
main
():
# device = torch.device("cpu")
device
=
torch
.
device
(
"cuda:0"
if
torch
.
cuda
.
is_available
()
else
"cpu"
)
data_transform
=
transforms
.
Compose
(
[
transforms
.
Resize
((
224
,
224
)),
transforms
.
ToTensor
(),
transforms
.
Normalize
((
0.5
,
0.5
,
0.5
),
(
0.5
,
0.5
,
0.5
))])
# load image
img_path
=
"data/train/tulips/112428665_d8f3632f36_n.jpg"
assert
os
.
path
.
exists
(
img_path
),
"file: '{}' dose not exist."
.
format
(
img_path
)
img
=
Image
.
open
(
img_path
)
# plt.switch_backend('agg')
plt
.
imshow
(
img
)
# [N, C, H, W]
img
=
data_transform
(
img
)
# expand batch dimension
img
=
torch
.
unsqueeze
(
img
,
dim
=
0
)
# read class_indict
json_path
=
"data/class_indices.json"
assert
os
.
path
.
exists
(
json_path
),
"file: '{}' dose not exist."
.
format
(
json_path
)
json_file
=
open
(
json_path
,
"r"
)
class_indict
=
json
.
load
(
json_file
)
# create model
model
=
AlexNet
(
num_classes
=
5
)
.
to
(
device
)
# load model weights
weights_path
=
"checkpoints/alex/alex_flower.pth"
assert
os
.
path
.
exists
(
weights_path
),
"file: '{}' dose not exist."
.
format
(
weights_path
)
model
.
load_state_dict
(
torch
.
load
(
weights_path
))
model
.
eval
()
with
torch
.
no_grad
():
# predict class
output
=
torch
.
squeeze
(
model
(
img
.
to
(
device
)))
.
cpu
()
predict
=
torch
.
softmax
(
output
,
dim
=
0
)
predict_cla
=
torch
.
argmax
(
predict
)
.
numpy
()
print_res
=
"class: {} prob: {:.3}"
.
format
(
class_indict
[
str
(
predict_cla
)],
predict
[
predict_cla
]
.
numpy
())
plt
.
title
(
print_res
)
for
i
in
range
(
len
(
predict
)):
print
(
"class: {:10} prob: {:.3}"
.
format
(
class_indict
[
str
(
i
)],
predict
[
i
]
.
numpy
()))
plt
.
show
()
# plt.savefig("./AelxNet_infor.jpg")
if
__name__
==
'__main__'
:
main
()
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