Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
hapi
提交
b3644f63
H
hapi
项目概览
PaddlePaddle
/
hapi
通知
11
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
7
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hapi
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
7
合并请求
7
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
b3644f63
编写于
3月 30, 2020
作者:
L
LielinJiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
addd datasetfolder
上级
7ac1c764
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
160 addition
and
2 deletion
+160
-2
datasets/folder.py
datasets/folder.py
+156
-0
image_classification/imagenet_dataset.py
image_classification/imagenet_dataset.py
+4
-2
未找到文件。
datasets/folder.py
0 → 100644
浏览文件 @
b3644f63
import
os
import
sys
import
cv2
from
paddle.fluid.io
import
Dataset
def
has_valid_extension
(
filename
,
extensions
):
"""Checks if a file is an allowed extension.
Args:
filename (string): path to a file
extensions (tuple of strings): extensions to consider (lowercase)
Returns:
bool: True if the filename ends with one of given extensions
"""
return
filename
.
lower
().
endswith
(
extensions
)
def
make_dataset
(
dir
,
class_to_idx
,
extensions
=
None
,
is_valid_file
=
None
):
images
=
[]
dir
=
os
.
path
.
expanduser
(
dir
)
if
not
((
extensions
is
None
)
^
(
is_valid_file
is
None
)):
raise
ValueError
(
"Both extensions and is_valid_file cannot be None or not None at the same time"
)
if
extensions
is
not
None
:
def
is_valid_file
(
x
):
return
has_valid_extension
(
x
,
extensions
)
for
target
in
sorted
(
class_to_idx
.
keys
()):
d
=
os
.
path
.
join
(
dir
,
target
)
if
not
os
.
path
.
isdir
(
d
):
continue
for
root
,
_
,
fnames
in
sorted
(
os
.
walk
(
d
,
followlinks
=
True
)):
for
fname
in
sorted
(
fnames
):
path
=
os
.
path
.
join
(
root
,
fname
)
if
is_valid_file
(
path
):
item
=
(
path
,
class_to_idx
[
target
])
images
.
append
(
item
)
return
images
class
DatasetFolder
(
Dataset
):
"""A generic data loader where the samples are arranged in this way: ::
root/class_a/1.ext
root/class_a/2.ext
root/class_a/3.ext
root/class_b/123.ext
root/class_b/456.ext
root/class_b/789.ext
Args:
root (string): Root directory path.
loader (callable, optional): A function to load a sample given its path.
extensions (tuple[string], optional): A list of allowed extensions.
both extensions and is_valid_file should not be passed.
transform (callable, optional): A function/transform that takes in
a sample and returns a transformed version.
E.g, ``transforms.RandomCrop`` for images.
target_transform (callable, optional): A function/transform that takes
in the target and transforms it.
is_valid_file (callable, optional): A function that takes path of a file
and check if the file is a valid file (used to check of corrupt files)
both extensions and is_valid_file should not be passed.
Attributes:
classes (list): List of the class names.
class_to_idx (dict): Dict with items (class_name, class_index).
samples (list): List of (sample path, class_index) tuples
targets (list): The class_index value for each image in the dataset
"""
def
__init__
(
self
,
root
,
loader
=
None
,
extensions
=
None
,
transform
=
None
,
target_transform
=
None
,
is_valid_file
=
None
):
self
.
root
=
root
if
extensions
is
None
:
extensions
=
IMG_EXTENSIONS
classes
,
class_to_idx
=
self
.
_find_classes
(
self
.
root
)
samples
=
make_dataset
(
self
.
root
,
class_to_idx
,
extensions
,
is_valid_file
)
if
len
(
samples
)
==
0
:
raise
(
RuntimeError
(
"Found 0 files in subfolders of: "
+
self
.
root
+
"
\n
"
"Supported extensions are: "
+
","
.
join
(
extensions
)))
self
.
loader
=
cv2_loader
if
loader
is
None
else
loader
self
.
extensions
=
extensions
self
.
classes
=
classes
self
.
class_to_idx
=
class_to_idx
self
.
samples
=
samples
self
.
targets
=
[
s
[
1
]
for
s
in
samples
]
def
_find_classes
(
self
,
dir
):
"""
Finds the class folders in a dataset.
Args:
dir (string): Root directory path.
Returns:
tuple: (classes, class_to_idx) where classes are relative to (dir), and class_to_idx is a dictionary.
Ensures:
No class is a subdirectory of another.
"""
if
sys
.
version_info
>=
(
3
,
5
):
# Faster and available in Python 3.5 and above
classes
=
[
d
.
name
for
d
in
os
.
scandir
(
dir
)
if
d
.
is_dir
()]
else
:
classes
=
[
d
for
d
in
os
.
listdir
(
dir
)
if
os
.
path
.
isdir
(
os
.
path
.
join
(
dir
,
d
))
]
classes
.
sort
()
class_to_idx
=
{
classes
[
i
]:
i
for
i
in
range
(
len
(
classes
))}
return
classes
,
class_to_idx
def
__getitem__
(
self
,
index
):
"""
Args:
index (int): Index
Returns:
tuple: (sample, target) where target is class_index of the target class.
"""
path
,
target
=
self
.
samples
[
index
]
sample
=
self
.
loader
(
path
)
if
self
.
transform
is
not
None
:
sample
=
self
.
transform
(
sample
)
if
self
.
target_transform
is
not
None
:
target
=
self
.
target_transform
(
target
)
return
sample
,
target
def
__len__
(
self
):
return
len
(
self
.
samples
)
IMG_EXTENSIONS
=
(
'.jpg'
,
'.jpeg'
,
'.png'
,
'.ppm'
,
'.bmp'
,
'.pgm'
,
'.tif'
,
'.tiff'
,
'.webp'
)
def
cv2_loader
(
path
):
return
cv2
.
imread
(
path
)
image_classification/imagenet_dataset.py
浏览文件 @
b3644f63
...
...
@@ -5,6 +5,8 @@ import random
import
numpy
as
np
from
paddle.fluid.io
import
Dataset
from
datasets.folder
import
DatasetFolder
def
center_crop_resize
(
img
):
h
,
w
=
img
.
shape
[:
2
]
...
...
@@ -81,9 +83,9 @@ def image_folder(path):
return
samples
class
ImageNetDataset
(
Dataset
):
class
ImageNetDataset
(
Dataset
Folder
):
def
__init__
(
self
,
path
,
mode
=
'train'
):
s
elf
.
samples
=
image_folder
(
path
)
s
uper
(
ImageNetDataset
,
self
).
__init__
(
path
)
self
.
mode
=
mode
if
self
.
mode
==
'train'
:
self
.
transform
=
compose
([
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录