diff --git a/.DS_Store b/.DS_Store index 54e3650..0f73a22 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/code/.DS_Store b/code/.DS_Store new file mode 100644 index 0000000..450e09f Binary files /dev/null and b/code/.DS_Store differ diff --git a/code/best_mem.pickle b/code/best_mem.pickle deleted file mode 100755 index 77d51ce..0000000 Binary files a/code/best_mem.pickle and /dev/null differ diff --git a/code/open_mem.py b/code/open_mem.py deleted file mode 100644 index 7686505..0000000 --- a/code/open_mem.py +++ /dev/null @@ -1,121 +0,0 @@ -#%% -import pickle -import os -import torch -import clip -import time -import pickle -import random -import argparse -import torch.nn as nn -import torch.optim as optim -from PIL import Image - -from torch.utils.data import DataLoader - -from config import * -from dataset import * -from models import * - -device = "cuda" if torch.cuda.is_available() else "cpu" - -path = 'best_mem.pickle' - -with open(path, 'rb') as f: - memory = pickle.load(f) -#%% -in_path = '/Users/filippomerlo/Desktop/Datasets/SOLA' -source = 'train' -memory = memory -in_base = bn_train -types = tyimgs -dic = dic_train_logical -vocab = all_vocabs -# %% -def my_clip_evaluation(in_path, source, memory, in_base, types, dic, vocab): - - with torch.no_grad(): - # get vocab dictionary - if source == 'train': - dic = dic_test - else: - dic = dic_train - - # get dataset - clip_model, clip_preprocess = clip.load("ViT-B/32", device=device) - dt = MyDataset(in_path, source, in_base, types, dic, vocab, - clip_preprocessor=clip_preprocess) - data_loader = DataLoader(dt, batch_size=132, shuffle=True) - - top3 = 0 - top3_color = 0 - top3_material = 0 - top3_shape = 0 - tot_num = 0 - i = 0 - #for base_is, images in data_loader: # labels (one hot), images (clip embs) - base_is, images = next(iter(data_loader)) - # Prepare the inputs - images = images.to(device) - ans = [] - rel = [] - batch_size_i = len(base_is) - - # go through memory - for label in vocab: # select a label es 'red' - if label not in memory.keys(): - ans.append(torch.full((batch_size_i, 1), 1000.0).squeeze(1)) - continue - - # load model - model = CLIP_AE_Encode(hidden_dim_clip, latent_dim, isAE=False) - model.load_state_dict(memory[label]['model']) # load weights corresponding to red - model.to(device) - model.eval() # freeze - - # load centroid - centroid_i = memory[label]['centroid'].to(device) - centroid_i = centroid_i.repeat(batch_size_i, 1) - - # compute stats - z = model(clip_model, images).squeeze(0) - disi = ((z - centroid_i)**2).mean(dim=1) - ans.append(disi.detach().to('cpu')) - - # get top3 indices - ans = torch.stack(ans, dim=1) - values, indices = ans.topk(3, largest=False) - _, indices_lb = base_is.topk(3) - # base_is = [00001000000010000001] - # indices_lb [5,12,19] - indices_lb, _ = torch.sort(indices_lb) - - # calculate stats - tot_num += len(indices) - for bi in range(len(indices)): - ci = 0 - mi = 0 - si = 0 - print('***',indices[bi],'***') - if indices_lb[bi][0] in indices[bi]: - print(indices_lb[bi][0]) - ci = 1 - if indices_lb[bi][1] in indices[bi]: - print(indices_lb[bi][1]) - mi = 1 - if indices_lb[bi][2] in indices[bi]: - print(indices_lb[bi][2]) - si = 1 - - top3_color += ci - top3_material += mi - top3_shape += si - if (ci == 1) and (mi == 1) and (si == 1): - top3 += 1 - - print(tot_num, top3_color/tot_num, top3_material/tot_num, - top3_shape/tot_num, top3/tot_num) - return top3/tot_num - -n = my_clip_evaluation(in_path, source, memory, in_base, types, dic, vocab) -print(n) \ No newline at end of file diff --git a/code/try.py b/code/try.py deleted file mode 100644 index 1f99422..0000000 --- a/code/try.py +++ /dev/null @@ -1,40 +0,0 @@ -#%% -from dataset import * -from config import * - -from torch.utils.data import DataLoader -from pprint import pprint - -import matplotlib.pyplot as plt - -in_path = '/Users/filippomerlo/Desktop/Datasets/SOLA' -source = 'train' -in_base = bn_train -types = tyimgs -dic = dic_train_logical -vocab = all_vocabs - -dt = MyDataset(in_path, source, in_base, types, dic, vocab) -data_loader = DataLoader(dt, batch_size=132, shuffle=True) - -#%% -train_labels, train_features = next(iter(data_loader)) -print(f"Feature batch shape: {train_features.size()}") -print(f"Labels batch shape: {train_labels.size()}") -pprint(train_features) - -#%% Class Methods -#dt.__len__() -a,b = dt.__getitem__(1240) -print(a) -#%% NEW Functions -# - With attributes pairing between positive and negative samples - -attr = 'color' -lesson = 'red' -names_sim, images_sim, names_dif, images_dif = dt.get_paired_batches(attr,lesson) - -for i,n in enumerate(names_sim): - print('**********',i,'**********') - print(n,'\n',names_dif[i]) -