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
Charles
人工智能系统实战第三期
Commits
c76dfd28
Commit
c76dfd28
authored
Dec 16, 2023
by
前钰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
b8c619cc
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
0 deletions
+78
-0
utils.py
人工智能系统实战第三期/实战代码/深度学习项目实战/生成对抗网络/utils.py
+78
-0
No files found.
人工智能系统实战第三期/实战代码/深度学习项目实战/生成对抗网络/utils.py
0 → 100644
View file @
c76dfd28
import
torch
import
torch
from
torch
import
nn
from
torch.autograd
import
Variable
import
torchvision.transforms
as
tfs
from
torch.utils.data
import
DataLoader
,
sampler
from
torchvision.datasets
import
MNIST
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
matplotlib.gridspec
as
gridspec
plt
.
rcParams
[
'figure.figsize'
]
=
(
10.0
,
8.0
)
# 设置画图的尺寸
plt
.
rcParams
[
'image.interpolation'
]
=
'nearest'
plt
.
rcParams
[
'image.cmap'
]
=
'gray'
def
show_images
(
images
):
# 定义画图工具
images
=
np
.
reshape
(
images
,
[
images
.
shape
[
0
],
-
1
])
sqrtn
=
int
(
np
.
ceil
(
np
.
sqrt
(
images
.
shape
[
0
])))
sqrtimg
=
int
(
np
.
ceil
(
np
.
sqrt
(
images
.
shape
[
1
])))
fig
=
plt
.
figure
(
figsize
=
(
sqrtn
,
sqrtn
))
gs
=
gridspec
.
GridSpec
(
sqrtn
,
sqrtn
)
gs
.
update
(
wspace
=
0.05
,
hspace
=
0.05
)
for
i
,
img
in
enumerate
(
images
):
ax
=
plt
.
subplot
(
gs
[
i
])
plt
.
axis
(
'off'
)
ax
.
set_xticklabels
([])
ax
.
set_yticklabels
([])
ax
.
set_aspect
(
'equal'
)
plt
.
imshow
(
img
.
reshape
([
sqrtimg
,
sqrtimg
]))
return
def
preprocess_img
(
x
):
x
=
tfs
.
ToTensor
()(
x
)
return
(
x
-
0.5
)
/
0.5
def
deprocess_img
(
x
):
return
(
x
+
1.0
)
/
2.0
class
ChunkSampler
(
sampler
.
Sampler
):
# 定义一个取样的函数
"""Samples elements sequentially from some offset.
Arguments:
num_samples: # of desired datapoints
start: offset where we should start selecting from
"""
def
__init__
(
self
,
num_samples
,
start
=
0
):
self
.
num_samples
=
num_samples
self
.
start
=
start
def
__iter__
(
self
):
return
iter
(
range
(
self
.
start
,
self
.
start
+
self
.
num_samples
))
def
__len__
(
self
):
return
self
.
num_samples
NUM_TRAIN
=
50000
NUM_VAL
=
5000
NOISE_DIM
=
96
batch_size
=
128
train_set
=
MNIST
(
'./mnist'
,
train
=
True
,
download
=
True
,
transform
=
preprocess_img
)
train_data
=
DataLoader
(
train_set
,
batch_size
=
batch_size
,
sampler
=
ChunkSampler
(
NUM_TRAIN
,
0
))
val_set
=
MNIST
(
'./mnist'
,
train
=
True
,
download
=
True
,
transform
=
preprocess_img
)
val_data
=
DataLoader
(
val_set
,
batch_size
=
batch_size
,
sampler
=
ChunkSampler
(
NUM_VAL
,
NUM_TRAIN
))
imgs
=
deprocess_img
(
train_data
.
__iter__
()
.
__next__
()[
0
]
.
view
(
batch_size
,
784
))
.
numpy
()
.
squeeze
()
# 可视化图片效果
show_images
(
imgs
)
\ No newline at end of file
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