-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
27 lines (19 loc) · 840 Bytes
/
utility.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
#Functions to load and print datasets (images)
from dependencies import json, Image, transforms
def parse_json_categories(file_name):
with open(file_name, 'r') as f:
cat_to_name = json.load(f)
return cat_to_name
def process_image(image_path, mean, std):
''' Scales, crops, and normalizes a PIL image for a PyTorch model,
returns an Numpy array
'''
# SOLVING BY TORCHVISION.TRANSFORMS and PIL
image = Image.open(image_path)
transform = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean,std)])
image = transform(image)
np_image = image.numpy()
return np_image