1、get MAE and RMSE values
for i in range(0, len(img_paths)):
img = transform(Image.open(img_paths[i]).convert('RGB')).cuda()
img = img.unsqueeze(0)
h,w = img.shape[2:4]
h_d = int(h/2)
w_d = int(w/2)
img_1 = Variable(img[:,:,:h_d,:w_d].cuda())
img_2 = Variable(img[:,:,:h_d,w_d:].cuda())
img_3 = Variable(img[:,:,h_d:,:w_d].cuda())
img_4 = Variable(img[:,:,h_d:,w_d:].cuda())
density_1 = model(img_1).data.cpu().numpy()
density_2 = model(img_2).data.cpu().numpy()
density_3 = model(img_3).data.cpu().numpy()
density_4 = model(img_4).data.cpu().numpy()
pure_name = os.path.splitext(os.path.basename(img_paths[i]))[0]
gt_file = h5py.File(img_paths[i].replace('.jpg', '.h5').replace('frame_', 'csvs_').replace('frames', 'csvs'), 'r')
groundtruth = np.asarray(gt_file['density'])
pred_sum = density_1.sum()+density_2.sum()+density_3.sum()+density_4.sum()
pred.append(pred_sum)
gt.append(np.sum(groundtruth))
mae = mean_absolute_error(pred,gt)
rmse = np.sqrt(mean_squared_error(pred,gt))
print('MAE: ', mae)
print('RMSE: ', rmse)
2、get density map
for i in range(0, 1):
img = transform(Image.open(img_paths[i]).convert('RGB')).cuda()
img = img.unsqueeze(0)
img = Variable(img.cuda())
density = model(img).data.cpu().numpy()
plt.imshow(density[0][0], cmap=CM.jet)
plt.show()
3、get counting number
for i in range(0, len(imgs)):
img = transform(Image.open(img_paths[i]).convert('RGB')).cuda()
img = img.unsqueeze(0)
img = Variable(img.cuda())
density = model(img).data.cpu().numpy()
pred_sum = density.sum()
if pred_sum < 0.5:
pred_sum = 0
else:
pred_sum = math.ceil(pred_sum)
pred.append(pred_sum)
sum = sum + pred_sum
print(sum)