def predict_test(): Vnet2d = Vnet2dModule(512, 512, channels=1, costname="dice coefficient", inference=True, model_path="log\segmeation\\vnet2d\model\\Vnet2d.pd") csvdata = pd.read_csv( r'D:\cjq\project\python\TNSCUI2020\dataprocess\segmentationtestdata.csv' ) traindata = csvdata.iloc[:, :].values imagedata = traindata[:, 0] maskdata = traindata[:, 1] iou_values = [] for i in range(len(imagedata)): src_image = cv2.imread(imagedata[i], cv2.IMREAD_GRAYSCALE) mask_image = cv2.imread(maskdata[i], cv2.IMREAD_GRAYSCALE) resize_image = cv2.resize(src_image, (512, 512)) pd_mask_image = Vnet2d.prediction(resize_image / 255.) pd_mask_image[pd_mask_image >= 128] = 255 pd_mask_image[pd_mask_image < 128] = 0 new_mask_image = cv2.resize(pd_mask_image, (mask_image.shape[1], mask_image.shape[0])) cv2.imwrite( "E:\MedicalData\TNSCUI2020\TNSCUI2020_train\\" + str(i) + "image.bmp", src_image) cv2.imwrite( "E:\MedicalData\TNSCUI2020\TNSCUI2020_train\\" + str(i) + "mask.bmp", mask_image) cv2.imwrite( "E:\MedicalData\TNSCUI2020\TNSCUI2020_train\\" + str(i) + "pdmask.bmp", new_mask_image) iou_value = calcu_iou(mask_image, new_mask_image, 255) iou_values.append(iou_value) print("mean iou:", np.mean(iou_values))
def predict_testzch(): Vnet2d = Vnet2dModule( 512, 512, channels=1, costname="dice coefficient", inference=True, model_path="log/segmeation/vnet2d_author/model/Vnet2d.pd") test_image_path = os.path.abspath('../..') + r"\1_or_data\image" test_gt_path = os.path.abspath('../..') + r"\1_or_data\mask" test_mask_path = os.path.abspath('../..') + r"\Vnet\test\mask" id_list = 'dataprocess/train_valid_test_id.xlsx' data_xlsx = xlrd.open_workbook(id_list) # 获取test集合 table_test = data_xlsx.sheet_by_name('test') fold_id_test = [ table_test.cell_value(i, 0) + '.bmp' for i in range(1, table_test.nrows) ] print(fold_id_test) print(len(fold_id_test)) # counter = 0 all_iou = [] for imagefile in fold_id_test: imagefilepath = os.path.join(test_image_path, imagefile) src_image = cv2.imread(imagefilepath, cv2.IMREAD_GRAYSCALE) gtfilepath = os.path.join(test_gt_path, imagefile) gt_image = cv2.imread(gtfilepath, cv2.IMREAD_GRAYSCALE) resize_image = cv2.resize(src_image, (512, 512)) pd_mask_image = Vnet2d.prediction(resize_image / 255.) new_mask_image = cv2.resize(pd_mask_image, (src_image.shape[1], src_image.shape[0])) # 计算IOU,先将GT和预测图像化为二值图 gt_image_iou = (gt_image / 255.).astype(np.float) new_mask_image_iou = ((new_mask_image / 255.) > 0.8).astype(np.float) IOU = getIOU(gt_image_iou, new_mask_image_iou) print(IOU) all_iou.append(IOU) if IOU < 0.7: print(imagefile) # counter += 1 # if counter == 50: # print('我该跳出了') # break maskfilepath = os.path.join(test_mask_path, imagefile) cv2.imwrite(maskfilepath, new_mask_image) mean_iou = np.mean(all_iou) print('Mean_iou:', mean_iou)
def train(): ''' Preprocessing for dataset ''' # Read data set (Train data from CSV file) csvdata = pd.read_csv('dataprocess\segandclassifydata.csv') trainData = csvdata.iloc[:, :].values np.random.shuffle(trainData) labeldata = trainData[:, 0] imagedata = trainData[:, 1] maskdata = trainData[:, 2] Vnet2d = Vnet2dModule(512, 512, channels=1, costname="dice coefficient") Vnet2d.train(imagedata, maskdata, "Vnet2d.pd", "log\\segmeation\\vnet2d\\", 0.001, 0.5, 10, 6)
def train(): ''' Preprocessing for dataset ''' # Read data set (Train data from CSV file) csvmaskdata = pd.read_csv('PROMISE2012Mask.csv') csvimagedata = pd.read_csv('PROMISE2012Image.csv') maskdata = csvmaskdata.iloc[:, :].values imagedata = csvimagedata.iloc[:, :].values # shuffle imagedata and maskdata together perm = np.arange(len(csvimagedata)) np.random.shuffle(perm) imagedata = imagedata[perm] maskdata = maskdata[perm] unet2d = Vnet2dModule(512, 512, channels=1, costname="dice coefficient") unet2d.train(imagedata, maskdata, "model\\Vnet2dModule.pd", "log\\", 0.001, 0.5, 100000, 4)
def train(): ''' Preprocessing for dataset ''' # Read data set (Train data from CSV file) csvdata = pd.read_csv(r'D:\cjq\project\python\TNSCUI2020\dataprocess\segmentationtraindata.csv') csvdataaug = pd.read_csv(r'D:\cjq\project\python\TNSCUI2020\dataprocess\segmeatationaugtraindata.csv') traindata = csvdata.iloc[:, :].values augtraindata = csvdataaug.iloc[:, :].values trainData = np.concatenate((traindata, augtraindata), axis=0) # shuffle imagedata and maskdata together np.random.shuffle(trainData) imagedata = trainData[:, 0] maskdata = trainData[:, 1] Vnet2d = Vnet2dModule(512, 512, channels=1, costname="dice coefficient") Vnet2d.train(imagedata, maskdata, "Vnet2d.pd", "log\\segmeation\\vnet2d\\", 0.001, 0.5, 5, 6)
def predict_validation(): ''' Preprocessing for dataset ''' Vnet2d = Vnet2dModule(512, 768, channels=1, costname="dice coefficient", inference=True, model_path="log\segmeation\model\\Vnet2d.pd") path = "C:\HC18\\training_set\process\\test\src\\" for i in range(990, 999, 1): src_image = cv2.imread(path + str(i) + ".bmp", cv2.IMREAD_GRAYSCALE) resize_image = cv2.resize(src_image, (768, 512)) mask_image = Vnet2d.prediction(resize_image) new_mask_image = cv2.resize(mask_image, (src_image.shape[1], src_image.shape[0])) cv2.imwrite(path + str(i) + "mask.bmp", new_mask_image)
def predict_test(): Vnet2d = Vnet2dModule(512, 512, channels=1, costname="dice coefficient", inference=True, model_path="log\segmeation\\vnet2d\model\Vnet2d.pd") test_image_path = r"E:\MedicalData\TNSCUI2020\TNSCUI2020_test\image" test_mask_path = r"E:\MedicalData\TNSCUI2020\TNSCUI2020_test\vnet_mask" allimagefiles = os.listdir(test_image_path) for imagefile in allimagefiles: imagefilepath = os.path.join(test_image_path, imagefile) src_image = cv2.imread(imagefilepath, cv2.IMREAD_GRAYSCALE) resize_image = cv2.resize(src_image, (512, 512)) pd_mask_image = Vnet2d.prediction(resize_image / 255.) new_mask_image = cv2.resize(pd_mask_image, (src_image.shape[1], src_image.shape[0])) maskfilepath = os.path.join(test_mask_path, imagefile) cv2.imwrite(maskfilepath, new_mask_image)
def predict_test(): Vnet2d = Vnet2dModule(512, 768, channels=1, costname="dice coefficient", inference=True, model_path="log\segmeation\model\\Vnet2d.pd") test_set_csv = pd.read_csv("C:\HC18\\test_set\\test_set_pixel_size.csv") src_test_set = "C:\HC18\\test_set\src\\" mask_test_set = "C:\HC18\\test_set\mask\\" imagedata = test_set_csv.iloc[:, 0].values for i in range(len(imagedata)): src_image = cv2.imread(src_test_set + imagedata[i], cv2.IMREAD_GRAYSCALE) resize_image = cv2.resize(src_image, (768, 512)) mask_image = Vnet2d.prediction(resize_image) new_mask_image = cv2.resize(mask_image, (src_image.shape[1], src_image.shape[0])) cv2.imwrite(mask_test_set + imagedata[i], new_mask_image)
def predict_test(): Vnet2d = Vnet2dModule( 512, 512, channels=1, costname="dice coefficient", inference=True, model_path="log/segmeation/vnet2d6000lr-3-balance/model/Vnet2d.pd") test_image_path = os.path.abspath('../..') + r"\Vnet\test\image" test_gt_path = os.path.abspath('../..') + r"\1_or_data\mask" test_mask_path = os.path.abspath('../..') + r"\Vnet\test\mask" print(test_gt_path) allimagefiles = os.listdir(test_image_path) # counter = 0 all_iou = [] for imagefile in allimagefiles: imagefilepath = os.path.join(test_image_path, imagefile) src_image = cv2.imread(imagefilepath, cv2.IMREAD_GRAYSCALE) gtfilepath = os.path.join(test_gt_path, imagefile) gt_image = cv2.imread(gtfilepath, cv2.IMREAD_GRAYSCALE) resize_image = cv2.resize(src_image, (512, 512)) pd_mask_image = Vnet2d.prediction(resize_image / 255.) new_mask_image = cv2.resize(pd_mask_image, (src_image.shape[1], src_image.shape[0])) # 计算IOU,先将GT和预测图像化为二值图 gt_image_iou = (gt_image / 255.).astype(np.float) new_mask_image_iou = ((new_mask_image / 255.) > 0.8).astype(np.float) IOU = getIOU(gt_image_iou, new_mask_image_iou) print(IOU) all_iou.append(IOU) if IOU < 0.7: print(imagefile) # counter += 1 # if counter == 50: # print('我该跳出了') # break maskfilepath = os.path.join(test_mask_path, imagefile) cv2.imwrite(maskfilepath, new_mask_image) mean_iou = np.mean(all_iou) print('Mean_iou:', mean_iou)
def train(): ''' Preprocessing for dataset ''' # Read data set (Train data from CSV file) # csvdata = pd.read_csv('dataprocess\segandclassifydata_zch.csv') # trainData = csvdata.iloc[:, :].values # print(type(trainData)) # np.random.shuffle(trainData) # print(type(trainData)) # labeldata = trainData[:, 0] # imagedata = trainData[:, 1] # maskdata = trainData[:, 2] # print(imagedata) # print(maskdata) # 读取测试集 img_path = 'G:/graduation project/TN-SCUI2020/my_code/1_or_data/image' mask_path = 'G:/graduation project/TN-SCUI2020/my_code/1_or_data/mask' id_list = 'dataprocess/train_valid_test_id.xlsx' data_xlsx = xlrd.open_workbook(id_list) # 获取train集合 table_train = data_xlsx.sheet_by_name('train') fold_id_train = [table_train.cell_value(i, 0) for i in range(1, table_train.nrows)] train_img_list = get_filelist_frompath_tnscui(img_path, 'bmp', sample_id=fold_id_train) train_img_list = np.array(train_img_list) np.random.shuffle(train_img_list) train_mask_list = np.array([mask_path + sep + i.split(sep)[-1] for i in train_img_list]) # 获取vlaid集合 table_valid = data_xlsx.sheet_by_name('valid') fold_id_valid = [table_train.cell_value(i, 0) for i in range(1, table_valid.nrows)] valid_img_list = get_filelist_frompath_tnscui(img_path, 'bmp', sample_id=fold_id_valid) valid_img_list = np.array(valid_img_list) np.random.shuffle(valid_img_list) valid_mask_list = np.array([mask_path + sep + i.split(sep)[-1] for i in valid_img_list]) # 拼接train和valid train_img_list = np.hstack((train_img_list, valid_img_list)) train_mask_list = np.hstack((train_mask_list, valid_mask_list)) Vnet2d = Vnet2dModule(512, 512, channels=1, costname="dice coefficient") Vnet2d.train(train_img_list, train_mask_list, "Vnet2d.pd", "log\\segmeation\\vnet2dtest\\", 0.001, 0.5, 10, 3, restore=False, step=6000)
def train(): ''' Preprocessing for dataset ''' # Read data set (Train data from CSV file) csvmaskdata = pd.read_csv('PROMISE2012Mask.csv') csvimagedata = pd.read_csv('PROMISE2012Image.csv') maskdata = csvmaskdata.iloc[:, :].values imagedata = csvimagedata.iloc[:, :].values # shuffle imagedata and maskdata together perm = np.arange(len(csvimagedata)) np.random.shuffle(perm) imagedata = imagedata[perm] maskdata = maskdata[perm] unet2d = Vnet2dModule(512, 512, channels=1, costname="dice coefficient") unet2d.train(imagedata, maskdata, "model\\Vnet2dModule.pd", "log\\", 0.001, 0.5, 100000, 4) def predict(): image = cv2.imread("1.bmp",0) unet2d = Vnet2dModule(512, 512, 1) predictvalue = unet2d.prediction("model\\Vnet2dModule.pd", image) cv2.imwrite("mask.bmp", predictvalue) train() # predict()