前言
缺陷檢測是工業上非常重要的一個應用,由于缺陷多種多樣,傳統的機器視覺算法很難做到對缺陷特征完整的建模和遷移,復用性不大,要求區分工況,這會浪費大量的人力成本。深度學習在特征提取和定位上取得了非常好的效果,越來越多的學者和工程人員開始將深度學習算法引入到缺陷檢測領域中。
導師一直鼓勵小編做一些小項目,將學習與動手相結合。于是最近小編找來了某個大數據競賽中的一道缺陷檢測題目,在開源目標檢測框架的基礎上實現了一個用于布匹瑕疵檢測的模型。現將過程稍作總結,供各位同學參考。
問題簡介
01
1
實際背景
布匹的疵點檢測是紡織工業中的一個十分重要的環節。當前,在紡織工業的布匹缺陷檢測領域,人工檢測仍然是主要的質量檢測方式。而近年來由于人力成本的提升,以及人工檢測存在的檢測速度慢、漏檢率高、一致性差、人員流動率高等問題,越來越多的工廠開始利用機器來代替人工進行質檢,以提高生產效率,節省人力成本。
2
題目內容
開發出高效準確的深度學習算法,檢驗布匹表面是否存在缺陷,如果存在缺陷,請標注出缺陷的類型和位置。
3
數據分析
? 題目數據集提供了9576張圖片用于訓練,其中有瑕疵圖片5913張,無瑕疵圖片3663張。
? 瑕疵共分為15個類別。分別為:沾污、錯花、水卬、花毛、縫頭、縫頭印、蟲粘、破洞、褶子、織疵、漏印、蠟斑、色差、網折、其它
? 尺寸:4096 * 1696
算法分享
02
本文算法基于開源框架YOLOv5,原框架代碼請前往https://github.com/ultralytics/yolov5查看,針對這次問題做出的修改和調整部分代碼請繼續向下閱讀。
1.框架選擇
比較流行的算法可以分為兩類,一類是基于Region Proposal的R-CNN系算法(R-CNN,Fast R-CNN, Faster R-CNN等),它們是two-stage的,需要先算法產生目標候選框,也就是目標位置,然后再對候選框做分類與回歸。而另一類是Yolo,SSD這類one-stage算法,其僅僅使用一個卷積神經網絡CNN直接預測不同目標的類別與位置。
第一類方法是準確度高一些,但是速度慢,但是第二類算法是速度快,但是準確性要低一些。考慮本次任務時間限制和小編電腦性能,本次小編采用了單階段YOLOV5的方案。
YOLO直接在輸出層回歸bounding box的位置和bounding box所屬類別,從而實現one-stage。通過這種方式,Yolo可實現45幀每秒的運算速度,完全能滿足實時性要求(達到24幀每秒,人眼就認為是連續的)。
2.環境配置(參考自 YOLOv5 requirements)
Cython numpy==1.17 opencv-python torch》=1.4 matplotlib pillow tensorboard PyYAML》=5.3torchvisionscipytqdmgit+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI
3.數據預處理
· 數據集文件結構
· 標注格式說明
· YOLO要求訓練數據文件結構:
· 比賽數據格式 -》 YOLO數據格式:
(針對本問題原創代碼)
for fold in [0]: val_index = index[len(index) * fold // 5:len(index) * (fold + 1) // 5] print(len(val_index)) for num, name in enumerate(name_list): print(c_list[num], x_center_list[num], y_center_list[num], w_list[num], h_list[num]) row = [c_list[num], x_center_list[num], y_center_list[num], w_list[num], h_list[num]] if name in val_index: path2save = ‘val/’ else: path2save = ‘train/’ if not os.path.exists(‘convertor/fold{}/labels/’.format(fold) + path2save): os.makedirs(‘convertor/fold{}/labels/’.format(fold) + path2save) with open(‘convertor/fold{}/labels/’.format(fold) + path2save + name.split(‘。’)[0] + “.txt”, ‘a+’) as f: for data in row: f.write(‘{} ’.format(data)) f.write(‘
’) if not os.path.exists(‘convertor/fold{}/images/{}’.format(fold, path2save)): os.makedirs(‘convertor/fold{}/images/{}’.format(fold, path2save)) sh.copy(os.path.join(image_path, name.split(‘。’)[0], name), ‘convertor/fold{}/images/{}/{}’.format(fold, path2save, name))
4.超參數設置(針對本問題原創代碼)
# Hyperparameters hyp = {‘lr0’: 0.01, # initial learning rate (SGD=1E-2, Adam=1E-3) ‘momentum’: 0.937, # SGD momentum ‘weight_decay’: 5e-4, # optimizer weight decay ‘giou’: 0.05, # giou loss gain ‘cls’: 0.58, # cls loss gain ‘cls_pw’: 1.0, # cls BCELoss positive_weight ‘obj’: 1.0, # obj loss gain (*=img_size/320 if img_size != 320) ‘obj_pw’: 1.0, # obj BCELoss positive_weight ‘iou_t’: 0.20, # iou training threshold ‘anchor_t’: 4.0, # anchor-multiple threshold ‘fl_gamma’: 0.0, # focal loss gamma (efficientDet default is gamma=1.5) ‘hsv_h’: 0.014, # image HSV-Hue augmentation (fraction) ‘hsv_s’: 0.68, # image HSV-Saturation augmentation (fraction) ‘hsv_v’: 0.36, # image HSV-Value augmentation (fraction) ‘degrees’: 0.0, # image rotation (+/- deg) ‘translate’: 0.0, # image translation (+/- fraction) ‘scale’: 0.5, # image scale (+/- gain) ‘shear’: 0.0} # image shear (+/- deg)}
5.模型核心代碼(針對本問題原創代碼)
import argparse from models.experimental import * class Detect(nn.Module): def __init__(self, nc=80, anchors=()): # detection layer super(Detect, self).__init__() self.stride = None # strides computed during build self.nc = nc # number of classes self.no = nc + 5 # number of outputs per anchor self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [torch.zeros(1)] * self.nl # init grid a = torch.tensor(anchors).float().view(self.nl, -1, 2) self.register_buffer(‘anchors’, a) # shape(nl,na,2) self.register_buffer(‘anchor_grid’, a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) self.export = False # onnx export def forward(self, x): # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) y = x[i].sigmoid() y[。。., 0:2] = (y[。。., 0:2] * 2. - 0.5 + self.grid[i].to(x[i].device)) * self.stride[i] # xy y[。。., 2:4] = (y[。。., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh z.append(y.view(bs, -1, self.no)) return x if self.training else (torch.cat(z, 1), x) @staticmethod def _make_grid(nx=20, ny=20): yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() class Model(nn.Module): def __init__(self, model_cfg=‘yolov5s.yaml’, ch=3, nc=None): # model, input channels, number of classes super(Model, self).__init__() if type(model_cfg) is dict: self.md = model_cfg # model dict else: # is *.yaml import yaml # for torch hub with open(model_cfg) as f: self.md = yaml.load(f, Loader=yaml.FullLoader) # model dict # Define model if nc and nc != self.md[‘nc’]: print(‘Overriding %s nc=%g with nc=%g’ % (model_cfg, self.md[‘nc’], nc)) self.md[‘nc’] = nc # override yaml value self.model, self.save = parse_model(self.md, ch=[ch]) # model, savelist, ch_out # print([x.shape for x in self.forward(torch.zeros(1, ch, 64, 64))]) # Build strides, anchors m = self.model[-1] # Detect() if isinstance(m, Detect): s = 128 # 2x min stride m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward m.anchors /= m.stride.view(-1, 1, 1) check_anchor_order(m) self.stride = m.stride self._initialize_biases() # only run once # print(‘Strides: %s’ % m.stride.tolist()) # Init weights, biases torch_utils.initialize_weights(self) self._initialize_biases() # only run once torch_utils.model_info(self) print(‘’) def forward(self, x, augment=False, profile=False): if augment: img_size = x.shape[-2:] # height, width s = [0.83, 0.67] # scales #1.2 0.83 y = [] for i, xi in enumerate((x, torch_utils.scale_img(x.flip(3), s[0]), # flip-lr and scale torch_utils.scale_img(x, s[1]), # scale )): # cv2.imwrite(‘img%g.jpg’ % i, 255 * xi[0].numpy().transpose((1, 2, 0))[:, :, ::-1]) y.append(self.forward_once(xi)[0]) y[1][。。., :4] /= s[0] # scale y[1][。。., 0] = img_size[1] - y[1][。。., 0] # flip lr y[2][。。., :4] /= s[1] # scale return torch.cat(y, 1), None # augmented inference, train else: return self.forward_once(x, profile) # single-scale inference, train def forward_once(self, x, profile=False): y, dt = [], [] # outputs for m in self.model: if m.f != -1: # if not from previous layer x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers
if profile: try: import thop o = thop.profile(m, inputs=(x,), verbose=False)[0] / 1E9 * 2 # FLOPS except: o = 0 t = torch_utils.time_synchronized() for _ in range(10): _ = m(x) dt.append((torch_utils.time_synchronized() - t) * 100) print(‘%10.1f%10.0f%10.1fms %-40s’ % (o, m.np, dt[-1], m.type)) x = m(x) # run y.append(x if m.i in self.save else None) # save output
if profile: print(‘%.1fms total’ % sum(dt)) return x
def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1. m = self.model[-1] # Detect() module for f, s in zip(m.f, m.stride): # from mi = self.model[f % m.i] b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85) b[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image) b[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)
def _print_biases(self): m = self.model[-1] # Detect() module for f in sorted([x % m.i for x in m.f]): # from b = self.model[f].bias.detach().view(m.na, -1).T # conv.bias(255) to (3,85) print((‘%g Conv2d.bias:’ + ‘%10.3g’ * 6) % (f, *b[:5].mean(1).tolist(), b[5:].mean()))
# def _print_weights(self): # for m in self.model.modules(): # if type(m) is Bottleneck: # print(‘%10.3g’ % (m.w.detach().sigmoid() * 2)) # shortcut weights
def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers print(‘Fusing layers.。。 ’, end=‘’) for m in self.model.modules(): if type(m) is Conv: m.conv = torch_utils.fuse_conv_and_bn(m.conv, m.bn) # update conv m.bn = None # remove batchnorm m.forward = m.fuseforward # update forward torch_utils.model_info(self) return self
def parse_model(md, ch): # model_dict, input_channels(3) print(‘
%3s%18s%3s%10s %-40s%-30s’ % (‘’, ‘from’, ‘n’, ‘params’, ‘module’, ‘arguments’)) anchors, nc, gd, gw = md[‘anchors’], md[‘nc’], md[‘depth_multiple’], md[‘width_multiple’] na = (len(anchors[0]) // 2) # number of anchors no = na * (nc + 5) # number of outputs = anchors * (classes + 5)
layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out for i, (f, n, m, args) in enumerate(md[‘backbone’] + md[‘head’]): # from, number, module, args m = eval(m) if isinstance(m, str) else m # eval strings for j, a in enumerate(args): try: args[j] = eval(a) if isinstance(a, str) else a # eval strings except: pass
n = max(round(n * gd), 1) if n 》 1 else n # depth gain if m in [nn.Conv2d, Conv, PW_Conv,Bottleneck, SPP, DWConv, MixConv2d, Focus, CrossConv, BottleneckCSP, C3, BottleneckMOB]: c1, c2 = ch[f], args[0]
# Normal # if i 》 0 and args[0] != no: # channel expansion factor # ex = 1.75 # exponential (default 2.0) # e = math.log(c2 / ch[1]) / math.log(2) # c2 = int(ch[1] * ex ** e) # if m != Focus: c2 = make_divisible(c2 * gw, 8) if c2 != no else c2
# Experimental # if i 》 0 and args[0] != no: # channel expansion factor # ex = 1 + gw # exponential (default 2.0) # ch1 = 32 # ch[1] # e = math.log(c2 / ch1) / math.log(2) # level 1-n # c2 = int(ch1 * ex ** e) # if m != Focus: # c2 = make_divisible(c2, 8) if c2 != no else c2
args = [c1, c2, *args[1:]] if m in [BottleneckCSP, C3]: args.insert(2, n) n = 1 elif m is nn.BatchNorm2d: args = [ch[f]] elif m is Concat: c2 = sum([ch[-1 if x == -1 else x + 1] for x in f]) elif m is Detect: f = f or list(reversed([(-1 if j == i else j - 1) for j, x in enumerate(ch) if x == no])) else: c2 = ch[f]
m_ = nn.Sequential(*[m(*args) for _ in range(n)]) if n 》 1 else m(*args) # module t = str(m)[8:-2].replace(‘__main__.’, ‘’) # module type np = sum([x.numel() for x in m_.parameters()]) # number params m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, ‘from’ index, type, number params print(‘%3s%18s%3s%10.0f %-40s%-30s’ % (i, f, n, np, t, args)) # print save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist layers.append(m_) ch.append(c2) return nn.Sequential(*layers), sorted(save)
if __name__ == ‘__main__’: parser = argparse.ArgumentParser() parser.add_argument(‘--cfg’, type=str, default=‘yolov5s.yaml’, help=‘model.yaml’) parser.add_argument(‘--device’, default=‘’, help=‘cuda device, i.e. 0 or 0,1,2,3 or cpu’) opt = parser.parse_args() opt.cfg = check_file(opt.cfg) # check file device = torch_utils.select_device(opt.device)
# Create model model = Model(opt.cfg).to(device) model.train()
訓練截圖
6.測試模型并生成結果(針對本問題原創代碼)
for *xyxy, conf, cls in det: if save_txt: # Write to file xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh with open(txt_path + ‘.txt’, ‘a’) as f: f.write((‘%g ’ * 5 + ‘
’) % (cls, *xywh)) # label format # write to json if save_json: name = os.path.split(txt_path)[-1] print(name)
x1, y1, x2, y2 = float(xyxy[0]), float(xyxy[1]), float( xyxy[2]), float(xyxy[3]) bbox = [x1, y1, x2, y2] img_name = name conf = float(conf)
#add solution remove other result.append({ ‘name’: img_name + ‘.jpg’, ‘category’: int(cls + 1), ‘bbox’: bbox, ‘score’: conf })
7.結果展示
后記
03
針對布匹瑕疵檢測問題,我們首先分析了題目要求,確定了我們的任務是檢測到布匹中可能存在的瑕疵,對其進行分類并將其在圖片中標注出來。接下來針對問題要求我們選擇了合適的目標檢測框架YOLOv5,并按照YOLOv5的格式要求對數據集和標注進行了轉換。然后我們根據問題規模設置了合適的超參數,采用遷移學習的思想,基于官方的預訓練模型進行訓練以加快收斂速度。模型訓練好以后,即可在驗證集上驗證我們模型的性能和準確性。
回顧整個過程我們可以發現,在越來越多的優秀目標檢測框架被提出并開源之后,目標檢測模型的實現門檻越來越低,我們可以很輕松的借用這些框架搭建模型來解決現實生活中的缺陷檢測問題,深度學習的應用并沒有我們想象的那么復雜。
當然,若想得到針對某個具體問題表現更加優秀的模型,還需要我們根據具體問題的具體特點對模型進行修正調優。例如針對本次布匹缺陷檢測數據集中部分缺陷種類樣本數量少、缺陷目標較小的問題,我們可以通過過采樣種類較少的樣本、數據增廣、增加anchor的數量等方法來進一步提高模型的準確率。如果有同學對該問題感興趣,想要進一步了解或在代碼理解、環境配置等各方面存在疑問的話,歡迎通過文末郵箱聯系小編,小編在這里期待與您交流。
文案:張宇(華中科技大學管理學院本科二年級)指導老師:曹菁菁(武漢理工大學物流工程學院)排版:程欣悅(荊楚理工學院本科三年級)審稿:張宇(華中科技大學管理學院本科二年級)。
—版權聲明—
來源:數據魔術師 作者:張宇
僅用于學術分享,版權屬于原作者。
若有侵權,請聯系微信號:刪除或修改!
編輯:jq
-
機器視覺
+關注
關注
162文章
4421瀏覽量
120910 -
數據集
+關注
關注
4文章
1212瀏覽量
24895 -
深度學習
+關注
關注
73文章
5522瀏覽量
121691
原文標題:深度學習實戰之布匹缺陷檢測
文章出處:【微信號:vision263com,微信公眾號:新機器視覺】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
在樹莓派上部署YOLOv5進行動物目標檢測的完整流程

在目標檢測中大物體的重要性

目標檢測與識別技術有哪些
目標檢測與識別技術的關系是什么
目標檢測識別主要應用于哪些方面
基于深度學習的小目標檢測
人臉檢測模型有哪些
AI驅動的雷達目標檢測:前沿技術與實現策略

評論