def predict(self): predList = glob.glob(self.config.test_img_path + "*." + self.config.test_datatype) for path in predList: orgImg = plt.imread(path) print("[Info] 解析文件名...", self.analyze_name(path)) height, width = orgImg.shape[:2] patches_pred, new_height, new_width, adjustImg = get_test_patches( orgImg, self.config) predictions = self.model.predict(patches_pred, batch_size=32, verbose=1) pred_patches = pred_to_patches(predictions, self.config) pred_imgs = recompone_overlap(pred_patches, self.config, new_height, new_width) pred_imgs = pred_imgs[:, 0:height, 0:width, :] adjustImg = adjustImg[0, 0:height, 0:width, :] print(adjustImg.shape) probResult = pred_imgs[0, :, :, 0] binaryResult = gray2binary(probResult) resultMerge = visualize([adjustImg, binaryResult], [1, 2]) resultMerge = cv2.cvtColor(resultMerge, cv2.COLOR_RGB2BGR) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + "_merge.jpg", resultMerge) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + "_prob.jpg", (probResult * 255).astype(np.uint8))
def predict(self): predList = glob.glob(self.config.test_img_path + "*." + self.config.test_datatype) print(len(predList)) for path in predList: orgImg_temp = plt.imread(path) #orgImg_temp=cv2.resize(orgImg_temp,(1727,1168)) # orgImg=orgImg_temp[:,:,1] thresh = 0.6 print('G_ratio:{} R_ratio:{}'.format(thresh, (1 - thresh))) orgImg = orgImg_temp[:, :, 1] * thresh + orgImg_temp[:, :, 0] * ( 1 - thresh) print("[Info] Analyze filename...", self.analyze_name(path)) height, width = orgImg.shape[:2] orgImg = np.reshape(orgImg, (height, width, 1)) patches_pred, new_height, new_width, adjustImg = get_test_patches( orgImg, self.config) predictions = self.model.predict(patches_pred, batch_size=32, verbose=1) # print(predictions.shape) pred_patches = pred_to_patches(predictions, self.config) pred_imgs = recompone_overlap(pred_patches, self.config, new_height, new_width) # print(pred_imgs.shape) gc.collect() pred_imgs = pred_imgs[:, 0:height, 0:width, :] new_construct = np.zeros( (pred_imgs.shape[1], pred_imgs.shape[2], 3)) for i in range(pred_imgs.shape[1]): for j in range(pred_imgs.shape[2]): # print(pred_imgs[0][i][j]) index = np.argmax(pred_imgs[0][i][j]) # print(index) new_construct[i][j] = color_list[index] # print(pred_imgs.shape) adjustImg = adjustImg[0, 0:height, 0:width, :] # print(adjustImg.shape) probResult = pred_imgs[0, :, :, 0] + pred_imgs[0, :, :, 2] + pred_imgs[0, :, :, 3] binaryResult = gray2binary(probResult, 0.35) resultMerge = visualize([adjustImg, binaryResult], [1, 2]) resultMerge = cv2.cvtColor(resultMerge, cv2.COLOR_RGB2BGR) #cv2.imwrite(self.config.test_result_path+self.analyze_name(path)+"_merge.jpg",resultMerge) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + "_prob.tif", ((1 - probResult) * 255).astype(np.uint8)) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + "_color_mask.tif", (new_construct).astype(np.uint8))
def predict(self): predList = glob.glob(self.config.test_img_path + "*." + self.config.test_datatype) # probList=glob.glob(self.config.test_img_path+"*."+self.config.test_datatype) # print(len(predList)) for path in predList: orgImg_temp = plt.imread(path) prob_path = self.config.test_prob_path + path.split( '/')[1].replace('test', 'test_prob') probImg = plt.imread(prob_path, 0) #orgImg_temp=cv2.resize(orgImg_temp,(1727,1168)) # orgImg=orgImg_temp[:,:,1] thresh = 1 print('G_ratio:{} R_ratio:{}'.format(thresh, (1 - thresh))) orgImg = orgImg_temp[:, :, 1] * thresh + orgImg_temp[:, :, 0] * ( 1 - thresh) print("[Info] Analyze filename...", self.analyze_name(path)) height, width = orgImg.shape[:2] orgImg = np.reshape(orgImg, (height, width, 1)) probImg = np.reshape(probImg, (height, width, 1)) patches_pred, new_height, new_width, adjustImg = get_test_patches1( orgImg, probImg, self.config) predictions = self.model.predict(patches_pred, batch_size=32, verbose=1) pred_patches = pred_to_patches(predictions, self.config) pred_imgs = recompone_overlap(pred_patches, self.config, new_height, new_width) gc.collect() pred_imgs = pred_imgs[:, 0:height, 0:width, :] adjustImg = adjustImg[0, 0:height, 0:width, :] print(adjustImg.shape) probResult = pred_imgs[0, :, :, 0] binaryResult = gray2binary(probResult, 0.35) resultMerge = visualize([adjustImg, binaryResult], [1, 2]) resultMerge = cv2.cvtColor(resultMerge, cv2.COLOR_RGB2BGR) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + "_merge.jpg", resultMerge) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + "_prob.bmp", (probResult * 255).astype(np.uint8)) cv2.imwrite( self.config.test_result_path + self.analyze_name(path).replace('training', 'manual1') + ".tif", (binaryResult * 255).astype(np.uint8))
def predict(self): predList = glob.glob(self.config.test_img_path + "*." + self.config.test_datatype) print(len(predList)) for index, path in enumerate(predList): orgImg_temp = plt.imread(predList[index]) # orgImg=orgImg_temp[:,:,1] thresh = 0.8 print('G_ratio:{} R_ratio:{}'.format(thresh, (1 - thresh))) orgImg = orgImg_temp[:, :, 1] * thresh + orgImg_temp[:, :, 0] * ( 1 - thresh) print("[Info] Analyze filename...", self.analyze_name(path)) height, width = orgImg.shape[:2] orgImg = np.reshape(orgImg, (height, width, 1)) patches_pred, new_height, new_width, adjustImg = get_test_patches( orgImg, self.config) # self.model.load_weights('./metric/STARE/weights/STARE_1_weight/keras_{}.model'.format(index)) self.model.load_weights( './metric/STARE/Unetweights/0.9/keras_{}.model'.format(index)) print("[Info] loading weight....{}".format(index)) predictions = self.model.predict(patches_pred, batch_size=32, verbose=1) pred_patches = pred_to_patches(predictions, self.config) pred_imgs = recompone_overlap(pred_patches, self.config, new_height, new_width) gc.collect() pred_imgs = pred_imgs[:, 0:height, 0:width, :] adjustImg = adjustImg[0, 0:height, 0:width, :] print(adjustImg.shape) probResult = pred_imgs[0, :, :, 0] binaryResult = gray2binary(probResult) resultMerge = visualize([adjustImg, binaryResult], [1, 2]) resultMerge = cv2.cvtColor(resultMerge, cv2.COLOR_RGB2BGR) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + "_merge.jpg", resultMerge) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + "_prob.bmp", (probResult * 255).astype(np.uint8)) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + ".tif", (binaryResult * 255).astype(np.uint8)) np.save( self.config.test_result_path + self.analyze_name(path) + ".npy", binaryResult)
def predict(self): predList = glob.glob(self.config.test_img_path + "*." + self.config.test_datatype) for path in predList: print(path) baseName = os.path.basename(path) orgImg_temp = cv2.imread(path)[..., 0] #orgImg=orgImg_temp[:,:,1]*0.75+orgImg_temp[:,:,0]*0.25 orgImg = orgImg_temp print("[Info] Analyze filename...", self.analyze_name(path)) height, width = orgImg.shape[:2] orgImg = np.reshape(orgImg, (height, width, 1)) patches_pred, new_height, new_width, adjustImg = get_test_patches( orgImg, self.config) predictions = self.model.predict(patches_pred, batch_size=32, verbose=1) pred_patches = pred_to_patches(predictions, self.config) pred_imgs = recompone_overlap(pred_patches, self.config, new_height, new_width) pred_imgs = pred_imgs[:, 0:height, 0:width, :] adjustImg = adjustImg[0, 0:height, 0:width, :] print(adjustImg.shape) probResult = pred_imgs[0, :, :, 0] binaryResult = gray2binary(probResult) resultMerge = visualize([adjustImg, binaryResult], [1, 2]) resultMerge = cv2.cvtColor(resultMerge, cv2.COLOR_RGB2BGR) test_path = os.path.join(self.config.test_result_path, self.analyze_name(path), self.config.exp_name, self.config.preprocess, self.config.dataset, 'result') #test_label = os.path.join(self.config.test_result_path, self.analyze_name(path), self.config.exp_name, self.config.preprocess, self.config.dataset, 'labels') mkdir_if_not_exist(test_path) #mkdir_if_not_exist(test_label) #cv2.imwrite(self.config.test_result_path+self.analyze_name(path)+"test.png", orgImg) #cv2.imwrite(self.config.test_result_path+self.analyze_name(path)+"_merge.png",resultMerge) cv2.imwrite(os.path.join(test_path, baseName), (probResult * 255).astype(np.uint8))
def predict(self): predList = glob.glob(self.config.test_img_path + "*." + self.config.test_datatype) for path in predList: orgImg_temp = plt.imread(path) if orgImg_temp.shape[0] > 1000 or orgImg_temp.shape[1] > 1000: orgImg_temp = imgResize(orgImg_temp, 0.5) orgImg = orgImg_temp[:, :, 1] * 0.75 + orgImg_temp[:, :, 0] * 0.25 print("[Info] Analyze filename...", self.analyze_name(path)) height, width = orgImg.shape[:2] orgImg = np.reshape(orgImg, (height, width, 1)) patches_pred, new_height, new_width, adjustImg = get_test_patches( orgImg, self.config) predictions = self.model.predict(patches_pred, batch_size=32, verbose=1) pred_patches = pred_to_patches(predictions, self.config) pred_imgs = recompone_overlap(pred_patches, self.config, new_height, new_width) pred_imgs = pred_imgs[:, 0:height, 0:width, :] probResult = pred_imgs[0, :, :, 0] binaryResult = gray2binary(probResult, threshold=0.5) binaryResult, probResult = postprocess( binaryResult, probResult) # post process to get the optic disc _, contours, _ = cv2.findContours(binaryResult, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) adjustImg = cv2.drawContours(orgImg_temp, contours, -1, (0, 255, 0), 2) resultMerge = visualize([adjustImg / 255., binaryResult], [1, 2]) resultMerge = cv2.cvtColor(resultMerge, cv2.COLOR_RGB2BGR) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + "_merge.jpg", resultMerge) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + "_prob.bmp", (probResult * 255).astype(np.uint8))
def predict(self): predList = glob.glob(self.config.test_img_path + "*." + self.config.test_datatype) print(predList) for path in predList: orgImg_temp = plt.imread(path) # img = Image.open(path) # img = img.resize((370, 250)) # orgImg_temp = np.asarray(img) orgImg = orgImg_temp[:, :, 1] * 0.75 + orgImg_temp[:, :, 0] * 0.25 print("[Info] Analyze filename...", self.analyze_name(path)) height, width = orgImg.shape[:2] orgImg = np.reshape(orgImg, (height, width, 1)) patches_pred, new_height, new_width, adjustImg = get_test_patches( orgImg, self.config) predictions = self.model.predict(patches_pred, batch_size=32, verbose=1) pred_patches = pred_to_patches(predictions, self.config) pred_imgs = recompone_overlap(pred_patches, self.config, new_height, new_width) pred_imgs = pred_imgs[:, 0:height, 0:width, :] adjustImg = adjustImg[0, 0:height, 0:width, :] print(adjustImg.shape) probResult = pred_imgs[0, :, :, 0] binaryResult = gray2binary(probResult) resultMerge = visualize([adjustImg, binaryResult], [1, 2]) resultMerge = cv2.cvtColor(resultMerge, cv2.COLOR_RGB2BGR) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + "_merge.jpg", resultMerge) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + "_prob.bmp", (probResult * 255).astype(np.uint8))
def predict(self): predList = glob.glob(self.config.test_img_path + "*." + self.config.test_datatype) print(len(predList)) for path in predList: orgImg_temp = plt.imread(path) #orgImg_temp=cv2.resize(orgImg_temp,(1727,1168)) # orgImg=orgImg_temp[:,:,1] if self.color_space == 'RGB': print('Color Space: RGB') if self.multi_proportion: print('G_ratio:{} R_ratio:{} B_ratio:{}'.format( self.G, self.R, self.B)) orgImg = orgImg_temp[:, :, 1] * self.G + orgImg_temp[:, :, 0] * self.R + orgImg_temp[:, :, 2] * self.B else: orgImg = np.asarray(orgImg_temp) elif self.color_space == 'HSV': print('Color Space: HSV') orgImg = cv2.cvtColor(orgImg_temp, cv2.COLOR_RGB2HSV) elif self.color_space == 'LAB': print('Color Space: LAB') orgImg = cv2.cvtColor(orgImg_temp, cv2.COLOR_RGB2LAB) # print('G_ratio:{} R_ratio:{} B_ratio:{}'.format(self.G,self.R,self.B)) # orgImg=orgImg_temp[:,:,1]*self.G+orgImg_temp[:,:,0]*self.R+orgImg_temp[:,:,2]*self.B print("[Info] Analyze filename...", self.analyze_name(path)) height, width = orgImg.shape[:2] if self.multi_proportion: orgImg = np.reshape(orgImg, (height, width, 1)) else: orgImg = np.reshape(orgImg, (height, width, 3)) patches_pred, new_height, new_width, adjustImg = get_test_patches( orgImg, self.config) predictions = self.model.predict(patches_pred, batch_size=32, verbose=1) #pred_patches=pred_to_patches(predictions,self.config) # predictions = self.model.predict(patches_pred, batch_size=32, verbose=1) # pred_patches=pred_to_patches(predictions,self.config) pred_imgs = recompone_overlap(predictions, self.config, new_height, new_width) # print(pred.shape) gc.collect() pred_imgs = pred_imgs[:, 0:height, 0:width, :] adjustImg = adjustImg[0, 0:height, 0:width, :] # print(adjustImg.shape) # new_ probResult = pred_imgs[0, :, :, 0] binaryResult = gray2binary( probResult, 0.35 ) #cv2.imwrite(self.config.test_result_path+self.analyze_name(path)+"_merge.jpg",resultMerge) cv2.imwrite( self.config.test_result_path + self.analyze_name(path) + "_prob.tif", (probResult * 255).astype(np.uint8))