forked from marian42/butterflies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_latent_codes.py
32 lines (22 loc) · 939 Bytes
/
create_latent_codes.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
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
import numpy as np
from autoencoder import Autoencoder, LATENT_CODE_SIZE
from collections import deque
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
AUTOENCODER_FILENAME = 'trained_models/autoencoder.to'
from image_loader import ImageDataset
dataset = ImageDataset()
BATCH_SIZE = 256
data_loader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4)
autoencoder = Autoencoder()
autoencoder.load_state_dict(torch.load(AUTOENCODER_FILENAME))
latent_codes = np.zeros((len(dataset), LATENT_CODE_SIZE), dtype=np.float32)
position = 0
with torch.no_grad():
for batch in tqdm(data_loader):
current = autoencoder.encode(batch.to(device))
latent_codes[position:position+current.shape[0], :] = current.cpu().numpy()
position += current.shape[0]
np.save('data/latent_codes.npy', latent_codes)