MMSegmentation practice-Ubuntu system

1.Training

1.1 Training methods

Single card training

Configure the file first

#在ubuntu命令行中执行
#configParameter文件是我自己整合的,在后面贴出来了
python configParameter.py 
#会生成一个自己的配置文件
python tools/train.py [config文件路径]

Doka training

#配置流程一样,唯一不同的是执行训练的语句有所差别
./tools/dist_train.sh [config文件路径] [GPU数量]
#示例命令:我这里用了3块GPU
./tools/dist_train.sh Zihao-Configs/WHUDataset_DeepLabV3+_20230818.py 3

1.2 Training modifications

[Training Interrupted] What should I do? ! How to implement breakpoint continuation of training? !

①In./configs/_base _/default_runtime.pyChange two lines of code in

The first line points to the last weight file of the last training
The second line changes the default setting of whether to continue training from False to True

load_from = "./work_dirs/WHUDataset_DeepLabV3+/best_mIoU_iter_21000.pth"
resume = True

②Reconfigure the model file and re-execute training.

After configuring the parameters of resume
Just go through the training process again!

My experimental process is based on the process of Brother Tongji Zihao, so the configParameter.py file is written based on Brother Tongji Zihao’s JupyterNotebook
The original link is as follows:


https://blog.csdn.net/ArcGis_Niu/article/details/133317054?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C% 22rType%22%3A%22article%22%2C%22rId%22%3A%22133317054%22%2C%22source%22%3A%22ArcGis_Niu%22%7D I compiled a python file myself, the file name is configParameter.py, Post in this blog https://github.com/TommyZihao/MMSegmentation_Tutorials/tree/main/20230816

#在命令行中执行
python configParameter.py 
-----------------------------------------------------------------------

#多卡训练,指定配置文件 最后的数字3是指定gpu数
./tools/dist_train.sh Zihao-Configs/WHUDataset_DeepLabV3+_20230818.py 3
-------------------------------------------------------------------------

2. Test

Just run test.py

Single card for testing

python tools/test.py  [配置文件路径] [权重文件路径]

Output result:


Multi-card test

./tools/dist_test.sh  [配置文件路径]  [权重文件路径] [GPU数]

3. Prediction

It’s also very simple, just run the following code to make a single image prediction:

python predict.py        #predict.py文件代码在下文中贴出
#这是predict.py文件的代码
#主要作用是对单幅图像进行预测
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

from mmseg.apis import init_model, inference_model, show_result_pyplot
import mmcv
import cv2


#载入模型配置文件
config_file = './Zihao-Configs/WHUDataset_UNet_20230818.py'

# 模型 checkpoint 权重文件
checkpoint_file = './work_dirs/WHUDataset-UNet/iter_40000.pth'
#提取epoch数
import re


pattern = r'(\d+)'
match = re.search(pattern, checkpoint_file)
if match:
    epoch = match.group(1)
    print(epoch)
else:
    print("No number found in the file path.")

# device = 'cpu'
device = 'cuda:0'

model = init_model(config_file, checkpoint_file, device=device)

img_path = '需要预测的图像的路径'

img_bgr = cv2.imread(img_path)

plt.figure(figsize=(8, 8))
plt.imshow(img_bgr[:,:,::-1])
plt.show()

result = inference_model(model, img_bgr)
result.keys()

pred_mask = result.pred_sem_seg.data[0].cpu().numpy()
pred_mask.shape

np.unique(pred_mask)


plt.figure(figsize=(8, 8))
plt.imshow(pred_mask)

save_path='./outputs/spaceNet50_'+epoch+'_predict.png'


plt.figure(figsize=(8, 8))
plt.imshow(pred_mask)
plt.savefig(save_path)
plt.show()

4. Training log visualization

I haven’t studied the visualization yet. Let me finish it and post it to communicate with everyone~~
To be continued...

Guess you like

Origin blog.csdn.net/ArcGis_Niu/article/details/133101087