def eaData3Classify(m=1000): distance = 8 np.random.seed(1) X1 = 2 * np.random.randn(2, m) Y1 = np.zeros((3, m)) Y1[0] = 1 type1 = np.zeros((1, m)) X2 = 2 * np.random.randn(2, m) X2[0] += distance Y2 = np.zeros((3, m)) Y2[1] = 1 type2 = 1 + np.zeros((1, m)) X3 = 2 * np.random.randn(2, m) X3[0] += distance / 2 X3[1] += distance * math.sin(math.pi / 3) Y3 = np.zeros((3, m)) Y3[2] = 1 type3 = 2 + np.zeros((1, m)) X = np.c_[X1, X2, X3] Y = np.c_[Y1, Y2, Y3] Type = np.c_[type1, type2, type3] eaData = Ea() eaData.X = X eaData.Y = Y eaData.Type = Type return eaData
def demoLinear(): eaData = eaDataLinear([2, -3.4], [4.2], m=1000) net = eaNetLinear(eaData, L=1, n=4) nnInitWb(net) nnFit(eaData, net, learn_rate=0.02) Ea.show(net, 3) plotCost("Linear Costs", net.costs, netInfo=eaNetInfo(net))
def demoLogistics(): eaData = eaDataTFRing() net = eaNetLogistics(eaData) nnInitWb(net) nnFit(eaData, net, learn_rate=0.1) Ea.show(net, 3) plotPredict(net, eaData.X, eaData.Y, cmap=['#0099CC', '#FF6666','#6622FF']) plotCost("Logistics Costs", net.costs,netInfo=eaNetInfo(net))
def eaDataLinear(w, b, m=1000): nx = len(w) np.random.seed(1) X = np.random.normal(size=(nx, m)) Y = np.dot(w, X) + b np.random.seed(2) noise = .01 * np.random.normal(size=Y.shape) # print(L2(noise,0)) Y += noise eaData = Ea() eaData.X = X eaData.Y = Y return eaData
def eaNetLinear(eaData, L=4, n=4): net = Ea() net.L = L net.n[0] = eaData.X.shape[0] # nx for l in range(1, L): net.n[l] = n net.activFun[l] = eaActivFun.linear net.n[L] = 1 net.activFun[L] = eaActivFun.linear net.costFun = eaCostFun.L2 return net
def debug(self): print(help(self.keypoints[0])) pts = [] for pt in self.keypoints: ePt = Ea() ePt.angle = pt.angle ePt.class_id = pt.class_id ePt.octave = pt.octave ePt.pt = (pt.pt[0], pt.pt[1]) ePt.response = pt.response ePt.size = pt.size pts.append(ePt) Ea.show(pts, 500) pass
def eaDataTFRing(m=1000): # np.random.seed(...) 保证每次输出结果不变。 np.random.seed(1) X = 2 * np.random.randn(2, m) # 很方便的求 2-范数 的方法(相当于求半径) R = np.linalg.norm(X, ord=2, axis=0).reshape(1, m) # 增加一个噪音 np.random.seed(2) noise = .3 * np.random.normal(size=R.shape) R = R + noise # 二值化输出 Y = np.int32(np.array(R < 2.5)) # print("dataTF X.shape",X.shape,"Y.shape",Y.shape) # 合并输入输出数据 (学会用 np.r_[...] 和 np.c_[...]) eaData = Ea() eaData.X = X eaData.Y = Y eaData.Type = Y return eaData
def __init__(self): cv = Ea() cv.orb = cv2.ORB_create(500) cv.bf = cv2.BFMatcher(cv2.NORM_HAMMING) cv.diff = 0.35 self.cv = cv self.status = 0 self.p1p2 = Ea()
return g def softmax(z): x_exp = np.exp(z) x_sum = np.sum(x_exp, axis=0, keepdims=True) s = x_exp / x_sum return s def dSoftmax(z): # 你需要指定 dCrossSoftmax 为损失导函数 return 1 eaActivFun = Ea() eaActivFun.linear.g = linear eaActivFun.linear.dg = dLinear eaActivFun.linear.name = 'linear' eaActivFun.sigmoid.g = sigmoid eaActivFun.sigmoid.dg = dSigmoid eaActivFun.sigmoid.name = 'sigmoid' eaActivFun.tanh.g = tanh eaActivFun.tanh.dg = dTanh eaActivFun.tanh.name = 'tanh' eaActivFun.relu.g = relu eaActivFun.relu.dg = dRelu eaActivFun.relu.name = 'relu'
class ORBKeyFrame(): def __init__(self): cv = Ea() cv.orb = cv2.ORB_create(500) cv.bf = cv2.BFMatcher(cv2.NORM_HAMMING) cv.diff = 0.35 self.cv = cv self.status = 0 self.p1p2 = Ea() def debug(self): np.set_printoptions(linewidth=1500, precision=2, threshold=32, edgeitems=3) # Ea.show(self.org) # Ea.show(self.cur) # Ea.show(self.rich) # print(self.org.cv.detect.dsps.shape) # Ea.show(self.match) print(self.p1p2.kps.shape) print(self.p1p2.kps) def rich2cv(self): rich_kps = [] rich_dsps = [] for v in self.rich.values(): kp = v.kp for dsp in v.dsps: rich_kps.append(kp) rich_dsps.append(dsp) return rich_kps, np.array(rich_dsps) def update(self, filter_frame, draw_frame): cv_kps, cv_dsps = self.cv.orb.detectAndCompute(filter_frame, None) # KEYFRAME_INITIALIZE if self.status == 0: # 重新初始化 原始关键帧 org keyframe org = Ea() org.cv.detect.kps = cv_kps org.cv.detect.dsps = cv_dsps org.cv.detect.count = len(cv_kps) self.org = org self.cur = Ea() self.cur.clone(org) self.org.filter_frame = filter_frame self.org.draw_frame = draw_frame # init rich data. _kpt = lambda x: int(x[0])*1000 + int(x[1]) rich = Ea() for kp,dsp in zip(cv_kps,cv_dsps): rich[_kpt(kp.pt)].kp = kp rich[_kpt(kp.pt)].dsps = [dsp] self.rich = rich self.status = 1 # KEYFRAME_READY elif self.status == 1: self.cur.cv.detect.kps = cv_kps self.cur.cv.detect.dsps = cv_dsps # KEYFRAME_ORG_MATCHES elif self.status == 2: self.cur.cv.detect.kps = cv_kps self.cur.cv.detect.dsps = cv_dsps matches = self.cv.bf.knnMatch(queryDescriptors=self.org.cv.detect.dsps, trainDescriptors=self.cur.cv.detect.dsps, k=2) good = [m for (m, n) in matches if m.distance < self.cv.diff * n.distance] self.cur.cv.matches = matches self.cur.cv.good = good # KEYFRAME_RICH_MATCHES elif self.status == 3: self.cur.cv.detect.kps = cv_kps self.cur.cv.detect.dsps = cv_dsps rich_kps, rich_dsps = self.rich2cv() self.cur.cv.rich.kps = rich_kps self.cur.cv.rich.dsps = rich_dsps matches = self.cv.bf.knnMatch(queryDescriptors=self.cur.cv.rich.dsps, trainDescriptors=self.cur.cv.detect.dsps, k=2) good = [m for (m, n) in matches if m.distance < self.cv.diff * n.distance] self.cur.cv.matches = matches self.cur.cv.good = good _p1p2 = [] for (m, n) in matches: if m.distance < self.cv.diff * n.distance and m.distance > self.cv.diff * 0.90 * n.distance: pt = self.cur.cv.rich.kps[m.queryIdx].pt pt2 = cv_kps[m.trainIdx].pt dsp = self.cur.cv.detect.dsps[m.trainIdx] _kpt = lambda x: int(x[0]) * 1000 + int(x[1]) _p1p2.append([pt[0],pt[1],pt2[0],pt2[1]]) if len(self.rich[_kpt(pt)].dsps) < 50: self.rich[_kpt(pt)].dsps.append(dsp) else: # print("max 50") self.rich[_kpt(pt)].dsps.pop(1) self.rich[_kpt(pt)].dsps.append(dsp) self.p1p2.kps = np.array(_p1p2) self.p1p2.org_frame = self.org.draw_frame self.p1p2.cur_frame = draw_frame pass def draw_cur_cv_detect(self, draw_frame, c=(51, 163, 236)): cv2.drawKeypoints(draw_frame, self.cur.cv.detect.kps, draw_frame, color=c) def draw_org_cv_maches(self,draw_frame): cv2.drawKeypoints(draw_frame, self.cur.cv.detect.kps, draw_frame, color=(163, 236, 51)) _ipt = lambda x: (int(x[0]),int(x[1])) for g in self.cur.cv.good: pt1 = self.org.cv.detect.kps[g.queryIdx].pt pt2 = self.cur.cv.detect.kps[g.trainIdx].pt cv2.line(draw_frame, _ipt(pt1), _ipt(pt2), color=(0, 0, 255)) matches_img = cv2.drawMatches(self.org.draw_frame, self.org.cv.detect.kps, draw_frame, self.cur.cv.detect.kps, self.cur.cv.good[:], draw_frame, flags=2) cv2.imshow("matches_img", matches_img) def draw_rich_cv_maches(self,draw_frame): cv2.drawKeypoints(draw_frame, self.cur.cv.detect.kps, draw_frame, color=(163, 236, 51)) _ipt = lambda x: (int(x[0]),int(x[1])) for g in self.cur.cv.good: pt1 = self.cur.cv.rich.kps[g.queryIdx].pt pt2 = self.cur.cv.detect.kps[g.trainIdx].pt cv2.line(draw_frame, _ipt(pt1), _ipt(pt2), color=(0, 0, 255),thickness=2) matches_img = cv2.drawMatches(self.org.draw_frame, self.cur.cv.rich.kps, draw_frame, self.cur.cv.detect.kps, self.cur.cv.good[:], draw_frame, flags=2) cv2.imshow("matches_img", matches_img)
def update(self, filter_frame, draw_frame): cv_kps, cv_dsps = self.cv.orb.detectAndCompute(filter_frame, None) # KEYFRAME_INITIALIZE if self.status == 0: # 重新初始化 原始关键帧 org keyframe org = Ea() org.cv.detect.kps = cv_kps org.cv.detect.dsps = cv_dsps org.cv.detect.count = len(cv_kps) self.org = org self.cur = Ea() self.cur.clone(org) self.org.filter_frame = filter_frame self.org.draw_frame = draw_frame # init rich data. _kpt = lambda x: int(x[0])*1000 + int(x[1]) rich = Ea() for kp,dsp in zip(cv_kps,cv_dsps): rich[_kpt(kp.pt)].kp = kp rich[_kpt(kp.pt)].dsps = [dsp] self.rich = rich self.status = 1 # KEYFRAME_READY elif self.status == 1: self.cur.cv.detect.kps = cv_kps self.cur.cv.detect.dsps = cv_dsps # KEYFRAME_ORG_MATCHES elif self.status == 2: self.cur.cv.detect.kps = cv_kps self.cur.cv.detect.dsps = cv_dsps matches = self.cv.bf.knnMatch(queryDescriptors=self.org.cv.detect.dsps, trainDescriptors=self.cur.cv.detect.dsps, k=2) good = [m for (m, n) in matches if m.distance < self.cv.diff * n.distance] self.cur.cv.matches = matches self.cur.cv.good = good # KEYFRAME_RICH_MATCHES elif self.status == 3: self.cur.cv.detect.kps = cv_kps self.cur.cv.detect.dsps = cv_dsps rich_kps, rich_dsps = self.rich2cv() self.cur.cv.rich.kps = rich_kps self.cur.cv.rich.dsps = rich_dsps matches = self.cv.bf.knnMatch(queryDescriptors=self.cur.cv.rich.dsps, trainDescriptors=self.cur.cv.detect.dsps, k=2) good = [m for (m, n) in matches if m.distance < self.cv.diff * n.distance] self.cur.cv.matches = matches self.cur.cv.good = good _p1p2 = [] for (m, n) in matches: if m.distance < self.cv.diff * n.distance and m.distance > self.cv.diff * 0.90 * n.distance: pt = self.cur.cv.rich.kps[m.queryIdx].pt pt2 = cv_kps[m.trainIdx].pt dsp = self.cur.cv.detect.dsps[m.trainIdx] _kpt = lambda x: int(x[0]) * 1000 + int(x[1]) _p1p2.append([pt[0],pt[1],pt2[0],pt2[1]]) if len(self.rich[_kpt(pt)].dsps) < 50: self.rich[_kpt(pt)].dsps.append(dsp) else: # print("max 50") self.rich[_kpt(pt)].dsps.pop(1) self.rich[_kpt(pt)].dsps.append(dsp) self.p1p2.kps = np.array(_p1p2) self.p1p2.org_frame = self.org.draw_frame self.p1p2.cur_frame = draw_frame pass
# softmax 结果的交叉熵函数(对应多分类问题) def crossSoftmax(A, Y): m = Y.shape[1] loss = -1 / m * np.sum( Y * np.log(A) + (1 - Y) * np.log(1 - A), axis=1, keepdims=False) loss = np.sum(loss) return loss # softmax+交叉熵 结果的交叉熵导函数(对应多分类问题) def dCrossSoftmax(A, Y): return A - Y eaCostFun = Ea() eaCostFun.L1.J = bias eaCostFun.L1.dJ = dBias eaCostFun.L1.name = 'bias' eaCostFun.L2.J = variance eaCostFun.L2.dJ = dVariance eaCostFun.L2.name = 'variance' eaCostFun.L3.J = cross eaCostFun.L3.dJ = dCross eaCostFun.L3.name = 'cross' eaCostFun.L4.J = crossSoftmax eaCostFun.L4.dJ = dCrossSoftmax eaCostFun.L4.name = 'cross'
def eaNetLogistics(eaData): net = Ea() net.L = 2 net.n[0] = eaData.X.shape[0] # nx net.n[1] = 20 net.n[2] = 4 net.n[3] = 4 net.n[net.L] = 1 net.activFun[1] = eaActivFun.relu net.activFun[2] = eaActivFun.relu net.activFun[3] = eaActivFun.sigmoid net.activFun[net.L] = eaActivFun.sigmoid net.costFun = eaCostFun.L3 return net