语义分割中数据增强笔记
transform中的注意事项:
对于train / val / test 中image 和label 的操作
- 注意image和label变换的相同随机性
- train和val和test的过程差异性
使用PIL处理图片:
基础知识
图像的模式定义了图像的类型和像素的位宽。当前支持如下模式:
1:1位像素,表示黑和白,但是存储的时候每个像素存储为8bit。
L:8位像素,表示黑和白。
P:8位像素,使用调色板映射到其他模式。
RGB:3x8位像素,为真彩色。
RGBA:4x8位像素,有透明通道的真彩色。
CMYK:4x8位像素,颜色分离。
YCbCr:3x8位像素,彩色视频格式。
PIL.Image与numpy.array之间的相互转换
Image ====> numpy
img = numpy.array(im)
numpy ====> Image
img = Image.fromarray(img.astype('uint8')),convert('RGB')
图片的同步变换问题
img = cv2.imread('../data/A/111.tif')
img_B = cv2.imread('../data/B/111.tif')
label = cv2.imread('../data/label/111.png',0)
transform_all = transforms.Compose([
transforms.ToPILImage(),
transforms.ColorJitter(0.4,0.42,0.52),
transforms.RandomRotation(180),
# transforms.PILToTensor(),
])
img = transform_all(img)
img_B = transform_all(img_B)
img.show()
img_B.show()
处理的图片显示:
解决方法
这里拿旋转举例
class RandomRotate(object):
def __init__(self, degree=15):
self.degree = degree
def __call__(self, sample):
img1, label = sample
if random.random() < 0.3:
rotate_degree = random.uniform(-1*self.degree, self.degree)
img1 = img1.rotate(rotate_degree, Image.BILINEAR)
# img2 = img2.rotate(rotate_degree, Image.BILINEAR)
label = label.rotate(rotate_degree, Image.NEAREST)
return img1, label
img1,label = RandomRotate()([img1,label])
重点需要保证每一幅对应的图像和标签使用相同的变化参数
如果有需要,更多语义分割中数据增强代码请见我的github