-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PortraitNet. #1132
Add PortraitNet. #1132
Conversation
txyugood
commented
Jul 8, 2021
- Add portraitNet model.
- Add RandomAffine in transform.py for data augmentation.
- Add kl_loss and focal_loss for PortraintNet.
- Modify train.py for PortraintNet.
- Add EG1800 and Supervisely dataset.
- Add stepdecay in config.py for training strategy.
|
||
|
||
@manager.TRANSFORMS.add_component | ||
class RandomAffine: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个指缩放+旋转吧?是否可以通过已有的StepScaling+Rotation组合就好了,不用另外开发?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个是平移、缩放和旋转。我之前试过通过StepScaling+Rotation但是效果完全不一样。所以按照论文实现了RandomAffine。
paddleseg/core/train.py
Outdated
@@ -104,8 +109,10 @@ def train(model, | |||
optimizer) # The return is Fleet object | |||
ddp_model = paddle.distributed.fleet.distributed_model(model) | |||
|
|||
batch_sampler = paddle.io.DistributedBatchSampler( | |||
train_dataset, batch_size=batch_size, shuffle=True, drop_last=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yapf版本改为0.26.0,所有文件重新格式化一下吧
paddleseg/datasets/eg1800.py
Outdated
@@ -0,0 +1,119 @@ | |||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
所有文件license里的2020改成2021
paddleseg/datasets/eg1800.py
Outdated
import paddleseg.transforms.functional as F | ||
import cv2 | ||
import numpy as np | ||
import copy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
所有文件的import顺序应该符合PEP8规范
type: adam | ||
weight_decay: 5.0e-4 | ||
|
||
learning_rate: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最新用lr_scheduler,learning_rate要被废弃了
paddleseg/datasets/eg1800.py
Outdated
EG1800 dataset `http://xiaoyongshen.me/webpage_portrait/index.html`. | ||
|
||
Args: | ||
transforms (list): A list of image transformations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
缺少transforms2的注释
paddleseg/datasets/supervisely.py
Outdated
# add augmentation | ||
std = np.array(self.std)[:, np.newaxis, np.newaxis] | ||
mean = np.array(self.mean)[:, np.newaxis, np.newaxis] | ||
im_aug = im * std + mean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个地方逻辑有点别扭。最好是经过共同的transform后,出来两个分支transform,分别处理im和im_aug。
EG1800也是一样
self.gamma = gamma | ||
self.ignore_index = ignore_index | ||
|
||
def forward(self, inp, target): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inp, target改成logit,label吧
The MobileNetV2 implementation based on PaddlePaddle. | ||
|
||
The original article refers to | ||
Song-Hai Zhanga, Xin Donga, Jia Lib, Ruilong Lia, Yong-Liang Yangc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reference改成mobilenetv2
def forward(self, inp, target): | ||
inp = paddle.reshape(inp, [inp.shape[0], inp.shape[1], -1]) | ||
inp = paddle.transpose(inp, [0, 2, 1]) | ||
inp = paddle.reshape(inp, [-1, 2]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
应该是inp = paddle.reshape(inp, [-1, inp.shape[2])吧,用的时候不一定是二分类
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加上注释 # N,H*W,C => N*H*W,C
self.ignore_index = ignore_index | ||
|
||
def forward(self, inp, target): | ||
inp = paddle.reshape(inp, [inp.shape[0], inp.shape[1], -1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加上注释 # N,C,H,W => N,C,H*W
|
||
def forward(self, inp, target): | ||
inp = paddle.reshape(inp, [inp.shape[0], inp.shape[1], -1]) | ||
inp = paddle.transpose(inp, [0, 2, 1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加上注释 # N,C,H*W => N,H*W,C
paddleseg/models/losses/kl_loss.py
Outdated
self.kl_loss = nn.KLDivLoss(reduction="mean") | ||
self.temperature = temperature | ||
|
||
def forward(self, inp, target): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inp, target改成logit,label
paddleseg/models/losses/kl_loss.py
Outdated
@manager.LOSSES.add_component | ||
class KLLoss(nn.Layer): | ||
""" | ||
The Kullback-Leibler divergence Loss implement of Portrait Net. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
去掉portraitnet
加上
Code referenced from:
https://github.com/peterliht/knowledge-distillation-pytorch/blob/master/model/net.py
Compute the knowledge-distillation (KD) loss given outputs, labels.
"Hyperparameters": temperature and alpha
and does not contribute to the input gradient. Default ``255``. | ||
""" | ||
|
||
def __init__(self, gamma=2.0, ignore_index=255): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为了能让focal loss更通用,加上edge_label=False参数,通过edge_label判断是不是用edge监督
def __init__(self, gamma=2.0, ignore_index=255): | ||
super(FocalLoss, self).__init__() | ||
self.gamma = gamma | ||
self.ignore_index = ignore_index |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加上self.edge_label = edge_label
paddleseg/core/train.py
Outdated
@@ -42,6 +42,11 @@ def loss_computation(logits_list, labels, losses, edges=None): | |||
# Whether to use edges as labels According to loss type. | |||
if loss_i.__class__.__name__ in ('BCELoss', ) and loss_i.edge_label: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改成 if loss_i.class.name in ('BCELoss', "FocalLoss") and loss_i.edge_label:
2.Add common transform for inputs of portrait net. 3.Standardize some variable names.
paddleseg/models/portraitnet.py
Outdated
|
||
|
||
class PortraitNetHead(nn.Layer): | ||
def __init__(self, num_classes, add_edge=False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
开放参数min_channel,channel_ratio吧
paddleseg/models/portraitnet.py
Outdated
pretrained (str, optional): The path or url of pretrained model. Default: None | ||
""" | ||
|
||
def __init__(self, num_classes, backbone, add_edge=False, pretrained=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
开放参数min_channel,channel_ratio吧
|
||
|
||
@manager.MODELS.add_component | ||
class MobileNetV2(nn.Layer): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
提到文件最前头吧