def training(self, trainData, mode = 'diag', subSpaceDim = 2): # num of classes K = len(trainData) # num of features D = len(trainData[1][0]) # mk = {} for k in range(1, K + 1): mk[k] = ut.getMean(trainData[k], 'array') # m = ut.getMean(reduce(ut.add, [trainData[k] for k in range(1, K + 1)])) # f = lambda k: len(trainData[k]) * np.dot( mk[k] - m, (mk[k] - m).transpose() ) S_B = reduce(ut.add, [f(k) for k in range(1, K - 1)]) # construct S_W S_W = [] Sk = range(K + 1) if mode == 'diag': S_W = np.diag([1] * D) else: g = lambda x, y: np.dot((x - y), (x - y).transpose()) for k in range(1, K + 1 ): Sk[k] = reduce(ut.add, [g(ut.ls2Vec(x), mk[k]) for x in trainData[k]]) S_W = reduce(ut.add, Sk[1:]) # [egs, vecs] = np.linalg.eigh(np.dot(np.linalg.inv(S_W), S_B)) pos = ut.getMaxPos(egs, subSpaceDim) W = ut.arrayExtract(vecs, pos) # now we will construct trainingData set in the # sub-feature space, then run Gaussian generative # method to train the new trainData set to get a # a classifier def projectFunc(v): tmp = np.dot(ut.ls2Vec(v).transpose(), W).tolist() return tmp[0] for k in range(K): trainData[k + 1] = map(projectFunc, trainData[k + 1]) self.projectFunc = projectFunc self.func = GaussianGM.GaussianGM(trainData)
def GaussianGM(trainData): """ given a set of training data return a function f(x) as a classifier f(x) will return the class of x trainData has the following form: { 1: list of feature vectors 2: list of feature vectors 3: ... } """ # num of classes K = len(trainData) # calculate p(C_k) p = [0] * (K) for i in range(K): p[i] = len(trainData[i + 1]) N = sum(p) p = (np.array(p) / (N + 0.0)).tolist() # calculate a_k (x)= W_k^T x + w_k0 a = [1] * K sigma = [1] * K mu = [1] * K W = [1] * K w0 = [1] * K for i in range(K): mu[i] = ut.getMean(trainData[i + 1], "array") f = lambda x: np.dot((ut.ls2Vec(x) - mu[i]), (ut.ls2Vec(x) - mu[i]).transpose()) sigma[i] = reduce(ut.add, map(f, trainData[i + 1])) sigma_inv = inv(reduce(ut.add, sigma) / (N + 0.0)) for k in range(K): W[k] = np.dot(sigma_inv, mu[k]) w0[k] = -0.5 * reduce(np.dot, [mu[k].transpose(), sigma_inv, mu[k]])[0, 0] + np.log(p[k]) a[k] = lambda x: np.dot(W[k].transpose(), ut.ls2Vec(x))[0, 0] + w0[k] # construct a classifier function def f(w, w0, x): tmp = [np.dot(ww.transpose(), ut.ls2Vec(x))[0, 0] + ww0 for (ww, ww0) in zip(w, w0)] tmp = ut.getMaxPos(tmp) return tmp[0] + 1 return lambda x: f(W, w0, x)
# we need to keep in mind aspect ratio so the image does # not look skewed or distorted -- therefore, we calculate # the ratio of the new image to the old image # if image.shape[0]<image.shape[1]: # r = 256.0 / image.shape[0] # dim = ( 256,int(image.shape[1] * r)) # else: # r = 256.0 / image.shape[1] # dim = ( int(image.shape[0] * r),256) # perform the actual resizing of the image and show it # return cv2.resize(image, dim, interpolation = cv2.INTER_AREA)[0:224, 0:224] #cv2.imshow("resized", resized) # cv2.waitKey(0) meanImage = utilities.getMean() # print meanImage.shape # Test pretrained model model = cnn_lstm.create_cnn_lstm(weightsfile) sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='categorical_crossentropy') #meantrasnformed = meanImage #meantrasnformed[:,:,[0,1,2]] = meanImage[:,:,[2,1,0]] #meantrasnformed = np.expand_dims(meantrasnformed, axis=0) posxs = [] posqs = [] imgs = [] howmanyaccepted = 0 counter = 0