├── README.md ├── data.py ├── images ├── test │ ├── 0.tif │ ├── 1.tif │ ├── 10.tif │ ├── 11.tif │ ├── 12.tif │ ├── 13.tif │ ├── 14.tif │ ├── 15.tif │ ├── 16.tif │ ├── 17.tif │ ├── 18.tif │ ├── 19.tif │ ├── 2.tif │ ├── 20.tif │ ├── 21.tif │ ├── 22.tif │ ├── 23.tif │ ├── 24.tif │ ├── 25.tif │ ├── 26.tif │ ├── 27.tif │ ├── 28.tif │ ├── 29.tif │ ├── 3.tif │ ├── 4.tif │ ├── 5.tif │ ├── 6.tif │ ├── 7.tif │ ├── 8.tif │ └── 9.tif └── train │ ├── images │ ├── 0.tif │ ├── 1.tif │ ├── 10.tif │ ├── 11.tif │ ├── 12.tif │ ├── 13.tif │ ├── 14.tif │ ├── 15.tif │ ├── 16.tif │ ├── 17.tif │ ├── 18.tif │ ├── 19.tif │ ├── 2.tif │ ├── 20.tif │ ├── 21.tif │ ├── 22.tif │ ├── 23.tif │ ├── 24.tif │ ├── 25.tif │ ├── 26.tif │ ├── 27.tif │ ├── 28.tif │ ├── 29.tif │ ├── 3.tif │ ├── 4.tif │ ├── 5.tif │ ├── 6.tif │ ├── 7.tif │ ├── 8.tif │ └── 9.tif │ └── label │ ├── 0.tif │ ├── 1.tif │ ├── 10.tif │ ├── 11.tif │ ├── 12.tif │ ├── 13.tif │ ├── 14.tif │ ├── 15.tif │ ├── 16.tif │ ├── 17.tif │ ├── 18.tif │ ├── 19.tif │ ├── 2.tif │ ├── 20.tif │ ├── 21.tif │ ├── 22.tif │ ├── 23.tif │ ├── 24.tif │ ├── 25.tif │ ├── 26.tif │ ├── 27.tif │ ├── 28.tif │ ├── 29.tif │ ├── 3.tif │ ├── 4.tif │ ├── 5.tif │ ├── 6.tif │ ├── 7.tif │ ├── 8.tif │ └── 9.tif ├── test_predict.py └── unet.py /README.md: -------------------------------------------------------------------------------- 1 | # Unet 2 | A demo of Unet to detect edges! 3 | 4 | - **[My CSDN Blog on how to use it](http://blog.csdn.net/awyyauqpmy/article/details/79290710)** 5 | -------------------------------------------------------------------------------- /data.py: -------------------------------------------------------------------------------- 1 | from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img 2 | import numpy as np 3 | import os 4 | import glob 5 | import cv2 6 | from libtiff import TIFF 7 | 8 | class myAugmentation(object): 9 | 10 | """ 11 | 一个用于图像增强的类: 12 | 首先:分别读取训练的图片和标签,然后将图片和标签合并用于下一个阶段使用 13 | 然后:使用Keras的预处理来增强图像 14 | 最后:将增强后的图片分解开,分为训练图片和训练标签 15 | """ 16 | def __init__(self, train_path="train", label_path="label", merge_path="merge", aug_merge_path="aug_merge", aug_train_path="aug_train", aug_label_path="aug_label", img_type="tif"): 17 | """ 18 | 使用glob从路径中得到所有的“.img_type”文件,初始化类:__init__() 19 | """ 20 | self.train_imgs = glob.glob(train_path+"/*."+img_type) 21 | self.label_imgs = glob.glob(label_path+"/*."+img_type) 22 | self.train_path = train_path 23 | self.label_path = label_path 24 | self.merge_path = merge_path 25 | self.img_type = img_type 26 | self.aug_merge_path = aug_merge_path 27 | self.aug_train_path = aug_train_path 28 | self.aug_label_path = aug_label_path 29 | self.slices = len(self.train_imgs) 30 | self.datagen = ImageDataGenerator( 31 | rotation_range=0.2, 32 | width_shift_range=0.05, 33 | height_shift_range=0.05, 34 | shear_range=0.05, 35 | zoom_range=0.05, 36 | horizontal_flip=True, 37 | fill_mode='nearest') 38 | 39 | def Augmentation(self): 40 | 41 | """ 42 | Start augmentation..... 43 | """ 44 | trains = self.train_imgs 45 | labels = self.label_imgs 46 | path_train = self.train_path 47 | path_label = self.label_path 48 | path_merge = self.merge_path 49 | imgtype = self.img_type 50 | path_aug_merge = self.aug_merge_path 51 | if len(trains) != len(labels) or len(trains) == 0 or len(trains) == 0: 52 | print ("trains can't match labels") 53 | return 0 54 | for i in range(len(trains)): 55 | img_t = load_img(path_train+"/"+str(i)+"."+imgtype) 56 | img_l = load_img(path_label+"/"+str(i)+"."+imgtype) 57 | x_t = img_to_array(img_t) 58 | x_l = img_to_array(img_l) 59 | x_t[:,:,2] = x_l[:,:,0] 60 | img_tmp = array_to_img(x_t) 61 | img_tmp.save(path_merge+"/"+str(i)+"."+imgtype) 62 | img = x_t 63 | img = img.reshape((1,) + img.shape) 64 | savedir = path_aug_merge + "/" + str(i) 65 | if not os.path.lexists(savedir): 66 | os.mkdir(savedir) 67 | self.doAugmentate(img, savedir, str(i)) 68 | 69 | def doAugmentate(self, img, save_to_dir, save_prefix, batch_size=1, save_format='tif', imgnum=30): 70 | # 增强一张图片的方法 71 | """ 72 | augmentate one image 73 | """ 74 | datagen = self.datagen 75 | i = 0 76 | for batch in datagen.flow(img, 77 | batch_size=batch_size, 78 | save_to_dir=save_to_dir, 79 | save_prefix=save_prefix, 80 | save_format=save_format): 81 | i += 1 82 | if i > imgnum: 83 | break 84 | 85 | def splitMerge(self): 86 | # 将合在一起的图片分开 87 | """ 88 | split merged image apart 89 | """ 90 | path_merge = self.aug_merge_path 91 | path_train = self.aug_train_path 92 | path_label = self.aug_label_path 93 | 94 | for i in range(self.slices): 95 | path = path_merge + "/" + str(i) 96 | train_imgs = glob.glob(path+"/*."+self.img_type) 97 | savedir = path_train + "/" + str(i) 98 | if not os.path.lexists(savedir): 99 | os.mkdir(savedir) 100 | savedir = path_label + "/" + str(i) 101 | 102 | if not os.path.lexists(savedir): 103 | os.mkdir(savedir) 104 | for imgname in train_imgs: 105 | midname = imgname[imgname.rindex("/")+1:imgname.rindex("."+self.img_type)] 106 | img = cv2.imread(imgname) 107 | img_train = img[:,:,2] #cv2 read image rgb->bgr 108 | img_label = img[:,:,0] 109 | cv2.imwrite(path_train+"/"+str(i)+"/"+midname+"_train"+"."+self.img_type,img_train) 110 | cv2.imwrite(path_label+"/"+str(i)+"/"+midname+"_label"+"."+self.img_type,img_label) 111 | 112 | def splitTransform(self): 113 | # 拆分透视变换后的图像 114 | """ 115 | split perspective transform images 116 | """ 117 | #path_merge = "transform" 118 | #path_train = "transform/data/" 119 | #path_label = "transform/label/" 120 | 121 | path_merge = "deform/deform_norm2" 122 | path_train = "deform/train/" 123 | path_label = "deform/label/" 124 | 125 | train_imgs = glob.glob(path_merge+"/*."+self.img_type) 126 | for imgname in train_imgs: 127 | midname = imgname[imgname.rindex("/")+1:imgname.rindex("."+self.img_type)] 128 | img = cv2.imread(imgname) 129 | img_train = img[:,:,2]#cv2 read image rgb->bgr 130 | img_label = img[:,:,0] 131 | cv2.imwrite(path_train+midname+"."+self.img_type,img_train) 132 | cv2.imwrite(path_label+midname+"."+self.img_type,img_label) 133 | 134 | class dataProcess(object): 135 | def __init__(self, out_rows, out_cols, data_path = "../deform/train", label_path = "../deform/label", test_path = "../test", npy_path = "../npydata", img_type = "tif"): 136 | # 数据处理类,初始化 137 | self.out_rows = out_rows 138 | self.out_cols = out_cols 139 | self.data_path = data_path 140 | self.label_path = label_path 141 | self.img_type = img_type 142 | self.test_path = test_path 143 | self.npy_path = npy_path 144 | 145 | # 创建训练数据 146 | def create_train_data(self): 147 | i = 0 148 | print('-'*30) 149 | print('Creating training images...') 150 | print('-'*30) 151 | imgs = glob.glob(self.data_path+"/*."+self.img_type) 152 | print(len(imgs)) 153 | 154 | imgdatas = np.ndarray((len(imgs),self.out_rows,self.out_cols,1), dtype=np.uint8) 155 | imglabels = np.ndarray((len(imgs),self.out_rows,self.out_cols,1), dtype=np.uint8) 156 | for imgname in imgs: 157 | midname = imgname[imgname.rindex("/")+1:] 158 | img = load_img(self.data_path + "/" + midname,grayscale = True) 159 | label = load_img(self.label_path + "/" + midname,grayscale = True) 160 | img = img_to_array(img) 161 | label = img_to_array(label) 162 | #img = cv2.imread(self.data_path + "/" + midname,cv2.IMREAD_GRAYSCALE) 163 | #label = cv2.imread(self.label_path + "/" + midname,cv2.IMREAD_GRAYSCALE) 164 | #img = np.array([img]) 165 | #label = np.array([label]) 166 | imgdatas[i] = img 167 | imglabels[i] = label 168 | if i % 100 == 0: 169 | print('Done: {0}/{1} images'.format(i, len(imgs))) 170 | i += 1 171 | print('loading done') 172 | np.save(self.npy_path + '/imgs_train.npy', imgdatas) 173 | np.save(self.npy_path + '/imgs_mask_train.npy', imglabels) 174 | print('Saving to .npy files done.') 175 | 176 | # 创建测试数据 177 | def create_test_data(self): 178 | i = 0 179 | print('-'*30) 180 | print('Creating test images...') 181 | print('-'*30) 182 | imgs = glob.glob(self.test_path+"/*."+self.img_type) 183 | print(len(imgs)) 184 | imgdatas = np.ndarray((len(imgs),self.out_rows,self.out_cols,1), dtype=np.uint8) 185 | for imgname in imgs: 186 | midname = imgname[imgname.rindex("/")+1:] 187 | img = load_img(self.test_path + "/" + midname,grayscale = True) 188 | img = img_to_array(img) 189 | #img = cv2.imread(self.test_path + "/" + midname,cv2.IMREAD_GRAYSCALE) 190 | #img = np.array([img]) 191 | imgdatas[i] = img 192 | i += 1 193 | print('loading done') 194 | np.save(self.npy_path + '/imgs_test.npy', imgdatas) 195 | print('Saving to imgs_test.npy files done.') 196 | 197 | # 加载训练图片与mask 198 | def load_train_data(self): 199 | print('-'*30) 200 | print('load train images...') 201 | print('-'*30) 202 | imgs_train = np.load(self.npy_path+"/imgs_train.npy") 203 | imgs_mask_train = np.load(self.npy_path+"/imgs_mask_train.npy") 204 | imgs_train = imgs_train.astype('float32') 205 | imgs_mask_train = imgs_mask_train.astype('float32') 206 | imgs_train /= 255 207 | mean = imgs_train.mean(axis = 0) 208 | imgs_train -= mean 209 | imgs_mask_train /= 255 210 | # 做一个阈值处理,输出的概率值大于0.5的就认为是对象,否则认为是背景 211 | imgs_mask_train[imgs_mask_train > 0.5] = 1 212 | imgs_mask_train[imgs_mask_train <= 0.5] = 0 213 | return imgs_train,imgs_mask_train 214 | 215 | # 加载测试图片 216 | def load_test_data(self): 217 | print('-'*30) 218 | print('load test images...') 219 | print('-'*30) 220 | imgs_test = np.load(self.npy_path+"/imgs_test.npy") 221 | imgs_test = imgs_test.astype('float32') 222 | imgs_test /= 255 223 | mean = imgs_test.mean(axis = 0) 224 | imgs_test -= mean 225 | return imgs_test 226 | 227 | 228 | if __name__ == "__main__": 229 | # 以下注释掉的部分为数据增强代码,通过他们可以将数据进行增强 230 | 231 | #aug = myAugmentation() 232 | #aug.Augmentation() 233 | #aug.splitMerge() 234 | #aug.splitTransform() 235 | 236 | mydata = dataProcess(512,512) 237 | mydata.create_train_data() 238 | mydata.create_test_data() 239 | 240 | imgs_train,imgs_mask_train = mydata.load_train_data() 241 | print (imgs_train.shape,imgs_mask_train.shape) 242 | -------------------------------------------------------------------------------- /images/test/0.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/0.tif -------------------------------------------------------------------------------- /images/test/1.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/1.tif -------------------------------------------------------------------------------- /images/test/10.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/10.tif -------------------------------------------------------------------------------- /images/test/11.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/11.tif -------------------------------------------------------------------------------- /images/test/12.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/12.tif -------------------------------------------------------------------------------- /images/test/13.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/13.tif -------------------------------------------------------------------------------- /images/test/14.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/14.tif -------------------------------------------------------------------------------- /images/test/15.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/15.tif -------------------------------------------------------------------------------- /images/test/16.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/16.tif -------------------------------------------------------------------------------- /images/test/17.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/17.tif -------------------------------------------------------------------------------- /images/test/18.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/18.tif -------------------------------------------------------------------------------- /images/test/19.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/19.tif -------------------------------------------------------------------------------- /images/test/2.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/2.tif -------------------------------------------------------------------------------- /images/test/20.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/20.tif -------------------------------------------------------------------------------- /images/test/21.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/21.tif -------------------------------------------------------------------------------- /images/test/22.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/22.tif -------------------------------------------------------------------------------- /images/test/23.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/23.tif -------------------------------------------------------------------------------- /images/test/24.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/24.tif -------------------------------------------------------------------------------- /images/test/25.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/25.tif -------------------------------------------------------------------------------- /images/test/26.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/26.tif -------------------------------------------------------------------------------- /images/test/27.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/27.tif -------------------------------------------------------------------------------- /images/test/28.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/28.tif -------------------------------------------------------------------------------- /images/test/29.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/29.tif -------------------------------------------------------------------------------- /images/test/3.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/3.tif -------------------------------------------------------------------------------- /images/test/4.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/4.tif -------------------------------------------------------------------------------- /images/test/5.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/5.tif -------------------------------------------------------------------------------- /images/test/6.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/6.tif -------------------------------------------------------------------------------- /images/test/7.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/7.tif -------------------------------------------------------------------------------- /images/test/8.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/8.tif -------------------------------------------------------------------------------- /images/test/9.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/test/9.tif -------------------------------------------------------------------------------- /images/train/images/0.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/0.tif -------------------------------------------------------------------------------- /images/train/images/1.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/1.tif -------------------------------------------------------------------------------- /images/train/images/10.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/10.tif -------------------------------------------------------------------------------- /images/train/images/11.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/11.tif -------------------------------------------------------------------------------- /images/train/images/12.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/12.tif -------------------------------------------------------------------------------- /images/train/images/13.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/13.tif -------------------------------------------------------------------------------- /images/train/images/14.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/14.tif -------------------------------------------------------------------------------- /images/train/images/15.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/15.tif -------------------------------------------------------------------------------- /images/train/images/16.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/16.tif -------------------------------------------------------------------------------- /images/train/images/17.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/17.tif -------------------------------------------------------------------------------- /images/train/images/18.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/18.tif -------------------------------------------------------------------------------- /images/train/images/19.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/19.tif -------------------------------------------------------------------------------- /images/train/images/2.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/2.tif -------------------------------------------------------------------------------- /images/train/images/20.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/20.tif -------------------------------------------------------------------------------- /images/train/images/21.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/21.tif -------------------------------------------------------------------------------- /images/train/images/22.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/22.tif -------------------------------------------------------------------------------- /images/train/images/23.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/23.tif -------------------------------------------------------------------------------- /images/train/images/24.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/24.tif -------------------------------------------------------------------------------- /images/train/images/25.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/25.tif -------------------------------------------------------------------------------- /images/train/images/26.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/26.tif -------------------------------------------------------------------------------- /images/train/images/27.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/27.tif -------------------------------------------------------------------------------- /images/train/images/28.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/28.tif -------------------------------------------------------------------------------- /images/train/images/29.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/29.tif -------------------------------------------------------------------------------- /images/train/images/3.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/3.tif -------------------------------------------------------------------------------- /images/train/images/4.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/4.tif -------------------------------------------------------------------------------- /images/train/images/5.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/5.tif -------------------------------------------------------------------------------- /images/train/images/6.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/6.tif -------------------------------------------------------------------------------- /images/train/images/7.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/7.tif -------------------------------------------------------------------------------- /images/train/images/8.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/8.tif -------------------------------------------------------------------------------- /images/train/images/9.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/images/9.tif -------------------------------------------------------------------------------- /images/train/label/0.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/0.tif -------------------------------------------------------------------------------- /images/train/label/1.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/1.tif -------------------------------------------------------------------------------- /images/train/label/10.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/10.tif -------------------------------------------------------------------------------- /images/train/label/11.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/11.tif -------------------------------------------------------------------------------- /images/train/label/12.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/12.tif -------------------------------------------------------------------------------- /images/train/label/13.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/13.tif -------------------------------------------------------------------------------- /images/train/label/14.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/14.tif -------------------------------------------------------------------------------- /images/train/label/15.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/15.tif -------------------------------------------------------------------------------- /images/train/label/16.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/16.tif -------------------------------------------------------------------------------- /images/train/label/17.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/17.tif -------------------------------------------------------------------------------- /images/train/label/18.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/18.tif -------------------------------------------------------------------------------- /images/train/label/19.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/19.tif -------------------------------------------------------------------------------- /images/train/label/2.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/2.tif -------------------------------------------------------------------------------- /images/train/label/20.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/20.tif -------------------------------------------------------------------------------- /images/train/label/21.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/21.tif -------------------------------------------------------------------------------- /images/train/label/22.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/22.tif -------------------------------------------------------------------------------- /images/train/label/23.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/23.tif -------------------------------------------------------------------------------- /images/train/label/24.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/24.tif -------------------------------------------------------------------------------- /images/train/label/25.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/25.tif -------------------------------------------------------------------------------- /images/train/label/26.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/26.tif -------------------------------------------------------------------------------- /images/train/label/27.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/27.tif -------------------------------------------------------------------------------- /images/train/label/28.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/28.tif -------------------------------------------------------------------------------- /images/train/label/29.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/29.tif -------------------------------------------------------------------------------- /images/train/label/3.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/3.tif -------------------------------------------------------------------------------- /images/train/label/4.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/4.tif -------------------------------------------------------------------------------- /images/train/label/5.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/5.tif -------------------------------------------------------------------------------- /images/train/label/6.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/6.tif -------------------------------------------------------------------------------- /images/train/label/7.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/7.tif -------------------------------------------------------------------------------- /images/train/label/8.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/8.tif -------------------------------------------------------------------------------- /images/train/label/9.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decouples/Unet/74d3cc8ff1d85359528d31003e11eaa4233cfdbe/images/train/label/9.tif -------------------------------------------------------------------------------- /test_predict.py: -------------------------------------------------------------------------------- 1 | from unet import * 2 | from data import * 3 | 4 | # mydata = dataProcess(512,512) 5 | # imgs_test = mydata.load_test_data() 6 | # 7 | # myunet = myUnet() 8 | # model = myunet.get_unet() 9 | # model.load_weights('my_unet.hdf5') 10 | # imgs_mask_test = model.predict(imgs_test, verbose=1) 11 | # 12 | # np.save('imgs_mask_test.npy', imgs_mask_test) 13 | 14 | # --------------------------------------------------------------------- 15 | import matplotlib.pyplot as plt 16 | import numpy as np 17 | 18 | # imgs_test = np.load('../npydata/imgs_test.npy') 19 | # imgs_test_predict = np.load('../results/imgs_mask_test.npy') 20 | # print(imgs_test.shape, imgs_test_predict.shape) 21 | # 22 | # 23 | # n = 2 24 | # plt.figure(figsize=(20, 4)) 25 | # for i in range(20, 22): 26 | # plt.gray() 27 | # ax = plt.subplot(2, n, (i-20)+1) 28 | # plt.imshow(imgs_test[i].reshape(512, 512)) 29 | # ax.get_xaxis().set_visible(False) 30 | # ax.get_yaxis().set_visible(False) 31 | # 32 | # ax = plt.subplot(2, n, (i - 20) + n + 1) 33 | # plt.imshow(imgs_test_predict[i].reshape(512, 512)) 34 | # ax.get_xaxis().set_visible(False) 35 | # ax.get_yaxis().set_visible(False) 36 | # plt.show() 37 | 38 | print("array to image") 39 | imgs = np.load('../results/imgs_mask_test.npy') 40 | for i in range(imgs.shape[0]): 41 | img = imgs[i] 42 | img = array_to_img(img) 43 | img.save("../results/%d.jpg" % (i)) -------------------------------------------------------------------------------- /unet.py: -------------------------------------------------------------------------------- 1 | import os 2 | #os.environ["CUDA_VISIBLE_DEVICES"] = "0" 3 | import numpy as np 4 | from keras.models import * 5 | from keras.layers import Input, merge, Conv2D, MaxPooling2D, UpSampling2D, Dropout, Cropping2D 6 | from keras.optimizers import * 7 | from keras.callbacks import ModelCheckpoint, LearningRateScheduler 8 | from keras import backend as keras 9 | from data import * 10 | 11 | 12 | 13 | class myUnet(object): 14 | def __init__(self, img_rows = 512, img_cols = 512): 15 | self.img_rows = img_rows 16 | self.img_cols = img_cols 17 | # 参数初始化定义 18 | def load_data(self): 19 | mydata = dataProcess(self.img_rows, self.img_cols) 20 | imgs_train, imgs_mask_train = mydata.load_train_data() 21 | imgs_test = mydata.load_test_data() 22 | return imgs_train, imgs_mask_train, imgs_test 23 | # 载入数据 24 | def get_unet(self): 25 | inputs = Input((self.img_rows, self.img_cols,1)) 26 | # 网络结构定义 27 | ''' 28 | #unet with crop(because padding = valid) 29 | 30 | conv1 = Conv2D(64, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(inputs) 31 | print "conv1 shape:",conv1.shape 32 | conv1 = Conv2D(64, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv1) 33 | print "conv1 shape:",conv1.shape 34 | crop1 = Cropping2D(cropping=((90,90),(90,90)))(conv1) 35 | print "crop1 shape:",crop1.shape 36 | pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) 37 | print "pool1 shape:",pool1.shape 38 | 39 | conv2 = Conv2D(128, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(pool1) 40 | print "conv2 shape:",conv2.shape 41 | conv2 = Conv2D(128, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv2) 42 | print "conv2 shape:",conv2.shape 43 | crop2 = Cropping2D(cropping=((41,41),(41,41)))(conv2) 44 | print "crop2 shape:",crop2.shape 45 | pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) 46 | print "pool2 shape:",pool2.shape 47 | 48 | conv3 = Conv2D(256, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(pool2) 49 | print "conv3 shape:",conv3.shape 50 | conv3 = Conv2D(256, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv3) 51 | print "conv3 shape:",conv3.shape 52 | crop3 = Cropping2D(cropping=((16,17),(16,17)))(conv3) 53 | print "crop3 shape:",crop3.shape 54 | pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) 55 | print "pool3 shape:",pool3.shape 56 | 57 | conv4 = Conv2D(512, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(pool3) 58 | conv4 = Conv2D(512, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv4) 59 | drop4 = Dropout(0.5)(conv4) 60 | crop4 = Cropping2D(cropping=((4,4),(4,4)))(drop4) 61 | pool4 = MaxPooling2D(pool_size=(2, 2))(drop4) 62 | 63 | conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(pool4) 64 | conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv5) 65 | drop5 = Dropout(0.5)(conv5) 66 | 67 | up6 = Conv2D(512, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(drop5)) 68 | merge6 = merge([crop4,up6], mode = 'concat', concat_axis = 3) 69 | conv6 = Conv2D(512, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(merge6) 70 | conv6 = Conv2D(512, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv6) 71 | 72 | up7 = Conv2D(256, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv6)) 73 | merge7 = merge([crop3,up7], mode = 'concat', concat_axis = 3) 74 | conv7 = Conv2D(256, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(merge7) 75 | conv7 = Conv2D(256, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv7) 76 | 77 | up8 = Conv2D(128, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv7)) 78 | merge8 = merge([crop2,up8], mode = 'concat', concat_axis = 3) 79 | conv8 = Conv2D(128, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(merge8) 80 | conv8 = Conv2D(128, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv8) 81 | 82 | up9 = Conv2D(64, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv8)) 83 | merge9 = merge([crop1,up9], mode = 'concat', concat_axis = 3) 84 | conv9 = Conv2D(64, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(merge9) 85 | conv9 = Conv2D(64, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv9) 86 | conv9 = Conv2D(2, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv9) 87 | ''' 88 | 89 | conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(inputs) 90 | print ("conv1 shape:",conv1.shape) 91 | conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv1) 92 | print ("conv1 shape:",conv1.shape) 93 | pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) 94 | print ("pool1 shape:",pool1.shape) 95 | 96 | conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool1) 97 | print ("conv2 shape:",conv2.shape) 98 | conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv2) 99 | print ("conv2 shape:",conv2.shape) 100 | pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) 101 | print ("pool2 shape:",pool2.shape) 102 | 103 | conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool2) 104 | print ("conv3 shape:",conv3.shape) 105 | conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv3) 106 | print ("conv3 shape:",conv3.shape) 107 | pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) 108 | print ("pool3 shape:",pool3.shape) 109 | 110 | conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool3) 111 | conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv4) 112 | drop4 = Dropout(0.5)(conv4) 113 | pool4 = MaxPooling2D(pool_size=(2, 2))(drop4) 114 | 115 | conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool4) 116 | conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv5) 117 | drop5 = Dropout(0.5)(conv5) 118 | 119 | up6 = Conv2D(512, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(drop5)) 120 | merge6 = merge([drop4,up6], mode = 'concat', concat_axis = 3) 121 | conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge6) 122 | conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv6) 123 | 124 | up7 = Conv2D(256, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv6)) 125 | merge7 = merge([conv3,up7], mode = 'concat', concat_axis = 3) 126 | conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge7) 127 | conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv7) 128 | 129 | up8 = Conv2D(128, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv7)) 130 | merge8 = merge([conv2,up8], mode = 'concat', concat_axis = 3) 131 | conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge8) 132 | conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv8) 133 | 134 | up9 = Conv2D(64, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv8)) 135 | merge9 = merge([conv1,up9], mode = 'concat', concat_axis = 3) 136 | conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge9) 137 | conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9) 138 | conv9 = Conv2D(2, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9) 139 | conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9) 140 | 141 | model = Model(input = inputs, output = conv10) 142 | model.compile(optimizer = Adam(lr = 1e-4), loss = 'binary_crossentropy', metrics = ['accuracy']) 143 | return model 144 | 145 | # 如果需要修改输入的格式,那么可以从以下开始修改,上面的结构部分不需要修改 146 | def train(self): 147 | print("loading data") 148 | imgs_train, imgs_mask_train, imgs_test = self.load_data() 149 | print("loading data done") 150 | model = self.get_unet() 151 | print("got unet") 152 | model_checkpoint = ModelCheckpoint('my_unet.hdf5', monitor='loss',verbose=1, save_best_only=True) 153 | print('Fitting model...') 154 | model.fit(imgs_train, imgs_mask_train, batch_size=2, nb_epoch=10, verbose=1,validation_split=0.2, shuffle=True, callbacks=[model_checkpoint]) 155 | 156 | print('predict test data') 157 | imgs_mask_test = model.predict(imgs_test, batch_size=1, verbose=1) 158 | np.save('../results/imgs_mask_test.npy', imgs_mask_test) 159 | 160 | def save_img(self): 161 | print("array to image") 162 | imgs = np.load('../results/imgs_mask_test.npy') 163 | for i in range(imgs.shape[0]): 164 | img = imgs[i] 165 | img = array_to_img(img) 166 | img.save("../results/%d.jpg"%(i)) 167 | 168 | if __name__ == '__main__': 169 | myunet = myUnet() 170 | myunet.train() 171 | myunet.save_img() 172 | --------------------------------------------------------------------------------