|
钢铁表面缺陷检测:
数据:https://www.kaggle.com/c/severstal-steel-defect-detection/data
- from pathlib import Path
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- import cv2
- class RLE:
- """
- Encapsulates run-length-encoding functionality.
- """
- MASK_H = 256
- MASK_W = 1600
- @classmethod
- def from_str(cls, s):
- if s != s:
- return cls()
- list_ = [int(n) for n in s.split(' ')]
- return cls.from_list(list_)
- @classmethod
- def from_mask(cls, mask):
- pixels = mask.T.flatten()
- pixels = np.concatenate([[0], pixels, [0]])
- runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
- runs[1::2] -= runs[::2]
- return cls.from_list(runs)
- @classmethod
- def from_list(cls, list_):
- n_items = int(len(list_) / 2)
- items = np.zeros((n_items, 2), dtype=np.uint64)
- for i in range(n_items):
- items[i, 0] = list_[i * 2]
- items[i, 1] = list_[i * 2 + 1]
- return cls(items)
- def __init__(self, items=np.zeros((0, 0))):
- self._items = items
- @property
- def items(self):
- return self._items
- def __iter__(self):
- for idx, item in enumerate(self.items):
- yield (item[0], item[1]) # run, length
- def __len__(self):
- return self.items.shape[0]
- def to_mask(self):
- mask = np.zeros(self.MASK_H * self.MASK_W, dtype=np.uint8)
- for run, length in self:
- run = int(run - 1)
- end = int(run + length)
- mask[run:end] = 1
- return mask.reshape(self.MASK_H, self.MASK_W, order='F')
- def to_str_list(self):
- list_ = []
- for run, length in self:
- list_.append(str(run))
- list_.append(str(length))
- return list_
- def __str__(self):
- if len(self) == 0:
- return ''
- return ' '.join(self.to_str_list())
- def __repr__(self):
- return self.__str__()
-
- def make_mask(labels):
- masks = np.zeros((256, 1600, 4), dtype=np.float32)
- for c, label in enumerate(labels.values):
- rle = RLE.from_str(label)
- masks[:, :, c] = rle.to_mask()
- return masks
- img_size = 256 * 1600
- train_csv = 'F:/sck/steel/train.csv'
- df = pd.read_csv(train_csv)
- df.head()
- N_CLASSES = 4
- rle_cols = [f'rle{i}' for i in range(N_CLASSES)]
- df['ImageId'], df['ClassId'] = zip(*df['ImageId_ClassId'].str.split('_'))
- df['ClassId'] = df['ClassId'].astype(int)
- df = df.pivot(index='ImageId', columns='ClassId', values='EncodedPixels')
- df.columns = rle_cols
- df['defects'] = df.count(axis=1)
- df = df.loc[df.defects > 0, :]
- df.head()
- r1 = df.loc['0007a71bf.jpg'][rle_cols]
- r1
- m1 = make_mask(r1)[:, :, 2]
- plt.imshow(m1)
- for i in range(len(df)):
- name=df.index[i]
- ri = df.loc[name][rle_cols]
- mi = make_mask(ri)
- cv2.imwrite('F:/sck/steel/fourclass/1/'+name[0:-4]+'.png', mi[:,:,0])
- cv2.imwrite('F:/sck/steel/fourclass/2/'+name[0:-4]+'.png', mi[:,:,1])
- cv2.imwrite('F:/sck/steel/fourclass/3/'+name[0:-4]+'.png', mi[:,:,2])
- cv2.imwrite('F:/sck/steel/fourclass/4/'+name[0:-4]+'.png', mi[:,:,3])
- if(mi[:,:,0].max()>1):
- print('11')
- if(mi[:,:,1].max()>1):
- print('11')
- if(mi[:,:,2].max()>1):
- print('11')
复制代码
参考:【1】https://www.kaggle.com/lightforever/severstal-mlcomp-catalyst-infer-0-90672
【2】https://blog.csdn.net/HRLSW/article/details/102779214
【3】AI Studio优质数据集
【4】https://aistudio.baidu.com/aistudio/datasetdetail/10941
|
|