安装paddleSeq2.7.0版本模块-笔记
先安装conda和python版本
本机安装的conda 22.9.0
python2.9.12
paddle2.4.2
paddlepaddle-gpu==2.4.2 cuda10.2
安装matplotlib3.5.0版本
opencv_python-4.5.4.60-cp39-cp39-win_amd64.whl
测试采用分割模型名称:BiSeNetv2
#BiSeNetv2保证速度的同时,也能保证精度
#2020年,相关学者基于可实现实时语义分割的双向网络BiSeNet(Bilateral Segmentation Network),
# 建设性地提出了BiSeNetv2,# 以期望获得更好的语义分割精度。
# 通常来说,语义分割任务既需要低级细节,也需要高级语义。
# 就目前的语义分割模型而言,于精度相比,更多的是追求速度,因此存在着一定缺陷。
# 而BiSeNetv2则是两方兼顾且行之有效的语义分割模型。
#(Bilateral Segmentation Network) 双边分割网络
测试分割结果图效果如下所示:
左图为分割结果图,中间图是分割后合成图,右图是测试的原图
一、第一步先激活环境
conda activate base
conda activate base
安装完paddleSeq后要关闭conda激活环境
conda deactivate
conda deactivate
二、第二步安装paddleSeq版本
本机选择安装paddleSeq2.7.0版本安装没有冲突OK
(base) D:\ProgramData\Anaconda3>pip install PaddleSeg==2.7.0
(base) D:\ProgramData\Anaconda3>
(base) D:\ProgramData\Anaconda3>pip install PaddleSeg==2.7.0
(base) D:\ProgramData\Anaconda3>
升级protobuf==3.20.0为3.20.0
(base) D:\ProgramData\Anaconda3>pip install protobuf==3.20.0
(base) D:\ProgramData\Anaconda3>pip install protobuf==3.20.0
Collecting protobuf==3.20.0
Downloading protobuf-3.20.0-cp39-cp39-win_amd64.whl (904 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 904.1/904.1 kB 510.8 kB/s eta 0:00:00
Installing collected packages: protobuf
Attempting uninstall: protobuf
Found existing installation: protobuf 3.19.0
Uninstalling protobuf-3.19.0:
Successfully uninstalled protobuf-3.19.0
Successfully installed protobuf-3.20.0
If you cannot immediately regenerate your protos, some other possible workarounds are:
1. Downgrade the protobuf package to 3.20.x or lower.
2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).
默认安装paddleSeq最新版2.8.0时有版本冲突问题,如下所示:
(base) D:\ProgramData\Anaconda3>pip install PaddleSeg
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
anaconda-project 0.11.1 requires ruamel-yaml, which is not installed.
paddlepaddle-gpu 2.4.2 requires protobuf<=3.20.0,>=3.1.0, but you have protobuf 4.24.0 which is incompatible.
conda-repo-cli 1.0.20 requires clyent==1.2.1, but you have clyent 1.2.2 which is incompatible.
conda-repo-cli 1.0.20 requires nbformat==5.4.0, but you have nbformat 5.5.0 which is incompatible.
三、paddleSeq2.7.0安装详细情况
显示Successfully installed PaddleSeg-2.7.0 sklearn-0.0
表示安装成功
(base) D:\ProgramData\Anaconda3>pip install PaddleSeg==2.7.0
Collecting PaddleSeg==2.7.0
Downloading paddleseg-2.7.0-py3-none-any.whl (349 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 349.4/349.4 kB 271.3 kB/s eta 0:00:00
Requirement already satisfied: prettytable in d:\programdata\anaconda3\lib\site-packages (from PaddleSeg==2.7.0) (3.8.0)
Requirement already satisfied: filelock in d:\programdata\anaconda3\lib\site-packages (from PaddleSeg==2.7.0) (3.6.0)
Requirement already satisfied: pyyaml>=5.1 in d:\programdata\anaconda3\lib\site-packages (from PaddleSeg==2.7.0) (6.0)
Requirement already satisfied: opencv-python in d:\programdata\anaconda3\lib\site-packages (from PaddleSeg==2.7.0) (4.5.4.60)
Requirement already satisfied: visualdl>=2.2.0 in d:\programdata\anaconda3\lib\site-packages (from PaddleSeg==2.7.0) (2.5.3)
Requirement already satisfied: scipy in d:\programdata\anaconda3\lib\site-packages (from PaddleSeg==2.7.0) (1.9.1)
Collecting sklearn==0.0
Downloading sklearn-0.0.tar.gz (1.1 kB)
Preparing metadata (setup.py) ... done
Requirement already satisfied: tqdm in d:\programdata\anaconda3\lib\site-packages (from PaddleSeg==2.7.0) (4.64.1)
Requirement already satisfied: scikit-learn in d:\programdata\anaconda3\lib\site-packages (from sklearn==0.0->PaddleSeg==2.7.0) (1.0.2)
Requirement already satisfied: numpy in d:\programdata\anaconda3\lib\site-packages (from visualdl>=2.2.0->PaddleSeg==2.7.0) (1.21.5)
Requirement already satisfied: requests in d:\programdata\anaconda3\lib\site-packages (from visualdl>=2.2.0->PaddleSeg==2.7.0) (2.28.1)
Requirement already satisfied: protobuf>=3.20.0 in d:\programdata\anaconda3\lib\site-packages (from visualdl>=2.2.0->PaddleSeg==2.7.0) (4.24.0)
Requirement already satisfied: matplotlib in d:\programdata\anaconda3\lib\site-packages (from visualdl>=2.2.0->PaddleSeg==2.7.0) (3.5.2)
Requirement already satisfied: bce-python-sdk in d:\programdata\anaconda3\lib\site-packages (from visualdl>=2.2.0->PaddleSeg==2.7.0) (0.8.87)
Requirement already satisfied: packaging in d:\programdata\anaconda3\lib\site-packages (from visualdl>=2.2.0->PaddleSeg==2.7.0) (21.3)
Requirement already satisfied: pandas in d:\programdata\anaconda3\lib\site-packages (from visualdl>=2.2.0->PaddleSeg==2.7.0) (1.4.4)
Requirement already satisfied: flask>=1.1.1 in d:\programdata\anaconda3\lib\site-packages (from visualdl>=2.2.0->PaddleSeg==2.7.0) (2.3.2)
Requirement already satisfied: Pillow>=7.0.0 in d:\programdata\anaconda3\lib\site-packages (from visualdl>=2.2.0->PaddleSeg==2.7.0) (9.2.0)
Requirement already satisfied: six>=1.14.0 in d:\programdata\anaconda3\lib\site-packages (from visualdl>=2.2.0->PaddleSeg==2.7.0) (1.16.0)
Requirement already satisfied: Flask-Babel>=3.0.0 in d:\programdata\anaconda3\lib\site-packages (from visualdl>=2.2.0->PaddleSeg==2.7.0) (3.1.0)
Requirement already satisfied: rarfile in d:\programdata\anaconda3\lib\site-packages (from visualdl>=2.2.0->PaddleSeg==2.7.0) (4.0)
Requirement already satisfied: psutil in d:\programdata\anaconda3\lib\site-packages (from visualdl>=2.2.0->PaddleSeg==2.7.0) (5.9.0)
Requirement already satisfied: wcwidth in d:\programdata\anaconda3\lib\site-packages (from prettytable->PaddleSeg==2.7.0) (0.2.5)
Requirement already satisfied: colorama in d:\programdata\anaconda3\lib\site-packages (from tqdm->PaddleSeg==2.7.0) (0.4.5)
Requirement already satisfied: itsdangerous>=2.1.2 in d:\programdata\anaconda3\lib\site-packages (from flask>=1.1.1->visualdl>=2.2.0->PaddleSeg==2.7.0) (2.1.2)
Requirement already satisfied: Jinja2>=3.1.2 in d:\programdata\anaconda3\lib\site-packages (from flask>=1.1.1->visualdl>=2.2.0->PaddleSeg==2.7.0) (3.1.2)
Requirement already satisfied: Werkzeug>=2.3.3 in d:\programdata\anaconda3\lib\site-packages (from flask>=1.1.1->visualdl>=2.2.0->PaddleSeg==2.7.0) (2.3.6)
Requirement already satisfied: click>=8.1.3 in d:\programdata\anaconda3\lib\site-packages (from flask>=1.1.1->visualdl>=2.2.0->PaddleSeg==2.7.0) (8.1.6)
Requirement already satisfied: blinker>=1.6.2 in d:\programdata\anaconda3\lib\site-packages (from flask>=1.1.1->visualdl>=2.2.0->PaddleSeg==2.7.0) (1.6.2)
Requirement already satisfied: importlib-metadata>=3.6.0 in d:\programdata\anaconda3\lib\site-packages (from flask>=1.1.1->visualdl>=2.2.0->PaddleSeg==2.7.0) (4.11.3)
Requirement already satisfied: Babel>=2.12 in d:\programdata\anaconda3\lib\site-packages (from Flask-Babel>=3.0.0->visualdl>=2.2.0->PaddleSeg==2.7.0) (2.12.1)
Requirement already satisfied: pytz>=2022.7 in d:\programdata\anaconda3\lib\site-packages (from Flask-Babel>=3.0.0->visualdl>=2.2.0->PaddleSeg==2.7.0) (2023.3)
Requirement already satisfied: pycryptodome>=3.8.0 in d:\programdata\anaconda3\lib\site-packages (from bce-python-sdk->visualdl>=2.2.0->PaddleSeg==2.7.0) (3.18.0)
Requirement already satisfied: future>=0.6.0 in d:\programdata\anaconda3\lib\site-packages (from bce-python-sdk->visualdl>=2.2.0->PaddleSeg==2.7.0) (0.18.2)
Requirement already satisfied: python-dateutil>=2.7 in d:\programdata\anaconda3\lib\site-packages (from matplotlib->visualdl>=2.2.0->PaddleSeg==2.7.0) (2.8.2)
Requirement already satisfied: cycler>=0.10 in d:\programdata\anaconda3\lib\site-packages (from matplotlib->visualdl>=2.2.0->PaddleSeg==2.7.0) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in d:\programdata\anaconda3\lib\site-packages (from matplotlib->visualdl>=2.2.0->PaddleSeg==2.7.0) (4.25.0)
Requirement already satisfied: pyparsing>=2.2.1 in d:\programdata\anaconda3\lib\site-packages (from matplotlib->visualdl>=2.2.0->PaddleSeg==2.7.0) (3.0.9)
Requirement already satisfied: kiwisolver>=1.0.1 in d:\programdata\anaconda3\lib\site-packages (from matplotlib->visualdl>=2.2.0->PaddleSeg==2.7.0) (1.4.2)
Requirement already satisfied: charset-normalizer<3,>=2 in d:\programdata\anaconda3\lib\site-packages (from requests->visualdl>=2.2.0->PaddleSeg==2.7.0) (2.0.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\programdata\anaconda3\lib\site-packages (from requests->visualdl>=2.2.0->PaddleSeg==2.7.0) (1.26.11)
Requirement already satisfied: idna<4,>=2.5 in d:\programdata\anaconda3\lib\site-packages (from requests->visualdl>=2.2.0->PaddleSeg==2.7.0) (3.3)
Requirement already satisfied: certifi>=2017.4.17 in d:\programdata\anaconda3\lib\site-packages (from requests->visualdl>=2.2.0->PaddleSeg==2.7.0) (2022.9.14)
Requirement already satisfied: threadpoolctl>=2.0.0 in d:\programdata\anaconda3\lib\site-packages (from scikit-learn->sklearn==0.0->PaddleSeg==2.7.0) (2.2.0)
Requirement already satisfied: joblib>=0.11 in d:\programdata\anaconda3\lib\site-packages (from scikit-learn->sklearn==0.0->PaddleSeg==2.7.0) (1.1.0)
Requirement already satisfied: zipp>=0.5 in d:\programdata\anaconda3\lib\site-packages (from importlib-metadata>=3.6.0->flask>=1.1.1->visualdl>=2.2.0->PaddleSeg==2.7.0) (3.8.0)
Requirement already satisfied: MarkupSafe>=2.0 in d:\programdata\anaconda3\lib\site-packages (from Jinja2>=3.1.2->flask>=1.1.1->visualdl>=2.2.0->PaddleSeg==2.7.0) (2.1.3)
Building wheels for collected packages: sklearn
Building wheel for sklearn (setup.py) ... done
Created wheel for sklearn: filename=sklearn-0.0-py2.py3-none-any.whl size=1304 sha256=292bc45b32f83827cb71ee7501b63c818b0b845e39c063fe776f07399e395fd6
Stored in directory: c:\users\hsgpc\appdata\local\pip\cache\wheels\e4\7b\98\b6466d71b8d738a0c547008b9eb39bf8676d1ff6ca4b22af1c
Successfully built sklearn
Installing collected packages: sklearn, PaddleSeg
Attempting uninstall: PaddleSeg
Found existing installation: paddleseg 2.8.0
Uninstalling paddleseg-2.8.0:
Successfully uninstalled paddleseg-2.8.0
Successfully installed PaddleSeg-2.7.0 sklearn-0.0
(base) D:\ProgramData\Anaconda3>
四、测试训练代码
测试结果正常运行
PS E:\project\python> & D:/ProgramData/Anaconda3/python.exe e:/project/python/Seq/test.py
Connecting to https://paddleseg.bj.bcebos.com/dataset/optic_disc_seg.zip
Downloading optic_disc_seg.zip
[==================================================] 100.00%
Uncompress optic_disc_seg.zip
[==================================================] 100.00%
W0811 20:07:56.475862 16348 gpu_resources.cc:61] Please NOTE: device: 0, GPU Compute Capability: 6.1, Driver API Version: 12.2, Runtime API Version: 10.2
W0811 20:07:56.523449 16348 gpu_resources.cc:91] device: 0, cuDNN Version: 7.6.
D:\ProgramData\Anaconda3\lib\site-packages\paddle\nn\layer\norm.py:712: UserWarning: When training, we now always track global mean and variance.
warnings.warn(
D:\ProgramData\Anaconda3\lib\site-packages\paddle\fluid\dygraph\math_op_patch.py:275: 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
warnings.warn(
2023-08-11 20:08:09 [INFO] [TRAIN] epoch: 1, iter: 10/1000, loss: 2.6801, lr: 0.009919, batch_cost: 1.0978, reader_cost: 0.09730, ips: 3.6436 samples/sec | ETA 00:18:06
2023-08-11 20:08:13 [INFO] [TRAIN] epoch: 1, iter: 20/1000, loss: 0.6180, lr: 0.009829, batch_cost: 0.3860, reader_cost: 0.00010, ips: 10.3626 samples/sec | ETA 00:06:18
2023-08-11 20:08:17 [INFO] [TRAIN] epoch: 1, iter: 30/1000, loss: 0.4047, lr: 0.009739, batch_cost: 0.4011, reader_cost: 0.00020, ips: 9.9725 samples/sec | ETA 00:06:29
2023-08-11 20:08:21 [INFO] [TRAIN] epoch: 1, iter: 40/1000, loss: 0.3404, lr: 0.009648, batch_cost: 0.4061, reader_cost: 0.00030, ips: 9.8509 samples/sec | ETA 00:06:29
2023-08-11 20:08:25 [INFO] [TRAIN] epoch: 1, iter: 50/1000, loss: 0.3043, lr: 0.009558, batch_cost: 0.3877, reader_cost: 0.00030, ips: 10.3161 samples/sec | ETA 00:06:08
2023-08-11 20:08:28 [INFO] [TRAIN] epoch: 1, iter: 60/1000, loss: 0.2918, lr: 0.009467, batch_cost: 0.3862, reader_cost: 0.00010, ips: 10.3586 samples/sec | ETA 00:06:02
2023-08-11 20:08:32 [INFO] [TRAIN] epoch: 2, iter: 70/1000, loss: 0.2548, lr: 0.009377, batch_cost: 0.4038, reader_cost: 0.01672, ips: 9.9049 samples/sec | ETA 00:06:15
2023-08-11 20:08:36 [INFO] [TRAIN] epoch: 2, iter: 80/1000, loss: 0.2641, lr: 0.009286, batch_cost: 0.3905, reader_cost: 0.00015, ips: 10.2446 samples/sec | ETA 00:05:59
2023-08-11 20:08:40 [INFO] [TRAIN] epoch: 2, iter: 90/1000, loss: 0.2426, lr: 0.009195, batch_cost: 0.3884, reader_cost: 0.00000, ips: 10.2990 samples/sec | ETA 00:05:53
2023-08-11 20:08:44 [INFO] [TRAIN] epoch: 2, iter: 100/1000, loss: 0.2403, lr: 0.009104, batch_cost: 0.3864, reader_cost: 0.00000, ips: 10.3526 samples/sec | ETA 00:05:47
2023-08-11 20:08:48 [INFO] [TRAIN] epoch: 2, iter: 110/1000, loss: 0.2847, lr: 0.009013, batch_cost: 0.3890, reader_cost: 0.00010, ips: 10.2830 samples/sec | ETA 00:05:46
2023-08-11 20:08:52 [INFO] [TRAIN] epoch: 2, iter: 120/1000, loss: 0.2442, lr: 0.008922, batch_cost: 0.3874, reader_cost: 0.00010, ips: 10.3261 samples/sec | ETA 00:05:40
2023-08-11 20:08:56 [INFO] [TRAIN] epoch: 2, iter: 130/1000, loss: 0.2243, lr: 0.008831, batch_cost: 0.3901, reader_cost: 0.00020, ips: 10.2547 samples/sec | ETA 00:05:39
2023-08-11 20:09:00 [INFO] [TRAIN] epoch: 3, iter: 140/1000, loss: 0.2251, lr: 0.008740, batch_cost: 0.3946, reader_cost: 0.00559, ips: 10.1366 samples/sec | ETA 00:05:39
2023-08-11 20:09:04 [INFO] [TRAIN] epoch: 3, iter: 150/1000, loss: 0.1888, lr: 0.008648, batch_cost: 0.3949, reader_cost: 0.00000, ips: 10.1292 samples/sec | ETA 00:05:35
2023-08-11 20:09:08 [INFO] [TRAIN] epoch: 3, iter: 160/1000, loss: 0.2272, lr: 0.008557, batch_cost: 0.3883, reader_cost: 0.00030, ips: 10.3016 samples/sec | ETA 00:05:26
2023-08-11 20:09:11 [INFO] [TRAIN] epoch: 3, iter: 170/1000, loss: 0.1854, lr: 0.008465, batch_cost: 0.3875, reader_cost: 0.00010, ips: 10.3239 samples/sec | ETA 00:05:21
2023-08-11 20:09:15 [INFO] [TRAIN] epoch: 3, iter: 180/1000, loss: 0.2287, lr: 0.008374, batch_cost: 0.3893, reader_cost: 0.00020, ips: 10.2751 samples/sec | ETA 00:05:19
2023-08-11 20:09:19 [INFO] [TRAIN] epoch: 3, iter: 190/1000, loss: 0.1802, lr: 0.008282, batch_cost: 0.4025, reader_cost: 0.00030, ips: 9.9373 samples/sec | ETA 00:05:26
2023-08-11 20:09:23 [INFO] [TRAIN] epoch: 4, iter: 200/1000, loss: 0.1878, lr: 0.008190, batch_cost: 0.3942, reader_cost: 0.00663, ips: 10.1465 samples/sec | ETA 00:05:15
2023-08-11 20:09:23 [INFO] Start evaluating (total_samples: 76, total_iters: 76)...
76/76 [==============================] - 2s 33ms/step - batch_cost: 0.0325 - reader cost: 3.1596e-04
2023-08-11 20:09:26 [INFO] [EVAL] #Images: 76 mIoU: 0.7409 Acc: 0.9900 Kappa: 0.6546 Dice: 0.8271
2023-08-11 20:09:26 [INFO] [EVAL] Class IoU:
[0.9899 0.4919]
2023-08-11 20:09:26 [INFO] [EVAL] Class Precision:
[0.9912 0.8759]
2023-08-11 20:09:26 [INFO] [EVAL] Class Recall:
[0.9986 0.5287]
2023-08-11 20:09:26 [INFO] [EVAL] The model with the best validation mIoU (0.7409) was saved at iter 200.
test.py
# 构建训练用的数据增强和预处理
import paddleseg.transforms as T
transforms = [
T.Resize(target_size=(512, 512)),
T.RandomHorizontalFlip(),
T.Normalize()
]
# 构建训练集
from paddleseg.datasets import OpticDiscSeg
train_dataset = OpticDiscSeg(
dataset_root='data/optic_disc_seg',
transforms=transforms,
mode='train'
)
from paddleseg.models import BiSeNetV2
model = BiSeNetV2(num_classes=2,
lambd=0.25,
align_corners=False,
pretrained=None)
# 构建验证用的数据增强和预处理
import paddleseg.transforms as T
transforms = [
T.Resize(target_size=(512, 512)),
T.Normalize()
]
# 构建验证集
from paddleseg.datasets import OpticDiscSeg
val_dataset = OpticDiscSeg(
dataset_root='data/optic_disc_seg',
transforms=transforms,
mode='val'
)
import paddle
# 设置学习率
base_lr = 0.01
lr = paddle.optimizer.lr.PolynomialDecay(base_lr, power=0.9, decay_steps=1000, end_lr=0)
optimizer = paddle.optimizer.Momentum(lr, parameters=model.parameters(), momentum=0.9, weight_decay=4.0e-5)
from paddleseg.models.losses import CrossEntropyLoss
losses = {}
losses['types'] = [CrossEntropyLoss()] * 5
losses['coef'] = [1]* 5
print(losses) #构造字典的值
#losses={
# 'types': [CrossEntropyLoss(), CrossEntropyLoss(), CrossEntropyLoss(), CrossEntropyLoss(), CrossEntropyLoss()],
# 'coef': [1, 1, 1, 1, 1]
# }
from paddleseg.core import train
train(
model=model,
train_dataset=train_dataset,
val_dataset=val_dataset,
optimizer=optimizer,
save_dir='output',
iters=1000,
batch_size=4,
save_interval=200,
log_iters=10,
num_workers=0,
losses=losses,
use_vdl=True)
五、评估结果
#optic disc seq 视盘分割 测试
import paddle
#模型评估
#============================================
#构建模型
from paddleseg.models import BiSeNetV2
model = BiSeNetV2(num_classes=2,
lambd=0.25,
align_corners=False,
pretrained=None)
#============================================
#BiSeNetv2保证速度的同时,也能保证精度
#2020年,相关学者基于可实现实时语义分割的双向网络BiSeNet(Bilateral Segmentation Network),
# 建设性地提出了BiSeNetv2,# 以期望获得更好的语义分割精度。
# 通常来说,语义分割任务既需要低级细节,也需要高级语义。
# 就目前的语义分割模型而言,于精度相比,更多的是追求速度,因此存在着一定缺陷。
# 而BiSeNetv2则是两方兼顾且行之有效的语义分割模型。
#(Bilateral Segmentation Network) 双边分割网络
#加载模型参数
model_path = 'output/best_model/model.pdparams'
if model_path:
para_state_dict = paddle.load(model_path)
model.set_dict(para_state_dict)
print('Loaded trained params of model successfully')
else:
raise ValueError('The model_path is wrong: {}'.format(model_path))
# 构建验证集
# 构建验证用的transforms
import paddleseg.transforms as T
transforms = [
T.Resize(target_size=(512, 512)),
T.Normalize()
]
# 构建验证集
from paddleseg.datasets import OpticDiscSeg
val_dataset = OpticDiscSeg(
dataset_root='data/optic_disc_seg',
transforms=transforms,
mode='val'
)
#评估
from paddleseg.core import evaluate
#evaluate(model,val_dataset)
'''
评估结果:准确率Acc: 0.9942
平均交并比:mIoU: 0.8601=(0.9941+0.7262)/2
2023-08-12 12:06:05 [INFO] [EVAL] #Images: 76 mIoU: 0.8601 Acc: 0.9942 Kappa: 0.8384 Dice: 0.9192
2023-08-12 12:06:05 [INFO] [EVAL] Class IoU: [0.9941 0.7262]
2023-08-12 12:06:05 [INFO] [EVAL] Class Precision:[0.9969 0.8497]
2023-08-12 12:06:05 [INFO] [EVAL] Class Recall: [0.9972 0.8331]
'''
#多尺度+翻转评估
evaluate(
model,
val_dataset,
aug_eval=True,
scales=[0.75, 1.0, 1.25],
flip_horizontal=True)
#
'''
评估结果:准确率Acc: 0.9943
平均交并比:mIoU: 0.8629=(0.9942+0.7315)/2
2023-08-12 12:14:34 [INFO] [EVAL] #Images: 76 mIoU: 0.8629 Acc: 0.9943 Kappa: 0.8421 Dice: 0.9210
2023-08-12 12:14:34 [INFO] [EVAL] Class IoU: [0.9942 0.7315]
2023-08-12 12:14:34 [INFO] [EVAL] Class Precision:[0.9971 0.8459]
2023-08-12 12:14:34 [INFO] [EVAL] Class Recall: [0.9971 0.844 ]
'''
六、预测结果代码
#predict 预测
#效果可视化
#构建模型
from paddleseg.models import BiSeNetV2
model = BiSeNetV2(num_classes=2,
lambd=0.25,
align_corners=False,
pretrained=None)
#创建transform
import paddleseg.transforms as T
transforms = T.Compose([
T.Resize(target_size=(512, 512)),
T.RandomHorizontalFlip(),
T.Normalize()
])
#构建待预测的图像列表
import os
def get_image_list(image_path):
"""Get image list"""
valid_suffix = [
'.JPEG', '.jpeg', '.JPG', '.jpg', '.BMP', '.bmp', '.PNG', '.png'
]
image_list = []
image_dir = None
if os.path.isfile(image_path):
if os.path.splitext(image_path)[-1] in valid_suffix:
image_list.append(image_path)
elif os.path.isdir(image_path):
image_dir = image_path
for root, dirs, files in os.walk(image_path):
for f in files:
if os.path.splitext(f)[-1] in valid_suffix:
image_list.append(os.path.join(root, f))
else:
raise FileNotFoundError(
'`--image_path` is not found. it should be an image file or a directory including images'
)
if len(image_list) == 0:
raise RuntimeError('There are not image file in `--image_path`')
return image_list, image_dir
#====================================================================================
image_path = 'data/optic_disc_seg/JPEGImages/N0010.jpg' # 也可以输入一个包含图像的目录 N0010.jpg/N0058/P0091
image_list, image_dir = get_image_list(image_path)
#====================================================================================
#预测
#=========================================
#预测输出结果保存到save_dir目录下
from paddleseg.core import predict
predict(
model,
model_path='output/best_model/model.pdparams',
transforms=transforms,
image_list=image_list,
image_dir=image_dir,
save_dir='Seq/output/results'
)
#=========================================
#copy old image to output dir
import os
from shutil import copyfile
copyfile(image_path,'Seq/output/'+os.path.basename(image_path))
—the—end—