-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinference.py
More file actions
46 lines (39 loc) · 1.31 KB
/
inference.py
File metadata and controls
46 lines (39 loc) · 1.31 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
import torch
from src.AutoEncoder import AutoEncoder
from src.Discriminator import Discriminator
def inference(
autoencoder: AutoEncoder,
discriminator: Discriminator,
scaled_image: torch.tensor,
attributes: torch.tensor,
device: torch.device,
) -> torch.tensor:
"""
Inference function for the autoencoder and the discriminator
"""
# send model, images and attributes to the device ( GPU if available )
autoencoder.to(device)
discriminator.to(device)
scaled_image, attributes = scaled_image.to(device), attributes.to(device)
# Generate the latent space and the decoded images (outputs from the autoencoder)
latent, decoded = autoencoder(scaled_image, attributes)
# Generate the prediction of the discriminator
y_pred = discriminator(latent)
return decoded, y_pred
decoded, y_pred = inference(
autoencoder=AutoEncoder(),
discriminator=Discriminator(),
scaled_image=torch.rand((1, 3, 256, 256)),
attributes=torch.rand((1, 40)),
device=torch.device("cuda" if torch.cuda.is_available() else "cpu"),
)
assert decoded.shape == (
1,
3,
256,
256,
), "The inference function does not work properly. Shape issue for decoded"
assert y_pred.shape == (
1,
40,
), "The inference function does not work properly. Shape issue for y_pred."