-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval_appa_real.py
94 lines (76 loc) · 3.01 KB
/
eval_appa_real.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Evaluate our model on APPA-real dataset."""
import os
import glob
import argparse
from sys import argv
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import load_model
from PIL import Image
# Fix tensorflow bug on rtx card
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
tf.compat.v1.Session(config=config)
def parse_args(argv):
"""Parse command line arguments."""
parser = argparse.ArgumentParser()
parser.add_argument('--model_path', type=str,
help='Path to trained model.')
parser.add_argument('--image_size', type=int, default=224,
help='The input image size.')
parser.add_argument('--data_dir', type=str,
help='Path to appa-real directory.')
parser.add_argument('--batch_size', type=int, default=32,
help='Evaluation batch size.')
return parser.parse_args(argv)
def main(args):
if args.model_path is None and not os.path.exists(args.model_path):
print('Not found model path.')
return
image_size = args.image_size
data_dir = args.data_dir
batch_size = args.batch_size
# Load image paths
if data_dir is None:
print('Not found data dir.')
return
image_dir = os.path.join(data_dir, 'appa-real-release')
if not os.path.exists(image_dir):
print('Not found image dir.')
return
valid_image_dir = os.path.join(image_dir, 'valid')
gt_valid_path = os.path.join(image_dir, 'gt_avg_valid.csv')
image_paths = glob.glob(os.path.join(valid_image_dir, '*_face.jpg'))
# Load the model and run prediction
model = load_model(args.model_path)
faces = np.zeros((batch_size, image_size, image_size, 3))
ages = []
image_names = []
from matplotlib import pyplot as plt
for i, image_path in enumerate(image_paths):
image_names.append(os.path.basename(image_path)[:-9])
image = Image.open(image_path).resize(size=(image_size, image_size))
plt.imshow(np.array(image))
plt.show()
break
image_np = np.array(image, dtype='float32')
image_np /= 255.
faces[i%batch_size] = image_np
if (i + 1) % batch_size == 0 or i == len(image_paths) - 1:
y_pred = model.predict(faces)
classes = np.arange(0, 101).reshape(-1, 1)
age_pred = y_pred[0].dot(classes).flatten()
ages += list(age_pred)
# Compute mean absolute error
name2age = {image_name: age for image_name, age in zip(image_names, ages)}
appa_abs_error = 0.
real_abs_error = 0.
df = pd.read_csv(gt_valid_path)
for i, row in df.iterrows():
appa_abs_error += abs(name2age[row.file_name] - row.apparent_age_avg)
real_abs_error += abs(name2age[row.file_name] - row.real_age)
print('MAE apparent: {}'.format(appa_abs_error / len(image_names)))
print('MAE real: {}'.format(real_abs_error / len(image_names)))
if __name__ == '__main__':
main(parse_args(argv[1:]))