操作步骤
步骤1:准备工作
步骤2:基础配置
步骤3:确认订单
步骤4:模型开发
步骤1:准备工作
进入算力导购页面
-
注册天翼云账号,完成实名认证并确保余额充足。
-
登录算力服务平台的控制台。
-
进入算力导购页面,点击【购买算力】按钮。
步骤2:基础配置
需求输入
-
任务名称:配置notebook-test。
-
业务场景:选择【模型开发】。
-
付费类型:选择【包年包月】。
-
时长:根据需求选择时长。
-
算力需求:GPU选择NVIDIA-A10。
-
点击按钮【下一步:算网评估】。
算网评估
-
编排调度策略选择:【成本最优】。
-
点击按钮【下一步:部署配置】。
部署配置
- 配置实例名称。
- 镜像选择:pytorch-jupyter:cuda11.1-torch1.9.0-cudnn8。
- 点击【下一步:确认订单】。
步骤3:确认订单
-
核对订单信息,提交信息。
-
核对订单金额,然后点击【支付订单】。
步骤4:模型开发
算网任务创建完成后,进入【智能计算】-【模型开发】,可在列表中查看已创建的模型开发实例,点击【打开】按钮,进入创建好的开发环境。
打开实例
新建开发脚本
点击左上角【+】按钮,选择Notebook-Python3 。
提交代码
1.数钢筋案例开始 - 下载代码和数据集
import os
if not os.path.exists('./rebar_count'):
print('Downloading code and datasets...')
os.system("wget -N -nv https://modelarts-labs-bj4-v2.obs.cn-north-4.myhuaweicloud.com/notebook/DL_rebar_count/rebar_count_code.zip")
os.system("wget -N -nv https://cnnorth4-modelhub-datasets-obsfs-sfnua.obs.cn-north-4.myhuaweicloud.com/content/c2c1853f-d6a6-4c9d-ac0e-203d4c304c88/NkxX5K/dataset/rebar_count_datasets.zip")
os.system("unzip rebar_count_code.zip; rm rebar_count_code.zip")
os.system("unzip -q rebar_count_datasets.zip; rm rebar_count_datasets.zip")
os.system("mv rebar_count_code rebar_count; mv rebar_count_datasets rebar_count/datasets")
if os.path.exists('./rebar_count'):
print('Download code and datasets success')
else:
print('Download code and datasets failed, please check the download url is valid or not.')
else:
print('./rebar_count already exists')
2.加载需要的python模块
import os
import sys
import cv2
import time
import random
import torch
import numpy as np
from PIL import Image, ImageDraw
import xml.etree.ElementTree as ET
from datetime import datetime
from collections import OrderedDict
import torch.optim as optim
import torch.utils.data as data
import torch.backends.cudnn as cudnn
sys.path.insert(0, '/app/rebar_count/src')
from rebar_count.src.data import VOCroot, VOC_Config, AnnotationTransform, VOCDetection, detection_collate, BaseTransform, preproc
from models.RFB_Net_vgg import build_net
from layers.modules import MultiBoxLoss
from layers.functions import Detect, PriorBox
from utils.visualize import *
from utils.nms_wrapper import nms
from utils.timer import Timer
import matplotlib.pyplot as plt
%matplotlib inline
ROOT_DIR = os.getcwd()
seed = 0
cudnn.benchmark = False
cudnn.deterministic = True
torch.manual_seed(seed) # 为CPU设置随机种子
torch.cuda.manual_seed_all(seed) # 为所有GPU设置随机种子
random.seed(seed)
np.random.seed(seed)
3.查看训练数据样例
def read_xml(xml_path):
'''读取xml标签'''
tree = ET.parse(xml_path)
root = tree.getroot()
boxes = []
labels = []
for element in root.findall('object'):
label = element.find('name').text
if label == 'steel':
bndbox = element.find('bndbox')
xmin = bndbox.find('xmin').text
ymin = bndbox.find('ymin').text
xmax = bndbox.find('xmax').text
ymax = bndbox.find('ymax').text
boxes.append([xmin, ymin, xmax, ymax])
labels.append(label)
return np.array(boxes, dtype=np.float64), labels
4.显示原图和标注框
train_img_dir = './rebar_count/datasets/VOC2007/JPEGImages'
train_xml_dir = './rebar_count/datasets/VOC2007/Annotations'
files = os.listdir(train_img_dir)
files.sort()
for index, file_name in enumerate(files[:2]):
img_path = os.path.join(train_img_dir, file_name)
xml_path = os.path.join(train_xml_dir, file_name.split('.jpg')[0] + '.xml')
boxes, labels = read_xml(xml_path)
img = Image.open(img_path)
resize_scale = 2048.0 / max(img.size)
img = img.resize((int(img.size[0] * resize_scale), int(img.size[1] * resize_scale)))
boxes *= resize_scale
plt.figure(figsize=(img.size[0]/100.0, img.size[1]/100.0))
plt.subplot(2,1,1)
plt.imshow(img)
img = img.convert('RGB')
img = np.array(img)
img = img.copy()
for box in boxes:
xmin, ymin, xmax, ymax = box.astype(np.int)
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), thickness=3)
plt.subplot(2,1,2)
plt.imshow(img)
plt.show()
5.定义训练超参,模型、日志保存路径
# 定义训练超参
num_classes = 2 # 数据集中只有 steel 一个标签,加上背景,所以总共有2个类
max_epoch = 25 # 默认值为1,调整为大于20的值,训练效果更佳
batch_size = 4
ngpu = 1
initial_lr = 0.01
img_dim = 416 # 模型输入图片大小
train_sets = [('2007', 'trainval')] # 指定训练集
cfg = VOC_Config
rgb_means = (104, 117, 123) # ImageNet数据集的RGB均值
save_folder = './rebar_count/model_snapshots' # 指定训练模型保存路径
if not os.path.exists(save_folder):
os.mkdir(save_folder)
log_path = os.path.join('./rebar_count/logs', datetime.now().isoformat()) # 指定日志保存路径
if not os.path.exists(log_path):
os.makedirs(log_path)
6.构建模型,定义优化器及损失函数
net = build_net('train', img_dim, num_classes=num_classes)
if ngpu > 1:
net = torch.nn.DataParallel(net)
net.cuda() # 本案例代码只能在GPU上训练
cudnn.benchmark = True
optimizer = optim.SGD(net.parameters(), lr=initial_lr,
momentum=0.9, weight_decay=0) # 定义优化器
criterion = MultiBoxLoss(num_classes,
overlap_thresh=0.4,
prior_for_matching=True,
bkg_label=0,
neg_mining=True,
neg_pos=3,
neg_overlap=0.3,
encode_target=False) # 定义损失函数
priorbox = PriorBox(cfg)
with torch.no_grad():
priors = priorbox.forward()
priors = priors.cuda()
7.定义自适应学习率函数
def adjust_learning_rate(optimizer, gamma, epoch, step_index, iteration, epoch_size):
"""
自适应学习率
"""
if epoch < 11:
lr = 1e-8 + (initial_lr-1e-8) * iteration / (epoch_size * 10)
else:
lr = initial_lr * (gamma ** (step_index))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
8.定义训练函数
def train():
"""
模型训练函数,每10次迭代打印一次日志,20个epoch之后,每个epoch保存一次模型
"""
net.train()
loc_loss = 0
conf_loss = 0
epoch = 0
print('Loading dataset...')
dataset = VOCDetection(VOCroot, train_sets, preproc(img_dim, rgb_means, p=0.0), AnnotationTransform())
epoch_size = len(dataset) // batch_size
max_iter = max_epoch * epoch_size
stepvalues = (25 * epoch_size, 35 * epoch_size)
step_index = 0
start_iter = 0
lr = initial_lr
for iteration in range(start_iter, max_iter):
if iteration % epoch_size == 0:
if epoch > 20:
torch.save(net.state_dict(), os.path.join(save_folder, 'epoch_' +
repr(epoch).zfill(3) + '_loss_'+ '%.4f' % loss.item() + '.pth'))
batch_iterator = iter(data.DataLoader(dataset, batch_size,
shuffle=True, num_workers=1, collate_fn=detection_collate))
loc_loss = 0
conf_loss = 0
epoch += 1
load_t0 = time.time()
if iteration in stepvalues:
step_index += 1
lr = adjust_learning_rate(optimizer, 0.2, epoch, step_index, iteration, epoch_size)
images, targets = next(batch_iterator)
images = Variable(images.cuda())
targets = [Variable(anno.cuda()) for anno in targets]
# forward
t0 = time.time()
out = net(images)
# backprop
optimizer.zero_grad()
loss_l, loss_c = criterion(out, priors, targets)
loss = loss_l + loss_c
loss.backward()
optimizer.step()
t1 = time.time()
loc_loss += loss_l.item()
conf_loss += loss_c.item()
load_t1 = time.time()
if iteration % 10 == 0:
print('Epoch:' + repr(epoch) + ' || epochiter: ' + repr(iteration % epoch_size) + '/' + repr(epoch_size)
+ '|| Totel iter ' +
repr(iteration) + ' || L: %.4f C: %.4f||' % (
loss_l.item(),loss_c.item()) +
'Batch time: %.4f sec. ||' % (load_t1 - load_t0) + 'LR: %.8f' % (lr))
torch.save(net.state_dict(), os.path.join(save_folder, 'epoch_' +
repr(epoch).zfill(3) + '_loss_'+ '%.4f' % loss.item() + '.pth'))
9.开始训练,每个epoch训练耗时约60秒
t1 = time.time()
print('开始训练,本次训练总共需%d个epoch,每个epoch训练耗时约60秒' % max_epoch)
train()
print('training cost %.2f s' % (time.time() - t1))
10.已完成训练,下面开始测试模型,首先需定义目标检测类
cfg = VOC_Config
img_dim = 416
rgb_means = (104, 117, 123)
priorbox = PriorBox(cfg)
with torch.no_grad():
priors = priorbox.forward()
if torch.cuda.is_available():
priors = priors.cuda()
class ObjectDetector:
"""
定义目标检测类
"""
def __init__(self, net, detection, transform, num_classes=num_classes, thresh=0.01, cuda=True):
self.net = net
self.detection = detection
self.transform = transform
self.num_classes = num_classes
self.thresh = thresh
self.cuda = torch.cuda.is_available()
def predict(self, img):
_t = {'im_detect': Timer(), 'misc': Timer()}
scale = torch.Tensor([img.shape[1], img.shape[0],
img.shape[1], img.shape[0]])
with torch.no_grad():
x = self.transform(img).unsqueeze(0)
if self.cuda:
x = x.cuda()
scale = scale.cuda()
_t['im_detect'].tic()
out = net(x) # forward pass
boxes, scores = self.detection.forward(out, priors)
detect_time = _t['im_detect'].toc()
boxes = boxes[0]
scores = scores[0]
# scale each detection back up to the image
boxes *= scale
boxes = boxes.cpu().numpy()
scores = scores.cpu().numpy()
_t['misc'].tic()
all_boxes = [[] for _ in range(num_classes)]
for j in range(1, num_classes):
inds = np.where(scores[:, j] > self.thresh)[0]
if len(inds) == 0:
all_boxes[j] = np.zeros([0, 5], dtype=np.float32)
continue
c_bboxes = boxes[inds]
c_scores = scores[inds, j]
c_dets = np.hstack((c_bboxes, c_scores[:, np.newaxis])).astype(
np.float32, copy=False)
keep = nms(c_dets, 0.2, force_cpu=False)
c_dets = c_dets[keep, :]
all_boxes[j] = c_dets
nms_time = _t['misc'].toc()
total_time = detect_time + nms_time
return all_boxes, total_time
11.定义推理网络,并加载前面训练的loss最低的模型
trained_models = os.listdir(os.path.join(ROOT_DIR, './rebar_count/model_snapshots')) # 模型文件所在目录
lowest_loss = 9999
best_model_name = ''
for model_name in trained_models:
if not model_name.endswith('pth'):
continue
loss = float(model_name.split('_loss_')[1].split('.pth')[0])
if loss < lowest_loss:
lowest_loss = loss
best_model_name = model_name
best_model_path = os.path.join(ROOT_DIR, './rebar_count/model_snapshots', best_model_name)
print('loading model from', best_model_path)
net = build_net('test', img_dim, num_classes) # 加载模型
state_dict = torch.load(best_model_path)
new_state_dict = OrderedDict()
for k, v in state_dict.items():
head = k[:7]
if head == 'module.':
name = k[7:]
else:
name = k
new_state_dict[name] = v
net.load_state_dict(new_state_dict)
net.eval()
print('Finish load model!')
if torch.cuda.is_available():
net = net.cuda()
cudnn.benchmark = True
else:
net = net.cpu()
detector = Detect(num_classes, 0, cfg)
transform = BaseTransform(img_dim, rgb_means, (2, 0, 1))
object_detector = ObjectDetector(net, detector, transform)
12.测试图片,输出每条钢筋的位置和图片中钢筋总条数
test_img_dir = r'./rebar_count/datasets/test_dataset' # 待预测的图片目录
files = os.listdir(test_img_dir)
files.sort()
for i, file_name in enumerate(files[:2]):
image_src = cv2.imread(os.path.join(test_img_dir, file_name))
detect_bboxes, tim = object_detector.predict(image_src)
image_draw = image_src.copy()
rebar_count = 0
for class_id, class_collection in enumerate(detect_bboxes):
if len(class_collection) > 0:
for i in range(class_collection.shape[0]):
if class_collection[i, -1] > 0.6:
pt = class_collection[i]
cv2.circle(image_draw, (int((pt[0] + pt[2]) * 0.5), int((pt[1] + pt[3]) * 0.5)), int((pt[2] - pt[0]) * 0.5 * 0.6), (255, 0, 0), -1)
rebar_count += 1
cv2.putText(image_draw, 'rebar_count: %d' % rebar_count, (25, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)
plt.figure(i, figsize=(30, 20))
plt.imshow(image_draw)
plt.show()
分段执行,可以呈现钢材位置并标记钢材的数量信息。