<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          深度學(xué)習(xí)實(shí)戰(zhàn)之布匹缺陷檢測(cè)

          共 23150字,需瀏覽 47分鐘

           ·

          2021-07-04 01:21



          前言

          缺陷檢測(cè)是工業(yè)上非常重要的一個(gè)應(yīng)用,由于缺陷多種多樣,傳統(tǒng)的機(jī)器視覺算法很難做到對(duì)缺陷特征完整的建模和遷移,復(fù)用性不大,要求區(qū)分工況,這會(huì)浪費(fèi)大量的人力成本。深度學(xué)習(xí)在特征提取和定位上取得了非常好的效果,越來越多的學(xué)者和工程人員開始將深度學(xué)習(xí)算法引入到缺陷檢測(cè)領(lǐng)域中。

          導(dǎo)師一直鼓勵(lì)小編做一些小項(xiàng)目,將學(xué)習(xí)與動(dòng)手相結(jié)合。于是最近小編找來了某個(gè)大數(shù)據(jù)競(jìng)賽中的一道缺陷檢測(cè)題目,在開源目標(biāo)檢測(cè)框架的基礎(chǔ)上實(shí)現(xiàn)了一個(gè)用于布匹瑕疵檢測(cè)的模型?,F(xiàn)將過程稍作總結(jié),供各位同學(xué)參考。



          問題簡(jiǎn)介

          01


          1

          實(shí)際背景

          布匹的疵點(diǎn)檢測(cè)是紡織工業(yè)中的一個(gè)十分重要的環(huán)節(jié)。當(dāng)前,在紡織工業(yè)的布匹缺陷檢測(cè)領(lǐng)域,人工檢測(cè)仍然是主要的質(zhì)量檢測(cè)方式。而近年來由于人力成本的提升,以及人工檢測(cè)存在的檢測(cè)速度慢、漏檢率高、一致性差、人員流動(dòng)率高等問題,越來越多的工廠開始利用機(jī)器來代替人工進(jìn)行質(zhì)檢,以提高生產(chǎn)效率,節(jié)省人力成本。

          2

          題目?jī)?nèi)容 

          開發(fā)出高效準(zhǔn)確的深度學(xué)習(xí)算法,檢驗(yàn)布匹表面是否存在缺陷,如果存在缺陷,請(qǐng)標(biāo)注出缺陷的類型和位置。

          3

          數(shù)據(jù)分析

          ? 題目數(shù)據(jù)集提供了9576張圖片用于訓(xùn)練,其中有瑕疵圖片5913張,無瑕疵圖片3663張。

          ? 瑕疵共分為15個(gè)類別。分別為:沾污、錯(cuò)花、水卬、花毛、縫頭、縫頭印、蟲粘、破洞、褶子、織疵、漏印、蠟斑、色差、網(wǎng)折、其它

          ? 尺寸:4096 * 1696


          算法分享

          02


          本文算法基于開源框架YOLOv5,原框架代碼請(qǐng)前往https://github.com/ultralytics/yolov5查看,針對(duì)這次問題做出的修改和調(diào)整部分代碼請(qǐng)繼續(xù)向下閱讀。


          1.框架選擇

          比較流行的算法可以分為兩類,一類是基于Region Proposal的R-CNN系算法(R-CNN,F(xiàn)ast R-CNN, Faster R-CNN等),它們是two-stage的,需要先算法產(chǎn)生目標(biāo)候選框,也就是目標(biāo)位置,然后再對(duì)候選框做分類與回歸。而另一類是Yolo,SSD這類one-stage算法,其僅僅使用一個(gè)卷積神經(jīng)網(wǎng)絡(luò)CNN直接預(yù)測(cè)不同目標(biāo)的類別與位置。第一類方法是準(zhǔn)確度高一些,但是速度慢,但是第二類算法是速度快,但是準(zhǔn)確性要低一些??紤]本次任務(wù)時(shí)間限制和小編電腦性能,本次小編采用了單階段YOLOV5的方案。

          YOLO直接在輸出層回歸bounding box的位置和bounding box所屬類別,從而實(shí)現(xiàn)one-stage。通過這種方式,Yolo可實(shí)現(xiàn)45幀每秒的運(yùn)算速度,完全能滿足實(shí)時(shí)性要求(達(dá)到24幀每秒,人眼就認(rèn)為是連續(xù)的)。

          整個(gè)系統(tǒng)如下圖所示

          2.環(huán)境配置(參考自 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.數(shù)據(jù)預(yù)處理

          · 數(shù)據(jù)集文件結(jié)構(gòu)


          · 標(biāo)注格式說明


          · YOLO要求訓(xùn)練數(shù)據(jù)文件結(jié)構(gòu):


          · 比賽數(shù)據(jù)格式 -> YOLO數(shù)據(jù)格式:

          (針對(duì)本問題原創(chuàng)代碼)

           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('\n')                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.超參數(shù)設(shè)置(針對(duì)本問題原創(chuàng)代碼)

           # 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.模型核心代碼(針對(duì)本問題原創(chuàng)代碼)

          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('\n%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()

          訓(xùn)練截圖


          6.測(cè)試模型并生成結(jié)果(針對(duì)本問題原創(chuàng)代碼)

          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 + '\n') %                                     (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.結(jié)果展示





          后記

          03


          針對(duì)布匹瑕疵檢測(cè)問題,我們首先分析了題目要求,確定了我們的任務(wù)是檢測(cè)到布匹中可能存在的瑕疵,對(duì)其進(jìn)行分類并將其在圖片中標(biāo)注出來。接下來針對(duì)問題要求我們選擇了合適的目標(biāo)檢測(cè)框架YOLOv5,并按照YOLOv5的格式要求對(duì)數(shù)據(jù)集和標(biāo)注進(jìn)行了轉(zhuǎn)換。然后我們根據(jù)問題規(guī)模設(shè)置了合適的超參數(shù),采用遷移學(xué)習(xí)的思想,基于官方的預(yù)訓(xùn)練模型進(jìn)行訓(xùn)練以加快收斂速度。模型訓(xùn)練好以后,即可在驗(yàn)證集上驗(yàn)證我們模型的性能和準(zhǔn)確性。

          回顧整個(gè)過程我們可以發(fā)現(xiàn),在越來越多的優(yōu)秀目標(biāo)檢測(cè)框架被提出并開源之后,目標(biāo)檢測(cè)模型的實(shí)現(xiàn)門檻越來越低,我們可以很輕松的借用這些框架搭建模型來解決現(xiàn)實(shí)生活中的缺陷檢測(cè)問題,深度學(xué)習(xí)的應(yīng)用并沒有我們想象的那么復(fù)雜。
          當(dāng)然,若想得到針對(duì)某個(gè)具體問題表現(xiàn)更加優(yōu)秀的模型,還需要我們根據(jù)具體問題的具體特點(diǎn)對(duì)模型進(jìn)行修正調(diào)優(yōu)。例如針對(duì)本次布匹缺陷檢測(cè)數(shù)據(jù)集中部分缺陷種類樣本數(shù)量少、缺陷目標(biāo)較小的問題,我們可以通過過采樣種類較少的樣本、數(shù)據(jù)增廣、增加anchor的數(shù)量等方法來進(jìn)一步提高模型的準(zhǔn)確率。
          如果有同學(xué)對(duì)該問題感興趣,想要進(jìn)一步了解或在代碼理解、環(huán)境配置等各方面存在疑問的話,歡迎通過文末郵箱聯(lián)系小編,小編在這里期待與您交流。



          文案:張宇(華中科技大學(xué)管理學(xué)院本科二年級(jí))
          指導(dǎo)老師:曹菁菁(武漢理工大學(xué)物流工程學(xué)院)
          排版:程欣悅(荊楚理工學(xué)院本科三年級(jí))
          審稿:張宇(華中科技大學(xué)管理學(xué)院本科二年級(jí)。

          —版權(quán)聲明—

          來源:數(shù)據(jù)魔術(shù)師  作者:張宇

          僅用于學(xué)術(shù)分享,版權(quán)屬于原作者。

          若有侵權(quán),請(qǐng)聯(lián)系微信號(hào):yiyang-sy 刪除或修改!


          —THE END—
          瀏覽 96
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  99久久精品人妻无码一区二区蜜桃 | www色五月 | 欧美日韩123区 | 殴欧美1区2区精品裸体照片 | 日日嗨夜夜嗨一区二区 |