def showRegions(h5File, labelFile, imgFolder='../../Data/all38044JPG/', imgType='.jpg'): [imgs, types] = parseNL(labelFile) fr = h5py.File(h5File, 'r') for n in range(0, len(imgs)): name = imgs[n] label = types[n] print name, label im = imread(imgFolder + name + imgType) if len(im.shape) == 2: img = skimage.color.gray2rgb(im) mask = np.array(fr.get(name)) print set(mask.flatten()) type_num = 6 colors = np.random.randint(0, 255, (type_num, 3)) color_regions = colors[mask] alpha = 0.6 result = (color_regions * alpha + img * (1. - alpha)).astype(np.uint8) plt.figure(11) plt.imshow(im, cmap='gray') plt.figure(111) plt.imshow(result) plt.show()
def calLBPFeaSet(dataFolder, labelFile, classNum, imgType, gridSize, sizeRange, classLabel, saveName, imResize=None, withIntensity=None, diffResolution=True): LBPFeaDim = 10 + 18 + 26 if withIntensity: LBPFeaDim += 3 posParaNum = 4 names, labels = plf.parseNL(labelFile) if classNum == 4: auroraData = plf.arrangeToClasses(names, labels, classNum, classLabel) else: auroraData, _ = plf.arrangeToClasses(names, labels, classNum, classLabel) f = h5py.File(saveName, 'w') f.attrs['dataFolder'] = dataFolder ad = f.create_group('auroraData') for c, imgs in auroraData.iteritems(): ascii_imgs = [n.encode('ascii', 'ignore') for n in imgs] ad.create_dataset(c, (len(ascii_imgs), ), 'S10', ascii_imgs) feaSet = f.create_group('feaSet') posSet = f.create_group('posSet') for c, imgs in auroraData.iteritems(): feaArr = np.empty((0, LBPFeaDim)) posArr = np.empty((0, posParaNum)) for name in imgs: imgFile = dataFolder + name + imgType if imResize: feaVec, posVec = calImgLBPFeatures( imgFile, gridSize, sizeRange, imResize=imResize, withIntensity=withIntensity, diffResolution=diffResolution) else: feaVec, posVec = calImgLBPFeatures( imgFile, gridSize, sizeRange, withIntensity=withIntensity, diffResolution=diffResolution) feaArr = np.append(feaArr, feaVec, axis=0) posArr = np.append(posArr, posVec, axis=0) feaSet.create_dataset(c, feaArr.shape, 'f', feaArr) posSet.create_dataset(c, posArr.shape, 'i', posArr) f.close() print saveName + ' saved' return 0
def generateSpecialCommonBbox(labelFile, savePath, dataFolder, imgType, paras): names, labels = plf.parseNL(labelFile) fs = h5py.File(savePath, 'w') for i in range(len(names)): name = names[i] label = labels[i] imgFile = dataFolder + name + imgType paras['imgFile'] = imgFile im = skimage.io.imread(imgFile) if len(im.shape) == 2: img = skimage.color.gray2rgb(im) paras['im'] = img paras['img'] = img paras['specialType'] = int( label) - 1 # 0: arc, 1: drapery, 2: radial, 3: hot-spot paras['th'] = 0.45 paras['returnRegionLabels'] = [1, 2] # 0: special, 1: rest, 2: common regions_special, angle_special = special_common_local_proposal(paras) regions_special = np.array(regions_special) paras['th'] = 0.25 paras['returnRegionLabels'] = [0, 1] # 0: special, 1: rest, 2: common regions_common, angle_common = special_common_local_proposal(paras) regions_common = np.array(regions_common) labels_special = np.zeros((regions_special.shape[0], ), dtype='i') labels_special.fill(label) labels_common = np.zeros((regions_common.shape[0], ), dtype='i') group = fs.create_group(str(i)) group.attrs['imgFile'] = imgFile group.attrs['imgName'] = name d_special = group.create_dataset('bbox_special', shape=regions_special.shape, dtype='i', data=regions_special) d_common = group.create_dataset('bbox_common', shape=regions_common.shape, dtype='i', data=regions_common) group.create_dataset('labels_special', shape=labels_special.shape, dtype='i', data=labels_special) group.create_dataset('labels_common', shape=labels_common.shape, dtype='i', data=labels_common) d_special.attrs['angle'] = angle_special d_common.attrs['angle'] = angle_common print name, 'bbox saved' fs.close() return 0
def calFusionFeaSet(dataFolder, labelFile, classNum, feaTypes, imgType, gridSize, sizeRange, classLabel, saveName): posParaNum = 4 names, labels = plf.parseNL(labelFile) if classNum == 4: auroraData = plf.arrangeToClasses(names, labels, classNum, classLabel) else: auroraData, _ = plf.arrangeToClasses(names, labels, classNum, classLabel) f = h5py.File(saveName, 'w') f.attrs['dataFolder'] = dataFolder ad = f.create_group('auroraData') for c, imgs in auroraData.iteritems(): ascii_imgs = [n.encode('ascii', 'ignore') for n in imgs] ad.create_dataset(c, (len(ascii_imgs), ), 'S10', ascii_imgs) feaSet = f.create_group('feaSet') posSet = f.create_group('posSet') for c, imgs in auroraData.iteritems(): feaArr = None posArr = None for name in imgs: imgFile = dataFolder + name + imgType print imgFile, c feaVec, posVec = calFusionFea(imgFile, feaTypes, gridSize, sizeRange) if feaArr is None: feaArr = np.empty((0, feaVec.shape[1])) posArr = np.empty((0, posParaNum)) feaArr = np.append(feaArr, feaVec, axis=0) posArr = np.append(posArr, posVec, axis=0) feaSet.create_dataset(c, feaArr.shape, 'f', feaArr) posSet.create_dataset(c, posArr.shape, 'i', posArr) f.close() print saveName + ' saved' return 0
import numpy as np import h5py from src.util.paseLabeledFile import parseNL if __name__ == '__main__': labeledFile = '../../Data/type4_b500.txt' names, labels = parseNL(labeledFile) bboxFile = '../../Data/type4_b500_bbox.hdf5' save_bboxFile = '../../Data/type4_b500_whole_bbox.hdf5' f = h5py.File(bboxFile, 'r') fs = h5py.File(save_bboxFile, 'w') for i in range(len(f)): im_i = f.get(str(i)) imgFile = im_i.attrs['imgFile'] name = im_i.attrs['imgName'] regions_special = im_i.get('bbox_special') regions_common = im_i.get('bbox_common') labels_special = im_i.get('labels_special') labels_common = im_i.get('labels_common') angle_special = regions_special.attrs['angle'] angle_common = regions_common.attrs['angle'] label = int(labels[names.index(name)]) print regions_special.shape whole = np.array([[0, 0, 440, 440]]) if regions_special.shape[0] == 0: regions_special = whole else: regions_special = np.append(regions_special, whole, axis=0) # print label if labels_special.shape[0] != 0:
import src.preprocess.esg as esg from scipy.misc import imsave from src.local_feature.adaptiveThreshold import calculateThreshold from scipy.misc import imread, imresize import os import scipy.io as sio from src.experiments.test_segmentation_accuracy import load_mask_mat from src.localization.generateSubRegions import detect_regions if __name__ == '__main__': data_folder = '/home/amax/NiuChuang/KLSA-auroral-images/Data/segmentation_data_v2/' heatmap_folder = '/home/amax/NiuChuang/CAM/aurora_heatmaps/' predict_file = '/home/amax/NiuChuang/CAM/aurora_predict.txt' type_num = 4 [names, predicts] = plf.parseNL(predict_file) k = 60 minSize = 50 sigma = 0.5 imSize = 440 eraseMap = np.zeros((imSize, imSize)) radius = imSize / 2 centers = np.array([219.5, 219.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 cls_confusion = np.zeros((type_num, type_num))
yl = [adaptiveLinear(x) for x in xl] return yl def calculateThreshold(imgFile): hist = calculateImgHist(imgFile) thresh = adaptiveLinear(hist[180:].sum()) return thresh if __name__ == '__main__': imgFile = '/home/niuchuang/PycharmProjects/KLSA-auroral-images/Data/labeled2003_38044/N20031225G100901.bmp' thresholdLabelFile = '../../Data/threshold_annotations.txt' imagesFolder_thresh = '../../Data/images_for_threshold/' imgType = '.jpg' [names, labels] = plf.parseNL(thresholdLabelFile) bright_nums = np.zeros((len(names), )) thresholds = np.zeros((len(names), )) for i in xrange(len(names)): imgFile = imagesFolder_thresh + names[i] + imgType hist = calculateImgHist(imgFile) bright_nums[i] = hist[150:].sum() thresholds[i] = int(labels[i]) plt.figure(1) xs = range(60000) ys = calAdapList(xs) plt.plot(xs, ys) plt.scatter(bright_nums, thresholds, 50, color='blue') plt.show() # outCircleSize = int(round(((440*440) - (math.pi * 221 * 221)))) # im = imread(imgFile)
import numpy as np from src.util.paseLabeledFile import parseNL from scipy.misc import imread if __name__ == '__main__': labelFile = '../../Data/Alllabel2003_38044.txt' imgsFolder = '../../Data/all38044JPG/' imgType = '.jpg' [names, labels] = parseNL(labelFile) img_num = len(names) mean = 0 for i in range(img_num): name = names[i] imgFile = imgsFolder + name + imgType im = imread(imgFile) mean += im.mean() mean = mean / img_num print mean
"""According to some self-define rules, this function selects the images for following experiments.""" import src.util.paseLabeledFile as plf filePath = '../../Data/Alllabel2003_38044.txt' file_selected = '../../Data/one_in_minute.txt' file_balance = '../../Data/balanceSampleFrom_one_in_minute.txt' file_own = '../../Data/one_in_minute_selected.txt' class_num = 4 select_num_perclass = 500 [names, labels] = plf.parseNL(filePath) arrangedImgs = plf.arrangeToClasses(names, labels, class_num) print 'total labeled number:' for i in range(4): print 'NO. class ' + str(i + 1), len(arrangedImgs[str(i + 1)]) # print plf.timeDiff(names[0], names[2]) [ids, sampledImgs] = plf.sampleImages(names) f_w = open(file_selected, 'w+') for i in range(len(ids)): f_w.write(sampledImgs[i] + ' ' + labels[ids[i]] + '\n') f_w.close() [names_s, labels_s] = plf.parseNL(file_selected)
patch_mean = float(f_mean.readline().split(' ')[1]) f_mean.close() # diff mean # patch_mean = 0 print 'patch_mean: ' + str(patch_mean) # saveSDAEFeas = '../../Data/Features/type3_SDAEFeas.hdf5' # saveSDAEFeas = '../../Data/Features/type4_SDAEFeas.hdf5' # saveSDAEFeas = '../../Data/Features/type4_SDAEFeas_diff_mean.hdf5' # saveSDAEFeas = '../../Data/Features/type4_SDAEFeas_same_mean_s16_600_300_300_300.hdf5' # saveSDAEFeas = '../../Data/Features/type4_SDAEFeas_same_mean_s28_b500_special.hdf5' saveSDAEFeas = '../../Data/Features/type4_SDAEFeas_same_mean_s28_b500_special_trained.hdf5' # saveSDAEFeas = '../../Data/Features/type4_SDAEFeas_same_mean_s28_b500_special_classification.hdf5' [images, labels] = plf.parseNL(labelFile) # arrImgs, _ = plf.arrangeToClasses(images, labels, classNum, classes) arrImgs = plf.arrangeToClasses(images, labels, classNum, classes) for i in arrImgs: print i, len(arrImgs[i]) f = h5py.File(saveSDAEFeas, 'w') f.attrs['dataFolder'] = dataFolder ad = f.create_group('auroraData') for c, imgs in arrImgs.iteritems(): ascii_imgs = [n.encode('ascii', 'ignore') for n in imgs] ad.create_dataset(c, (len(ascii_imgs), ), 'S10', ascii_imgs) feaSet = f.create_group('feaSet') posSet = f.create_group('posSet')
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
def makePatchData(labelFile, patchSize, gridSize=np.array([10, 10]), imgType='.bmp', channels=1, savePath='../../Data/one_in_minute_patch_diff_mean.hdf5', same_mean_file='../.../Data/patchData_mean_s16.txt', imagesFolder='../../Data/labeled2003_38044/', patchMean=True, saveList='../../Data/patchList_diff_mean.txt', subtract_same_mean=False): sizeRange = (patchSize, patchSize) [images, labels] = plf.parseNL(labelFile) # arragedImages = plf.arrangeToClasses(images, labels, classNum) f = h5py.File(savePath, 'w') data = f.create_dataset('data', (0, channels, patchSize, patchSize), dtype='f', maxshape=(None, channels, patchSize, patchSize)) label = f.create_dataset('label', (0, ), dtype='i', maxshape=(None, )) if subtract_same_mean: patches_mean = 0 for i in range(len(images)): imf = imagesFolder + images[i] + imgType gridPatchData, gridList, _ = esg.generateGridPatchData( imf, gridSize, sizeRange) patchData = np.array(gridPatchData) patches_mean += patchData.mean() patch_mean = patches_mean / len(images) print 'patch number: ' + str(data.shape[0]) print 'patch mean: ' + str(patch_mean) with open(same_mean_file, 'w') as f2: f2.write('patch_mean: ' + str(patch_mean)) f2.close() else: patch_mean = 0 print 'patch_mean: ', patch_mean for i in range(len(images)): imf = imagesFolder + images[i] + imgType print imf gridPatchData, gridList, _ = esg.generateGridPatchData( imf, gridSize, sizeRange) patchData = [ p.reshape(channels, patchSize, patchSize) for p in gridPatchData ] patchData = np.array(patchData) - patch_mean if patchMean: means = np.mean(np.mean(patchData, axis=-1), axis=-1) means = means.reshape(means.shape[0], means.shape[1], 1, 1) means = np.tile(means, (1, 1, patchSize, patchSize)) patchData -= means labelData = np.full((len(gridList), ), int(labels[i]), dtype='i') oldNum = data.shape[0] newNum = oldNum + patchData.shape[0] data.resize(newNum, axis=0) data[oldNum:newNum, :, :, :] = patchData label.resize(newNum, axis=0) label[oldNum:newNum, ] = labelData f.close() print 'make patch data done!' with open(saveList, 'w') as f1: f1.write(savePath) f1.close() print saveList + ' saved!' return 0
def calCascadeFeaSet(dataFolder, labelFile, siftFeaFile_reduce, lbpFeaFile_reduce, sdaeFeaFile_reduce_d, sdaeFeaFile_reduce_s, classNum, imgType, gridSize, sizeRange, classLabel, sdaePara, saveName, saveFolder='../../Data/Features/'): sift_f = h5py.File(siftFeaFile_reduce, 'r') sdae_f_d = h5py.File(sdaeFeaFile_reduce_d, 'r') sdae_f_s = h5py.File(sdaeFeaFile_reduce_s, 'r') lbp_f = h5py.File(lbpFeaFile_reduce, 'r') names, labels = plf.parseNL(labelFile) if classNum == 4: auroraData = plf.arrangeToClasses(names, labels, classNum, classLabel) else: auroraData, _ = plf.arrangeToClasses(names, labels, classNum, classLabel) f = h5py.File(saveFolder + saveName, 'w') f.attrs['dataFolder'] = dataFolder ad = f.create_group('auroraData') for c, imgs in auroraData.iteritems(): ascii_imgs = [n.encode('ascii', 'ignore') for n in imgs] ad.create_dataset(c, (len(ascii_imgs), ), 'S10', ascii_imgs) feaSet = f.create_group('feaSet') posSet = f.create_group('posSet') for c, imgs in auroraData.iteritems(): # sift_u = np.array(sift_f.get('uSet/'+c)) # lbp_u = np.array(lbp_f.get('uSet/'+c)) # sdae_u_d = np.array(sdae_f_d.get('uSet/'+c)) # sdae_u_s = np.array(sdae_f_s.get('uSet/'+c)) sift_u = np.array(sift_f.get('uSet/u')) lbp_u = np.array(lbp_f.get('uSet/u')) sdae_u_d = np.array(sdae_f_d.get('uSet/u')) sdae_u_s = np.array(sdae_f_s.get('uSet/u')) imgFile = dataFolder + imgs[0] + imgType _, gl, _ = esg.generateGridPatchData(imgFile, gridSize, sizeRange) feaVec, posVec = extractCascadeFeatures(imgFile, sift_u, lbp_u, sdae_u_d, sdae_u_s, gl, gridSize, sizeRange, sdaePara) feaArr = np.empty((0, feaVec.shape[1])) posArr = np.empty((0, posVec.shape[1])) for name in imgs: imgFile = dataFolder + name + imgType batchSize = len(gl) inputShape = (batchSize, 1, sizeRange[0], sizeRange[0]) sdaePara['inputShape'] = inputShape feaVec, posVec = extractCascadeFeatures(imgFile, sift_u, lbp_u, sdae_u_d, sdae_u_s, gl, gridSize, sizeRange, sdaePara) feaArr = np.append(feaArr, feaVec, axis=0) posArr = np.append(posArr, posVec, axis=0) feaSet.create_dataset(c, feaArr.shape, 'f', feaArr) posSet.create_dataset(c, posArr.shape, 'i', posArr) f.close() print saveFolder + saveName + ' saved' return 0