File size: 937 Bytes
de3c81a |
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 |
import tensorflow as tf
from pathlib import Path
import numpy as np
from PIL import Image
model_path = 'saved_model_age_regressor'
img_path = Path('data/UTKFace/53_1_1_20170110122449716.jpg.chip.jpg')
print('Model path:', model_path, flush=True)
print('Image path:', img_path, flush=True)
m = tf.keras.models.load_model(model_path, compile=False)
print('Loaded model type:', type(m), flush=True)
try:
m.summary()
except Exception as e:
print('model.summary failed:', e, flush=True)
img = Image.open(img_path).convert('RGB').resize((224,224))
arr = np.array(img, dtype=np.float32)/255.0
x = np.expand_dims(arr, 0)
print('Input shape:', x.shape, flush=True)
pred = m.predict(x)
print('Raw prediction output:', pred, 'shape:', getattr(pred, 'shape', None), flush=True)
try:
print('Predicted age:', float(pred.flatten()[0]), flush=True)
except Exception as e:
print('Error converting prediction to float:', e, flush=True)
|