Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
862c2147
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
接近 2 年 前同步成功
通知
116
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
862c2147
编写于
9月 17, 2021
作者:
C
cuicheng01
提交者:
GitHub
9月 17, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1169 from TingquanGao/dev/fix_augmentation
fix: fix augmentation
上级
f5b32a02
1f8cfbd6
变更
4
展开全部
隐藏空白更改
内联
并排
Showing
4 changed file
with
972 addition
and
33 deletion
+972
-33
ppcls/data/preprocess/__init__.py
ppcls/data/preprocess/__init__.py
+24
-17
ppcls/data/preprocess/ops/operators.py
ppcls/data/preprocess/ops/operators.py
+18
-0
ppcls/data/preprocess/ops/random_erasing.py
ppcls/data/preprocess/ops/random_erasing.py
+51
-16
ppcls/data/preprocess/ops/timm_autoaugment.py
ppcls/data/preprocess/ops/timm_autoaugment.py
+879
-0
未找到文件。
ppcls/data/preprocess/__init__.py
浏览文件 @
862c2147
...
...
@@ -14,6 +14,7 @@
from
ppcls.data.preprocess.ops.autoaugment
import
ImageNetPolicy
as
RawImageNetPolicy
from
ppcls.data.preprocess.ops.randaugment
import
RandAugment
as
RawRandAugment
from
ppcls.data.preprocess.ops.timm_autoaugment
import
RawTimmAutoAugment
from
ppcls.data.preprocess.ops.cutout
import
Cutout
from
ppcls.data.preprocess.ops.hide_and_seek
import
HideAndSeek
...
...
@@ -31,7 +32,6 @@ from ppcls.data.preprocess.ops.operators import AugMix
from
ppcls.data.preprocess.batch_ops.batch_operators
import
MixupOperator
,
CutmixOperator
,
OpSampler
,
FmixOperator
import
six
import
numpy
as
np
from
PIL
import
Image
...
...
@@ -47,20 +47,14 @@ class AutoAugment(RawImageNetPolicy):
""" ImageNetPolicy wrapper to auto fit different img types """
def
__init__
(
self
,
*
args
,
**
kwargs
):
if
six
.
PY2
:
super
(
AutoAugment
,
self
).
__init__
(
*
args
,
**
kwargs
)
else
:
super
().
__init__
(
*
args
,
**
kwargs
)
super
().
__init__
(
*
args
,
**
kwargs
)
def
__call__
(
self
,
img
):
if
not
isinstance
(
img
,
Image
.
Image
):
img
=
np
.
ascontiguousarray
(
img
)
img
=
Image
.
fromarray
(
img
)
if
six
.
PY2
:
img
=
super
(
AutoAugment
,
self
).
__call__
(
img
)
else
:
img
=
super
().
__call__
(
img
)
img
=
super
().
__call__
(
img
)
if
isinstance
(
img
,
Image
.
Image
):
img
=
np
.
asarray
(
img
)
...
...
@@ -72,20 +66,33 @@ class RandAugment(RawRandAugment):
""" RandAugment wrapper to auto fit different img types """
def
__init__
(
self
,
*
args
,
**
kwargs
):
if
six
.
PY2
:
super
(
RandAugment
,
self
).
__init__
(
*
args
,
**
kwargs
)
else
:
super
().
__init__
(
*
args
,
**
kwargs
)
super
().
__init__
(
*
args
,
**
kwargs
)
def
__call__
(
self
,
img
):
if
not
isinstance
(
img
,
Image
.
Image
):
img
=
np
.
ascontiguousarray
(
img
)
img
=
Image
.
fromarray
(
img
)
if
six
.
PY2
:
img
=
super
(
RandAugment
,
self
).
__call__
(
img
)
else
:
img
=
super
().
__call__
(
img
)
img
=
super
().
__call__
(
img
)
if
isinstance
(
img
,
Image
.
Image
):
img
=
np
.
asarray
(
img
)
return
img
class
TimmAutoAugment
(
RawTimmAutoAugment
):
""" TimmAutoAugment wrapper to auto fit different img tyeps. """
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
def
__call__
(
self
,
img
):
if
not
isinstance
(
img
,
Image
.
Image
):
img
=
np
.
ascontiguousarray
(
img
)
img
=
Image
.
fromarray
(
img
)
img
=
super
().
__call__
(
img
)
if
isinstance
(
img
,
Image
.
Image
):
img
=
np
.
asarray
(
img
)
...
...
ppcls/data/preprocess/ops/operators.py
浏览文件 @
862c2147
...
...
@@ -26,6 +26,7 @@ import random
import
cv2
import
numpy
as
np
from
PIL
import
Image
from
paddle.vision.transforms
import
ColorJitter
as
RawColorJitter
from
.autoaugment
import
ImageNetPolicy
from
.functional
import
augmentations
...
...
@@ -363,3 +364,20 @@ class AugMix(object):
mixed
=
(
1
-
m
)
*
image
+
m
*
mix
return
mixed
.
astype
(
np
.
uint8
)
class
ColorJitter
(
RawColorJitter
):
"""ColorJitter.
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
def
__call__
(
self
,
img
):
if
not
isinstance
(
img
,
Image
.
Image
):
img
=
np
.
ascontiguousarray
(
img
)
img
=
Image
.
fromarray
(
img
)
img
=
super
().
_apply_image
(
img
)
if
isinstance
(
img
,
Image
.
Image
):
img
=
np
.
asarray
(
img
)
return
img
ppcls/data/preprocess/ops/random_erasing.py
浏览文件 @
862c2147
...
...
@@ -12,7 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#This code is based on https://github.com/zhunzhong07/Random-Erasing
#This code is adapted from https://github.com/zhunzhong07/Random-Erasing, and refer to Timm.
from
functools
import
partial
import
math
import
random
...
...
@@ -20,36 +22,69 @@ import random
import
numpy
as
np
class
Pixels
(
object
):
def
__init__
(
self
,
mode
=
"const"
,
mean
=
[
0.
,
0.
,
0.
]):
self
.
_mode
=
mode
self
.
_mean
=
mean
def
__call__
(
self
,
h
=
224
,
w
=
224
,
c
=
3
):
if
self
.
_mode
==
"rand"
:
return
np
.
random
.
normal
(
size
=
(
1
,
1
,
3
))
elif
self
.
_mode
==
"pixel"
:
return
np
.
random
.
normal
(
size
=
(
h
,
w
,
c
))
elif
self
.
_mode
==
"const"
:
return
self
.
_mean
else
:
raise
Exception
(
"Invalid mode in RandomErasing, only support
\"
const
\"
,
\"
rand
\"
,
\"
pixel
\"
"
)
class
RandomErasing
(
object
):
def
__init__
(
self
,
EPSILON
=
0.5
,
sl
=
0.02
,
sh
=
0.4
,
r1
=
0.3
,
mean
=
[
0.
,
0.
,
0.
]):
self
.
EPSILON
=
EPSILON
self
.
mean
=
mean
self
.
sl
=
sl
self
.
sh
=
sh
self
.
r1
=
r1
"""RandomErasing.
"""
def
__init__
(
self
,
EPSILON
=
0.5
,
sl
=
0.02
,
sh
=
0.4
,
r1
=
0.3
,
mean
=
[
0.
,
0.
,
0.
],
attempt
=
100
,
use_log_aspect
=
False
,
mode
=
'const'
):
self
.
EPSILON
=
eval
(
EPSILON
)
if
isinstance
(
EPSILON
,
str
)
else
EPSILON
self
.
sl
=
eval
(
sl
)
if
isinstance
(
sl
,
str
)
else
sl
self
.
sh
=
eval
(
sh
)
if
isinstance
(
sh
,
str
)
else
sh
r1
=
eval
(
r1
)
if
isinstance
(
r1
,
str
)
else
r1
self
.
r1
=
(
math
.
log
(
r1
),
math
.
log
(
1
/
r1
))
if
use_log_aspect
else
(
r1
,
1
/
r1
)
self
.
use_log_aspect
=
use_log_aspect
self
.
attempt
=
attempt
self
.
get_pixels
=
Pixels
(
mode
,
mean
)
def
__call__
(
self
,
img
):
if
random
.
uniform
(
0
,
1
)
>
self
.
EPSILON
:
if
random
.
random
(
)
>
self
.
EPSILON
:
return
img
for
_
in
range
(
100
):
for
_
in
range
(
self
.
attempt
):
area
=
img
.
shape
[
0
]
*
img
.
shape
[
1
]
target_area
=
random
.
uniform
(
self
.
sl
,
self
.
sh
)
*
area
aspect_ratio
=
random
.
uniform
(
self
.
r1
,
1
/
self
.
r1
)
aspect_ratio
=
random
.
uniform
(
*
self
.
r1
)
if
self
.
use_log_aspect
:
aspect_ratio
=
math
.
exp
(
aspect_ratio
)
h
=
int
(
round
(
math
.
sqrt
(
target_area
*
aspect_ratio
)))
w
=
int
(
round
(
math
.
sqrt
(
target_area
/
aspect_ratio
)))
if
w
<
img
.
shape
[
1
]
and
h
<
img
.
shape
[
0
]:
pixels
=
self
.
get_pixels
(
h
,
w
,
img
.
shape
[
2
])
x1
=
random
.
randint
(
0
,
img
.
shape
[
0
]
-
h
)
y1
=
random
.
randint
(
0
,
img
.
shape
[
1
]
-
w
)
if
img
.
shape
[
0
]
==
3
:
img
[
x1
:
x1
+
h
,
y1
:
y1
+
w
,
0
]
=
self
.
mean
[
0
]
img
[
x1
:
x1
+
h
,
y1
:
y1
+
w
,
1
]
=
self
.
mean
[
1
]
img
[
x1
:
x1
+
h
,
y1
:
y1
+
w
,
2
]
=
self
.
mean
[
2
]
if
img
.
shape
[
2
]
==
3
:
img
[
x1
:
x1
+
h
,
y1
:
y1
+
w
,
:]
=
pixels
else
:
img
[
0
,
x1
:
x1
+
h
,
y1
:
y1
+
w
]
=
self
.
mean
[
1
]
img
[
x1
:
x1
+
h
,
y1
:
y1
+
w
,
0
]
=
pixels
[
0
]
return
img
return
img
ppcls/data/preprocess/ops/timm_autoaugment.py
0 → 100644
浏览文件 @
862c2147
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录