def KLSLoc(label, classHeatMap, paras): paras['specialType'] = label paras['thresh'] = calculateThreshold(imgFile) maps3, common_labels, F0 = region_special_map(paras) kls, categoryMap, classMap = mergePatchAndRegion(classHeatMap, maps3, label, 0.5) return kls
nums = np.zeros((type_num, )) result_file = '../../Data/Results/segmentation/RD/rd.txt' f = open(result_file, 'w') for t in range(0, type_num): mask_folder_t = data_folder + str(t + 1) + '_mask/' mask_names = os.listdir(mask_folder_t) img_folder_t = data_folder + str(t + 1) + '_selected/' for i in range(len(mask_names)): mask_name = mask_names[i] mask_path = mask_folder_t + mask_name im_name = mask_name[:-4] label_mask = load_mask_mat(mask_path) img_path = img_folder_t + im_name + '.jpg' th = calculateThreshold(img_path) detect_region, F0, remove_labels = detect_regions( img_path, eraseMap, k, minSize, sigma, th) # predict_heatmap = imresize(predict_heatmap, [imSize, imSize]).astype(np.float) / 255.0 # predict_heatmap_th = np.copy(predict_heatmap) # predict_heatmap_th[np.where(predict_heatmap_th < 0.5)] = 0 # predict_segment_labels = set(F0[np.where(predict_heatmap_th>0)]) # predict_mask = np.zeros(F0.shape).astype(np.uint8) # for sl in predict_segment_labels: # if sl not in remove_labels: # predict_mask[np.where(F0==sl)] = 1 # cls_confusion[t, predict_label-1] += 1 intersectionPixelNum = len( np.argwhere((detect_region * label_mask) > 0)) unionPixelNum = len(np.argwhere((detect_region + label_mask) > 0))
paras['returnRegionLabels'] = [0] # 0: special, 1: rest, 2: common paras['train'] = False is_showProposals = paras['is_showProposals'] = True resultsSaveFolder = '../../Data/Results/segmentation/' imName = imgFile[-20:-4] rotation = False # ----no rotation---- if rotation == False: classHeatMap = region_class_heatMap(paras) class_names = ['background', 'arc', 'drapery', 'radial', 'hot-spot'] labels = mapsToLabels(classHeatMap) # visRegionClassHeatMap(classHeatMap, class_names) paras['specialType'] = labels # 0: arc, 1: drapery, 2: radial, 3: hot-spot paras['thresh'] = calculateThreshold(imgFile) print calculateThreshold(imgFile) maps3, common_labels, F0 = region_special_map(paras, isReturnMaps=True) # showMaps3(maps3) # showSelectRegions(F0, common_labels) # kls = mergePatchAndRegion(classHeatMap, maps3, labels, paras['th']*0.95) kls, categoryMap, classMap = mergePatchAndRegion(classHeatMap, maps3, labels, 0.5) intersectionPixelNum = len(np.argwhere((kls * mask_c) > 0)) unionPixelNum = len(np.argwhere((kls + mask_c) > 0)) IoU = float(intersectionPixelNum) / float(unionPixelNum) print 'IoU:', IoU for ci in xrange(classHeatMap.shape[2]): imsave(resultsSaveFolder+imName+'_classMap_'+str(ci)+'.jpg', classHeatMap[:, :, ci])
def testSegmentation(feaType, wordsNum, patchSize, mk, paras, labeledDataFolder, resultSaveFolder, classNum=4, wordsFolder='../../Data/Features/', mini=False, detection=True, merge=True): paras['sizeRange'] = (patchSize, patchSize) paras['patchSize'] = np.array([patchSize, patchSize]) paras['feaType'] = feaType if mini is True: mini_str = '_mini' else: mini_str = '' if detection is True: detection_str = '' else: detection_str = '_noDetection' if merge is True: merge_str = '' else: merge_str = '_noMerge' if feaType == 'LBP': paras['lbp_wordsFile_s1'] = wordsFolder + 'type4_LBPWords_s1_s' + str( patchSize) + '_b300_w' + str(wordsNum) + mini_str + '.hdf5' paras['lbp_wordsFile_s2'] = wordsFolder + 'type4_LBPWords_s2_s' + str( patchSize) + '_b300_w' + str(wordsNum) + mini_str + '.hdf5' paras['lbp_wordsFile_s3'] = wordsFolder + 'type4_LBPWords_s3_s' + str( patchSize) + '_b300_w' + str(wordsNum) + mini_str + '.hdf5' paras['lbp_wordsFile_s4'] = wordsFolder + 'type4_LBPWords_s4_s' + str( patchSize) + '_b300_w' + str(wordsNum) + mini_str + '.hdf5' if feaType == 'SIFT': paras[ 'sift_wordsFile_s1'] = wordsFolder + 'type4_SIFTWords_s1_s' + str( patchSize) + '_b300_w' + str(wordsNum) + mini_str + '.hdf5' paras[ 'sift_wordsFile_s2'] = wordsFolder + 'type4_SIFTWords_s2_s' + str( patchSize) + '_b300_w' + str(wordsNum) + mini_str + '.hdf5' paras[ 'sift_wordsFile_s3'] = wordsFolder + 'type4_SIFTWords_s3_s' + str( patchSize) + '_b300_w' + str(wordsNum) + mini_str + '.hdf5' paras[ 'sift_wordsFile_s4'] = wordsFolder + 'type4_SIFTWords_s4_s' + str( patchSize) + '_b300_w' + str(wordsNum) + mini_str + '.hdf5' if feaType == 'His': paras['his_wordsFile_s1'] = wordsFolder + 'type4_HisWords_s1_s' + str( patchSize) + '_b300_w' + str(wordsNum) + mini_str + '.hdf5' paras['his_wordsFile_s2'] = wordsFolder + 'type4_HisWords_s2_s' + str( patchSize) + '_b300_w' + str(wordsNum) + mini_str + '.hdf5' paras['his_wordsFile_s3'] = wordsFolder + 'type4_HisWords_s3_s' + str( patchSize) + '_b300_w' + str(wordsNum) + mini_str + '.hdf5' paras['his_wordsFile_s4'] = wordsFolder + 'type4_HisWords_s4_s' + str( patchSize) + '_b300_w' + str(wordsNum) + mini_str + '.hdf5' resultFile = 'segmentation_' + feaType + '_w' + str(wordsNum) + '_s' + str( patchSize) + '_mk' + str(mk) + detection_str + merge_str + '.txt' f_result = open(resultSaveFolder + resultFile, 'w') if mk == 0: paras['mk'] = None else: paras['mk'] = mk IoU_accuracy = np.zeros((classNum, )) confusionArray_c = np.zeros((classNum, classNum)) for c in xrange(0, classNum): labelImgFolder_c = labeledDataFolder + str(c + 1) + '_selected/' labelMaskFolder_c = labeledDataFolder + str(c + 1) + '_mask/' imgFiles = os.listdir(labelImgFolder_c) for imgName in imgFiles: imgFile = labelImgFolder_c + imgName imName = imgName[:-4] mask_c = load_mask_mat(labelMaskFolder_c + imName + '.mat') if detection is True: paras['thresh'] = calculateThreshold(imgFile) else: paras['thresh'] = 0 paras['imgFile'] = imgFile im = skimage.io.imread(imgFile) if len(im.shape) == 2: img = skimage.color.gray2rgb(im) paras['img'] = img paras['im'] = im # imName = imgFile[-20:-4] # ----no rotation---- class_names = [ 'background', 'arc', 'drapery', 'radial', 'hot-spot' ] labels, kls, categoryMap, classMap, classHeatMap = SCKLS(paras) confusionArray_c[c, labels] += 1 if False: # show segmentation results plt.figure(10) plt.imshow(kls, cmap='gray') plt.title(class_names[labels + 1] + '_predict') plt.axis('off') plt.figure(11) plt.imshow(mask_c, cmap='gray') plt.title(class_names[c + 1] + '_groundTruth') plt.axis('off') plt.figure(12) plt.imshow(im, cmap='gray') plt.title('raw image') plt.axis('off') plt.show() mask_c = mask_c.astype(np.int) kls = kls.astype(np.int) # else: # print 'classification error!' if merge is False: kls = np.zeros(kls.shape) kls[np.where(categoryMap > 0.3)] = 1 intersectionPixelNum = len(np.argwhere((kls * mask_c) > 0)) unionPixelNum = len(np.argwhere((kls + mask_c) > 0)) IoU = float(intersectionPixelNum) / float(unionPixelNum) print 'IoU:', IoU if labels == c: IoU_accuracy[c] += IoU f_result.write(imgName + ' ' + str(c) + ' ' + str(labels) + ' ' + str(IoU) + '\n') f_result.close() print confusionArray_c accuracy = confusionArray_c / np.sum(confusionArray_c, axis=1).reshape( classNum, 1) rightNums = [confusionArray_c[k, k] for k in xrange(classNum)] rightNums = np.array(rightNums, dtype='f') IoUs = IoU_accuracy / rightNums print accuracy print rightNums print IoUs return 0
def generateRegions(): paras = {} paras['k'] = 60 paras['minSize'] = 50 paras['patchSize'] = np.array([16, 16]) paras['region_patch_ratio'] = 0.1 paras['sigma'] = 0.5 paras['alpha'] = 0.6 paras['types'] = ['arc', 'drapery', 'radial', 'hot_spot'] paras[ 'lbp_wordsFile_s1'] = '../../Data/Features/type4_LBPWords_s1_s16_b300_w500.hdf5' paras[ 'lbp_wordsFile_s2'] = '../../Data/Features/type4_LBPWords_s2_s16_b300_w500.hdf5' paras[ 'lbp_wordsFile_s3'] = '../../Data/Features/type4_LBPWords_s3_s16_b300_w500.hdf5' paras[ 'lbp_wordsFile_s4'] = '../../Data/Features/type4_LBPWords_s4_s16_b300_w500.hdf5' eraseMapPath = '../../Data/eraseMap.bmp' if not os.path.exists(eraseMapPath): imSize = 440 eraseMap = np.zeros((imSize, imSize)) radius = imSize / 2 centers = np.array([219.5, 219.5]) for i in range(440): for j in range(440): if np.linalg.norm(np.array([i, j]) - centers) > 220 + 5: eraseMap[i, j] = 1 imsave(eraseMapPath, eraseMap) else: eraseMap = imread(eraseMapPath) / 255 paras['eraseMap'] = eraseMap paras['sizeRange'] = (16, 16) paras['imResize'] = (256, 256) paras['imgSize'] = (440, 440) paras['nk'] = 1 resolution = 1 gridSize = np.array([resolution, resolution]) paras['resolution'] = resolution paras['gridSize'] = gridSize paras['sdaePara'] = None paras['feaType'] = 'LBP' paras['mk'] = 0 paras['withIntensity'] = False paras['diffResolution'] = False paras['isSave'] = False paras['is_rotate'] = False paras['train'] = False labelFile = '../../Data/type4_b500.txt' imageFolder = '../../Data/all38044JPG/' imgType = '.jpg' [names, labels] = parseNL(labelFile) f = h5py.File('../../Data/regions.hdf5', 'w') # imgFile = '../../Data/labeled2003_38044/N20031221G071131.bmp' # label = 2 for i in range(len(names)): name = names[i] imgFile = imageFolder + name + imgType label = labels[i] paras['imgFile'] = imgFile im = np.array(imread(imgFile), dtype='f') / 255 paras['im'] = im im1 = skimage.io.imread(imgFile) if len(im1.shape) == 2: img = skimage.color.gray2rgb(im1) paras['img'] = img paras['thresh'] = calculateThreshold(imgFile) paras['specialType'] = int( label) - 1 # 0: arc, 1: drapery, 2: radial, 3: hot-spot paras['returnRegionLabels'] = [0] # 0: special, 1: rest, 2: common paras['th'] = 0.45 F0, region_labels, eraseLabels = region_special_map(paras) # save type: 0: common, 1: arc, 2, drapery, 3: radial, 4: hot-spot, 5: rest mask = np.zeros(F0.shape, dtype='i') mask.fill(5) for i in region_labels: mask[np.where(F0 == i)] = int(label) paras['specialType'] = int( label) - 1 # 0: arc, 1: drapery, 2: radial, 3: hot-spot paras['returnRegionLabels'] = [2] # 0: special, 1: rest, 2: common paras['th'] = 0.25 # paras['thresh'] = 0 F0, region_labels, eraseLabels, filterout_labels = region_special_map( paras, returnFilteroutLabels=True) # print filterout_labels region_labels = region_labels + filterout_labels for i in region_labels: mask[np.where(F0 == i)] = 0 f.create_dataset(name, mask.shape, dtype='i', data=mask) print name + ' saved!' f.close() return 0
minSize = 300 patchSize = np.array([28, 28]) region_patch_ratio = 0.2 sigma = 0.5 alpha = 0.6 imSize = 224 eraseMap = np.zeros((imSize, imSize)) radius = imSize / 2 centers = np.array([radius - 0.5, radius - 0.5]) for i in range(imSize): for j in range(imSize): if np.linalg.norm(np.array([i, j]) - centers) > radius + 5: eraseMap[i, j] = 1 th = calculateThreshold(imgFile) # F0, region_patch_list, _ = generate_subRegions(imgFile, patchSize, region_patch_ratio, eraseMap, k, minSize, sigma) # show_region_patch_grid(imgFile, F0, region_patch_list, alpha, eraseMap, saveFolder='/home/ljm/NiuChuang/KLSA-auroral-images/Data/Results/initial_segmentation/') # F0, region_patch_list, eraseLabels, filteroutLabels = generate_subRegions(imgFile, patchSize, region_patch_ratio, eraseMap, k, minSize, sigma, returnFilteroutLabels=True, thresh=th) # F0, eraseLabels, filteroutLabels = detect_regions(imgFile, eraseMap, k, minSize, sigma, th) detect_regions, _, _ = detect_regions(imgFile, eraseMap, k, minSize, sigma, th, imResize=imSize) # detect_regions = numpy.ones(F0.shape).astype(numpy.uint8) * 255 # removeLabels = list(eraseLabels) + filteroutLabels # for l in removeLabels: # detect_regions[np.where(F0==l)] = 0