-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
195 changed files
with
34,866 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.optim as optim | ||
import torch.utils.data as data | ||
import torch.nn.functional as F | ||
from torch.utils.data.dataset import Dataset | ||
import torch.utils.data as data | ||
import scipy.io | ||
import numpy as np | ||
from tqdm import tqdm | ||
|
||
def getScalars(arr): | ||
temp = np.zeros(len(arr),dtype=int) | ||
for i,a in enumerate(arr): | ||
temp[i] = a.item() | ||
return temp | ||
|
||
|
||
class data_loader(Dataset): | ||
""" | ||
docstring for CUB | ||
""" | ||
def __init__(self, transform, root, split='train', device='cpu'): | ||
path_features = root + 'featuresT.mat' # 'res101.mat' | ||
path_att_splits = root + 'labelsT.mat' # 'att_splits.mat' | ||
self.res101 = scipy.io.loadmat(path_features) | ||
att_splits = scipy.io.loadmat(path_att_splits) | ||
|
||
self.scale = transform | ||
|
||
self.labels, self.feats, self.sig = self.get_data(att_splits, split) | ||
assert len(self.labels) == len(self.feats) == len(self.sig) | ||
if len(self.feats) == 0: | ||
raise(RuntimeError('Found zero feats in the directory: ' + root)) | ||
self.labels = getScalars(self.labels) | ||
self.feats_ = torch.from_numpy(self.feats).float().to(device) | ||
self.labels_ = torch.from_numpy(self.labels).long().to(device) | ||
self.sig_ = torch.from_numpy(self.sig).float().to(device) | ||
|
||
def __getitem__(self, index): | ||
# index = np.random.randint(1,50) | ||
x = self.feats_[index, :] | ||
sig = self.sig_[index, :] | ||
y = self.labels_[index] | ||
return x, y, sig | ||
|
||
def __get_feats_per_class__(self, index): | ||
if index in np.unique(self.labels_): | ||
idx = np.where(self.labels_ == index) | ||
return self.feats_[idx[0], :] | ||
|
||
def __num_classes__(self): | ||
return np.unique(self.labels_) | ||
|
||
def __get_att_len__(self): | ||
len_sig = self.sig.shape[1] | ||
return len_sig | ||
|
||
def __get_len__(self): | ||
len_feats = self.feats.shape[1] | ||
return len_feats | ||
|
||
def __totalClasses__(self): | ||
return len(np.unique(self.res101['labels']).tolist()) | ||
|
||
def __attributeVector__(self): | ||
return self.signature[:,np.unique(self.labels_)-1].transpose(), np.unique(self.labels_) | ||
|
||
def __Test_Features_Labels__(self): | ||
return self.feats_, self.labels_ | ||
|
||
def check_unique_labels(self, labels, att_splits): | ||
train_val_loc = 'train_val_loc' | ||
train_loc = 'train_loc' | ||
val_loc = 'val_loc' | ||
test_loc = 'test_unseen_loc' | ||
|
||
self.labels_train = labels[np.squeeze(att_splits[train_loc]-1)] | ||
self.labels_val = labels[np.squeeze(att_splits[val_loc]-1)] | ||
self.labels_train_val = labels[np.squeeze(att_splits[train_val_loc]-1)] | ||
self.labels_test = labels[np.squeeze(att_splits[test_loc]-1)] | ||
|
||
self.train_labels_seen = np.unique(self.labels_train) | ||
self.val_labels_unseen = np.unique(self.labels_val) | ||
self.train_val_labels_seen = np.unique(self.labels_train_val) | ||
self.test_labels_unseen = np.unique(self.labels_test) | ||
|
||
# print('Number of overlapping classes between train and val:', | ||
# len(set(self.train_labels_seen).intersection(set(self.val_labels_unseen)))) | ||
# print('Number of overlapping classes between train_val and test:', | ||
# len(set(self.train_val_labels_seen).intersection(set(self.test_labels_unseen)))) | ||
|
||
def __len__(self): | ||
return self.feats.shape[0] | ||
|
||
def get_data(self, att_splits, split): | ||
labels = self.res101['labels'] | ||
x_features = self.res101['features'] | ||
self.signature = att_splits['att'] | ||
|
||
# self.check_unique_labels(labels, att_splits) | ||
if split == 'train_val': | ||
loc = 'train_val_loc' | ||
elif split == 'train': | ||
loc = 'train_loc' | ||
elif split == 'val': | ||
loc = 'val_loc' | ||
elif split == 'test_seen': | ||
loc = 'test_seen_loc' | ||
else: | ||
loc = 'test_unseen_loc' | ||
|
||
labels_loc = labels[np.squeeze(att_splits[loc]-1)] | ||
feat_vec = np.transpose(x_features[:, np.squeeze(att_splits[loc]-1)]) | ||
print(feat_vec.shape) | ||
unique_labels = np.unique(labels_loc) | ||
sig_vec = np.zeros((labels_loc.shape[0], self.signature.shape[0])) | ||
labels_list = np.squeeze(labels_loc).tolist() | ||
for i, idx in enumerate(labels_list): | ||
sig_vec[i, :] = self.signature[:, idx-1] | ||
|
||
self.scale.fit_transform(feat_vec) | ||
labels_loc_ = np.int64(labels_loc) | ||
|
||
return labels_loc_, feat_vec, sig_vec | ||
|
||
|
||
class classifier_data_loader(Dataset): | ||
""" | ||
docstring for classifier_dataloader | ||
""" | ||
def __init__(self, features_img, labels, device): | ||
self.labels = labels.long().to(device) | ||
self.feats = features_img.float().to(device) | ||
|
||
def __getitem__(self, index): | ||
X = self.feats[index, :] | ||
y = self.labels[index] # -1 # for NLL loss | ||
return X, y | ||
|
||
def __len__(self): | ||
return len(self.labels) | ||
|
||
def __targetClasses__(self): | ||
return np.unique(self.labels) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import matplotlib | ||
import matplotlib.pyplot as plt | ||
from matplotlib import gridspec | ||
import numpy as np | ||
from torch.autograd import Variable | ||
import torch | ||
|
||
|
||
def get_X_batch(data_loader, params, size=None): | ||
if size is None: | ||
size = data_loader.batch_size | ||
for X, target in data_loader: | ||
break | ||
|
||
train_batch_size = params['train_batch_size'] | ||
X_dim = params['X_dim'] | ||
cuda = params['cuda'] | ||
|
||
X = X * 0.3081 + 0.1307 | ||
|
||
X = X[:size] | ||
target = target[:size] | ||
|
||
X.resize_(train_batch_size, X_dim) | ||
X, target = Variable(X), Variable(target) | ||
|
||
if cuda: | ||
X, target = X.cuda(), target.cuda() | ||
|
||
return X | ||
|
||
|
||
def create_reconstruction(Q, P, data_loader, params): | ||
Q.eval() | ||
P.eval() | ||
X = get_X_batch(data_loader, params, size=1) | ||
|
||
z_c, z_g = Q(X) | ||
z = torch.cat((z_c, z_g), 1) | ||
x = P(z) | ||
|
||
img_orig = np.array(X[0].data.tolist()).reshape(28, 28) | ||
img_rec = np.array(x[0].data.tolist()).reshape(28, 28) | ||
plt.subplot(1, 2, 1) | ||
plt.imshow(img_orig) | ||
plt.subplot(1, 2, 2) | ||
plt.imshow(img_rec) | ||
|
||
|
||
def grid_plot(Q, P, data_loader, params): | ||
Q.eval() | ||
P.eval() | ||
X = get_X_batch(data_loader, params, size=10) | ||
_, z_g = Q(X) | ||
|
||
n_classes = params['n_classes'] | ||
cuda = params['cuda'] | ||
z_dim = params['z_dim'] | ||
|
||
z_cat = np.arange(0, n_classes) | ||
z_cat = np.eye(n_classes)[z_cat].astype('float32') | ||
z_cat = torch.from_numpy(z_cat) | ||
z_cat = Variable(z_cat) | ||
if cuda: | ||
z_cat = z_cat.cuda() | ||
|
||
nx, ny = 5, n_classes | ||
plt.subplot() | ||
gs = gridspec.GridSpec(nx, ny, hspace=0.05, wspace=0.05) | ||
|
||
for i, g in enumerate(gs): | ||
z_gauss = z_g[i / ny].resize(1, z_dim) | ||
z_gauss0 = z_g[i / ny].resize(1, z_dim) | ||
|
||
for _ in range(n_classes - 1): | ||
z_gauss = torch.cat((z_gauss, z_gauss0), 0) | ||
|
||
z = torch.cat((z_cat, z_gauss), 1) | ||
x = P(z) | ||
|
||
ax = plt.subplot(g) | ||
img = np.array(x[i % ny].data.tolist()).reshape(28, 28) | ||
ax.imshow(img, ) | ||
ax.set_xticks([]) | ||
ax.set_yticks([]) | ||
ax.set_aspect('auto') | ||
|
||
|
||
def grid_plot2d(Q, P, data_loader, params): | ||
Q.eval() | ||
P.eval() | ||
|
||
cuda = params['cuda'] | ||
|
||
z1 = Variable(torch.from_numpy(np.arange(-10, 10, 1.5).astype('float32'))) | ||
z2 = Variable(torch.from_numpy(np.arange(-10, 10, 1.5).astype('float32'))) | ||
if cuda: | ||
z1, z2 = z1.cuda(), z2.cuda() | ||
|
||
nx, ny = len(z1), len(z2) | ||
plt.subplot() | ||
gs = gridspec.GridSpec(nx, ny, hspace=0.05, wspace=0.05) | ||
|
||
for i, g in enumerate(gs): | ||
z = torch.cat((z1[i / ny], z2[i % nx])).resize(1, 2) | ||
x = P(z) | ||
|
||
ax = plt.subplot(g) | ||
img = np.array(x.data.tolist()).reshape(28, 28) | ||
ax.imshow(img, ) | ||
ax.set_xticks([]) | ||
ax.set_yticks([]) | ||
ax.set_aspect('auto') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.optim as optim | ||
import torch.utils.data as data | ||
import torch.nn.functional as F | ||
from torch.utils.data.dataset import Dataset | ||
import torch.utils.data as data | ||
import scipy.io | ||
import numpy as np | ||
from tqdm import tqdm | ||
|
||
|
||
class dataloader(Dataset): | ||
"""docstring for CUB""" | ||
def __init__(self,transform, root='/content/drive/My Drive/computer_vision/datasets/CUB/', split='train', device='cpu'): | ||
path_features = root + 'featuresT.mat'#'res101.mat' | ||
path_att_splits = root + 'labelsT.mat'#'att_splits.mat' | ||
self.res101 = scipy.io.loadmat(path_features) | ||
att_splits = scipy.io.loadmat(path_att_splits) | ||
|
||
self.scaler = transform | ||
|
||
self.labels, self.feats, self.sig = self.get_data(att_splits, split) | ||
assert len(self.labels) == len(self.feats) == len(self.sig) | ||
if len(self.feats) == 0: | ||
raise(RuntimeError("Found zero feats in the directory: "+ root)) | ||
|
||
self.feats_ = torch.from_numpy(self.feats).float().to(device) | ||
self.labels_ = torch.from_numpy(self.labels).long().to(device) | ||
self.sig_ = torch.from_numpy(self.sig).float().to(device) | ||
|
||
def __getitem__(self, index): | ||
#index = np.random.randint(1,50) | ||
x = self.feats_[index,:] | ||
sig = self.sig_[index,:] | ||
y = self.labels_[index] | ||
return x,y,sig | ||
|
||
def __get_perclass_feats__(self, index): | ||
if index in np.unique(self.labels_): | ||
idx = np.where(self.labels_==index) | ||
return self.feats_[idx[0],:] | ||
|
||
def __NumClasses__(self): | ||
return np.unique(self.labels_) | ||
|
||
def __get_attlen__(self): | ||
len_sig = self.sig.shape[1] | ||
return len_sig | ||
|
||
def __getlen__(self): | ||
len_feats = self.feats.shape[1] | ||
return len_feats | ||
|
||
def __totalClasses__(self): | ||
return len(np.unique(self.res101['labels']).tolist()) | ||
|
||
def __attributeVector__(self): | ||
return self.signature[:,np.unique(self.labels_)-1].transpose(), np.unique(self.labels_) | ||
|
||
def __Test_Features_Labels__(self): | ||
return self.feats_, self.labels_ | ||
|
||
|
||
def check_unique_labels(self, labels, att_splits): | ||
trainval_loc = 'trainval_loc' | ||
train_loc = 'train_loc' | ||
val_loc = 'val_loc' | ||
test_loc = 'test_unseen_loc' | ||
|
||
self.labels_train = labels[np.squeeze(att_splits[train_loc]-1)] | ||
self.labels_val = labels[np.squeeze(att_splits[val_loc]-1)] | ||
self.labels_trainval = labels[np.squeeze(att_splits[trainval_loc]-1)] | ||
self.labels_test = labels[np.squeeze(att_splits[test_loc]-1)] | ||
|
||
self.train_labels_seen = np.unique(self.labels_train) | ||
self.val_labels_unseen = np.unique(self.labels_val) | ||
self.trainval_labels_seen = np.unique(self.labels_trainval) | ||
self.test_labels_unseen = np.unique(self.labels_test) | ||
|
||
#print("Number of overlapping classes between train and val:", | ||
#len(set(self.train_labels_seen).intersection(set(self.val_labels_unseen)))) | ||
#print("Number of overlapping classes between trainval and test:", | ||
#len(set(self.trainval_labels_seen).intersection(set(self.test_labels_unseen)))) | ||
|
||
def __len__(self): | ||
return self.feats.shape[0] | ||
|
||
def get_data(self, att_splits, split): | ||
labels = self.res101['labels'] | ||
X_features = self.res101['features'] | ||
self.signature = att_splits['att'] | ||
|
||
# self.check_unique_labels(labels, att_splits) | ||
if split == 'trainval': | ||
loc = 'trainval_loc' | ||
elif split == 'train': | ||
loc = 'train_loc' | ||
elif split == 'val': | ||
loc = 'val_loc' | ||
elif split == 'test_seen': | ||
loc = 'test_seen_loc' | ||
else: | ||
loc = 'test_unseen_loc' | ||
|
||
labels_loc = labels[np.squeeze(att_splits[loc]-1)] | ||
feat_vec = np.transpose(X_features[:,np.squeeze(att_splits[loc]-1)]) | ||
print(feat_vec.shape) | ||
|
||
unique_labels = np.unique(labels_loc) | ||
sig_vec = np.zeros((labels_loc.shape[0],self.signature.shape[0])) | ||
labels_list = np.squeeze(labels_loc).tolist() | ||
for i, idx in enumerate(labels_list): | ||
sig_vec[i,:] = self.signature[:,idx-1] | ||
|
||
self.scaler.fit_transform(feat_vec) | ||
|
||
labels_loc_ = np.int64(labels_loc) | ||
|
||
return labels_loc_, feat_vec, sig_vec | ||
|
||
class classifier_dataloader(Dataset): | ||
"""docstring for classifier_dataloader""" | ||
def __init__(self, features_img, labels, device): | ||
self.labels = labels.long().to(device) | ||
self.feats = features_img.float().to(device) | ||
|
||
def __getitem__(self, index): | ||
X = self.feats[index, :] | ||
y = self.labels[index]#-1 #for NLLL loss | ||
return X, y | ||
|
||
def __len__(self): | ||
return len(self.labels) | ||
|
||
def __targetClasses__(self): | ||
return np.unique(self.labels) |
Oops, something went wrong.