def test_rf_batch(i, train_range, root_dir):
  data_dir = root_dir + 'person_' + str(i).zfill(2) + '/'
  test_data = get_data_ensemble(data_dir, test_batch)
  print 'Loading test batch from', data_dir

  pred_prob_ensemble = np.zeros((len(test_data['labels']), num_joints))
  for j in train_range:
    rf = joblib.load(root_dir + 'models/rf_' + str(j).zfill(2) + '.pkl')
    print 'Model loaded for', root_dir + 'models/rf_' + str(j).zfill(2) + '.pkl'
    pred_prob_ensemble += rf.predict_proba(test_data['features'])

  pred_label = np.argmax(pred_prob_ensemble, axis=1)
  np.save(data_dir+'pred_prob.npy', pred_prob_ensemble)
  np.save(data_dir+'pred_label.npy', pred_label)

  avg_accuracy = util.get_cm_acc(test_data['labels'].astype(int), pred_label)
  print 'MAP for person', i, ':', avg_accuracy

  accuracy = 0.0
  accClass = np.zeros((num_joints,2))
  for j in range(pred_label.shape[0]):
    accClass[int(test_data['labels'][j])][1] += 1
    if pred_label[j] == test_data['labels'][j]:
      accClass[pred_label[j]][0] += 1
      accuracy += 1
  accuracy /= float(pred_label.shape[0])

  for j in range(num_joints):
    print 'Person', i, 'Class', j, ':', accClass[j][0], '/', accClass[j][1], accClass[j][0]/accClass[j][1]
  print 'Person', i, 'Total accuracy:', accuracy
Esempio n. 2
0
  rf = RandomForestClassifier(n_estimators=3, criterion='entropy', max_depth=20)
  rf.fit(train_data['features'], train_data['labels'])

  del train_data

  # prediction
  pred_label = rf.predict(test_data['features'])
  pred_prob = rf.predict_proba(test_data['features'])
  ensemble_prob += pred_prob

  # accuracy
  score = rf.score(test_data['features'], test_data['labels'])
  print('Ensemble ' + str(i) +' Score: ' + str(score))

ensemble_label = np.argmax(ensemble_prob, axis=1)
avg_accuracy = util.get_cm_acc(test_data['labels'].astype(int), ensemble_label)
print('Avg accuracy: ' + str(avg_accuracy))

pred_label_path = root_dir + 'out/ensemble2_label_'+str(ensemble)+'_'+str(num_train_images)+'.npy'
pred_prob_path = root_dir + 'out/ensemble2_prob_'+str(ensemble)+'_'+str(num_train_images)+'.npy'
np.save(pred_label_path, ensemble_label)
np.save(pred_prob_path, ensemble_prob)

accuracy = 0.0
accClass = np.zeros((numJoints,2))
for i in range(0,ensemble_label.shape[0]):
  accClass[int(test_data['labels'][i])][1] += 1
  if ensemble_label[i] == test_data['labels'][i]:
    accClass[ensemble_label[i]][0] += 1
    accuracy += 1
accuracy /= float(ensemble_label.shape[0])