-
Notifications
You must be signed in to change notification settings - Fork 9
/
pytorch_predict.py
48 lines (32 loc) · 1.41 KB
/
pytorch_predict.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
#predict the beauty score of a face on an image
import argparse
from PIL import Image
import torch
from torchvision import transforms
def image_loader(path_image):
"""load image, returns cuda tensor"""
image = Image.open(path_image)
image = loader(image).float()
image = image.view(1, *image.shape)
return image.to(device) #assumes that you're using GPU
def predict(path_model, image, device):
model = torch.load(path_model, map_location=torch.device(device))
model = model.to(device)
model.eval()
result = model(image)
return result
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--image_path', type=str, help='image file name',
default = 'inference_samples/girl.jpg' )
parser.add_argument('--model_path', type=str, help='pytorch model file name',
default = 'pytorch_trained_models/densenet_MSE_Adam_3_dropouts_nocrop.pht' )
args = parser.parse_args()
image_path = args.image_path
model_path = args.model_path
imsize = (256, 256)
loader = transforms.Compose([transforms.Resize(imsize), transforms.ToTensor()])
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
image = image_loader(image_path)
score = predict(model_path, image, device)
print('Beauty score: {:.2f}'.format(score.item()))