Skip to content
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

Merged
merged 14 commits into from
Aug 4, 2021
Merged

Add PortraitNet. #1132

merged 14 commits into from
Aug 4, 2021

Conversation

txyugood
Copy link
Contributor

@txyugood txyugood commented Jul 8, 2021

  1. Add portraitNet model.
  2. Add RandomAffine in transform.py for data augmentation.
  3. Add kl_loss and focal_loss for PortraintNet.
  4. Modify train.py for PortraintNet.
  5. Add EG1800 and Supervisely dataset.
  6. Add stepdecay in config.py for training strategy.



@manager.TRANSFORMS.add_component
class RandomAffine:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个指缩放+旋转吧?是否可以通过已有的StepScaling+Rotation组合就好了,不用另外开发?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是平移、缩放和旋转。我之前试过通过StepScaling+Rotation但是效果完全不一样。所以按照论文实现了RandomAffine。

paddleseg/datasets/eg1800.py Outdated Show resolved Hide resolved
paddleseg/datasets/supervisely.py Outdated Show resolved Hide resolved
paddleseg/datasets/eg1800.py Outdated Show resolved Hide resolved
paddleseg/models/portraitnet.py Outdated Show resolved Hide resolved
paddleseg/models/portraitnet.py Outdated Show resolved Hide resolved
paddleseg/models/portraitnet.py Outdated Show resolved Hide resolved
LutaoChu
LutaoChu previously approved these changes Jul 13, 2021
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yapf版本改为0.26.0,所有文件重新格式化一下吧

@@ -0,0 +1,119 @@
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所有文件license里的2020改成2021

import paddleseg.transforms.functional as F
import cv2
import numpy as np
import copy
Copy link
Contributor

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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最新用lr_scheduler,learning_rate要被废弃了

EG1800 dataset `http://xiaoyongshen.me/webpage_portrait/index.html`.

Args:
transforms (list): A list of image transformations.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缺少transforms2的注释

# 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
Copy link
Contributor

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):
Copy link
Contributor

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
Copy link
Contributor

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])
Copy link
Contributor

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])吧,用的时候不一定是二分类

Copy link
Contributor

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])
Copy link
Contributor

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])
Copy link
Contributor

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

self.kl_loss = nn.KLDivLoss(reduction="mean")
self.temperature = temperature

def forward(self, inp, target):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inp, target改成logit,label

@manager.LOSSES.add_component
class KLLoss(nn.Layer):
"""
The Kullback-Leibler divergence Loss implement of Portrait Net.
Copy link
Contributor

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):
Copy link
Contributor

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
Copy link
Contributor

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

@@ -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:
Copy link
Contributor

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:

paddleseg/core/train.py Show resolved Hide resolved


class PortraitNetHead(nn.Layer):
def __init__(self, num_classes, add_edge=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

开放参数min_channel,channel_ratio吧

pretrained (str, optional): The path or url of pretrained model. Default: None
"""

def __init__(self, num_classes, backbone, add_edge=False, pretrained=None):
Copy link
Contributor

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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提到文件最前头吧

LutaoChu
LutaoChu previously approved these changes Jul 22, 2021
@LutaoChu LutaoChu closed this Aug 2, 2021
@LutaoChu LutaoChu reopened this Aug 2, 2021
@michaelowenliu michaelowenliu merged commit 462da0d into PaddlePaddle:develop Aug 4, 2021
@txyugood txyugood deleted the dev branch August 8, 2021 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants