-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathprocess_features.py
49 lines (38 loc) · 1.45 KB
/
process_features.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import torch
import numpy as np
def inference(loader, model, device):
feature_vector = []
labels_vector = []
for step, (x, y) in enumerate(loader):
x = x.to(device)
# get encoding
with torch.no_grad():
h = model(x)
h = h.squeeze()
h = h.detach()
feature_vector.extend(h.cpu().detach().numpy())
labels_vector.extend(y.numpy())
if step % 5 == 0:
print(f"Step [{step}/{len(loader)}]\t Computing features...")
feature_vector = np.array(feature_vector)
labels_vector = np.array(labels_vector)
print("Features shape {}".format(feature_vector.shape))
return feature_vector, labels_vector
def get_features(model, train_loader, test_loader, device):
train_X, train_y = inference(train_loader, model, device)
test_X, test_y = inference(test_loader, model, device)
return train_X, train_y, test_X, test_y
def create_data_loaders_from_arrays(X_train, y_train, X_test, y_test, batch_size):
train = torch.utils.data.TensorDataset(
torch.from_numpy(X_train), torch.from_numpy(y_train)
)
train_loader = torch.utils.data.DataLoader(
train, batch_size=batch_size, shuffle=False
)
test = torch.utils.data.TensorDataset(
torch.from_numpy(X_test), torch.from_numpy(y_test)
)
test_loader = torch.utils.data.DataLoader(
test, batch_size=batch_size, shuffle=False
)
return train_loader, test_loader