Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/PaddleCV-SIG/iann
Browse files Browse the repository at this point in the history
  • Loading branch information
linhandev committed May 9, 2021
2 parents 3b3747e + b68b703 commit ea66cf2
Show file tree
Hide file tree
Showing 68 changed files with 615 additions and 9 deletions.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ python iann/__main__.py
- [x] 解决图片放大后缩小,顶部定格显示不能居中的问题
- [x] 解决放大图像后平移速度变快
- [x] 解决图像切换后窗口缩放没有清除(顺便缩放到显示框最佳)

## 新提交

1. 模型未加载时直接打开文件夹会报错,发现在打开文件夹直接加载模型前就访问了controller的属性,而模型加载时才创建的controller;这里改了一下代码的顺序。
2. 直接加载模型会报错,发现在加载模型时默认打开的图像,当未选择时图像为None;这里加了一个判断,如果直接加载模型,当图像为None就不加载。
3. 每次加载图像时根据图像大小重新设定scene的区域,这样缩小图像和切换不会造成不能居中的问题。
4. 添加最佳缩放,每次切换图片后,自适应缩放到最佳显示。
5. update_image时判断是否应该缩放重置,避免在点击点时也进行了图像缩放重置。
- [ ] 训练代码整理和训练配置的抽出(初步实现)
- [ ] 重新审查修改按键图标,确保图标完整清晰
- [ ] 界面配色以paddle蓝色为主,同时做一个ico窗口图标
- [ ] 标签可修改?
44 changes: 44 additions & 0 deletions iann/data/mdiy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from pathlib import Path

import cv2
import numpy as np

from .base import ISDataset


class MyDataset(ISDataset):
def __init__(self, dataset_path, folder_name,
images_dir_name, masks_dir_name,
**kwargs):
super(MyDataset, self).__init__(**kwargs)

self.dataset_path = Path(dataset_path) / folder_name
self._images_path = self.dataset_path / images_dir_name
self._insts_path = self.dataset_path / masks_dir_name

self.dataset_samples = [x.name for x in sorted(self._images_path.glob('*.*'))]
self._masks_paths = {x.stem: x for x in self._insts_path.glob('*.*')}

def get_sample(self, index):
image_name = self.dataset_samples[index]
image_path = str(self._images_path / image_name)
mask_path = str(self._masks_paths[image_name.split('.')[0]])

image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
instances_mask = np.max(cv2.imread(mask_path).astype(np.int32), axis=2)
instances_mask[instances_mask > 0] = 1

instances_ids = [1]

instances_info = {
x: {'ignore': False}
for x in instances_ids
}

return {
'image': image,
'instances_mask': instances_mask,
'instances_info': instances_info,
'image_id': index
}
133 changes: 133 additions & 0 deletions iann/model/loss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import numpy as np
import paddle
import paddle.nn as nn
import paddle.nn.functional as F
import util.util as U


class NormalizedFocalLossSigmoid(nn.Layer):
def __init__(self, axis=-1, alpha=0.25, gamma=2,
from_logits=False, batch_axis=0,
weight=None, size_average=True, detach_delimeter=True,
eps=1e-12, scale=1.0,
ignore_label=-1):
super(NormalizedFocalLossSigmoid, self).__init__()
self._axis = axis
self._alpha = alpha
self._gamma = gamma
self._ignore_label = ignore_label
self._weight = weight if weight is not None else 1.0
self._batch_axis = batch_axis

self._scale = scale
self._from_logits = from_logits
self._eps = eps
self._size_average = size_average
self._detach_delimeter = detach_delimeter
self._k_sum = 0

def forward(self, pred, label, sample_weight=None):
one_hot = label > 0
sample_weight = label != self._ignore_label
sample_weight = sample_weight.astype('float32')

if not self._from_logits:
pred = F.sigmoid(pred)

x = sample_weight * 0.5
y = (1 - self._alpha) * sample_weight
alpha = paddle.where(one_hot, x, y)
pt = paddle.where(one_hot, pred, 1 - pred)
sample_weight = sample_weight.astype('bool')
pt = paddle.where(sample_weight, pt, paddle.ones_like(pt))
beta = (1 - pt) ** self._gamma
sample_weight = sample_weight.astype('float32')
sw_sum = paddle.sum(sample_weight, axis=(-2, -1), keepdim=True)
beta_sum = paddle.sum(beta, axis=(-2, -1), keepdim=True)
mult = sw_sum / (beta_sum + self._eps)

