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 myBuildDBN(self,Skeleton_matrix): #提取所有帧中,原始的骨架点,得到一个矩阵,Skeleton_matrix ,同时返回骨架是否归0化 #Skeleton_matrix, valid_skel = Extract_feature_UNnormalized(smp, used_joints, 1, smp.getNumFrames()) time_tic = time.time() Feature = Extract_feature_Realtime(Skeleton_matrix, self.njoints) Feature_normalized = normalize(Feature, self.Mean1, self.Std1) 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) return log_observ_likelihood
if file_count<800: print("\t Processing file " + file) # Create the object to access the sample smp=GestureSample(os.path.join(data,file)) # ############################################### # USE Ground Truth information to learn the model # ############################################### # Get the list of actions for this frame gesturesList=smp.getGestures() frame_num = smp.getNumFrames() Feature_Array = np.zeros(shape = (frame_num , (njoints*(njoints-1)/2 + njoints**2)*3),dtype=np.float32) # Target = np.zeros( shape=(frame_num, target_category), dtype=np.uint8) #feature generate Skeleton_matrix, valid_skel = Extract_feature_UNnormalized(smp,used_joints, 1, frame_num) Feature_Array = Extract_feature_Realtime(Skeleton_matrix, njoints) Feature_all.append(Feature_Array) #target generate labels = np.zeros(frame_num, np.uint8) for row in gesturesList: labels[int(row[1])-1:int(row[2])-1] = int(row[0]) Target_all.append(labels) del smp # save the skeleton file: import cPickle as pickle f = open('Feature_train_realtime.pkl','wb')
def generate_eigenjoint(feature_name='sk_eigenjoint_nor_528', labels_name='labels_raw'): # Data folder (Training data) print("Extracting the training files") data = os.path.join("E:\\program\\Chalearn\\rawdata\\train\\") target_dir = 'E:\\program\\Chalearn\\Chalearn_LSTM\\target\\' # Get the list of training samples samples = os.listdir(data) output_dir = 'E:\\program\\Chalearn\\Chalearn_LSTM\\feature\\' + feature_name if not os.path.exists(output_dir): os.makedirs(output_dir) used_joints = [ 'ElbowLeft', 'WristLeft', 'ShoulderLeft', 'HandLeft', 'ElbowRight', 'WristRight', 'ShoulderRight', 'HandRight', 'Head', 'Spine', 'HipCenter' ] njoints = len(used_joints) f = open('SK_normalization.pkl', 'r') normal_params = pickle.load(f) f.close() Mean = normal_params['Mean1'] Std = normal_params['Std1'] count = 0 # target_category = 21 Target_all = [] #Feature_all = numpy.zeros(shape=(400000, (njoints*(njoints-1)/2 + njoints**2)*3),dtype=numpy.float32) for file_count, file in enumerate(samples): if int(file[-8:-4]) != 417 and int(file[-8:-4]) != 675: print("\t Processing file " + file) # Create the object to access the sample smp = GestureSample(os.path.join(data, file)) # ############################################### # USE Ground Truth information to learn the model # ############################################### # Get the list of actions for this frame gesturesList = smp.getGestures() frame_num = smp.getNumFrames() Feature_Array = np.zeros( shape=(frame_num, (njoints * (njoints - 1) / 2 + njoints**2) * 3), dtype=np.float32) # Target = np.zeros( shape=(frame_num, target_category), dtype=np.uint8) #feature generate Skeleton_matrix, valid_skel = Extract_feature_UNnormalized( smp, used_joints, 1, frame_num) Feature_Array = Extract_feature_Realtime(Skeleton_matrix, njoints) Feature_Array = normalize(Feature_Array, Mean, Std) add_ = Feature_Array[-1].reshape((1, Feature_Array.shape[1])) Feature_Array = np.concatenate((Feature_Array, add_), axis=0) #save sample sk features output_name = '%04d.npy' % count count += 1 np.save(os.path.join(output_dir, output_name), Feature_Array) #target generate labels = np.zeros(frame_num, np.uint8) for row in gesturesList: labels[int(row[1]) - 1:int(row[2]) - 1] = int(row[0]) Target_all.append(labels) del smp np.save(target_dir + '%s.npy' % labels_name, Target_all)
# Iterate for each action in this sample for gesture in gesturesList: #对于每个手势数据 # Get the gesture ID, and start and end frames for the gesture gestureID,startFrame,endFrame=gesture#每个手势包含了 手势的ID , 开始结束帧的序号。 Skeleton_matrix, valid_skel = Extract_feature_UNnormalized(smp, used_joints, startFrame, endFrame) #骨架的节点坐标矩阵。 是否骨架归一化了(中心位置) # to see we actually detect a skeleton: if not valid_skel: print "No detected Skeleton: ", gestureID else: ### extract the features according to the CVPR2014 paper Feature = Extract_feature_Realtime(Skeleton_matrix, njoints)##提取特征,这里只有当前帧的坐标差, 和前后帧坐标差 FC CC Target = numpy.zeros( shape=(Feature.shape[0], STATE_NO*20+1)) #hmm中20个动作 每个都有10个隐含状态。 fr_no = Feature.shape[0]#有多少个特征值 for i in range(STATE_NO): #HMM states force alignment 这里是强制将动作帧分成10份,每个作为一个状态 begin_fr = numpy.round(fr_no* i /STATE_NO) + 1 end_fr = numpy.round( fr_no*(i+1) /STATE_NO) #print "begin: %d, end: %d"%(begin_fr-1, end_fr) seg_length=end_fr-begin_fr + 1 targets = numpy.zeros( shape =(STATE_NO*20+1,1))#201 行,1列 的结果矩阵 targets[ i + STATE_NO*(gestureID-1)] = 1#对应这个动作*20+隐含状态序号==》标签的全局序号赋值为1 begin_frame = count end_frame = count+seg_length Feature_all[begin_frame:end_frame,:] = Feature[begin_fr-1:end_fr,:]#将特征拼接起来