未验证 提交 5cd7a05d 编写于 作者: W Wei Shengyu 提交者: GitHub

Merge pull request #1034 from cuicheng01/develop

support load pretrain from url
...@@ -38,7 +38,7 @@ class CosMargin(paddle.nn.Layer): ...@@ -38,7 +38,7 @@ class CosMargin(paddle.nn.Layer):
input_norm = paddle.sqrt( input_norm = paddle.sqrt(
paddle.sum(paddle.square(input), axis=1, keepdim=True)) paddle.sum(paddle.square(input), axis=1, keepdim=True))
input = paddle.divide(input, x_norm) input = paddle.divide(input, input_norm)
weight = self.fc.weight weight = self.fc.weight
weight_norm = paddle.sqrt( weight_norm = paddle.sqrt(
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
Eval: Eval:
- CELoss: - CELoss:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
Eval: Eval:
- CELoss: - CELoss:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
...@@ -22,7 +22,7 @@ Arch: ...@@ -22,7 +22,7 @@ Arch:
# loss function config for traing/eval process # loss function config for traing/eval process
Loss: Loss:
Train: Train:
- CELoss: - MixCELoss:
weight: 1.0 weight: 1.0
epsilon: 0.1 epsilon: 0.1
Eval: Eval:
......
# global configs # global configs
Global: Global:
checkpoints: null checkpoints: null
# please download pretrained model via this link: pretrained_model: "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/pretrain/product_ResNet50_vd_Aliproduct_v1.0_pretrained.pdparams"
# https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/pretrain/product_ResNet50_vd_Aliproduct_v1.0_pretrained.pdparams
pretrained_model: product_ResNet50_vd_Aliproduct_v1.0_pretrained
output_dir: ./output/ output_dir: ./output/
device: gpu device: gpu
save_interval: 10 save_interval: 10
......
# global configs # global configs
Global: Global:
checkpoints: null checkpoints: null
# please download pretrained model via this link: pretrained_model: "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/pretrain/product_ResNet50_vd_Aliproduct_v1.0_pretrained.pdparams"
# https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/pretrain/product_ResNet50_vd_Aliproduct_v1.0_pretrained.pdparams
pretrained_model: product_ResNet50_vd_Aliproduct_v1.0_pretrained
output_dir: ./output/ output_dir: ./output/
device: gpu device: gpu
save_interval: 10 save_interval: 10
......
...@@ -40,7 +40,7 @@ from ppcls.arch import apply_to_static ...@@ -40,7 +40,7 @@ from ppcls.arch import apply_to_static
from ppcls.loss import build_loss from ppcls.loss import build_loss
from ppcls.metric import build_metrics from ppcls.metric import build_metrics
from ppcls.optimizer import build_optimizer from ppcls.optimizer import build_optimizer
from ppcls.utils.save_load import load_dygraph_pretrain from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url
from ppcls.utils.save_load import init_model from ppcls.utils.save_load import init_model
from ppcls.utils import save_load from ppcls.utils import save_load
...@@ -78,8 +78,12 @@ class Trainer(object): ...@@ -78,8 +78,12 @@ class Trainer(object):
apply_to_static(self.config, self.model) apply_to_static(self.config, self.model)
if self.config["Global"]["pretrained_model"] is not None: if self.config["Global"]["pretrained_model"] is not None:
load_dygraph_pretrain(self.model, if self.config["Global"]["pretrained_model"].startswith("http"):
self.config["Global"]["pretrained_model"]) load_dygraph_pretrain_from_url(
self.model, self.config["Global"]["pretrained_model"])
else:
load_dygraph_pretrain(
self.model, self.config["Global"]["pretrained_model"])
if self.config["Global"]["distributed"]: if self.config["Global"]["distributed"]:
self.model = paddle.DataParallel(self.model) self.model = paddle.DataParallel(self.model)
...@@ -174,9 +178,12 @@ class Trainer(object): ...@@ -174,9 +178,12 @@ class Trainer(object):
out = self.model(batch[0]) out = self.model(batch[0])
else: else:
out = self.model(batch[0], batch[1]) out = self.model(batch[0], batch[1])
# calc loss # calc loss
loss_dict = self.train_loss_func(out, batch[1]) if self.config["DataLoader"]["Train"]["dataset"].get(
"batch_transform_ops", None):
loss_dict = self.train_loss_func(out, batch[1:])
else:
loss_dict = self.train_loss_func(out, batch[1])
for key in loss_dict: for key in loss_dict:
if not key in output_info: if not key in output_info:
......
...@@ -4,7 +4,7 @@ import paddle ...@@ -4,7 +4,7 @@ import paddle
import paddle.nn as nn import paddle.nn as nn
from ppcls.utils import logger from ppcls.utils import logger
from .celoss import CELoss from .celoss import CELoss, MixCELoss
from .googlenetloss import GoogLeNetLoss from .googlenetloss import GoogLeNetLoss
from .centerloss import CenterLoss from .centerloss import CenterLoss
from .emlloss import EmlLoss from .emlloss import EmlLoss
...@@ -30,7 +30,6 @@ class CombinedLoss(nn.Layer): ...@@ -30,7 +30,6 @@ class CombinedLoss(nn.Layer):
assert isinstance(config_list, list), ( assert isinstance(config_list, list), (
'operator config should be a list') 'operator config should be a list')
for config in config_list: for config in config_list:
print(config)
assert isinstance(config, assert isinstance(config,
dict) and len(config) == 1, "yaml format error" dict) and len(config) == 1, "yaml format error"
name = list(config)[0] name = list(config)[0]
......
...@@ -18,6 +18,10 @@ import paddle.nn.functional as F ...@@ -18,6 +18,10 @@ import paddle.nn.functional as F
class CELoss(nn.Layer): class CELoss(nn.Layer):
"""
Cross entropy loss
"""
def __init__(self, epsilon=None): def __init__(self, epsilon=None):
super().__init__() super().__init__()
if epsilon is not None and (epsilon <= 0 or epsilon >= 1): if epsilon is not None and (epsilon <= 0 or epsilon >= 1):
...@@ -50,3 +54,21 @@ class CELoss(nn.Layer): ...@@ -50,3 +54,21 @@ class CELoss(nn.Layer):
loss = F.cross_entropy(x, label=label, soft_label=soft_label) loss = F.cross_entropy(x, label=label, soft_label=soft_label)
loss = loss.mean() loss = loss.mean()
return {"CELoss": loss} return {"CELoss": loss}
class MixCELoss(CELoss):
"""
Cross entropy loss with mix(mixup, cutmix, fixmix)
"""
def __init__(self, epsilon=None):
super().__init__()
self.epsilon = epsilon
def __call__(self, input, batch):
target0, target1, lam = batch
loss0 = super().forward(input, target0)["CELoss"]
loss1 = super().forward(input, target1)["CELoss"]
loss = lam * loss0 + (1.0 - lam) * loss1
loss = paddle.mean(loss)
return {"MixCELoss": loss}
...@@ -54,7 +54,7 @@ def load_dygraph_pretrain(model, path=None): ...@@ -54,7 +54,7 @@ def load_dygraph_pretrain(model, path=None):
return return
def load_dygraph_pretrain_from_url(model, pretrained_url, use_ssld): def load_dygraph_pretrain_from_url(model, pretrained_url, use_ssld=False):
if use_ssld: if use_ssld:
pretrained_url = pretrained_url.replace("_pretrained", pretrained_url = pretrained_url.replace("_pretrained",
"_ssld_pretrained") "_ssld_pretrained")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册