使用标注工具Labelme和PaddleSeg实现铁路的图像分割
此项目针对从谷歌地图上截取得到的铁路数据集,使用Labelme进行标注,并使用PaddleSeg中的Unet模型进行训练
一、前言
该项目是用标注工具Labelme和PaddleSeg实现卫星遥感图像的铁路分割。
Labelme是一个标注工具,可以基本实现深度学习的标注任务,让我们标注自己的数据集,不过麻烦的一点就是要自己手动标注。
PaddleSeg是百度飞桨的专门用于图像分割的套件,百度飞桨还有很多很好的开发套件,感谢百度飞桨降低了我们实现深度学习的门槛!
二、准备工作
2.1 安装PaddleSeg
# 解压PaddleSeg到/home/aistudio/目录下
!unzip -oq /home/aistudio/data/data159250/PaddleSeg-release-2.4.zip
!mv PaddleSeg-release-2.4 PaddleSeg
!pip install paddleseg -i https://mirror.baidu.com/pypi/simple #导入PaddleSeg包
Looking in indexes: https://mirror.baidu.com/pypi/simple
Collecting paddleseg
Downloading https://mirror.baidu.com/pypi/packages/17/76/84a07245cb5a0ceae11a9a94c5d2be8a2cec94b3a0b883676d166eeacf2a/paddleseg-2.5.0-py3-none-any.whl (295 kB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m295.9/295.9 kB[0m [31m3.3 MB/s[0m eta [36m0:00:00[0ma [36m0:00:01[0m
[?25hRequirement already satisfied: prettytable in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddleseg) (0.7.2)
Requirement already satisfied: pyyaml>=5.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddleseg) (5.1.2)
Requirement already satisfied: scipy in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddleseg) (1.6.3)
Requirement already satisfied: filelock in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddleseg) (3.0.12)
Requirement already satisfied: tqdm in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddleseg) (4.36.1)
Requirement already satisfied: sklearn in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddleseg) (0.0)
Requirement already satisfied: opencv-python in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddleseg) (4.1.1.26)
Requirement already satisfied: visualdl>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddleseg) (2.2.0)
Requirement already satisfied: flake8>=3.7.9 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddleseg) (4.0.1)
Requirement already satisfied: pre-commit in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddleseg) (1.21.0)
Requirement already satisfied: matplotlib in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddleseg) (2.2.3)
Requirement already satisfied: Flask-Babel>=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddleseg) (1.0.0)
Requirement already satisfied: bce-python-sdk in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddleseg) (0.8.53)
Requirement already satisfied: six>=1.14.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddleseg) (1.16.0)
Requirement already satisfied: shellcheck-py in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddleseg) (0.7.1.1)
Requirement already satisfied: pandas in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddleseg) (1.1.5)
Requirement already satisfied: flask>=1.1.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddleseg) (1.1.1)
Requirement already satisfied: protobuf>=3.11.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddleseg) (3.20.1)
Requirement already satisfied: numpy in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddleseg) (1.20.3)
Requirement already satisfied: requests in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddleseg) (2.22.0)
Requirement already satisfied: Pillow>=7.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddleseg) (7.1.2)
Requirement already satisfied: scikit-learn in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from sklearn->paddleseg) (0.24.2)
Requirement already satisfied: importlib-metadata<4.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8>=3.7.9->visualdl>=2.0.0->paddleseg) (4.2.0)
Requirement already satisfied: mccabe<0.7.0,>=0.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8>=3.7.9->visualdl>=2.0.0->paddleseg) (0.6.1)
Requirement already satisfied: pycodestyle<2.9.0,>=2.8.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8>=3.7.9->visualdl>=2.0.0->paddleseg) (2.8.0)
Requirement already satisfied: pyflakes<2.5.0,>=2.4.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8>=3.7.9->visualdl>=2.0.0->paddleseg) (2.4.0)
Requirement already satisfied: Jinja2>=2.10.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.1->visualdl>=2.0.0->paddleseg) (3.0.0)
Requirement already satisfied: click>=5.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.1->visualdl>=2.0.0->paddleseg) (7.0)
Requirement already satisfied: itsdangerous>=0.24 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.1->visualdl>=2.0.0->paddleseg) (1.1.0)
Requirement already satisfied: Werkzeug>=0.15 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.1->visualdl>=2.0.0->paddleseg) (0.16.0)
Requirement already satisfied: pytz in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Flask-Babel>=1.0.0->visualdl>=2.0.0->paddleseg) (2019.3)
Requirement already satisfied: Babel>=2.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Flask-Babel>=1.0.0->visualdl>=2.0.0->paddleseg) (2.8.0)
Requirement already satisfied: future>=0.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from bce-python-sdk->visualdl>=2.0.0->paddleseg) (0.18.0)
Requirement already satisfied: pycryptodome>=3.8.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from bce-python-sdk->visualdl>=2.0.0->paddleseg) (3.9.9)
Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib->visualdl>=2.0.0->paddleseg) (1.1.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib->visualdl>=2.0.0->paddleseg) (3.0.9)
Requirement already satisfied: python-dateutil>=2.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib->visualdl>=2.0.0->paddleseg) (2.8.2)
Requirement already satisfied: cycler>=0.10 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib->visualdl>=2.0.0->paddleseg) (0.10.0)
Requirement already satisfied: aspy.yaml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.0.0->paddleseg) (1.3.0)
Requirement already satisfied: cfgv>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.0.0->paddleseg) (2.0.1)
Requirement already satisfied: nodeenv>=0.11.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.0.0->paddleseg) (1.3.4)
Requirement already satisfied: toml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.0.0->paddleseg) (0.10.0)
Requirement already satisfied: identify>=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.0.0->paddleseg) (1.4.10)
Requirement already satisfied: virtualenv>=15.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.0.0->paddleseg) (16.7.9)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.0.0->paddleseg) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.0.0->paddleseg) (2019.9.11)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.0.0->paddleseg) (1.25.6)
Requirement already satisfied: idna<2.9,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.0.0->paddleseg) (2.8)
Requirement already satisfied: joblib>=0.11 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from scikit-learn->sklearn->paddleseg) (0.14.1)
Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from scikit-learn->sklearn->paddleseg) (2.1.0)
Requirement already satisfied: typing-extensions>=3.6.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from importlib-metadata<4.3->flake8>=3.7.9->visualdl>=2.0.0->paddleseg) (4.3.0)
Requirement already satisfied: zipp>=0.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from importlib-metadata<4.3->flake8>=3.7.9->visualdl>=2.0.0->paddleseg) (3.8.1)
Requirement already satisfied: MarkupSafe>=2.0.0rc2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Jinja2>=2.10.1->flask>=1.1.1->visualdl>=2.0.0->paddleseg) (2.0.1)
Requirement already satisfied: setuptools in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from kiwisolver>=1.0.1->matplotlib->visualdl>=2.0.0->paddleseg) (56.2.0)
Installing collected packages: paddleseg
Successfully installed paddleseg-2.5.0
2.2 导入相关包
import os
import io
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image as PilImage
import paddle
import warnings
warnings.filterwarnings('ignore')
paddle.__version__#查看paddlepaddle版本
'2.2.2'
2.3 数据集准备
文件目录
|---|image1 #原始图片
|---|---|railway_050.png
|---|---|railway_051.png
|---|---|……
|---|mask1 #生成的标注图片
|---|---|railway_050.png
|---|---|railway_051.png
|---|---|……
默认解压到work/train 目录下,该目录会在每次进入环境时重置,但可以节省项目启动时间
#
!unzip -oq /home/aistudio/data/data159221/dataset.zip -d work/train
#解压我已经用Labelme工具完成标注和生成的数据集到work目录中,具体制作数据集的方法见下文说明
2.4 搜集数据
这里使用从谷歌地球上截取得到的遥感图像进行训练并进行测试,json文件为后续手工标注
2.5 用Labelme工具标注分割信息
关于Labelme工具的安装和使用,
首先修改label.txt
一共为两类
对从谷歌地球上截取得到的所有图片进行标注
(这里展示使用Labelme工具进行标注出来的图片)
2.5 用标注完生成的json文件生成数据集
在本地用python文件批量生成数据集
得到的转换后的文件为
# 批量转换算法 已转换完毕,不用运行
# ### 将Labelme生成的数据集转换为PaddleSeg支持的数据集
import base64
import json
import os
import os.path as osp
import numpy as np
import PIL.Image
# from labelme import utils
'''
制作自己的语义分割数据集需要注意以下几点:
1、我使用的labelme版本是3.16.7,建议使用该版本的labelme,有些版本的labelme会发生错误,
具体错误为:Too many dimensions: 3 > 2
安装方式为命令行pip install labelme==3.16.7
2、此处生成的标签图是8位彩色图,与视频中看起来的数据集格式不太一样。
虽然看起来是彩图,但事实上只有8位,此时每个像素点的值就是这个像素点所属的种类。
所以其实和视频中VOC数据集的格式一样。因此这样制作出来的数据集是可以正常使用的。也是正常的。
'''
# if __name__ == '__main__':
def dataset():
jpgs_path = "datasets/JPEGImages"
pngs_path = "datasets/SegmentationClass"
classes = ["_background_","railway"]
count = os.listdir("./datasets/before/")
for i in range(0, len(count)):
path = os.path.join("./datasets/before", count[i])
if os.path.isfile(path) and path.endswith('json'):
data = json.load(open(path))
if data['imageData']:
imageData = data['imageData']
else:
imagePath = os.path.join(os.path.dirname(path), data['imagePath'])
with open(imagePath, 'rb') as f:
imageData = f.read()
imageData = base64.b64encode(imageData).decode('utf-8')
img = utils.img_b64_to_arr(imageData)
label_name_to_value = {'_background_': 0}
for shape in data['shapes']:
label_name = shape['label']
if label_name in label_name_to_value:
label_value = label_name_to_value[label_name]
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value
# label_values must be dense
label_values, label_names = [], []
for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
label_values.append(lv)
label_names.append(ln)
assert label_values == list(range(len(label_values)))
lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
PIL.Image.fromarray(img).save(osp.join(jpgs_path, count[i].split(".")[0]+'.jpg'))
new = np.zeros([np.shape(img)[0],np.shape(img)[1]])
for name in label_names:
index_json = label_names.index(name)
index_all = classes.index(name)
new = new + index_all*(np.array(lbl) == index_json)
utils.lblsave(osp.join(pngs_path, count[i].split(".")[0]+'.png'), new)
print('Saved ' + count[i].split(".")[0] + '.jpg and ' + count[i].split(".")[0] + '.png')
2.6 统计数据集的样本数量
# 统计样本数量
images_path = "work/train/dataset/images1" #设置数据集路径
# images_path = "work/Line" #设置数据集路径
image_count = len([os.path.join(images_path, image_name)
for image_name in os.listdir(images_path)])
print("用于训练的图片样本数量:", image_count)
用于训练的图片样本数量: 29
2.7 生成train.txt test.txt eval.txt
image_dir_1 = "work/train/dataset/masks1"
def _sort_images(image_dir):
"""
对文件夹内的图像进行按照文件名排序
"""
images = []
labels = []
for image_name in os.listdir(image_dir):
images.append(os.path.join(os.path.join(image_dir, image_name)))
# if os.path.isdir(os.path.join(image_dir, image_name)):
# images.append(os.path.join(os.path.join(image_dir, image_name), 'img.png'))
# labels.append(os.path.join(os.path.join(image_dir, image_name), 'label.png'))
# print(images)
for image_name in os.listdir(image_dir_1):
labels.append(os.path.join(os.path.join(image_dir_1, image_name)))
# if os.path.isdir(os.path.join(image_dir, image_name)):
# images.append(os.path.join(os.path.join(image_dir, image_name), 'img.png'))
# labels.append(os.path.join(os.path.join(image_dir, image_name), 'label.png'))
# print(images)
return sorted(images), sorted(labels)
"""
这里的分割符是\t,后面使用PaddleSeg的时候要注意修改相关代码,因为PaddleSeg读取文件时默认的分割符是空格。
当然也可以将这里的\t替换为空格。
"""
def write_file(mode, images, labels):
with open('./{}.txt'.format(mode), 'w') as f:
for i in range(len(images)):
#f.write('{}\t{}\n'.format(images[i], labels[i]))
f.write('{} {}\n'.format(images[i], labels[i]))
"""
由于所有文件都是散落在文件夹中,在训练时我们需要使用的是数据集和标签对应的数据关系,
所以我们第一步是对原始的数据集进行整理,得到数据集和标签两个数组,分别一一对应。
这样可以在使用的时候能够很方便的找到原始数据和标签的对应关系,否则对于原有的文件夹图片数据无法直接应用。
"""
images, labels = _sort_images(images_path)
print( len(images))
print( len(labels))
eval_num = int(image_count * 0.15)
print(eval_num)
"""
由于图片数量有限,这里的测试集和验证集采用相同的一组图片。
"""
write_file('train', images[:-eval_num], labels[:-eval_num])
write_file('test', images[-eval_num:], labels[-eval_num:])
write_file('eval', images[-eval_num:], labels[-eval_num:])
29
29
4
2.8 观察读取的图像
原始图像VS标注图像
with open('./train.txt', 'r') as f:
i = 0
for line in f.readlines():
image_path, label_path = line.strip().split(' ')
image = np.array(PilImage.open(image_path))
label = np.array(PilImage.open(label_path))
if i > 2:
break
# 进行图片的展示
plt.figure()
plt.subplot(1,2,1),
plt.title('Train Image')
plt.imshow(image.astype('uint8'))
plt.axis('off')
plt.subplot(1,2,2),
plt.title('Label')
plt.imshow(label.astype('uint8'), cmap='gray')
plt.axis('off')
plt.show()
i = i + 1
三、配置训练模型所需的配置文件
配置文件说明
根据自己的需求修改batch_size,iters,以及num_classes
batch_size: 2
iters: 1000
train_dataset:
type: Dataset
dataset_root: /home/aistudio
train_path: train.txt
num_classes: 2
transforms:
- type: ResizeStepScaling
min_scale_factor: 0.5
max_scale_factor: 2.0
scale_step_size: 0.25
- type: RandomPaddingCrop
crop_size: [512, 512]
- type: RandomHorizontalFlip
- type: RandomDistort
brightness_range: 0.4
contrast_range: 0.4
saturation_range: 0.4
- type: Normalize
mode: train
val_dataset:
type: Dataset
dataset_root: /home/aistudio
val_path: eval.txt
transforms:
- type: Normalize
mode: val
num_classes: 2
optimizer:
type: sgd
momentum: 0.9
weight_decay: 0.0005
lr_scheduler: # 学习率的相关设置
type: PolynomialDecay # 一种学习率类型。共支持12种策略
learning_rate: 0.01
power: 0.9
end_lr: 0
loss:
types:
- type: CrossEntropyLoss
coef: [1]
model:
type: FCN
backbone:
type: HRNet_W48
pretrained: https://bj.bcebos.com/paddleseg/dygraph/hrnet_w48_ssld.tar.gz
num_classes: 2
pretrained: Null
backbone_indices: [-1]
四、模型训练
PaddleSeg 模型训练官网指导文件
模型训练
本项目采用U-Net模型,U-Net网络结构因为形似字母“U”而得名,最早是在医学影像的细胞分割任务中提出,结构简单适合处理小数量级的数据集。比较于FCN网络的像素相加,U-Net是对通道进行concat操作,保留上下文信息的同时,加强了它们之间的语义联系。整体是一个Encode-Decode的结构,如下图所示。
- 知识点1:下采样Encode包括conv和max pool,上采样Decode包括up-conv和conv。
- 知识点2:U-Net特点在于灰色箭头,利用通道融合使上下文信息紧密联系起来。
# U-Net 模型核心代码,用来参考学习
"""
paddlepaddle-gpu==2.2.1
time:2021.07.16 9:00
author:CP
backbone:U-net
"""
import paddle
from paddle import nn
class Encoder(nn.Layer):#下采样:两层卷积,两层归一化,最后池化。
def __init__(self, num_channels, num_filters):
super(Encoder,self).__init__()#继承父类的初始化
self.conv1 = nn.Conv2D(in_channels=num_channels,
out_channels=num_filters,
kernel_size=3,#3x3卷积核,步长为1,填充为1,不改变图片尺寸[H W]
stride=1,
padding=1)
self.bn1 = nn.BatchNorm(num_filters,act="relu")#归一化,并使用了激活函数
self.conv2 = nn.Conv2D(in_channels=num_filters,
out_channels=num_filters,
kernel_size=3,
stride=1,
padding=1)
self.bn2 = nn.BatchNorm(num_filters,act="relu")
self.pool = nn.MaxPool2D(kernel_size=2,stride=2,padding="SAME")#池化层,图片尺寸减半[H/2 W/2]
def forward(self,inputs):
x = self.conv1(inputs)
x = self.bn1(x)
x = self.conv2(x)
x = self.bn2(x)
x_conv = x #两个输出,灰色 ->
x_pool = self.pool(x)#两个输出,红色 |
return x_conv, x_pool
class Decoder(nn.Layer):#上采样:一层反卷积,两层卷积层,两层归一化
def __init__(self, num_channels, num_filters):
super(Decoder,self).__init__()
self.up = nn.Conv2DTranspose(in_channels=num_channels,
out_channels=num_filters,
kernel_size=2,
stride=2,
padding=0)#图片尺寸变大一倍[2*H 2*W]
self.conv1 = nn.Conv2D(in_channels=num_filters*2,
out_channels=num_filters,
kernel_size=3,
stride=1,
padding=1)
self.bn1 = nn.BatchNorm(num_filters,act="relu")
self.conv2 = nn.Conv2D(in_channels=num_filters,
out_channels=num_filters,
kernel_size=3,
stride=1,
padding=1)
self.bn2 = nn.BatchNorm(num_filters,act="relu")
def forward(self,input_conv,input_pool):
x = self.up(input_pool)
h_diff = (input_conv.shape[2]-x.shape[2])
w_diff = (input_conv.shape[3]-x.shape[3])
pad = nn.Pad2D(padding=[h_diff//2, h_diff-h_diff//2, w_diff//2, w_diff-w_diff//2])
x = pad(x) #以下采样保存的feature map为基准,填充上采样的feature map尺寸
x = paddle.concat(x=[input_conv,x],axis=1)#考虑上下文信息,in_channels扩大两倍
x = self.conv1(x)
x = self.bn1(x)
x = self.conv2(x)
x = self.bn2(x)
return x
class UNet_2(nn.Layer):
def __init__(self,num_classes=59):
super(UNet_2,self).__init__()
self.down1 = Encoder(num_channels= 3, num_filters=64) #下采样
self.down2 = Encoder(num_channels= 64, num_filters=128)
self.down3 = Encoder(num_channels=128, num_filters=256)
self.down4 = Encoder(num_channels=256, num_filters=512)
self.mid_conv1 = nn.Conv2D(512,1024,1) #中间层
self.mid_bn1 = nn.BatchNorm(1024,act="relu")
self.mid_conv2 = nn.Conv2D(1024,1024,1)
self.mid_bn2 = nn.BatchNorm(1024,act="relu")
self.up4 = Decoder(1024,512) #上采样
self.up3 = Decoder(512,256)
self.up2 = Decoder(256,128)
self.up1 = Decoder(128,64)
self.last_conv = nn.Conv2D(64,num_classes,1) #1x1卷积,softmax做分类
def forward(self,inputs):
x1, x = self.down1(inputs)
x2, x = self.down2(x)
x3, x = self.down3(x)
x4, x = self.down4(x)
x = self.mid_conv1(x)
x = self.mid_bn1(x)
x = self.mid_conv2(x)
x = self.mid_bn2(x)
x = self.up4(x4, x)
x = self.up3(x3, x)
x = self.up2(x2, x)
x = self.up1(x1, x)
x = self.last_conv(x)
return x
paddle.summary(UNet_2(), (1, 3, 600, 600))
W0721 15:32:39.766975 192 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1
W0721 15:32:39.770218 192 device_context.cc:465] device: 0, cuDNN Version: 7.6.
---------------------------------------------------------------------------------------------------------------------
Layer (type) Input Shape Output Shape Param #
=====================================================================================================================
Conv2D-1 [[1, 3, 600, 600]] [1, 64, 600, 600] 1,792
BatchNorm-1 [[1, 64, 600, 600]] [1, 64, 600, 600] 256
Conv2D-2 [[1, 64, 600, 600]] [1, 64, 600, 600] 36,928
BatchNorm-2 [[1, 64, 600, 600]] [1, 64, 600, 600] 256
MaxPool2D-1 [[1, 64, 600, 600]] [1, 64, 300, 300] 0
Encoder-1 [[1, 3, 600, 600]] [[1, 64, 600, 600], [1, 64, 300, 300]] 0
Conv2D-3 [[1, 64, 300, 300]] [1, 128, 300, 300] 73,856
BatchNorm-3 [[1, 128, 300, 300]] [1, 128, 300, 300] 512
Conv2D-4 [[1, 128, 300, 300]] [1, 128, 300, 300] 147,584
BatchNorm-4 [[1, 128, 300, 300]] [1, 128, 300, 300] 512
MaxPool2D-2 [[1, 128, 300, 300]] [1, 128, 150, 150] 0
Encoder-2 [[1, 64, 300, 300]] [[1, 128, 300, 300], [1, 128, 150, 150]] 0
Conv2D-5 [[1, 128, 150, 150]] [1, 256, 150, 150] 295,168
BatchNorm-5 [[1, 256, 150, 150]] [1, 256, 150, 150] 1,024
Conv2D-6 [[1, 256, 150, 150]] [1, 256, 150, 150] 590,080
BatchNorm-6 [[1, 256, 150, 150]] [1, 256, 150, 150] 1,024
MaxPool2D-3 [[1, 256, 150, 150]] [1, 256, 75, 75] 0
Encoder-3 [[1, 128, 150, 150]] [[1, 256, 150, 150], [1, 256, 75, 75]] 0
Conv2D-7 [[1, 256, 75, 75]] [1, 512, 75, 75] 1,180,160
BatchNorm-7 [[1, 512, 75, 75]] [1, 512, 75, 75] 2,048
Conv2D-8 [[1, 512, 75, 75]] [1, 512, 75, 75] 2,359,808
BatchNorm-8 [[1, 512, 75, 75]] [1, 512, 75, 75] 2,048
MaxPool2D-4 [[1, 512, 75, 75]] [1, 512, 38, 38] 0
Encoder-4 [[1, 256, 75, 75]] [[1, 512, 75, 75], [1, 512, 38, 38]] 0
Conv2D-9 [[1, 512, 38, 38]] [1, 1024, 38, 38] 525,312
BatchNorm-9 [[1, 1024, 38, 38]] [1, 1024, 38, 38] 4,096
Conv2D-10 [[1, 1024, 38, 38]] [1, 1024, 38, 38] 1,049,600
BatchNorm-10 [[1, 1024, 38, 38]] [1, 1024, 38, 38] 4,096
Conv2DTranspose-1 [[1, 1024, 38, 38]] [1, 512, 76, 76] 2,097,664
Conv2D-11 [[1, 1024, 75, 75]] [1, 512, 75, 75] 4,719,104
BatchNorm-11 [[1, 512, 75, 75]] [1, 512, 75, 75] 2,048
Conv2D-12 [[1, 512, 75, 75]] [1, 512, 75, 75] 2,359,808
BatchNorm-12 [[1, 512, 75, 75]] [1, 512, 75, 75] 2,048
Decoder-1 [[1, 512, 75, 75], [1, 1024, 38, 38]] [1, 512, 75, 75] 0
Conv2DTranspose-2 [[1, 512, 75, 75]] [1, 256, 150, 150] 524,544
Conv2D-13 [[1, 512, 150, 150]] [1, 256, 150, 150] 1,179,904
BatchNorm-13 [[1, 256, 150, 150]] [1, 256, 150, 150] 1,024
Conv2D-14 [[1, 256, 150, 150]] [1, 256, 150, 150] 590,080
BatchNorm-14 [[1, 256, 150, 150]] [1, 256, 150, 150] 1,024
Decoder-2 [[1, 256, 150, 150], [1, 512, 75, 75]] [1, 256, 150, 150] 0
Conv2DTranspose-3 [[1, 256, 150, 150]] [1, 128, 300, 300] 131,200
Conv2D-15 [[1, 256, 300, 300]] [1, 128, 300, 300] 295,040
BatchNorm-15 [[1, 128, 300, 300]] [1, 128, 300, 300] 512
Conv2D-16 [[1, 128, 300, 300]] [1, 128, 300, 300] 147,584
BatchNorm-16 [[1, 128, 300, 300]] [1, 128, 300, 300] 512
Decoder-3 [[1, 128, 300, 300], [1, 256, 150, 150]] [1, 128, 300, 300] 0
Conv2DTranspose-4 [[1, 128, 300, 300]] [1, 64, 600, 600] 32,832
Conv2D-17 [[1, 128, 600, 600]] [1, 64, 600, 600] 73,792
BatchNorm-17 [[1, 64, 600, 600]] [1, 64, 600, 600] 256
Conv2D-18 [[1, 64, 600, 600]] [1, 64, 600, 600] 36,928
BatchNorm-18 [[1, 64, 600, 600]] [1, 64, 600, 600] 256
Decoder-4 [[1, 64, 600, 600], [1, 128, 300, 300]] [1, 64, 600, 600] 0
Conv2D-19 [[1, 64, 600, 600]] [1, 59, 600, 600] 3,835
=====================================================================================================================
Total params: 18,476,155
Trainable params: 18,452,603
Non-trainable params: 23,552
---------------------------------------------------------------------------------------------------------------------
Input size (MB): 4.12
Forward/backward pass size (MB): 3998.34
Params size (MB): 70.48
Estimated Total Size (MB): 4072.94
---------------------------------------------------------------------------------------------------------------------
{'total_params': 18476155, 'trainable_params': 18452603}
!export CUDA_VISIBLE_DEVICES=0 # 设置1张可用的卡
# windows下请执行以下命令
# set CUDA_VISIBLE_DEVICES=0
!python PaddleSeg/train.py \
--config mynet.yml \
--do_eval \
--use_vdl \
--save_interval 100 \
--save_dir output
2022-07-21 15:33:07 [INFO]
------------Environment Information-------------
platform: Linux-4.15.0-140-generic-x86_64-with-debian-stretch-sid
Python: 3.7.4 (default, Aug 13 2019, 20:35:49) [GCC 7.3.0]
Paddle compiled with cuda: True
NVCC: Cuda compilation tools, release 10.1, V10.1.243
cudnn: 7.6
GPUs used: 1
CUDA_VISIBLE_DEVICES: None
GPU: ['GPU 0: Tesla V100-SXM2-32GB']
GCC: gcc (Ubuntu 7.5.0-3ubuntu1~16.04) 7.5.0
PaddleSeg: 2.4.0
PaddlePaddle: 2.2.2
OpenCV: 4.1.1
------------------------------------------------
2022-07-21 15:33:07 [INFO]
---------------Config Information---------------
batch_size: 2
iters: 1000
loss:
coef:
- 1
types:
- ignore_index: 255
type: CrossEntropyLoss
lr_scheduler:
end_lr: 0
learning_rate: 0.01
power: 0.9
type: PolynomialDecay
model:
backbone:
pretrained: https://bj.bcebos.com/paddleseg/dygraph/hrnet_w48_ssld.tar.gz
type: HRNet_W48
backbone_indices:
- -1
num_classes: 2
pretrained: null
type: FCN
optimizer:
momentum: 0.9
type: sgd
weight_decay: 0.0005
train_dataset:
dataset_root: /home/aistudio
mode: train
num_classes: 2
train_path: train.txt
transforms:
- max_scale_factor: 2.0
min_scale_factor: 0.5
scale_step_size: 0.25
type: ResizeStepScaling
- crop_size:
- 512
- 512
type: RandomPaddingCrop
- type: RandomHorizontalFlip
- brightness_range: 0.4
contrast_range: 0.4
saturation_range: 0.4
type: RandomDistort
- type: Normalize
type: Dataset
val_dataset:
dataset_root: /home/aistudio
mode: val
num_classes: 2
transforms:
- type: Normalize
type: Dataset
val_path: eval.txt
------------------------------------------------
W0721 15:33:07.569408 563 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1
W0721 15:33:07.569442 563 device_context.cc:465] device: 0, cuDNN Version: 7.6.
2022-07-21 15:33:11 [INFO] Loading pretrained model from https://bj.bcebos.com/paddleseg/dygraph/hrnet_w48_ssld.tar.gz
Connecting to https://bj.bcebos.com/paddleseg/dygraph/hrnet_w48_ssld.tar.gz
Downloading hrnet_w48_ssld.tar.gz
[==================================================] 100.00%
Uncompress hrnet_w48_ssld.tar.gz
[==================================================] 100.00%
2022-07-21 15:33:27 [INFO] There are 1525/1525 variables loaded into HRNet.
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/nn/layer/norm.py:653: UserWarning: When training, we now always track global mean and variance.
"When training, we now always track global mean and variance.")
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/math_op_patch.py:253: UserWarning: The dtype of left and right variables are not the same, left dtype is paddle.float32, but right dtype is paddle.int64, the right dtype will convert to paddle.float32
format(lhs_dtype, rhs_dtype, lhs_dtype))
2022-07-21 15:33:31 [INFO] [TRAIN] epoch: 1, iter: 10/1000, loss: 0.6690, lr: 0.009919, batch_cost: 0.3581, reader_cost: 0.07111, ips: 5.5854 samples/sec | ETA 00:05:54
2022-07-21 15:33:33 [INFO] [TRAIN] epoch: 2, iter: 20/1000, loss: 0.5120, lr: 0.009829, batch_cost: 0.2637, reader_cost: 0.00918, ips: 7.5854 samples/sec | ETA 00:04:18
2022-07-21 15:33:36 [INFO] [TRAIN] epoch: 3, iter: 30/1000, loss: 0.5978, lr: 0.009739, batch_cost: 0.2628, reader_cost: 0.01122, ips: 7.6107 samples/sec | ETA 00:04:14
2022-07-21 15:33:39 [INFO] [TRAIN] epoch: 4, iter: 40/1000, loss: 0.5446, lr: 0.009648, batch_cost: 0.2523, reader_cost: 0.00835, ips: 7.9258 samples/sec | ETA 00:04:02
2022-07-21 15:33:41 [INFO] [TRAIN] epoch: 5, iter: 50/1000, loss: 0.4189, lr: 0.009558, batch_cost: 0.2666, reader_cost: 0.01063, ips: 7.5012 samples/sec | ETA 00:04:13
2022-07-21 15:33:44 [INFO] [TRAIN] epoch: 5, iter: 60/1000, loss: 0.4695, lr: 0.009467, batch_cost: 0.2466, reader_cost: 0.00018, ips: 8.1087 samples/sec | ETA 00:03:51
2022-07-21 15:33:46 [INFO] [TRAIN] epoch: 6, iter: 70/1000, loss: 0.4872, lr: 0.009377, batch_cost: 0.2601, reader_cost: 0.00963, ips: 7.6893 samples/sec | ETA 00:04:01
2022-07-21 15:33:49 [INFO] [TRAIN] epoch: 7, iter: 80/1000, loss: 0.4103, lr: 0.009286, batch_cost: 0.2602, reader_cost: 0.00833, ips: 7.6863 samples/sec | ETA 00:03:59
2022-07-21 15:33:52 [INFO] [TRAIN] epoch: 8, iter: 90/1000, loss: 0.4378, lr: 0.009195, batch_cost: 0.2549, reader_cost: 0.00888, ips: 7.8468 samples/sec | ETA 00:03:51
2022-07-21 15:33:54 [INFO] [TRAIN] epoch: 9, iter: 100/1000, loss: 0.4017, lr: 0.009104, batch_cost: 0.2593, reader_cost: 0.00699, ips: 7.7136 samples/sec | ETA 00:03:53
2022-07-21 15:33:54 [INFO] Start evaluating (total_samples: 4, total_iters: 4)...
4/4 [==============================] - 0s 96ms/step - batch_cost: 0.0955 - reader cost: 0.00
2022-07-21 15:33:55 [INFO] [EVAL] #Images: 4 mIoU: 0.3853 Acc: 0.7707 Kappa: 0.0000 Dice: 0.4352
2022-07-21 15:33:55 [INFO] [EVAL] Class IoU:
[0.7707 0. ]
2022-07-21 15:33:55 [INFO] [EVAL] Class Acc:
[0.7707 0. ]
2022-07-21 15:34:00 [INFO] [EVAL] The model with the best validation mIoU (0.3853) was saved at iter 100.
2022-07-21 15:34:02 [INFO] [TRAIN] epoch: 10, iter: 110/1000, loss: 0.5353, lr: 0.009013, batch_cost: 0.2572, reader_cost: 0.00663, ips: 7.7759 samples/sec | ETA 00:03:48
2022-07-21 15:34:05 [INFO] [TRAIN] epoch: 10, iter: 120/1000, loss: 0.4664, lr: 0.008922, batch_cost: 0.2397, reader_cost: 0.00013, ips: 8.3440 samples/sec | ETA 00:03:30
2022-07-21 15:34:08 [INFO] [TRAIN] epoch: 11, iter: 130/1000, loss: 0.5065, lr: 0.008831, batch_cost: 0.2674, reader_cost: 0.00935, ips: 7.4785 samples/sec | ETA 00:03:52
2022-07-21 15:34:10 [INFO] [TRAIN] epoch: 12, iter: 140/1000, loss: 0.3433, lr: 0.008740, batch_cost: 0.2469, reader_cost: 0.00667, ips: 8.1013 samples/sec | ETA 00:03:32
2022-07-21 15:34:13 [INFO] [TRAIN] epoch: 13, iter: 150/1000, loss: 0.3888, lr: 0.008648, batch_cost: 0.2565, reader_cost: 0.00821, ips: 7.7966 samples/sec | ETA 00:03:38
2022-07-21 15:34:15 [INFO] [TRAIN] epoch: 14, iter: 160/1000, loss: 0.4190, lr: 0.008557, batch_cost: 0.2560, reader_cost: 0.00921, ips: 7.8119 samples/sec | ETA 00:03:35
2022-07-21 15:34:18 [INFO] [TRAIN] epoch: 15, iter: 170/1000, loss: 0.3911, lr: 0.008465, batch_cost: 0.2449, reader_cost: 0.00631, ips: 8.1667 samples/sec | ETA 00:03:23
2022-07-21 15:34:20 [INFO] [TRAIN] epoch: 15, iter: 180/1000, loss: 0.5541, lr: 0.008374, batch_cost: 0.2386, reader_cost: 0.00017, ips: 8.3812 samples/sec | ETA 00:03:15
2022-07-21 15:34:22 [INFO] [TRAIN] epoch: 16, iter: 190/1000, loss: 0.4818, lr: 0.008282, batch_cost: 0.2526, reader_cost: 0.00770, ips: 7.9178 samples/sec | ETA 00:03:24
2022-07-21 15:34:25 [INFO] [TRAIN] epoch: 17, iter: 200/1000, loss: 0.3490, lr: 0.008190, batch_cost: 0.2418, reader_cost: 0.00584, ips: 8.2705 samples/sec | ETA 00:03:13
2022-07-21 15:34:25 [INFO] Start evaluating (total_samples: 4, total_iters: 4)...
4/4 [==============================] - 0s 84ms/step - batch_cost: 0.0840 - reader cost: 0.00
2022-07-21 15:34:25 [INFO] [EVAL] #Images: 4 mIoU: 0.3853 Acc: 0.7707 Kappa: 0.0000 Dice: 0.4352
2022-07-21 15:34:25 [INFO] [EVAL] Class IoU:
[0.7707 0. ]
2022-07-21 15:34:25 [INFO] [EVAL] Class Acc:
[0.7707 0. ]
2022-07-21 15:34:26 [INFO] [EVAL] The model with the best validation mIoU (0.3853) was saved at iter 100.
2022-07-21 15:34:29 [INFO] [TRAIN] epoch: 18, iter: 210/1000, loss: 0.4200, lr: 0.008098, batch_cost: 0.2453, reader_cost: 0.00803, ips: 8.1524 samples/sec | ETA 00:03:13
2022-07-21 15:34:31 [INFO] [TRAIN] epoch: 19, iter: 220/1000, loss: 0.4336, lr: 0.008005, batch_cost: 0.2379, reader_cost: 0.00620, ips: 8.4075 samples/sec | ETA 00:03:05
2022-07-21 15:34:34 [INFO] [TRAIN] epoch: 20, iter: 230/1000, loss: 0.3803, lr: 0.007913, batch_cost: 0.2410, reader_cost: 0.00539, ips: 8.2976 samples/sec | ETA 00:03:05
2022-07-21 15:34:36 [INFO] [TRAIN] epoch: 20, iter: 240/1000, loss: 0.4228, lr: 0.007821, batch_cost: 0.2293, reader_cost: 0.00013, ips: 8.7236 samples/sec | ETA 00:02:54
2022-07-21 15:34:38 [INFO] [TRAIN] epoch: 21, iter: 250/1000, loss: 0.3879, lr: 0.007728, batch_cost: 0.2432, reader_cost: 0.00616, ips: 8.2238 samples/sec | ETA 00:03:02
2022-07-21 15:34:41 [INFO] [TRAIN] epoch: 22, iter: 260/1000, loss: 0.4500, lr: 0.007635, batch_cost: 0.2470, reader_cost: 0.00493, ips: 8.0988 samples/sec | ETA 00:03:02
2022-07-21 15:34:43 [INFO] [TRAIN] epoch: 23, iter: 270/1000, loss: 0.3834, lr: 0.007543, batch_cost: 0.2335, reader_cost: 0.00494, ips: 8.5656 samples/sec | ETA 00:02:50
2022-07-21 15:34:45 [INFO] [TRAIN] epoch: 24, iter: 280/1000, loss: 0.3129, lr: 0.007450, batch_cost: 0.2355, reader_cost: 0.00543, ips: 8.4912 samples/sec | ETA 00:02:49
2022-07-21 15:34:48 [INFO] [TRAIN] epoch: 25, iter: 290/1000, loss: 0.3466, lr: 0.007357, batch_cost: 0.2498, reader_cost: 0.00824, ips: 8.0062 samples/sec | ETA 00:02:57
2022-07-21 15:34:50 [INFO] [TRAIN] epoch: 25, iter: 300/1000, loss: 0.3158, lr: 0.007264, batch_cost: 0.2341, reader_cost: 0.00012, ips: 8.5420 samples/sec | ETA 00:02:43
2022-07-21 15:34:50 [INFO] Start evaluating (total_samples: 4, total_iters: 4)...
4/4 [==============================] - 0s 80ms/step - batch_cost: 0.0799 - reader cost: 0.00
2022-07-21 15:34:51 [INFO] [EVAL] #Images: 4 mIoU: 0.6495 Acc: 0.8213 Kappa: 0.5665 Dice: 0.7801
2022-07-21 15:34:51 [INFO] [EVAL] Class IoU:
[0.7782 0.5208]
2022-07-21 15:34:51 [INFO] [EVAL] Class Acc:
[0.947 0.5749]
2022-07-21 15:34:54 [INFO] [EVAL] The model with the best validation mIoU (0.6495) was saved at iter 300.
2022-07-21 15:34:56 [INFO] [TRAIN] epoch: 26, iter: 310/1000, loss: 0.2964, lr: 0.007170, batch_cost: 0.2522, reader_cost: 0.00523, ips: 7.9295 samples/sec | ETA 00:02:54
2022-07-21 15:34:59 [INFO] [TRAIN] epoch: 27, iter: 320/1000, loss: 0.3348, lr: 0.007077, batch_cost: 0.2440, reader_cost: 0.00552, ips: 8.1969 samples/sec | ETA 00:02:45
2022-07-21 15:35:01 [INFO] [TRAIN] epoch: 28, iter: 330/1000, loss: 0.3446, lr: 0.006983, batch_cost: 0.2368, reader_cost: 0.00886, ips: 8.4456 samples/sec | ETA 00:02:38
2022-07-21 15:35:04 [INFO] [TRAIN] epoch: 29, iter: 340/1000, loss: 0.2897, lr: 0.006889, batch_cost: 0.2354, reader_cost: 0.00589, ips: 8.4950 samples/sec | ETA 00:02:35
2022-07-21 15:35:06 [INFO] [TRAIN] epoch: 30, iter: 350/1000, loss: 0.3283, lr: 0.006796, batch_cost: 0.2342, reader_cost: 0.00934, ips: 8.5409 samples/sec | ETA 00:02:32
2022-07-21 15:35:08 [INFO] [TRAIN] epoch: 30, iter: 360/1000, loss: 0.3124, lr: 0.006702, batch_cost: 0.2252, reader_cost: 0.00010, ips: 8.8795 samples/sec | ETA 00:02:24
2022-07-21 15:35:11 [INFO] [TRAIN] epoch: 31, iter: 370/1000, loss: 0.3253, lr: 0.006607, batch_cost: 0.2446, reader_cost: 0.00624, ips: 8.1764 samples/sec | ETA 00:02:34
2022-07-21 15:35:13 [INFO] [TRAIN] epoch: 32, iter: 380/1000, loss: 0.2911, lr: 0.006513, batch_cost: 0.2429, reader_cost: 0.00599, ips: 8.2346 samples/sec | ETA 00:02:30
2022-07-21 15:35:16 [INFO] [TRAIN] epoch: 33, iter: 390/1000, loss: 0.2778, lr: 0.006419, batch_cost: 0.2477, reader_cost: 0.00762, ips: 8.0753 samples/sec | ETA 00:02:31
2022-07-21 15:35:18 [INFO] [TRAIN] epoch: 34, iter: 400/1000, loss: 0.3425, lr: 0.006324, batch_cost: 0.2425, reader_cost: 0.00661, ips: 8.2472 samples/sec | ETA 00:02:25
2022-07-21 15:35:18 [INFO] Start evaluating (total_samples: 4, total_iters: 4)...
4/4 [==============================] - 0s 81ms/step - batch_cost: 0.0807 - reader cost: 0.00
2022-07-21 15:35:18 [INFO] [EVAL] #Images: 4 mIoU: 0.5487 Acc: 0.7262 Kappa: 0.4482 Dice: 0.7033
2022-07-21 15:35:18 [INFO] [EVAL] Class IoU:
[0.6471 0.4503]
2022-07-21 15:35:18 [INFO] [EVAL] Class Acc:
[0.99 0.4549]
2022-07-21 15:35:19 [INFO] [EVAL] The model with the best validation mIoU (0.6495) was saved at iter 300.
2022-07-21 15:35:22 [INFO] [TRAIN] epoch: 35, iter: 410/1000, loss: 0.3291, lr: 0.006229, batch_cost: 0.2394, reader_cost: 0.00721, ips: 8.3545 samples/sec | ETA 00:02:21
2022-07-21 15:35:24 [INFO] [TRAIN] epoch: 35, iter: 420/1000, loss: 0.3165, lr: 0.006134, batch_cost: 0.2390, reader_cost: 0.00012, ips: 8.3684 samples/sec | ETA 00:02:18
2022-07-21 15:35:27 [INFO] [TRAIN] epoch: 36, iter: 430/1000, loss: 0.2764, lr: 0.006039, batch_cost: 0.2482, reader_cost: 0.00726, ips: 8.0568 samples/sec | ETA 00:02:21
2022-07-21 15:35:29 [INFO] [TRAIN] epoch: 37, iter: 440/1000, loss: 0.2465, lr: 0.005944, batch_cost: 0.2427, reader_cost: 0.00701, ips: 8.2410 samples/sec | ETA 00:02:15
2022-07-21 15:35:31 [INFO] [TRAIN] epoch: 38, iter: 450/1000, loss: 0.4015, lr: 0.005848, batch_cost: 0.2437, reader_cost: 0.00596, ips: 8.2053 samples/sec | ETA 00:02:14
2022-07-21 15:35:34 [INFO] [TRAIN] epoch: 39, iter: 460/1000, loss: 0.2761, lr: 0.005753, batch_cost: 0.2392, reader_cost: 0.00912, ips: 8.3600 samples/sec | ETA 00:02:09
2022-07-21 15:35:36 [INFO] [TRAIN] epoch: 40, iter: 470/1000, loss: 0.3243, lr: 0.005657, batch_cost: 0.2371, reader_cost: 0.00578, ips: 8.4358 samples/sec | ETA 00:02:05
2022-07-21 15:35:38 [INFO] [TRAIN] epoch: 40, iter: 480/1000, loss: 0.2901, lr: 0.005561, batch_cost: 0.2303, reader_cost: 0.00012, ips: 8.6862 samples/sec | ETA 00:01:59
2022-07-21 15:35:41 [INFO] [TRAIN] epoch: 41, iter: 490/1000, loss: 0.2666, lr: 0.005465, batch_cost: 0.2508, reader_cost: 0.00786, ips: 7.9742 samples/sec | ETA 00:02:07
2022-07-21 15:35:43 [INFO] [TRAIN] epoch: 42, iter: 500/1000, loss: 0.2541, lr: 0.005369, batch_cost: 0.2439, reader_cost: 0.00539, ips: 8.2018 samples/sec | ETA 00:02:01
2022-07-21 15:35:43 [INFO] Start evaluating (total_samples: 4, total_iters: 4)...
4/4 [==============================] - 0s 85ms/step - batch_cost: 0.0853 - reader cost: 0.00
2022-07-21 15:35:44 [INFO] [EVAL] #Images: 4 mIoU: 0.7347 Acc: 0.8851 Kappa: 0.6822 Dice: 0.8411
2022-07-21 15:35:44 [INFO] [EVAL] Class IoU:
[0.8599 0.6096]
2022-07-21 15:35:44 [INFO] [EVAL] Class Acc:
[0.934 0.7339]
2022-07-21 15:35:49 [INFO] [EVAL] The model with the best validation mIoU (0.7347) was saved at iter 500.
2022-07-21 15:35:52 [INFO] [TRAIN] epoch: 43, iter: 510/1000, loss: 0.2376, lr: 0.005272, batch_cost: 0.2428, reader_cost: 0.00824, ips: 8.2381 samples/sec | ETA 00:01:58
2022-07-21 15:35:54 [INFO] [TRAIN] epoch: 44, iter: 520/1000, loss: 0.2546, lr: 0.005175, batch_cost: 0.2389, reader_cost: 0.00604, ips: 8.3713 samples/sec | ETA 00:01:54
2022-07-21 15:35:56 [INFO] [TRAIN] epoch: 45, iter: 530/1000, loss: 0.2518, lr: 0.005078, batch_cost: 0.2427, reader_cost: 0.00799, ips: 8.2395 samples/sec | ETA 00:01:54
2022-07-21 15:35:59 [INFO] [TRAIN] epoch: 45, iter: 540/1000, loss: 0.2570, lr: 0.004981, batch_cost: 0.2338, reader_cost: 0.00012, ips: 8.5554 samples/sec | ETA 00:01:47
2022-07-21 15:36:01 [INFO] [TRAIN] epoch: 46, iter: 550/1000, loss: 0.2536, lr: 0.004884, batch_cost: 0.2498, reader_cost: 0.00731, ips: 8.0063 samples/sec | ETA 00:01:52
2022-07-21 15:36:04 [INFO] [TRAIN] epoch: 47, iter: 560/1000, loss: 0.2203, lr: 0.004786, batch_cost: 0.2440, reader_cost: 0.00544, ips: 8.1966 samples/sec | ETA 00:01:47
2022-07-21 15:36:06 [INFO] [TRAIN] epoch: 48, iter: 570/1000, loss: 0.2542, lr: 0.004688, batch_cost: 0.2417, reader_cost: 0.00752, ips: 8.2763 samples/sec | ETA 00:01:43
2022-07-21 15:36:08 [INFO] [TRAIN] epoch: 49, iter: 580/1000, loss: 0.2159, lr: 0.004590, batch_cost: 0.2426, reader_cost: 0.00877, ips: 8.2435 samples/sec | ETA 00:01:41
2022-07-21 15:36:11 [INFO] [TRAIN] epoch: 50, iter: 590/1000, loss: 0.2217, lr: 0.004492, batch_cost: 0.2439, reader_cost: 0.00557, ips: 8.2003 samples/sec | ETA 00:01:39
2022-07-21 15:36:13 [INFO] [TRAIN] epoch: 50, iter: 600/1000, loss: 0.1788, lr: 0.004394, batch_cost: 0.2390, reader_cost: 0.00014, ips: 8.3679 samples/sec | ETA 00:01:35
2022-07-21 15:36:13 [INFO] Start evaluating (total_samples: 4, total_iters: 4)...
4/4 [==============================] - 0s 86ms/step - batch_cost: 0.0858 - reader cost: 0.00
2022-07-21 15:36:14 [INFO] [EVAL] #Images: 4 mIoU: 0.7769 Acc: 0.9038 Kappa: 0.7417 Dice: 0.8706
2022-07-21 15:36:14 [INFO] [EVAL] Class IoU:
[0.8799 0.6739]
2022-07-21 15:36:14 [INFO] [EVAL] Class Acc:
[0.9586 0.7515]
2022-07-21 15:36:26 [INFO] [EVAL] The model with the best validation mIoU (0.7769) was saved at iter 600.
2022-07-21 15:36:28 [INFO] [TRAIN] epoch: 51, iter: 610/1000, loss: 0.2435, lr: 0.004295, batch_cost: 0.2491, reader_cost: 0.00597, ips: 8.0276 samples/sec | ETA 00:01:37
2022-07-21 15:36:31 [INFO] [TRAIN] epoch: 52, iter: 620/1000, loss: 0.2067, lr: 0.004196, batch_cost: 0.2428, reader_cost: 0.00590, ips: 8.2359 samples/sec | ETA 00:01:32
2022-07-21 15:36:33 [INFO] [TRAIN] epoch: 53, iter: 630/1000, loss: 0.2416, lr: 0.004097, batch_cost: 0.2429, reader_cost: 0.00728, ips: 8.2350 samples/sec | ETA 00:01:29
2022-07-21 15:36:35 [INFO] [TRAIN] epoch: 54, iter: 640/1000, loss: 0.2269, lr: 0.003997, batch_cost: 0.2423, reader_cost: 0.00765, ips: 8.2538 samples/sec | ETA 00:01:27
2022-07-21 15:36:38 [INFO] [TRAIN] epoch: 55, iter: 650/1000, loss: 0.2244, lr: 0.003897, batch_cost: 0.2497, reader_cost: 0.00834, ips: 8.0093 samples/sec | ETA 00:01:27
2022-07-21 15:36:40 [INFO] [TRAIN] epoch: 55, iter: 660/1000, loss: 0.2318, lr: 0.003797, batch_cost: 0.2297, reader_cost: 0.00012, ips: 8.7086 samples/sec | ETA 00:01:18
2022-07-21 15:36:43 [INFO] [TRAIN] epoch: 56, iter: 670/1000, loss: 0.2207, lr: 0.003697, batch_cost: 0.2442, reader_cost: 0.00545, ips: 8.1891 samples/sec | ETA 00:01:20
2022-07-21 15:36:45 [INFO] [TRAIN] epoch: 57, iter: 680/1000, loss: 0.1869, lr: 0.003596, batch_cost: 0.2457, reader_cost: 0.00829, ips: 8.1416 samples/sec | ETA 00:01:18
2022-07-21 15:36:48 [INFO] [TRAIN] epoch: 58, iter: 690/1000, loss: 0.1832, lr: 0.003495, batch_cost: 0.2387, reader_cost: 0.00707, ips: 8.3786 samples/sec | ETA 00:01:13
2022-07-21 15:36:50 [INFO] [TRAIN] epoch: 59, iter: 700/1000, loss: 0.2233, lr: 0.003394, batch_cost: 0.2403, reader_cost: 0.00913, ips: 8.3218 samples/sec | ETA 00:01:12
2022-07-21 15:36:50 [INFO] Start evaluating (total_samples: 4, total_iters: 4)...
4/4 [==============================] - 0s 83ms/step - batch_cost: 0.0830 - reader cost: 0.00
2022-07-21 15:36:50 [INFO] [EVAL] #Images: 4 mIoU: 0.7004 Acc: 0.8578 Kappa: 0.6376 Dice: 0.8176
2022-07-21 15:36:50 [INFO] [EVAL] Class IoU:
[0.8235 0.5773]
2022-07-21 15:36:50 [INFO] [EVAL] Class Acc:
[0.9498 0.6445]
2022-07-21 15:36:59 [INFO] [EVAL] The model with the best validation mIoU (0.7769) was saved at iter 600.
2022-07-21 15:37:01 [INFO] [TRAIN] epoch: 60, iter: 710/1000, loss: 0.1831, lr: 0.003292, batch_cost: 0.2468, reader_cost: 0.00728, ips: 8.1028 samples/sec | ETA 00:01:11
2022-07-21 15:37:03 [INFO] [TRAIN] epoch: 60, iter: 720/1000, loss: 0.2241, lr: 0.003190, batch_cost: 0.2288, reader_cost: 0.00011, ips: 8.7428 samples/sec | ETA 00:01:04
2022-07-21 15:37:06 [INFO] [TRAIN] epoch: 61, iter: 730/1000, loss: 0.1621, lr: 0.003088, batch_cost: 0.2447, reader_cost: 0.00752, ips: 8.1721 samples/sec | ETA 00:01:06
2022-07-21 15:37:08 [INFO] [TRAIN] epoch: 62, iter: 740/1000, loss: 0.2119, lr: 0.002985, batch_cost: 0.2364, reader_cost: 0.00625, ips: 8.4585 samples/sec | ETA 00:01:01
2022-07-21 15:37:11 [INFO] [TRAIN] epoch: 63, iter: 750/1000, loss: 0.1956, lr: 0.002882, batch_cost: 0.2370, reader_cost: 0.00552, ips: 8.4371 samples/sec | ETA 00:00:59
2022-07-21 15:37:13 [INFO] [TRAIN] epoch: 64, iter: 760/1000, loss: 0.1423, lr: 0.002779, batch_cost: 0.2424, reader_cost: 0.00903, ips: 8.2512 samples/sec | ETA 00:00:58
2022-07-21 15:37:15 [INFO] [TRAIN] epoch: 65, iter: 770/1000, loss: 0.1720, lr: 0.002675, batch_cost: 0.2338, reader_cost: 0.00574, ips: 8.5546 samples/sec | ETA 00:00:53
2022-07-21 15:37:18 [INFO] [TRAIN] epoch: 65, iter: 780/1000, loss: 0.1781, lr: 0.002570, batch_cost: 0.2306, reader_cost: 0.00010, ips: 8.6724 samples/sec | ETA 00:00:50
2022-07-21 15:37:20 [INFO] [TRAIN] epoch: 66, iter: 790/1000, loss: 0.1906, lr: 0.002465, batch_cost: 0.2390, reader_cost: 0.00637, ips: 8.3688 samples/sec | ETA 00:00:50
2022-07-21 15:37:22 [INFO] [TRAIN] epoch: 67, iter: 800/1000, loss: 0.1710, lr: 0.002360, batch_cost: 0.2419, reader_cost: 0.00617, ips: 8.2681 samples/sec | ETA 00:00:48
2022-07-21 15:37:22 [INFO] Start evaluating (total_samples: 4, total_iters: 4)...
4/4 [==============================] - 0s 80ms/step - batch_cost: 0.0803 - reader cost: 0.00
2022-07-21 15:37:23 [INFO] [EVAL] #Images: 4 mIoU: 0.7481 Acc: 0.8950 Kappa: 0.7000 Dice: 0.8500
2022-07-21 15:37:23 [INFO] [EVAL] Class IoU:
[0.873 0.6231]
2022-07-21 15:37:23 [INFO] [EVAL] Class Acc:
[0.9283 0.7791]
2022-07-21 15:37:31 [INFO] [EVAL] The model with the best validation mIoU (0.7769) was saved at iter 600.
2022-07-21 15:37:34 [INFO] [TRAIN] epoch: 68, iter: 810/1000, loss: 0.1725, lr: 0.002254, batch_cost: 0.2433, reader_cost: 0.00563, ips: 8.2203 samples/sec | ETA 00:00:46
2022-07-21 15:37:36 [INFO] [TRAIN] epoch: 69, iter: 820/1000, loss: 0.1774, lr: 0.002147, batch_cost: 0.2470, reader_cost: 0.00742, ips: 8.0962 samples/sec | ETA 00:00:44
2022-07-21 15:37:39 [INFO] [TRAIN] epoch: 70, iter: 830/1000, loss: 0.1750, lr: 0.002040, batch_cost: 0.2440, reader_cost: 0.00549, ips: 8.1960 samples/sec | ETA 00:00:41
2022-07-21 15:37:41 [INFO] [TRAIN] epoch: 70, iter: 840/1000, loss: 0.1889, lr: 0.001933, batch_cost: 0.2264, reader_cost: 0.00009, ips: 8.8323 samples/sec | ETA 00:00:36
2022-07-21 15:37:43 [INFO] [TRAIN] epoch: 71, iter: 850/1000, loss: 0.1751, lr: 0.001824, batch_cost: 0.2399, reader_cost: 0.00840, ips: 8.3354 samples/sec | ETA 00:00:35
2022-07-21 15:37:46 [INFO] [TRAIN] epoch: 72, iter: 860/1000, loss: 0.1682, lr: 0.001715, batch_cost: 0.2415, reader_cost: 0.00709, ips: 8.2819 samples/sec | ETA 00:00:33
2022-07-21 15:37:48 [INFO] [TRAIN] epoch: 73, iter: 870/1000, loss: 0.1540, lr: 0.001605, batch_cost: 0.2422, reader_cost: 0.00681, ips: 8.2560 samples/sec | ETA 00:00:31
2022-07-21 15:37:51 [INFO] [TRAIN] epoch: 74, iter: 880/1000, loss: 0.1710, lr: 0.001495, batch_cost: 0.2359, reader_cost: 0.00514, ips: 8.4779 samples/sec | ETA 00:00:28
2022-07-21 15:37:53 [INFO] [TRAIN] epoch: 75, iter: 890/1000, loss: 0.1636, lr: 0.001383, batch_cost: 0.2459, reader_cost: 0.00781, ips: 8.1335 samples/sec | ETA 00:00:27
2022-07-21 15:37:55 [INFO] [TRAIN] epoch: 75, iter: 900/1000, loss: 0.1928, lr: 0.001270, batch_cost: 0.2320, reader_cost: 0.00011, ips: 8.6192 samples/sec | ETA 00:00:23
2022-07-21 15:37:55 [INFO] Start evaluating (total_samples: 4, total_iters: 4)...
4/4 [==============================] - 0s 95ms/step - batch_cost: 0.0946 - reader cost: 0.00
2022-07-21 15:37:56 [INFO] [EVAL] #Images: 4 mIoU: 0.7236 Acc: 0.8771 Kappa: 0.6672 Dice: 0.8334
2022-07-21 15:37:56 [INFO] [EVAL] Class IoU:
[0.8496 0.5976]
2022-07-21 15:37:56 [INFO] [EVAL] Class Acc:
[0.9369 0.7056]
2022-07-21 15:38:04 [INFO] [EVAL] The model with the best validation mIoU (0.7769) was saved at iter 600.
2022-07-21 15:38:07 [INFO] [TRAIN] epoch: 76, iter: 910/1000, loss: 0.1375, lr: 0.001156, batch_cost: 0.2477, reader_cost: 0.00589, ips: 8.0752 samples/sec | ETA 00:00:22
2022-07-21 15:38:09 [INFO] [TRAIN] epoch: 77, iter: 920/1000, loss: 0.1397, lr: 0.001041, batch_cost: 0.2499, reader_cost: 0.00850, ips: 8.0022 samples/sec | ETA 00:00:19
2022-07-21 15:38:12 [INFO] [TRAIN] epoch: 78, iter: 930/1000, loss: 0.1662, lr: 0.000925, batch_cost: 0.2375, reader_cost: 0.00591, ips: 8.4227 samples/sec | ETA 00:00:16
2022-07-21 15:38:14 [INFO] [TRAIN] epoch: 79, iter: 940/1000, loss: 0.1451, lr: 0.000807, batch_cost: 0.2362, reader_cost: 0.00529, ips: 8.4658 samples/sec | ETA 00:00:14
2022-07-21 15:38:16 [INFO] [TRAIN] epoch: 80, iter: 950/1000, loss: 0.1618, lr: 0.000687, batch_cost: 0.2420, reader_cost: 0.00812, ips: 8.2633 samples/sec | ETA 00:00:12
2022-07-21 15:38:19 [INFO] [TRAIN] epoch: 80, iter: 960/1000, loss: 0.1670, lr: 0.000564, batch_cost: 0.2295, reader_cost: 0.00011, ips: 8.7164 samples/sec | ETA 00:00:09
2022-07-21 15:38:21 [INFO] [TRAIN] epoch: 81, iter: 970/1000, loss: 0.1384, lr: 0.000439, batch_cost: 0.2429, reader_cost: 0.00722, ips: 8.2351 samples/sec | ETA 00:00:07
2022-07-21 15:38:24 [INFO] [TRAIN] epoch: 82, iter: 980/1000, loss: 0.1480, lr: 0.000309, batch_cost: 0.2453, reader_cost: 0.00506, ips: 8.1523 samples/sec | ETA 00:00:04
2022-07-21 15:38:26 [INFO] [TRAIN] epoch: 83, iter: 990/1000, loss: 0.1794, lr: 0.000173, batch_cost: 0.2435, reader_cost: 0.00473, ips: 8.2122 samples/sec | ETA 00:00:02
2022-07-21 15:38:29 [INFO] [TRAIN] epoch: 84, iter: 1000/1000, loss: 0.1583, lr: 0.000020, batch_cost: 0.2478, reader_cost: 0.00836, ips: 8.0724 samples/sec | ETA 00:00:00
2022-07-21 15:38:29 [INFO] Start evaluating (total_samples: 4, total_iters: 4)...
4/4 [==============================] - 0s 89ms/step - batch_cost: 0.0893 - reader cost: 0.00
2022-07-21 15:38:29 [INFO] [EVAL] #Images: 4 mIoU: 0.7597 Acc: 0.8990 Kappa: 0.7167 Dice: 0.8584
2022-07-21 15:38:29 [INFO] [EVAL] Class IoU:
[0.8766 0.6427]
2022-07-21 15:38:29 [INFO] [EVAL] Class Acc:
[0.9376 0.7733]
2022-07-21 15:38:38 [INFO] [EVAL] The model with the best validation mIoU (0.7769) was saved at iter 600.
<class 'paddle.nn.layer.conv.Conv2D'>'s flops has been counted
<class 'paddle.nn.layer.norm.BatchNorm2D'>'s flops has been counted
<class 'paddle.nn.layer.activation.ReLU'>'s flops has been counted
Cannot find suitable count function for <class 'paddleseg.models.layers.wrap_functions.Add'>. Treat it as zero FLOPs.
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
if data.dtype == np.object:
Total Flops: 93548011520 Total Params: 65932658
五、模型评估
模型评估
在小型的铁路数据集上对测试集进行综合评估,评价指标结果如下
本次比赛使用IoU曲线作为评价指标,即利用预测的目标的定位概率图,计算不同阈值下预测结果与真实目标之间的IoU分数,最后取一个最高点作为最终的分数。在理想状态下,IoU曲线最高值接近1.0,对应的阈值为255,因为阈值越高,目标对象与背景的对比度越高。准确率的含义是预测类别正确的像素数占总像素数的比例。变量kappa决定了近邻像素间边的相对权重。Dice系数是一种集合相似度度量函数,通常用于计算两个样本的相似度,取值为[0,1]。
上述指标的结果如下所示:
mIoU: 0.7769
Acc: 0.9038
Kappa: 0.7417
Dice: 0.8706
#config为配置文件的设置,model_path是模型参数数文件,可以根据自己的需要选择模型参数
!python PaddleSeg/val.py \
--config mynet.yml \
--model_path output/best_model/model.pdparams
2022-07-21 15:39:36 [INFO]
---------------Config Information---------------
batch_size: 2
iters: 1000
loss:
coef:
- 1
types:
- type: CrossEntropyLoss
lr_scheduler:
end_lr: 0
learning_rate: 0.01
power: 0.9
type: PolynomialDecay
model:
backbone:
pretrained: https://bj.bcebos.com/paddleseg/dygraph/hrnet_w48_ssld.tar.gz
type: HRNet_W48
backbone_indices:
- -1
num_classes: 2
pretrained: null
type: FCN
optimizer:
momentum: 0.9
type: sgd
weight_decay: 0.0005
train_dataset:
dataset_root: /home/aistudio
mode: train
num_classes: 2
train_path: train.txt
transforms:
- max_scale_factor: 2.0
min_scale_factor: 0.5
scale_step_size: 0.25
type: ResizeStepScaling
- crop_size:
- 512
- 512
type: RandomPaddingCrop
- type: RandomHorizontalFlip
- brightness_range: 0.4
contrast_range: 0.4
saturation_range: 0.4
type: RandomDistort
- type: Normalize
type: Dataset
val_dataset:
dataset_root: /home/aistudio
mode: val
num_classes: 2
transforms:
- type: Normalize
type: Dataset
val_path: eval.txt
------------------------------------------------
W0721 15:39:36.640465 1308 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1
W0721 15:39:36.640496 1308 device_context.cc:465] device: 0, cuDNN Version: 7.6.
2022-07-21 15:39:39 [INFO] Loading pretrained model from https://bj.bcebos.com/paddleseg/dygraph/hrnet_w48_ssld.tar.gz
2022-07-21 15:39:43 [INFO] There are 1525/1525 variables loaded into HRNet.
2022-07-21 15:39:43 [INFO] Loading pretrained model from output/best_model/model.pdparams
2022-07-21 15:39:44 [INFO] There are 1533/1533 variables loaded into FCN.
2022-07-21 15:39:44 [INFO] Loaded trained params of model successfully
2022-07-21 15:39:44 [INFO] Start evaluating (total_samples: 4, total_iters: 4)...
4/4 [==============================] - 1s 192ms/step - batch_cost: 0.1915 - reader cost: 0.102
2022-07-21 15:39:45 [INFO] [EVAL] #Images: 4 mIoU: 0.7769 Acc: 0.9038 Kappa: 0.7417 Dice: 0.8706
2022-07-21 15:39:45 [INFO] [EVAL] Class IoU:
[0.8799 0.6739]
2022-07-21 15:39:45 [INFO] [EVAL] Class Acc:
[0.9586 0.7515]
六、模型预测
!python PaddleSeg/predict.py \
--config mynet.yml \
--model_path output/best_model/model.pdparams \
--image_path work/train/dataset/images1/railway_041.png \
--save_dir output/result #这里对work/Line/0114目录下的图片进行预测,结果保存在output/result目录下,要注意匹配配置文件和模型参数还有待处理图片的路径
import cv2
mask = cv2.imread('output/result/added_prediction/railway_041.png')
#用matplotlib将图片展示出来
# plt.subplot(3, 2, 1)
plt.imshow(mask)
plt.title('Image')
# plt.axis("off")
2022-07-21 15:43:16 [INFO]
---------------Config Information---------------
batch_size: 2
iters: 1000
loss:
coef:
- 1
types:
- type: CrossEntropyLoss
lr_scheduler:
end_lr: 0
learning_rate: 0.01
power: 0.9
type: PolynomialDecay
model:
backbone:
pretrained: https://bj.bcebos.com/paddleseg/dygraph/hrnet_w48_ssld.tar.gz
type: HRNet_W48
backbone_indices:
- -1
num_classes: 2
pretrained: null
type: FCN
optimizer:
momentum: 0.9
type: sgd
weight_decay: 0.0005
train_dataset:
dataset_root: /home/aistudio
mode: train
num_classes: 2
train_path: train.txt
transforms:
- max_scale_factor: 2.0
min_scale_factor: 0.5
scale_step_size: 0.25
type: ResizeStepScaling
- crop_size:
- 512
- 512
type: RandomPaddingCrop
- type: RandomHorizontalFlip
- brightness_range: 0.4
contrast_range: 0.4
saturation_range: 0.4
type: RandomDistort
- type: Normalize
type: Dataset
val_dataset:
dataset_root: /home/aistudio
mode: val
num_classes: 2
transforms:
- type: Normalize
type: Dataset
val_path: eval.txt
------------------------------------------------
W0721 15:43:16.770813 1529 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1
W0721 15:43:16.770848 1529 device_context.cc:465] device: 0, cuDNN Version: 7.6.
2022-07-21 15:43:20 [INFO] Loading pretrained model from https://bj.bcebos.com/paddleseg/dygraph/hrnet_w48_ssld.tar.gz
2022-07-21 15:43:23 [INFO] There are 1525/1525 variables loaded into HRNet.
2022-07-21 15:43:23 [INFO] Number of predict images = 1
2022-07-21 15:43:23 [INFO] Loading pretrained model from output/best_model/model.pdparams
2022-07-21 15:43:24 [INFO] There are 1533/1533 variables loaded into FCN.
2022-07-21 15:43:24 [INFO] Start to predict...
1/1 [==============================] - 0s 132ms/step
Text(0.5,1,'Image')
七、结果可视化
#展示效果看不清的话可以根据代码中的路径找到文件所在位置
import cv2
#work/Line2/line2/railway_100_json
image = cv2.imread('work/train/dataset/images1/railway_041.png')
label = cv2.imread('work/train/dataset/masks1/railway_041.png')
# label_viz = cv2.imread('work/Line2/line2/railway_100_json/label_viz.png')
predict = cv2.imread('output/result/pseudo_color_prediction/railway_041.png')
import cv2
mask = cv2.imread('output/result/added_prediction/railway_041.png')
#用matplotlib将图片展示出来
plt.subplot(3, 2, 1)
plt.imshow(image)
plt.title('Input Image')
plt.axis("off")
plt.subplot(3, 2, 2)
plt.imshow(label, cmap='gray')
plt.title('Label')
plt.axis("off")
# plt.subplot(3, 2, 3)
# plt.imshow(label_viz, cmap='gray')
# plt.title('Label_viz')
# plt.axis("off")
plt.subplot(3, 2, 3)
plt.imshow(predict, cmap='gray')
plt.title('Predict')
plt.axis("off")
plt.subplot(3, 2, 4)
plt.imshow(mask, cmap='gray')
plt.title('Mask')
y')
plt.title('Label')
plt.axis("off")
# plt.subplot(3, 2, 3)
# plt.imshow(label_viz, cmap='gray')
# plt.title('Label_viz')
# plt.axis("off")
plt.subplot(3, 2, 3)
plt.imshow(predict, cmap='gray')
plt.title('Predict')
plt.axis("off")
plt.subplot(3, 2, 4)
plt.imshow(mask, cmap='gray')
plt.title('Mask')
plt.axis("off")
(-0.5, 255.5, 255.5, -0.5)
声明
此项目为搬运
原项目链接
更多推荐
所有评论(0)