update code styles
This commit is contained in:
		| @@ -1,3 +1,5 @@ | ||||
| from .evaluation_utils import obtain_accuracy | ||||
| from .gpu_manager      import GPUManager | ||||
| from .flop_benchmark   import get_model_infos | ||||
| from .affine_utils     import normalize_points, denormalize_points | ||||
| from .affine_utils     import identity2affine, solve2theta, affine2image | ||||
|   | ||||
							
								
								
									
										125
									
								
								lib/utils/affine_utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										125
									
								
								lib/utils/affine_utils.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,125 @@ | ||||
| # functions for affine transformation | ||||
| import math, torch | ||||
| import numpy as np | ||||
| import torch.nn.functional as F | ||||
|  | ||||
| def identity2affine(full=False): | ||||
|   if not full: | ||||
|     parameters = torch.zeros((2,3)) | ||||
|     parameters[0, 0] = parameters[1, 1] = 1 | ||||
|   else: | ||||
|     parameters = torch.zeros((3,3)) | ||||
|     parameters[0, 0] = parameters[1, 1] = parameters[2, 2] = 1 | ||||
|   return parameters | ||||
|  | ||||
| def normalize_L(x, L): | ||||
|   return -1. + 2. * x / (L-1) | ||||
|  | ||||
| def denormalize_L(x, L): | ||||
|   return (x + 1.0) / 2.0 * (L-1) | ||||
|  | ||||
| def crop2affine(crop_box, W, H): | ||||
|   assert len(crop_box) == 4, 'Invalid crop-box : {:}'.format(crop_box) | ||||
|   parameters = torch.zeros(3,3) | ||||
|   x1, y1 = normalize_L(crop_box[0], W), normalize_L(crop_box[1], H) | ||||
|   x2, y2 = normalize_L(crop_box[2], W), normalize_L(crop_box[3], H) | ||||
|   parameters[0,0] = (x2-x1)/2 | ||||
|   parameters[0,2] = (x2+x1)/2 | ||||
|  | ||||
|   parameters[1,1] = (y2-y1)/2 | ||||
|   parameters[1,2] = (y2+y1)/2 | ||||
|   parameters[2,2] = 1 | ||||
|   return parameters | ||||
|  | ||||
| def scale2affine(scalex, scaley): | ||||
|   parameters = torch.zeros(3,3) | ||||
|   parameters[0,0] = scalex | ||||
|   parameters[1,1] = scaley | ||||
|   parameters[2,2] = 1 | ||||
|   return parameters | ||||
|   | ||||
| def offset2affine(offx, offy): | ||||
|   parameters = torch.zeros(3,3) | ||||
|   parameters[0,0] = parameters[1,1] = parameters[2,2] = 1 | ||||
|   parameters[0,2] = offx | ||||
|   parameters[1,2] = offy | ||||
|   return parameters | ||||
|  | ||||
| def horizontalmirror2affine(): | ||||
|   parameters = torch.zeros(3,3) | ||||
|   parameters[0,0] = -1 | ||||
|   parameters[1,1] = parameters[2,2] = 1 | ||||
|   return parameters | ||||
|  | ||||
| # clockwise rotate image = counterclockwise rotate the rectangle | ||||
| # degree is between [0, 360] | ||||
| def rotate2affine(degree): | ||||
|   assert degree >= 0 and degree <= 360, 'Invalid degree : {:}'.format(degree) | ||||
|   degree = degree / 180 * math.pi | ||||
|   parameters = torch.zeros(3,3) | ||||
|   parameters[0,0] =  math.cos(-degree) | ||||
|   parameters[0,1] = -math.sin(-degree) | ||||
|   parameters[1,0] =  math.sin(-degree) | ||||
|   parameters[1,1] =  math.cos(-degree) | ||||
|   parameters[2,2] = 1 | ||||
|   return parameters | ||||
|  | ||||
| # shape is a tuple [H, W] | ||||
| def normalize_points(shape, points): | ||||
|   assert (isinstance(shape, tuple) or isinstance(shape, list)) and len(shape) == 2, 'invalid shape : {:}'.format(shape)   | ||||
|   assert isinstance(points, torch.Tensor) and (points.shape[0] == 2), 'points are wrong : {:}'.format(points.shape) | ||||
|   (H, W), points = shape, points.clone() | ||||
|   points[0, :] = normalize_L(points[0,:], W) | ||||
|   points[1, :] = normalize_L(points[1,:], H) | ||||
|   return points | ||||
|  | ||||
| # shape is a tuple [H, W] | ||||
| def normalize_points_batch(shape, points): | ||||
|   assert (isinstance(shape, tuple) or isinstance(shape, list)) and len(shape) == 2, 'invalid shape : {:}'.format(shape)   | ||||
|   assert isinstance(points, torch.Tensor) and (points.size(-1) == 2), 'points are wrong : {:}'.format(points.shape) | ||||
|   (H, W), points = shape, points.clone() | ||||
|   x = normalize_L(points[...,0], W) | ||||
|   y = normalize_L(points[...,1], H) | ||||
|   return torch.stack((x,y), dim=-1) | ||||
|  | ||||
| # shape is a tuple [H, W] | ||||
| def denormalize_points(shape, points): | ||||
|   assert (isinstance(shape, tuple) or isinstance(shape, list)) and len(shape) == 2, 'invalid shape : {:}'.format(shape)   | ||||
|   assert isinstance(points, torch.Tensor) and (points.shape[0] == 2), 'points are wrong : {:}'.format(points.shape) | ||||
|   (H, W), points = shape, points.clone() | ||||
|   points[0, :] = denormalize_L(points[0,:], W) | ||||
|   points[1, :] = denormalize_L(points[1,:], H) | ||||
|   return points | ||||
|  | ||||
| # shape is a tuple [H, W] | ||||
| def denormalize_points_batch(shape, points): | ||||
|   assert (isinstance(shape, tuple) or isinstance(shape, list)) and len(shape) == 2, 'invalid shape : {:}'.format(shape)   | ||||
|   assert isinstance(points, torch.Tensor) and (points.shape[-1] == 2), 'points are wrong : {:}'.format(points.shape) | ||||
|   (H, W), points = shape, points.clone() | ||||
|   x = denormalize_L(points[...,0], W) | ||||
|   y = denormalize_L(points[...,1], H) | ||||
|   return torch.stack((x,y), dim=-1) | ||||
|  | ||||
| # make target * theta = source | ||||
| def solve2theta(source, target): | ||||
|   source, target = source.clone(), target.clone() | ||||
|   oks = source[2, :] == 1 | ||||
|   assert torch.sum(oks).item() >= 3, 'valid points : {:} is short'.format(oks) | ||||
|   if target.size(0) == 2: target = torch.cat((target, oks.unsqueeze(0).float()), dim=0) | ||||
|   source, target = source[:, oks], target[:, oks] | ||||
|   source, target = source.transpose(1,0), target.transpose(1,0) | ||||
|   assert source.size(1) == target.size(1) == 3 | ||||
|   #X, residual, rank, s = np.linalg.lstsq(target.numpy(), source.numpy()) | ||||
|   #theta = torch.Tensor(X.T[:2, :]) | ||||
|   X_, qr = torch.gels(source, target) | ||||
|   theta = X_[:3, :2].transpose(1, 0) | ||||
|   return theta | ||||
|  | ||||
| # shape = [H,W] | ||||
| def affine2image(image, theta, shape): | ||||
|   C, H, W = image.size() | ||||
|   theta = theta[:2, :].unsqueeze(0) | ||||
|   grid_size = torch.Size([1, C, shape[0], shape[1]]) | ||||
|   grid  = F.affine_grid(theta, grid_size) | ||||
|   affI  = F.grid_sample(image.unsqueeze(0), grid, mode='bilinear', padding_mode='border') | ||||
|   return affI.squeeze(0) | ||||
| @@ -1,4 +1,4 @@ | ||||
| import copy, torch | ||||
| import torch | ||||
| import torch.nn as nn | ||||
| import numpy as np | ||||
|  | ||||
|   | ||||
| @@ -27,7 +27,7 @@ class GPUManager(): | ||||
|         find = False | ||||
|         for gpu in all_gpus: | ||||
|           if gpu['index'] == CUDA_VISIBLE_DEVICE: | ||||
|             assert find==False, 'Duplicate cuda device index : {}'.format(CUDA_VISIBLE_DEVICE) | ||||
|             assert not find, 'Duplicate cuda device index : {}'.format(CUDA_VISIBLE_DEVICE) | ||||
|             find = True | ||||
|             selected_gpus.append( gpu.copy() ) | ||||
|             selected_gpus[-1]['index'] = '{}'.format(idx) | ||||
|   | ||||
							
								
								
									
										52
									
								
								lib/utils/nas_utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								lib/utils/nas_utils.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| # This file is for experimental usage | ||||