if self._detach_delimeter:
mult = mult.detach()

beta = beta * mult
ignore_area = paddle.sum((label == self._ignore_label).astype('float32'), axis=tuple(range(1, len(label.shape)))).numpy()
sample_mult = paddle.mean(mult, axis=tuple(range(1, len(mult.shape)))).numpy()
if np.any(ignore_area == 0):
self._k_sum = 0.9 * self._k_sum + 0.1 * sample_mult[ignore_area == 0].mean()
loss = -alpha * beta * paddle.log(paddle.mean(pt + self._eps))
loss = self._weight * (loss * sample_weight)

if self._size_average:
bsum = paddle.sum(sample_weight, axis=U.get_dims_with_exclusion(len(sample_weight.shape), self._batch_axis))
loss = paddle.sum(loss, axis=U.get_dims_with_exclusion(len(loss.shape), self._batch_axis)) / (
bsum + self._eps)
else:
loss = paddle.sum(loss, axis=U.get_dims_with_exclusion(len(loss.shape), self._batch_axis))

return self._scale * loss


class FocalLoss(nn.Layer):
def __init__(self, axis=-1, alpha=0.25, gamma=2,
from_logits=False, batch_axis=0,
weight=None, num_class=None,
eps=1e-9, size_average=True, scale=1.0):
super(FocalLoss, self).__init__()
self._axis = axis
self._alpha = alpha
self._gamma = gamma
self._weight = weight if weight is not None else 1.0
self._batch_axis = batch_axis

self._scale = scale
self._num_class = num_class
self._from_logits = from_logits
self._eps = eps
self._size_average = size_average

def forward(self, pred, label, sample_weight=None):
if not self._from_logits:
pred = F.sigmoid(pred)

one_hot = label > 0
pt = paddle.where(one_hot, pred, 1 - pred)
t = label != -1
alpha = paddle.where(one_hot, self._alpha * t, (1 - self._alpha) * t)
beta = (1 - pt) ** self._gamma

loss = -alpha * beta * paddle.log(paddle.min(pt + self._eps, paddle.ones(1, dtype='float32')))
sample_weight = label != -1

loss = self._weight * (loss * sample_weight)

if self._size_average:
tsum = paddle.sum(label == 1, axis=U.get_dims_with_exclusion(len(label.shape), self._batch_axis))
loss = paddle.sum(loss, axis=U.get_dims_with_exclusion(len(loss.shape), self._batch_axis)) / (
tsum + self._eps)
else:
loss = paddle.sum(loss, axis=U.get_dims_with_exclusion(len(loss.shape), self._batch_axis))

return self._scale * loss


class SigmoidBinaryCrossEntropyLoss(nn.Layer):
def __init__(self, from_sigmoid=False, weight=None, batch_axis=0, ignore_label=-1):
super(SigmoidBinaryCrossEntropyLoss, self).__init__()
self._from_sigmoid = from_sigmoid
self._ignore_label = ignore_label
self._weight = weight if weight is not None else 1.0
self._batch_axis = batch_axis

def forward(self, pred, label):
label = label.reshape(pred.shape)
sample_weight = label != self._ignore_label
label = paddle.where(sample_weight, label, paddle.zeros_like(label))

if not self._from_sigmoid:
loss = F.relu(pred) - pred * label + F.softplus(-paddle.abs(pred))
else:
eps = 1e-12
loss = -(paddle.log(pred + eps) * label + paddle.log(1. - pred + eps) * (1. - label))
loss = self._weight * (loss * sample_weight)
return paddle.mean(loss, axis=U.get_dims_with_exclusion(len(loss.shape), self._batch_axis))

81 changes: 81 additions & 0 deletions iann/train/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 训练iann可用的自定义模型

目前已经可以通过简单的配置完成模型训练了,但其中有些设置还不能通过配置文件进行修改。

## 一、数据组织

