def kMean(clusterNum): g = Graph(r"E:\ds2018") vocaDir = r"temp/vocabulary/" if not os.path.exists(vocaDir): os.makedirs(vocaDir) sift = SIFT() centers = [] for i in range(g.getTypeNum()): print("[kmeans]:" + str(i)) imgPaths = g.getTrainSet(i) features = np.float32([]).reshape((0, 128)) for imgPath, type in imgPaths: imgMat = g.getGreyGraph(imgPath) if imgMat is None: print("[kmean]:" + imgPath + " is None") continue feature = sift.getFeature(imgMat) features = np.append(features, feature, axis=0) kmeans = KMeans(n_clusters=clusterNum).fit(features) filename = os.path.join(vocaDir, str(i) + ".npy") np.save(filename, kmeans.cluster_centers_) centers.append(kmeans.cluster_centers_) return centers
def getFeatVecForSvm(self, imgList, load=0): if load == 1: feats = np.load(r"temp/featVectHog.npy") return feats g = Graph(r"E:\ds2018") feats = np.float32([]).reshape((0, self.getVecLength())) for imgPath, type in imgList: mat = g.getGreyGraph(imgPath) if mat is None: continue feat = self.getFeature(mat) feats = np.append(feats, feat, axis=0) np.save(r"temp/featVectHog.npy", feats) return feats
def calcVectorForSvm(self, imgList, n_cluster, load=0): if load == 1: types = np.load(r"temp/types.npy") featVec = np.load(r"temp/featVectSift.npy") centers = np.load(r"temp/centersSift.npy") return (types, featVec, centers) g = Graph(r"E:\ds2018") featMat = np.float32([]).reshape((0, 128)) featList = [] featVec = np.float32([]).reshape((0, n_cluster)) types = np.float32([]) i = 0 for imgPath, type in imgList: print("[kmeans before]:" + str(i)) i += 1 mat = g.getGreyGraph(imgPath) if mat is None: continue feat = self.getFeature(mat) featList.append(feat) featMat = np.append(featMat, feat, axis=0) types = np.append(types, np.float32([type])) kmeans = KMeans(n_cluster) kmeans.fit(featMat) centers = kmeans.cluster_centers_ i = 0 for feature in featList: print("[kmeans after]:" + str(i)) i += 1 featVec = np.append(featVec, self._calcFeatVec(feature, kmeans, n_cluster, centers), axis=0) np.save(r"temp/types.npy", types) np.save(r"temp/featVectSift.npy", featVec) np.save(r"temp/centersSift.npy", centers) return (types, featVec, centers)
def getFeatVecForSvm(self, imgList, load=0): if load == 1: feats = np.load(r"temp/featVectLbp.npy") return feats g = Graph(r"E:\ds2018") feats = np.float32([]).reshape((0, self.getVecLength())) i = 0 for imgPath, type in imgList: print("[lbp]:" + str(i)) i += 1 mat = g.getGreyGraph(imgPath) if mat is None: continue feat = self.getFeature(mat) feats = np.append(feats, feat.reshape((1, -1)), axis=0) np.save(r"temp/featVectLbp.npy", feats) return feats
from sklearn import svm from Graph.Graph import Graph from Features.LBP import LBP from sklearn.decomposition import PCA import numpy as np g = Graph(r"E:\ds2018") if not g.isDivided(): g.divideTrainTest("ds2018") trainList = g.readTrainCSV() features = np.float32([]).reshape(0, 128 * 128) types = [] for imgPath, type in trainList: lbp = LBP([8], [1], "default") imgMatrix = g.getGreyGraph(imgPath) if imgMatrix is None: continue lbpFeatures = lbp.getFeature(imgMatrix) for j in lbpFeatures: print(features.shape) print(j.shape) np.append(features, j.reshape(1, 128 * 128), axis=0) types.append(type) clf = svm.SVC(C=0.8, kernel='rbf', gamma=20, decision_function_shape='ovr') clf.fit(features, types) print("done")
''' def __init__(self,ps,rs,method="default"): self.p=ps self.r=rs self.method=method def getFeature(self,imgMat): features=[] for i in self.p: for j in self.r: features.append(feature.local_binary_pattern(imgMat,i,j,method=self.method)) return features g=Graph(r"E:\ds2018") trainList=g.readTrainCSV() lbp=LBP([8],[1]) features=[] print(trainList) for img in trainList: #print(img[0]) mat=g.getGreyGraph(img[0]) if mat is None: print("None") continue features.extend(lbp.getFeature(mat))