def predict_activitiy(index,fold,checkpoint): path_to_dataset = '/scr/ashesh/activity-anticipation/dataset/{0}'.format(fold) path_to_checkpoints = '/scr/ashesh/activity-anticipation/checkpoints/{0}'.format(fold) test_data = cPickle.load(open('{1}/test_data_{0}.pik'.format(index,path_to_dataset))) Y_te = test_data['labels'] X_te = test_data['features'] ground_truth_test_data = cPickle.load(open('{1}/ground_truth_test_data_{0}.pik'.format(index,path_to_dataset))) Y_te_ground_truth = ground_truth_test_data['labels'] rnn = load('{2}/{0}/checkpoint.{1}'.format(index,checkpoint,path_to_checkpoints)) predictions = [] errors = 0 N = 0 for xte,yte,gte in zip(X_te,Y_te,Y_te_ground_truth): prediction = rnn.predict_output(xte,OutputMaxProb) predictions.append(prediction) t = np.nonzero(yte-prediction) ''' print "{0}".format(gte[:,0]) print "{0}".format(yte[:,0]) print "{0}".format(prediction[:,0]) print "" #print t ''' errors += len(t[0]) N += yte.shape[0] #cPickle.dump(predictions,open('{1}/prediction_{0}.pik'.format(index,path_to_dataset),'wb')) print 'error = {0}'.format(errors*1.0/N) return (errors*1.0/N)
def predict_activitiy(index, fold, checkpoint): path_to_dataset = '/scr/ashesh/activity-anticipation/dataset/{0}'.format( fold) path_to_checkpoints = '/scr/ashesh/activity-anticipation/checkpoints/{0}'.format( fold) test_data = cPickle.load( open('{1}/test_data_{0}.pik'.format(index, path_to_dataset))) Y_te = test_data['labels'] X_te = test_data['features'] ground_truth_test_data = cPickle.load( open('{1}/ground_truth_test_data_{0}.pik'.format( index, path_to_dataset))) Y_te_ground_truth = ground_truth_test_data['labels'] rnn = load('{2}/{0}/checkpoint.{1}'.format(index, checkpoint, path_to_checkpoints)) predictions = [] errors = 0 N = 0 for xte, yte, gte in zip(X_te, Y_te, Y_te_ground_truth): prediction = rnn.predict_output(xte, OutputMaxProb) predictions.append(prediction) t = np.nonzero(yte - prediction) ''' print "{0}".format(gte[:,0]) print "{0}".format(yte[:,0]) print "{0}".format(prediction[:,0]) print "" #print t ''' errors += len(t[0]) N += yte.shape[0] #cPickle.dump(predictions,open('{1}/prediction_{0}.pik'.format(index,path_to_dataset),'wb')) print 'error = {0}'.format(errors * 1.0 / N) return (errors * 1.0 / N)
def evaluate(index,fold,checkpoint,model_type='lstm_one_layer',path_to_load_from=''): path_to_dataset = '/scr/ashesh/brain4cars/dataset/{0}'.format(fold) if len(path_to_load_from) > 0: path_to_dataset = path_to_load_from path_to_checkpoints = '/scr/ashesh/brain4cars/checkpoints/{0}'.format(fold) test_data = cPickle.load(open('{1}/test_data_{0}.pik'.format(index,path_to_dataset))) Y_te = test_data['labels'] X_te = test_data['features'] actions = [] if test_data.has_key('actions'): actions = test_data['actions'] else: actions = ['end_action','lchange','rchange','lturn','rturn'] # Prediction rnn = [] if model_type == 'multipleRNNs': if len(path_to_load_from) > 0: rnn = loadMultipleRNNsCombined('{0}/checkpoint.{1}'.format(path_to_load_from,checkpoint)) else: rnn = loadMultipleRNNsCombined('{2}/{0}/checkpoint.{1}'.format(index,checkpoint,path_to_checkpoints)) else: if len(path_to_load_from) > 0: rnn = load('{0}/checkpoint.{1}'.format(path_to_load_from,checkpoint)) else: rnn = load('{2}/{0}/checkpoint.{1}'.format(index,checkpoint,path_to_checkpoints)) predictions = [] errors = 0 N = 0 P = [] Y = [] Time_before_maneuver = [] for xte,yte in zip(X_te,Y_te): inputD = xte.shape[2] road_feature_dimension = 4 prediction = [] if model_type == 'multipleRNNs': prediction = rnn.predict_output([xte[:,:,(inputD-road_feature_dimension):],xte[:,:,:(inputD-road_feature_dimension)]],OutputActionThresh) #[:,:,:(inputD-road_feature_dimension)] else: prediction = rnn.predict_output(xte,OutputActionThresh) #print prediction.T predictions.append(prediction) t = np.nonzero(yte-prediction) # Label 0 is the dummy label. Currently maneuvers are labeled [1..n] prediction = prediction[:,0] actual = yte[:,0] prediction[prediction>0] -= 1 actual[actual>0] -= 1 p,anticipation_time = predictManeuver(prediction,actions) y = actual[-1] P.append(p) Y.append(y) Time_before_maneuver.append(anticipation_time) result = {'actual':y,'prediction':p,'timeseries':list(prediction)} #print result.values() errors += len(t[0]) N += yte.shape[0] P = np.array(P) Y = np.array(Y) Time_before_maneuver = np.array(Time_before_maneuver) [conMat,p_mat,re_mat,time_mat] = confusionMat(P,Y,Time_before_maneuver) return conMat,p_mat,re_mat,time_mat
def load_rnn(index,fold,checkpoint): path_to_dataset = '/scr/ashesh/brain4cars/dataset/{0}'.format(fold) path_to_checkpoints = '/scr/ashesh/brain4cars/checkpoints/{0}'.format(fold) rnn = load('{2}/{0}/checkpoint.{1}'.format(index,checkpoint,path_to_checkpoints)) return rnn
layers = [TemporalInputFeatures(inputD),LSTM('tanh','sigmoid','orthogonal',4,512),softmax(num_classes)] trY = T.lmatrix() # Initializing network rnn = RNN(layers,softmax_loss,trY,1e-3) if not os.path.exists('{1}/{0}/'.format(index,path_to_checkpoints)): os.mkdir('{1}/{0}/'.format(index,path_to_checkpoints)) # Fitting model rnn.fitModel(X_tr,Y_tr,1,'{1}/{0}/'.format(index,path_to_checkpoints),epochs,batch_size,learning_rate_decay,decay_after) else: checkpoint = sys.argv[3] # Prediction rnn = load('{2}/{0}/checkpoint.{1}'.format(index,checkpoint,path_to_checkpoints)) if train_more: rnn.fitModel(X_tr,Y_tr,1,'{1}/{0}/'.format(index,path_to_checkpoints),epochs,batch_size,learning_rate_decay,decay_after) predictions = [] errors = 0 N = 0 for xte,yte in zip(X_te,Y_te): prediction = rnn.predict_output(xte,OutputMaxProb) predictions.append(prediction) t = np.nonzero(yte-prediction) print t errors += len(t[0]) N += yte.shape[0] cPickle.dump(predictions,open('{1}/prediction_{0}.pik'.format(index,path_to_dataset),'wb'))