-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpredict_preprocessing.py
More file actions
75 lines (58 loc) · 2.16 KB
/
predict_preprocessing.py
File metadata and controls
75 lines (58 loc) · 2.16 KB
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
# Dependencies
import argparse
import numpy as np
import torch
import matplotlib.pyplot as plt
from torchvision import datasets, transforms, models
from torch import nn, optim
import torch.nn.functional as F
from collections import OrderedDict
import time
from PIL import Image
import matplotlib
from workspace_utils import active_session
# Image Preprocessing
def process_image(image_path):
''' Scales, crops, and normalizes a PIL image for a PyTorch model,
returns an Numpy array
'''
pil_image = Image.open(image_path)
# Process a PIL image for use in a PyTorch model
# Resize the images where the shortest side is 256 pixels, keeping the aspect ratio
width, height = pil_image.size
aspect_ratio = width / height
if aspect_ratio > 1:
pil_image = pil_image.resize((round(aspect_ratio * 256), 256))
else:
pil_image = pil_image.resize((256, round(256 / aspect_ratio)))
# Crop out the center 224x224 portion of the image
width, height = pil_image.size
new_width = 224
new_height = 224
left = (width - new_width)/2
top = (height - new_height)/2
right = (width + new_width)/2
bottom = (height + new_height)/2
pil_image = pil_image.crop((round(left), round(top), round(right), round(bottom)))
# Convert color channels to 0-1
np_image = np.array(pil_image) / 255
# Normalize the image
np_image = (np_image - np.array([0.485, 0.456, 0.406])) / np.array([0.229, 0.224, 0.225])
# Reorder dimensions
np_image = np_image.transpose((2, 0, 1))
return np_image
# Display the original image (cropped)
def imshow(image, ax=None, title=None):
if ax is None:
fig, ax = plt.subplots()
# PyTorch tensors assume the color channel is the first dimension
# but matplotlib assumes is the third dimension
image = image.transpose((1, 2, 0))
# Undo preprocessing
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
image = std * image + mean
# Image needs to be clipped between 0 and 1 or it looks like noise when displayed
image = np.clip(image, 0, 1)
ax.imshow(image)
return ax