机器学习与数据挖掘Coder

U-Net分割细胞壁总结

2018-05-16  本文已影响0人  Manfestain

此处训练集只有30张图片,为训练神经网络,必须将原有数据集进行增强。


原始数据集是有标签数据,增强时为了保持标签和训练集的同步,将train和label分别转换成矩阵,然后将label的第一个通道放在train的第二个通道出,做数据增强

此处的train和label为灰度图像,虽然是三通道图像,但是只有第一个通道是有值的,第二和第三通道均为0。

img_t = load_img(path_train + "/" + str(i) + '.' + imgtype)   # 读入train
img_l = load_img(path_label + '/' + str(i) + '.' + imgtype)   # 读入label
x_t = img_to_array(img_t)
x_l = img_to_array(img_l)
x_t[:, :, 2] = x_l[:, :, 0]   # 把label当成train的第三个通道
img_tmp = array_to_img(x_t)

此处的数据增强使用了keras的数据增强方式:keras.preprocessing.image.ImageDataGenerator

函数详情见http://keras-cn.readthedocs.io/en/latest/preprocessing/image/#imagedatagenerator

datagen = ImageDataGenerator(
            rotation_range=0.2,
            width_shift_range=0.05,
            height_shift_range=0.05,
            shear_range=0.05,
            zoom_range=0.05,
            horizontal_flip=True,
            fill_mode='nearest')
...  # 此处忽略掉中间部分
for batch in datagen.flow(img,
                          batch_size=batch_size,
                          save_to_dir=save_to_dir,
                          save_prefix=save_prefix,
                          save_format=save_format):
            i += 1
            if i > imgnum:
                break

其中,img为待增强图像,save_to_dir为增强后图像的保存路径,save_prefix为增强后的图像名,save_format为增强后的图像格式。
当运行imgnum次时,就跳出for循环,也就是共增强imgnum张图像。
在增强结束后,将train和label分开。此处要注意在train和label与放入之前相反,train在第二通道,label在第三通道。

img = cv2.imread(imgname)
img_train = img[:, :, 2]
img_label = img[:, :, 0]
cv2.imwrite(path_train + "/" + str(i) + "/" + midname + "_train" + "." + self.img_type, img_train)
cv2.imwrite(path_label + "/" + str(i) + "/" + midname + "_label" + "." + self.img_type, img_label)

其它Trick
  1. train_imgs = glob.glob(train_path + "/*." + img_type)
    glob.glob()匹配所有符合条件的文件,以list的形式返回
  2. midname = imgname[imgname.rindex("/") + 1:imgname.rindex("." + self.img_type)]
    rindex(str)返回子字符串str在字符串中最后出现的位置。此处可以通过该方法拿到不带完整路径的文件名。

在windows下,系统可能会使用"\",例如在使用os.path.join()时

list[index1 + index2:index3表示在list中先取出从index1开始到结束的数据,然后在刚取出的数据中从index2取到index3(下标仍从0开始计算)。

  1. 可以将曾强后的图片保存为.npy格式,便于使用。
imgdatas = np.ndarray((count, self.out_rows, self.out_cols, 1), dtype=np.uint8)
for imgname in imgs:
    img = img_to_array(img)
    imgdatas[i] = img
    i+=1

此处忽略掉一些细节步骤。其中,imgdatascount\*row\*col\*1arraycount为train总数。

上一篇下一篇

猜你喜欢

热点阅读