import numpy as np import torch import torchvision.transforms as transforms from…
- User
import numpy as np import torch import torchvision.transforms as transforms from sklearn.linear_model import LogisticRegression import random
def setup_seed(seed): torch.manual_seed(seed) torch.cuda.manual_seed_all(seed) np.random.seed(seed) random.seed(seed) torch.backends.cudnn.deterministic = True
def test(novel_loader, model, params): iter_num = len(novel_loader) acc_all_LR = [] with torch.no_grad(): for i, (x, _) in enumerate(novel_loader): x_query = x[:, params.n_support:, :, :, :].contiguous().view(params.n_way * params.n_query, *x.size()[2:]).cuda() x_support = x[:, :params.n_support, :, :, :].contiguous().view(params.n_way * params.n_support, *x.size()[2:]).cuda() # (25, 3, 224, 224)
if params.use_random_crops: # Create random crops of the support set crop_support = [] for img in x_support: crops = [] for _ in range(4): transform = transforms.RandomCrop(params.image_size) random_crop = transform(img) crops.append(random_crop) crops.append(img) # Include the complete image from the support set crop_support.extend(crops) crop_support = torch.stack(crop_support) else: crop_support = x_support out_support = model(crop_support) # (way*shot*5, 512) out_query = model(x_query) # (way*query, 512) beta = 0.5 out_support = torch.pow(out_support, beta) out_query = torch.pow(out_query, beta) _, c = out_support.size() out_support_LR_with_GC = out_support.cpu().numpy() out_query_LR_with_GC = out_query.cpu().numpy() y = np.tile(range(params.n_way), params.n_support) y.sort() # Perform logistic regression for each random crop classifiers = [] for _ in range(4): classifier = LogisticRegression(max_iter=1000).fit(X=out_support_LR_with_GC, y=y) classifiers.append(classifier) # Perform logistic regression for the complete support set complete_classifier = LogisticRegression(max_iter=1000).fit(X=out_support_LR_with_GC, y=y) avg_pred = None for j, classifier in enumerate(classifiers): pred = classifier.predict(out_query_LR_with_GC) pred = pred.reshape(params.n_query, params.n_way, -1).astype(np.float64) # Reshape pred and convert to float64 if i == 0: avg_pred = pred # Initialize avg_pred with the first pred values else: avg_pred += pred.mean(axis=2) # Average predictions along the third dimensionRest of the code remains the same...
...
# Perform logistic regression for the complete support set complete_pred = complete_classifier.predict(out_query_LR_with_GC) complete_pred = complete_pred.reshape(params.n_query, params.n_way, -1) # Reshape pred to (n_query, way, 5) avg_pred += complete_pred.mean(axis=2).astype(np.float64) # Average predictions along the third dimension gt = np.repeat(range(params.n_way), params.n_query) gt = torch.from_numpy(gt.astype(np.float64)).cuda() acc_LG = np.mean(avg_pred.argmax(axis=1) == gt) * 100. acc_all_LR.append(acc_LG) acc_all = np.asarray(acc_all_LR) acc_mean = np.mean(acc_all) acc_std = np.std(acc_all) print('acc: %4.2f%% +- %4.2f%%' % (acc_mean, 1.96 * acc_std / np.sqrt(iter_num))) - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.