File size: 1,337 Bytes
fb30cff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from PIL import Image
from torchvision import transforms


# -------------------------------------------------
# Device
# -------------------------------------------------

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


# -------------------------------------------------
# Labels
# -------------------------------------------------

LABELS = [
    "Real",
    "Fake"
]


# -------------------------------------------------
# Image Transform
# -------------------------------------------------

transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]
    )
])


# -------------------------------------------------
# Load Image
# -------------------------------------------------

def load_image(image_file):
    """
    Loads uploaded image as RGB PIL Image.
    """
    image = Image.open(image_file).convert("RGB")
    return image


# -------------------------------------------------
# Preprocess Image
# -------------------------------------------------

def preprocess_image(image):
    """
    Converts PIL image into model input tensor.
    """
    tensor = transform(image)
    tensor = tensor.unsqueeze(0)
    tensor = tensor.to(device)
    return tensor