def getPrCurveMergeToOne( methods=['MY', 'DRFI', 'GMR', 'MEAN'], num=None, ): ''' 过时的方法 全部读入内存 ''' imgGt = reduce( lambda x, y: np.r_[x, y], map( lambda name: io.imread(IMG_DIR + name[:-3] + 'png').astype(np.bool) .ravel(), IMG_NAME_LIST[:num])) print 'imgGt OK!' resoultDic = dict(zip(methods, [None] * len(methods))) print resoultDic.keys() for name in IMG_NAME_LIST[:num]: dic = getCoarseDic(name, methods, COARSE_DIR) for k, v in dic.items(): resoultDic[k] = np.r_[ resoultDic[k], v.ravel()] if resoultDic[k] != None else v.ravel() print '%d/%d' % (IMG_NAME_LIST.index(name), num), name precisions, recalls = [], [] for method in methods: # p, r ,_ = precisionRecallCurve(imgGt,resoultDic[method]) p, r, _ = precision_recall_curve(imgGt, resoultDic[method]) precisions += [p] recalls += [r] print method return precisions, recalls, methods
def grabCutTest(): coarseMethods = ['MY4','QCUT','DRFI'] imgInd = 1 n_segments,compactness = 200,10 imgName = IMG_NAME_LIST[imgInd] img,imgGt = readImg(imgName) coarseDic = getCoarseDic(imgName,coarseMethods) refinedImg = coarseDic['DRFI'] mask = grabCut(img,refinedImg) imgCut = img*mask[:,:,np.newaxis] show([img,imgCut]) show([refinedImg,mask])
def integratImgsBy3wayTest(): from algorithm import getSlic,getCoarseDic,integratImgsBy3way,buildMethodDic labelMap = getSlic(rgbImg,300) img = rgbImg m, n = labelMap.shape coarseMethods = ['GT'] coarseDic = getCoarseDic(imgName,coarseMethods) # show(coarseDic) coarseImgs=coarseDic.values() g() refinedImgs=map(lambda f:f(img,coarseImgs,labelMap),buildMethodDic.values()) img = integratImgsBy3way(refinedImgs) show(img)
def getRefindImgsTest(): IMG_DIR = r'E:\3-experiment\SalBenchmark-master\Data\DataSet1\Imgs/' COARSE_DIR =r'E:\3-experiment\SalBenchmark-master\Data\DataSet1\Saliency/' IMG_DIR = 'test/' COARSE_DIR ='test/' IMG_NAME_LIST = filter(lambda x:x[-3:]=='jpg',listdir(IMG_DIR)) coarseMethods = ['QCUT','DRFI'] coarseMethods = ['GT'] imgInd = 0 n_segments,compactness = 200,10 imgName = IMG_NAME_LIST[imgInd] img,imgGt = readImg(imgName) coarseDic = getCoarseDic(imgName,coarseMethods) #show(coarsesDic) sumCoarseImg = getSumCoarseImg(coarseDic) coarseImgs=coarseDic.values() labelMap = getSlic(img,n_segments,compactness) rgb = img img = sk.color.rgb2lab(img) #show([mark_boundaries(img,labelMap),imgGt]) # 获得4+4维 distance degreeVectors, Ws = getVectors(img, labelMap) vectors = getWeightSum(labelMap, degreeVectors, Ws) vectorsTrains = [] coarseTrains = [] for coarseImg in coarseImgs: coarseTrain, vectorsTrainTag = getCoarseTrain(coarseImg, labelMap) vectorsTrains += list(vectors[vectorsTrainTag]) coarseTrains += list(coarseTrain) elm = getElm(np.array(vectorsTrains), np.array(coarseTrains)) refined = elm.predict(vectors)[:,0] refinedImg = valueToLabelMap(labelMap,normalizing(refined)) vectorsImg = valueToLabelMap(labelMap,normalizing(vectors.sum(1))) show([rgb,refinedImg]) show([rgb,vectorsImg]) show(vectorsImg-refinedImg) loga(vectorsImg-refinedImg) g()
def my4test(): #if 1: from algorithm import * labelMap = getSlic(rgbImg,200) maxLabel = labelMap.max()+1 m, n = labelMap.shape coarseMethods = ['MEAN'] coarseDic = getCoarseDic(imgName,coarseMethods) # show(coarseDic) sumCoarseImg = getSumCoarseImg(coarseDic) coarseImgs=coarseDic.values() img = sk.color.rgb2lab(rgbImg) # 获得4+4维 distance degreeVectors, Ws = getVectors(img, labelMap) weightSumVectors = getWeightSum(labelMap, degreeVectors, Ws) diffEdges,diffNeighbors = getAllDiffEdgeAndNeighbor(labelMap,Ws) vectors = np.append(weightSumVectors,diffEdges,1) vectors = np.append(vectors,diffNeighbors,1) vectorsTrains = [] coarseTrains = [] for coarseImg in coarseImgs: coarseTrain, vectorsTrainTag = getCoarseTrain(coarseImg, labelMap) vectorsTrains += list(vectors[vectorsTrainTag]) coarseTrains += list(coarseTrain) elm = getElm(np.array(vectorsTrains), np.array(coarseTrains)) refined = elm.predict(vectors)[:,0] refinedImg = valueToLabelMap(labelMap,normalizing(refined)) show(mark_boundaries(sk.color.lab2rgb(img),labelMap)) print diffEdges.shape show(valueToLabelMap(labelMap,diffEdges.sum(1))) show(valueToLabelMap(labelMap,diffNeighbors[:,:4].sum(1))) # show(valueToLabelMap(labelMap,diffNeighbors[:,4:].sum(1))) # show(valueToLabelMap(labelMap,diffNeighbors.sum(1))) show(valueToLabelMap(labelMap,vectors[:,4:8].sum(1)))
def getPrCurve( methods=['DRFI', 'GMR', 'MEAN'], num=None, ): ''' 返回数据库前num张图片的平均PR曲线 dataDic 结构 img->[imgname]->[method]->auc,pr method->[method]->auc,pr ''' num = num if num else len(IMG_NAME_LIST) dataDic = {'img': {}} for name in IMG_NAME_LIST[:num]: print '%d/%d' % (IMG_NAME_LIST.index(name), num), name imgGt = io.imread(IMG_DIR + name[:-3] + 'png').astype(np.bool).ravel() coarseDic = getCoarseDic(name, methods, COARSE_DIR) dataDic['img'][name] = {} for k, v in coarseDic.items(): dataDic['img'][name][k] = {} _dic = dataDic['img'][name][k] coarseImg = v.ravel() p, r, _ = precisionRecallCurve(imgGt, coarseImg) _dic['pr'] = (p, r) _dic['auc'] = metrics.roc_auc_score(imgGt.ravel(), coarseImg) dataDic['method'] = dict(zip(methods, [{} for i in range(len(methods))])) precisions, recalls = [], [] for method in methods: _dic = dataDic['method'][method] p, r = sum([v[method]['pr'][0] for k,v in dataDic['img'].items()])/num,\ sum([v[method]['pr'][1] for k,v in dataDic['img'].items()])/num _dic['pr'] = (p, r) _dic['auc'] = metrics.auc(r, p) precisions += [p] recalls += [r] return precisions, recalls, methods, dataDic
def saveImgData(imgName, methods): ''' save data to LABEL_DATA_DIR return default method`s AUC ''' if isinstance(imgName, int): imgName = IMG_NAME_LIST[imgName] imgGt = io.imread(IMG_DIR + imgName[:-3] + 'png') / 255. coarseDic = getCoarseDic(imgName, methods, COARSE_DIR) data = {} data['name'] = imgName data['method'] = {} for method, refindImg in coarseDic.items(): data['method'][method] = getAucAndPr(imgGt, refindImg, method) dataName = LABEL_DATA_DIR + imgName[:-3] + 'data' AUC_METHOD = "DRFI" data['aucMethod'] = AUC_METHOD data['auc'] = data['method'][AUC_METHOD][0] saveData(data, dataName) return data['auc']
def aucBug(): # from saliency import * # from algorithm import * # from analysis import * ll = [('184.jpg', 0.83881506713016329), ('138.jpg', 0.84727243912368833), ('122.jpg', 0.88417034903928537), ('153.jpg', 0.92387361400695434)] name = ll[0][0] imgGt = io.imread(IMG_DIR + name[:-3] + 'png').astype(np.bool) #.ravel() methods = ['DISC2', 'DRFI', 'GMR', 'MEAN'] method = 'DISC2' coarseDic = getCoarseDic(name, methods, COARSE_DIR) print coarseDic.keys() # show(coarseDic.values()) resoult = coarseDic[method] show([resoult]) show(imgGt) ##imgGt.ravel() #resoult.ravel() p, r, _ = precisionRecallCurve(imgGt, resoult) #l = zip(resoult,imgGt) print method, 'auc', metrics.roc_auc_score(imgGt.ravel(), resoult.ravel()) plt.plot(p, r) plt.show()