-
Notifications
You must be signed in to change notification settings - Fork 490
/
Copy pathinference.py
93 lines (61 loc) · 1.79 KB
/
inference.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
#!/usr/bin/env python
# coding: utf-8
#infrence code to detect font styles.
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import os
import pandas as pd
from sklearn.model_selection import train_test_split
import shutil
import time
import copy
from PIL import Image
import glob
filepath = 'mask1_model_resnet50.pth'
model = torch.load(filepath)
class_names = ['with_mask',
'without_mask'
]
def process_image(image):
''' Scales, crops, and normalizes a PIL image for a PyTorch model,
returns an Numpy array
'''
# TODO: Process a PIL image for use in a PyTorch model
pil_image = Image.open(image)
#pil_image = image
image_transforms = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
img = image_transforms(pil_image)
return img
def classify_face(image_path):
device = torch.device("cpu")
img = process_image(image_path)
print('image_processed')
img = img.unsqueeze_(0)
img = img.float()
model.eval()
model.cpu()
output = model(img)
print(output,'##############output###########')
_, predicted = torch.max(output, 1)
print(predicted.data[0],"predicted")
classification1 = predicted.data[0]
index = int(classification1)
print(class_names[index])
return class_names[index]
if __name__ == '__main__':
map_location=torch.device('cpu')
image_path = 'IMG20200402191409_02.jpg'
label = classify_face(image_path)
print("the label is", label)