Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
582157f2
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
1 年多 前同步成功
通知
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看板
提交
582157f2
编写于
6月 03, 2021
作者:
W
weishengyu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify format and remove loss
上级
586af751
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
2 addition
and
176 deletion
+2
-176
ppcls/arch/loss_metrics/loss.py
ppcls/arch/loss_metrics/loss.py
+0
-154
ppcls/data/dataloader/imagenet_dataset.py
ppcls/data/dataloader/imagenet_dataset.py
+1
-12
ppcls/data/dataloader/multilabel_dataset.py
ppcls/data/dataloader/multilabel_dataset.py
+1
-10
未找到文件。
ppcls/arch/loss_metrics/loss.py
已删除
100644 → 0
浏览文件 @
586af751
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
paddle
import
paddle.nn.functional
as
F
__all__
=
[
'CELoss'
,
'MixCELoss'
,
'GoogLeNetLoss'
,
'JSDivLoss'
,
'MultiLabelLoss'
]
class
Loss
(
object
):
"""
Loss
"""
def
__init__
(
self
,
class_dim
=
1000
,
epsilon
=
None
):
assert
class_dim
>
1
,
"class_dim=%d is not larger than 1"
%
(
class_dim
)
self
.
_class_dim
=
class_dim
if
epsilon
is
not
None
and
epsilon
>=
0.0
and
epsilon
<=
1.0
:
self
.
_epsilon
=
epsilon
self
.
_label_smoothing
=
True
else
:
self
.
_epsilon
=
None
self
.
_label_smoothing
=
False
def
_labelsmoothing
(
self
,
target
):
if
target
.
shape
[
-
1
]
!=
self
.
_class_dim
:
one_hot_target
=
F
.
one_hot
(
target
,
self
.
_class_dim
)
else
:
one_hot_target
=
target
soft_target
=
F
.
label_smooth
(
one_hot_target
,
epsilon
=
self
.
_epsilon
)
soft_target
=
paddle
.
reshape
(
soft_target
,
shape
=
[
-
1
,
self
.
_class_dim
])
return
soft_target
def
_binary_crossentropy
(
self
,
input
,
target
):
if
self
.
_label_smoothing
:
target
=
self
.
_labelsmoothing
(
target
)
cost
=
F
.
binary_cross_entropy_with_logits
(
logit
=
input
,
label
=
target
)
else
:
cost
=
F
.
binary_cross_entropy_with_logits
(
logit
=
input
,
label
=
target
)
avg_cost
=
paddle
.
mean
(
cost
)
return
avg_cost
def
_crossentropy
(
self
,
input
,
target
):
if
self
.
_label_smoothing
:
target
=
self
.
_labelsmoothing
(
target
)
input
=
-
F
.
log_softmax
(
input
,
axis
=-
1
)
cost
=
paddle
.
sum
(
target
*
input
,
axis
=-
1
)
else
:
cost
=
F
.
cross_entropy
(
input
=
input
,
label
=
target
)
avg_cost
=
paddle
.
mean
(
cost
)
return
avg_cost
def
_kldiv
(
self
,
input
,
target
,
name
=
None
):
eps
=
1.0e-10
cost
=
target
*
paddle
.
log
(
(
target
+
eps
)
/
(
input
+
eps
))
*
self
.
_class_dim
return
cost
def
_jsdiv
(
self
,
input
,
target
):
input
=
F
.
softmax
(
input
)
target
=
F
.
softmax
(
target
)
cost
=
self
.
_kldiv
(
input
,
target
)
+
self
.
_kldiv
(
target
,
input
)
cost
=
cost
/
2
avg_cost
=
paddle
.
mean
(
cost
)
return
avg_cost
def
__call__
(
self
,
input
,
target
):
pass
class
MultiLabelLoss
(
Loss
):
"""
Multilabel loss based binary cross entropy
"""
def
__init__
(
self
,
class_dim
=
1000
,
epsilon
=
None
):
super
(
MultiLabelLoss
,
self
).
__init__
(
class_dim
,
epsilon
)
def
__call__
(
self
,
input
,
target
):
cost
=
self
.
_binary_crossentropy
(
input
,
target
)
return
cost
class
CELoss
(
Loss
):
"""
Cross entropy loss
"""
def
__init__
(
self
,
class_dim
=
1000
,
epsilon
=
None
):
super
(
CELoss
,
self
).
__init__
(
class_dim
,
epsilon
)
def
__call__
(
self
,
input
,
target
):
cost
=
self
.
_crossentropy
(
input
,
target
)
return
cost
class
MixCELoss
(
Loss
):
"""
Cross entropy loss with mix(mixup, cutmix, fixmix)
"""
def
__init__
(
self
,
class_dim
=
1000
,
epsilon
=
None
):
super
(
MixCELoss
,
self
).
__init__
(
class_dim
,
epsilon
)
def
__call__
(
self
,
input
,
target0
,
target1
,
lam
):
cost0
=
self
.
_crossentropy
(
input
,
target0
)
cost1
=
self
.
_crossentropy
(
input
,
target1
)
cost
=
lam
*
cost0
+
(
1.0
-
lam
)
*
cost1
avg_cost
=
paddle
.
mean
(
cost
)
return
avg_cost
class
GoogLeNetLoss
(
Loss
):
"""
Cross entropy loss used after googlenet
"""
def
__init__
(
self
,
class_dim
=
1000
,
epsilon
=
None
):
super
(
GoogLeNetLoss
,
self
).
__init__
(
class_dim
,
epsilon
)
def
__call__
(
self
,
input0
,
input1
,
input2
,
target
):
cost0
=
self
.
_crossentropy
(
input0
,
target
)
cost1
=
self
.
_crossentropy
(
input1
,
target
)
cost2
=
self
.
_crossentropy
(
input2
,
target
)
cost
=
cost0
+
0.3
*
cost1
+
0.3
*
cost2
avg_cost
=
paddle
.
mean
(
cost
)
return
avg_cost
class
JSDivLoss
(
Loss
):
"""
JSDiv loss
"""
def
__init__
(
self
,
class_dim
=
1000
,
epsilon
=
None
):
super
(
JSDivLoss
,
self
).
__init__
(
class_dim
,
epsilon
)
def
__call__
(
self
,
input
,
target
):
cost
=
self
.
_jsdiv
(
input
,
target
)
return
cost
ppcls/data/dataloader/imagenet_dataset.py
浏览文件 @
582157f2
...
...
@@ -14,22 +14,13 @@
from
__future__
import
print_function
import
io
import
tarfile
import
numpy
as
np
from
PIL
import
Image
#all use default backend
import
paddle
from
paddle.io
import
Dataset
import
pickle
import
os
import
cv2
import
random
from
.common_dataset
import
CommonDataset
class
ImageNetDataset
(
CommonDataset
):
class
ImageNetDataset
(
CommonDataset
):
def
_load_anno
(
self
,
seed
=
None
):
assert
os
.
path
.
exists
(
self
.
_cls_path
)
assert
os
.
path
.
exists
(
self
.
_img_root
)
...
...
@@ -47,5 +38,3 @@ class ImageNetDataset(CommonDataset):
self
.
images
.
append
(
os
.
path
.
join
(
self
.
_img_root
,
l
[
0
]))
self
.
labels
.
append
(
int
(
l
[
1
]))
assert
os
.
path
.
exists
(
self
.
images
[
-
1
])
ppcls/data/dataloader/multilabel_dataset.py
浏览文件 @
582157f2
...
...
@@ -14,26 +14,17 @@
from
__future__
import
print_function
import
io
import
tarfile
import
numpy
as
np
from
PIL
import
Image
#all use default backend
import
paddle
from
paddle.io
import
Dataset
import
pickle
import
os
import
cv2
import
random
from
ppcls.data
import
preprocess
from
ppcls.data.preprocess
import
transform
from
ppcls.utils
import
logger
from
.common_dataset
import
CommonDataset
class
MultiLabelDataset
(
CommonDataset
):
class
MultiLabelDataset
(
CommonDataset
):
def
_load_anno
(
self
):
assert
os
.
path
.
exists
(
self
.
_cls_path
)
assert
os
.
path
.
exists
(
self
.
_img_root
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录