詳解Python中數(shù)據(jù)處理的方法總結(jié)及實(shí)現(xiàn)
背景
數(shù)據(jù)增強(qiáng)作為前處理的關(guān)鍵步驟,在整個(gè)計(jì)算機(jī)視覺(jué)中有著具足輕重的地位;
數(shù)據(jù)增強(qiáng)往往是決定數(shù)據(jù)集質(zhì)量的關(guān)鍵,主要用于數(shù)據(jù)增廣,在基于深度學(xué)習(xí)的任務(wù)中,數(shù)據(jù)的多樣性和數(shù)量往往能夠決定模型的上限;
本次記錄主要是對(duì)數(shù)據(jù)增強(qiáng)中一些方法的源碼實(shí)現(xiàn);
常用數(shù)據(jù)增強(qiáng)方法
首先如果是使用Pytorch框架,其內(nèi)部的torchvision已經(jīng)包裝好了數(shù)據(jù)增強(qiáng)的很多方法;
from torchvision import transforms data_aug = transforms.Compose[ transforms.Resize(size=240), transforms.RandomHorizontalFlip(0.5), transforms.ToTensor() ]
接下來(lái)自己實(shí)現(xiàn)一些主要的方法;
常見(jiàn)的數(shù)據(jù)增強(qiáng)方法有:Compose、RandomHflip、RandomVflip、Reszie、RandomCrop、Normalize、Rotate、RandomRotate
1、Compose
作用:對(duì)多個(gè)方法的排序整合,并且依次調(diào)用;
# 排序(compose) class Compose(object): def __init__(self, transforms): self.transforms = transforms def __call__(self, img): for t in self.transforms: img = t(img) # 通過(guò)循環(huán)不斷調(diào)用列表中的方法 return img
2、RandomHflip
作用:隨機(jī)水平翻轉(zhuǎn);
# 隨機(jī)水平翻轉(zhuǎn)(random h flip) class RandomHflip(object): def __call__(self, image): if random.randint(2): return cv2.flip(image, 1) else: return image
通過(guò)隨機(jī)數(shù)0或1,實(shí)現(xiàn)對(duì)圖像可能反轉(zhuǎn)或不翻轉(zhuǎn);
3、RandomVflip
作用:隨機(jī)垂直翻轉(zhuǎn)
class RandomVflip(object): def __call__(self, image): if random.randint(2): return cv2.flip(image, 0) else: return image
4、RandomCrop
作用:隨機(jī)裁剪;
# 縮放(scale) def scale_down(src_size, size): w, h = size sw, sh = src_size if sh < h: w, h = float(w * sh) / h, sh if sw < w: w, h = sw, float(h * sw) / w return int(w), int(h) # 固定裁剪(fixed crop) def fixed_crop(src, x0, y0, w, h, size=None): out = src[y0:y0 + h, x0:x0 + w] if size is not None and (w, h) != size: out = cv2.resize(out, (size[0], size[1]), interpolation=cv2.INTER_CUBIC) return out # 隨機(jī)裁剪(random crop) class RandomCrop(object): def __init__(self, size): self.size = size def __call__(self, image): h, w, _ = image.shape new_w, new_h = scale_down((w, h), self.size) if w == new_w: x0 = 0 else: x0 = random.randint(0, w - new_w) if h == new_h: y0 = 0 else: y0 = random.randint(0, h - new_h) ??????? out = fixed_crop(image, x0, y0, new_w, new_h, self.size) return out
5、Normalize
作用:對(duì)圖像數(shù)據(jù)進(jìn)行正則化,也就是減均值除方差的作用;
# 正則化(normalize) class Normalize(object): def __init__(self,mean, std): ''' :param mean: RGB order :param std: RGB order ''' self.mean = np.array(mean).reshape(3,1,1) self.std = np.array(std).reshape(3,1,1) def __call__(self, image): ''' :param image: (H,W,3) RGB :return: ''' return (image.transpose((2, 0, 1)) / 255. - self.mean) / self.std
6、Rotate
作用:對(duì)圖像進(jìn)行旋轉(zhuǎn);
# 旋轉(zhuǎn)(rotate) def rotate_nobound(image, angle, center=None, scale=1.): (h, w) = image.shape[:2] # if the center is None, initialize it as the center of the image if center is None: center = (w // 2, h // 2) # perform the rotation M = cv2.getRotationMatrix2D(center, angle, scale) # 這里是實(shí)現(xiàn)得到旋轉(zhuǎn)矩陣 rotated = cv2.warpAffine(image, M, (w, h)) # 通過(guò)矩陣進(jìn)行仿射變換 return rotated
7、RandomRotate
作用:隨機(jī)旋轉(zhuǎn),廣泛適用于圖像增強(qiáng);
# 隨機(jī)旋轉(zhuǎn)(random rotate) class FixRandomRotate(object): # 這里的隨機(jī)旋轉(zhuǎn)是指在0、90、180、270四個(gè)角度下的 def __init__(self, angles=[0,90,180,270], bound=False): self.angles = angles self.bound = bound def __call__(self,img): do_rotate = random.randint(0, 4) angle=self.angles[do_rotate] if self.bound: img = rotate_bound(img, angle) else: img = rotate_nobound(img, angle) return img
8、Resize
作用:實(shí)現(xiàn)縮放;
# 大小重置(resize) class Resize(object): def __init__(self, size, inter=cv2.INTER_CUBIC): self.size = size self.inter = inter def __call__(self, image): return cv2.resize(image, (self.size[0], self.size[0]), interpolation=self.inter)
其他數(shù)據(jù)增強(qiáng)方法
其他一些數(shù)據(jù)增強(qiáng)的方法大部分是特殊的裁剪;
1、中心裁剪
# 中心裁剪(center crop) def center_crop(src, size): h, w = src.shape[0:2] new_w, new_h = scale_down((w, h), size) x0 = int((w - new_w) / 2) y0 = int((h - new_h) / 2) out = fixed_crop(src, x0, y0, new_w, new_h, size) return out
2、隨機(jī)亮度增強(qiáng)
# 隨機(jī)亮度增強(qiáng)(random brightness) class RandomBrightness(object): def __init__(self, delta=10): assert delta >= 0 assert delta <= 255 self.delta = delta def __call__(self, image): if random.randint(2): delta = random.uniform(-self.delta, self.delta) image = (image + delta).clip(0.0, 255.0) # print('RandomBrightness,delta ',delta) return image
3、隨機(jī)對(duì)比度增強(qiáng)
# 隨機(jī)對(duì)比度增強(qiáng)(random contrast) class RandomContrast(object): def __init__(self, lower=0.9, upper=1.05): self.lower = lower self.upper = upper assert self.upper >= self.lower, "contrast upper must be >= lower." assert self.lower >= 0, "contrast lower must be non-negative." # expects float image def __call__(self, image): if random.randint(2): alpha = random.uniform(self.lower, self.upper) # print('contrast:', alpha) image = (image * alpha).clip(0.0,255.0) return image
4、隨機(jī)飽和度增強(qiáng)
# 隨機(jī)飽和度增強(qiáng)(random saturation) class RandomSaturation(object): def __init__(self, lower=0.8, upper=1.2): self.lower = lower self.upper = upper assert self.upper >= self.lower, "contrast upper must be >= lower." assert self.lower >= 0, "contrast lower must be non-negative." def __call__(self, image): if random.randint(2): alpha = random.uniform(self.lower, self.upper) image[:, :, 1] *= alpha # print('RandomSaturation,alpha',alpha) return image
5、邊界擴(kuò)充
# 邊界擴(kuò)充(expand border) class ExpandBorder(object): def __init__(self, mode='constant', value=255, size=(336,336), resize=False): self.mode = mode self.value = value self.resize = resize self.size = size def __call__(self, image): h, w, _ = image.shape if h > w: pad1 = (h-w)//2 pad2 = h - w - pad1 if self.mode == 'constant': image = np.pad(image, ((0, 0), (pad1, pad2), (0, 0)), self.mode, constant_values=self.value) else: image = np.pad(image,((0,0), (pad1, pad2),(0,0)), self.mode) elif h < w: pad1 = (w-h)//2 pad2 = w-h - pad1 if self.mode == 'constant': image = np.pad(image, ((pad1, pad2),(0, 0), (0, 0)), self.mode,constant_values=self.value) else: image = np.pad(image, ((pad1, pad2), (0, 0), (0, 0)),self.mode) if self.resize: image = cv2.resize(image, (self.size[0], self.size[0]),interpolation=cv2.INTER_LINEAR) return image
當(dāng)然還有很多其他數(shù)據(jù)增強(qiáng)的方式,在這里就不繼續(xù)做說(shuō)明了;
拓展
除了可以使用Pytorch中自帶的數(shù)據(jù)增強(qiáng)包之外,也可以使用imgaug這個(gè)包(一個(gè)基于數(shù)據(jù)處理的包、包含大量的數(shù)據(jù)處理方法,并且代碼完全開(kāi)源)
代碼地址:https://github.com/aleju/imgaug
說(shuō)明文檔:https://imgaug.readthedocs.io/en/latest/index.html
強(qiáng)烈建議大家看看這個(gè)說(shuō)明文檔,其中的很多數(shù)據(jù)處理方法可以快速的應(yīng)用到實(shí)際項(xiàng)目中,也可以加深對(duì)圖像處理的理解;
到此這篇關(guān)于詳解Python中數(shù)據(jù)處理的方法總結(jié)及實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python selenium模擬手動(dòng)操作實(shí)現(xiàn)無(wú)人值守刷積分功能
這篇文章主要介紹了Python selenium模擬手動(dòng)操作達(dá)到無(wú)人值守刷積分目的,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05python使用for循環(huán)和海龜繪圖實(shí)現(xiàn)漂亮螺旋線(xiàn)
這篇文章主要為大家介紹了python使用for循環(huán)和海龜繪圖實(shí)現(xiàn)漂亮螺旋線(xiàn)實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06pycharm運(yùn)行程序時(shí)看不到任何結(jié)果顯示的解決
今天小編就為大家分享一篇pycharm運(yùn)行程序時(shí)看不到任何結(jié)果顯示的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02python3去掉string中的標(biāo)點(diǎn)符號(hào)方法
今天小編就為大家分享一篇python3去掉string中的標(biāo)點(diǎn)符號(hào)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01詳解多線(xiàn)程Django程序耗盡數(shù)據(jù)庫(kù)連接的問(wèn)題
這篇文章主要介紹了多線(xiàn)程Django程序耗盡數(shù)據(jù)庫(kù)連接的問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Python實(shí)戰(zhàn)之生成有關(guān)聯(lián)單選問(wèn)卷
這篇文章主要為大家分享了一個(gè)Python實(shí)戰(zhàn)小案例——生成有關(guān)聯(lián)單選問(wèn)卷,并且能根據(jù)問(wèn)卷總分?jǐn)?shù)生成對(duì)應(yīng)判斷文案結(jié)果,感興趣的可以了解一下2023-04-04Python學(xué)習(xí)筆記之變量、自定義函數(shù)用法示例
這篇文章主要介紹了Python學(xué)習(xí)筆記之變量、自定義函數(shù)用法,結(jié)合實(shí)例形式分析了Python變量、自定義函數(shù)的概念、功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-05-05python多線(xiàn)程并發(fā)及測(cè)試框架案例
這篇文章主要介紹了python多線(xiàn)程并發(fā)及測(cè)試框架案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Python3.5模塊的定義、導(dǎo)入、優(yōu)化操作圖文詳解
這篇文章主要介紹了Python3.5模塊的定義、導(dǎo)入、優(yōu)化操作,結(jié)合圖文與實(shí)例形式詳細(xì)分析了Python3.5模塊的定義、導(dǎo)入及優(yōu)化等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-04-04pycharm 使用心得(八)如何調(diào)用另一文件中的函數(shù)
事件環(huán)境: pycharm 編寫(xiě)了函數(shù)do() 保存在make.py 如何在另一個(gè)file里調(diào)用do函數(shù)?2014-06-06