在需要训练自己的数据集时,目前需要将数据集构造为如下格式,直接放在datasets文件夹中。文件名可以根据要求来进行设置,只需要在配置文件中设定好即可,图像和标签与平时使用的分割图像的用法相同。

```
datasets
|
├── train_data
| ├── img
| | └── filename_1.jpg
| └── gt
| └── filename_1.png
|
└── eval_data
├── img
| └── filename_1.jpg
└── gt
└── filename_1.png
```

## 二、训练

直接运行ritm_train.py即可开始训练。

```python
%cd train
! python ritm_train.py --config train_config.yaml
```

目前一些简单的参数已经可以在yaml配置文件中进行自定义设置,不过现阶段仍然不够灵活,可能出现各种问题。

```
iters: 100000 # 训练轮数
batch_size: 16 # bs大小
save_interval: 1000 # 保存间隔
log_iters: 10 # 打印log的间隔
worker: 4 # 子进程数
save_dir: model_output # 保存路径
use_vdl: False # 是否使用vdl
dataset:
dataset_path: iann/train/datasets # 数据集所在路径
image_name: img # 图像文件夹的名称
label_name: gt # 标签文件夹的名称
train_dataset: # 训练数据
crop_size: [320, 480] # 裁剪大小
folder_name: train_data # 训练数据文件夹的名称
val_dataset: # 验证数据
folder_name: val_data # 验证数据文件夹的名称
optimizer:
type: adam # 优化器,目前仅可以选择‘adam’和‘sgd’
learning_rate:
value_1: 5e-5 # 需要设置两个学习率
value_2: 5e-6
decay:
type: poly # 学习率衰减,目前仅支持‘poly’,可以修改下面的参数
steps: 1000
power: 0.9
end_lr: 0.0
model:
type: deeplab # 模型名称,目前支持‘hrnet’、‘deeplab’以及‘shufflenet’
backbone: resnet18 # 下面的参数是模型对应的参数,可在源码中查看
is_ritm: True
weights: None # 加载权重的路径
```



### * 说明

1. 这里有个坑,数据不能有没有标签的纯背景,这样找不到正样点训练就会卡住,并且还不报错。

Binary file added iann/train/datasets/train_data/gt/frame_2665.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2666.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2667.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2668.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2669.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2670.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2671.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2672.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2675.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2676.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2678.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2679.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2681.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2682.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2683.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2685.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/gt/frame_2686.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/img/frame_2668.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iann/train/datasets/train_data/img/frame_2670.jpg
Binary file added iann/train/datasets/train_data/img/frame_2678.jpg
Binary file added iann/train/datasets/train_data/img/frame_2680.jpg
Binary file added iann/train/datasets/train_data/img/frame_2681.jpg
Binary file added iann/train/datasets/train_data/img/frame_2686.jpg
Binary file added iann/train/datasets/val_data/gt/frame_2687.png
Binary file added iann/train/datasets/val_data/gt/frame_2689.png
Binary file added iann/train/datasets/val_data/gt/frame_2690.png
Binary file added iann/train/datasets/val_data/gt/frame_2691.png
Binary file added iann/train/datasets/val_data/gt/frame_2692.png
Binary file added iann/train/datasets/val_data/gt/frame_2693.png
Binary file added iann/train/datasets/val_data/gt/frame_2695.png
Binary file added iann/train/datasets/val_data/gt/frame_2697.png
Binary file added iann/train/datasets/val_data/gt/frame_2698.png
Binary file added iann/train/datasets/val_data/gt/frame_2699.png
Binary file added iann/train/datasets/val_data/img/frame_2687.jpg
Binary file added iann/train/datasets/val_data/img/frame_2689.jpg
Binary file added iann/train/datasets/val_data/img/frame_2690.jpg
Binary file added iann/train/datasets/val_data/img/frame_2692.jpg
Binary file added iann/train/datasets/val_data/img/frame_2693.jpg
Binary file added iann/train/datasets/val_data/img/frame_2695.jpg
Binary file added iann/train/datasets/val_data/img/frame_2697.jpg
Binary file added iann/train/datasets/val_data/img/frame_2698.jpg
Binary file added iann/train/datasets/val_data/img/frame_2699.jpg
Loading

0 comments on commit ea66cf2

Please sign in to comment.