| import os, sys, torch, random | ||||
| import numpy as np | ||||
| from copy import deepcopy | ||||
| from tqdm import tqdm | ||||
| import torch.nn as nn | ||||
|  | ||||
| from utils  import obtain_accuracy | ||||
| from models import CellStructure | ||||
| from log_utils import time_string | ||||
|  | ||||
| def evaluate_one_shot(model, xloader, api, cal_mode, seed=111): | ||||
|   weights = deepcopy(model.state_dict()) | ||||
|   model.train(cal_mode) | ||||
|   with torch.no_grad(): | ||||
|     logits = nn.functional.log_softmax(model.arch_parameters, dim=-1) | ||||
|     archs = CellStructure.gen_all(model.op_names, model.max_nodes, False) | ||||
|     probs, accuracies, gt_accs = [], [], [] | ||||
|     loader_iter = iter(xloader) | ||||
|     random.seed(seed) | ||||
|     random.shuffle(archs) | ||||
|     for idx, arch in enumerate(archs): | ||||
|       arch_index = api.query_index_by_arch( arch ) | ||||
|       metrics = api.get_more_info(arch_index, 'cifar10-valid', None, False, False) | ||||
|       gt_accs.append( metrics['valid-accuracy'] ) | ||||
|       select_logits = [] | ||||
|       for i, node_info in enumerate(arch.nodes): | ||||
|         for op, xin in node_info: | ||||
|           node_str = '{:}<-{:}'.format(i+1, xin) | ||||
|           op_index = model.op_names.index(op) | ||||
|           select_logits.append( logits[model.edge2index[node_str], op_index] ) | ||||
|       cur_prob = sum(select_logits).item() | ||||
|       probs.append( cur_prob ) | ||||
|     cor_prob = np.corrcoef(probs, gt_accs)[0,1] | ||||
|     print ('correlation for probabilities : {:}'.format(cor_prob)) | ||||
|        | ||||
|     for idx, arch in enumerate(archs): | ||||
|       model.set_cal_mode('dynamic', arch) | ||||
|       try: | ||||
|         inputs, targets = next(loader_iter) | ||||
|       except: | ||||
|         loader_iter = iter(xloader) | ||||
|         inputs, targets = next(loader_iter) | ||||
|       _, logits = model(inputs.cuda()) | ||||
|       _, preds  = torch.max(logits, dim=-1) | ||||
|       correct = (preds == targets.cuda() ).float() | ||||
|       accuracies.append( correct.mean().item() ) | ||||
|       if idx != 0 and (idx % 300 == 0 or idx + 1 == len(archs) or idx == 10): | ||||
|         cor_accs = np.corrcoef(accuracies, gt_accs[:idx+1])[0,1] | ||||
|         print ('{:} {:03d}/{:03d} mode={:5s}, correlation : accs={:.4f}, arch={:}'.format(time_string(), idx, len(archs), 'Train' if cal_mode else 'Eval', cor_accs, arch)) | ||||
|   model.load_state_dict(weights) | ||||
|   return archs, probs, accuracies | ||||
		Reference in New Issue
	
	Block a user