def myBuildDBNtest(self): #提取所有帧中,原始的骨架点,得到一个矩阵,Skeleton_matrix ,同时返回骨架是否归0化 #Skeleton_matrix, valid_skel = Extract_feature_UNnormalized(smp, used_joints, 1, smp.getNumFrames()) time_tic = time.time() import cPickle Skeleton_matrix=cPickle.load(open("testSkeleton_matrix","rb")) #print Skeleton_matrix Feature = Extract_feature_Realtime(Skeleton_matrix, self.njoints) Feature_normalized = normalize(Feature, self.Mean1, self.Std1) ''' ########################## ### model 1 第一种网络构架模式 # ########################## dbn = GRBM_DBN(numpy_rng=numpy_rng, n_ins=528, hidden_layers_sizes=[1000, 1000, 500], n_outs=201) dbn.load('dbn_2014-05-23-20-07-28.npy')#预先训练好的构架 #这里就是theano的奇葩函数构架 validate_model = theano.function(inputs=[], outputs=dbn.logLayer.p_y_given_x,#输出是逻辑回归层的输出 givens={ dbn.x: shared_x}) ''' observ_likelihood_1 = self.validate_model(Feature_normalized)#调用函数得到结果 ########################## # viterbi path decoding ##################### observ_likelihood_1=observ_likelihood_1[0:50,:] #这里自己改了,只有第一个网络结构的, log_observ_likelihood = log(observ_likelihood_1.T) #这里是一个矩阵, 行是样本,列是概率 [1884个样本, 201列] 用T转置了 print "处理时间 %d sec" % int(time.time() - time_tic) time_tic = time.time() #下面就是vibiter算法了 print("\t Viterbi path decoding " ) # do it in log space avoid numeric underflow [path, predecessor_state_index, global_score] =viterbi_path_log( self.Prior, self.Transition_matrix, log_observ_likelihood ) label=viterbi_endframe(path,5,30) # Some gestures are not within the vocabulary #[pred_label, begin_frame, end_frame, Individual_score, frame_length] = viterbi_colab_clean( # path, global_score, threshold=-100, mini_frame=19) print "标记是:" print label print "viterbi处理时间 %d sec" % int(time.time() - time_tic)
def myViterbi(self,log_observ_likelihood): time_tic = time.time() #下面就是vibiter算法了 print("\t Viterbi path decoding " ) # do it in log space avoid numeric underflow [path, predecessor_state_index, global_score] =viterbi_path_log( self.Prior, self.Transition_matrix, log_observ_likelihood ) label=viterbi_endframe(path,5,30) # Some gestures are not within the vocabulary #[pred_label, begin_frame, end_frame, Individual_score, frame_length] = viterbi_colab_clean( # path, global_score, threshold=-100, mini_frame=19) print "标记是:" print label print "viterbi处理时间 %d sec" % int(time.time() - time_tic) return label
data = numpy.reshape(data, (4 * 90 * 90, data.shape[-1])) data = numpy.array(data - data_mean[:, numpy.newaxis], dtype=numpy.single) ftrs = model.do_write_features(data) ########################## # viterbi path decoding ##################### log_observ_likelihood = log(ftrs.T) log_observ_likelihood[-1, 0:5] = 0 log_observ_likelihood[-1, -5:] = 0 print("\t Viterbi path decoding ") # do it in log space avoid numeric underflow [path, predecessor_state_index, global_score] = viterbi_path_log(log(Prior), log(Transition_matrix), log_observ_likelihood) #[path, predecessor_state_index, global_score] = viterbi_path(Prior, Transition_matrix, observ_likelihood) # Some gestures are not within the vocabulary [pred_label, begin_frame, end_frame, Individual_score, frame_length] = viterbi_colab_clean(path, global_score, threshold=-100, mini_frame=19) #begin_frame = begin_frame + 1 end_frame = end_frame + 5 # note here 3DCNN should add 5 frames because we used 4 frames ### plot the path and prediction if False: im = imdisplay(global_score) plt.imshow(im, cmap='gray')
##################### if Flag_multiview: accumulate_prob = numpy.zeros((ftrs.shape[0]/5, ftrs.shape[1])) for i in range(5): accumulate_prob += ftrs[i*ftrs.shape[0]/5:(i+1)*ftrs.shape[0]/5,:] accumulate_prob = accumulate_prob/5.0 log_observ_likelihood = log(accumulate_prob.T + numpy.finfo(numpy.float32).eps) else: log_observ_likelihood = log(ftrs.T + numpy.finfo(numpy.float32).eps) log_observ_likelihood[-1, 0:5] = 0 log_observ_likelihood[-1, -5:] = 0 print("\t Viterbi path decoding " ) # do it in log space avoid numeric underflow [path, predecessor_state_index, global_score] = viterbi_path_log(log(Prior), log(Transition_matrix), log_observ_likelihood) #[path, predecessor_state_index, global_score] = viterbi_path(Prior, Transition_matrix, observ_likelihood) # Some gestures are not within the vocabulary [pred_label, begin_frame, end_frame, Individual_score, frame_length] = viterbi_colab_clean(path, global_score, threshold=-100, mini_frame=19) #begin_frame = begin_frame + 1 end_frame = end_frame + 5 # note here 3DCNN should add 5 frames because we used 4 frames ### plot the path and prediction if True: im = imdisplay(global_score) plt.clf() plt.imshow(im, cmap='gray') plt.plot(range(global_score.shape[-1]), path, color='c',linewidth=2.0) plt.xlim((0, global_score.shape[-1])) # plot ground truth
time_tic = time.time() load_path_cropped= os.path.join(outPred_cropped,file) load_path_depth= os.path.join(outPred_depth,file) dic_cropped= cPickle.load( open(load_path_cropped, "rb" ) ) dic_depth = cPickle.load( open(load_path_depth, "rb" ) ) log_observ_likelihood_cropped= dic_cropped['log_observ_likelihood'] log_observ_likelihood_depth = dic_depth['log_observ_likelihood'] log_observ_likelihood = log_observ_likelihood_cropped + log_observ_likelihood_depth print "Viterbi path decoding " + file # do it in log space avoid numeric underflow [path, predecessor_state_index, global_score] = viterbi_path_log(log(Prior), log(Transition_matrix), log_observ_likelihood) #[path, predecessor_state_index, global_score] = viterbi_path(Prior, Transition_matrix, observ_likelihood) # Some gestures are not within the vocabulary [pred_label, begin_frame, end_frame, Individual_score, frame_length] = viterbi_colab_clean(path, global_score, threshold=-100, mini_frame=19) ### In theory we need add frame, but it seems that the groutnd truth is about 3 frames more, a bit random end_frame = end_frame + 3 print "Elapsed time %d sec" % int(time.time() - time_tic) prediction=[] for i in range(len(begin_frame)): prediction.append([ pred_label[i], begin_frame[i], end_frame[i]] ) if True: import matplotlib.pyplot as plt im = imdisplay(global_score)