Spaces:
Sleeping
Sleeping
| '''Some helper functions for PyTorch, including: | |
| - get_mean_and_std: calculate the mean and std value of dataset. | |
| - msr_init: net parameter initialization. | |
| - progress_bar: progress bar mimic xlua.progress. | |
| ''' | |
| import os | |
| import sys | |
| import time | |
| import math | |
| import logging | |
| from datetime import datetime | |
| import torch | |
| import numpy as np | |
| from torch.nn import Parameter | |
| def get_logger(out_dir): | |
| logger = logging.getLogger('Exp') | |
| logger.setLevel(logging.INFO) | |
| formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s") | |
| ts = str(datetime.now()).split(".")[0].replace(" ", "_") | |
| ts = ts.replace(":", "_").replace("-", "_") | |
| file_path = os.path.join(out_dir, "run_{}.log".format(ts)) if os.path.isdir(out_dir) else out_dir.replace('.pth.tar', '') | |
| file_hdlr = logging.FileHandler(file_path) | |
| file_hdlr.setFormatter(formatter) | |
| strm_hdlr = logging.StreamHandler(sys.stdout) | |
| strm_hdlr.setFormatter(formatter) | |
| logger.addHandler(file_hdlr) | |
| logger.addHandler(strm_hdlr) | |
| return logger | |
| class AverageMeter(object): | |
| """Computes and stores the average and current value""" | |
| def __init__(self): | |
| self.reset() | |
| def reset(self): | |
| self.val = 0 | |
| self.avg = 0 | |
| self.sum = 0 | |
| self.count = 0 | |
| def update(self, val, n=1): | |
| self.val = val | |
| self.sum += val * n | |
| self.count += n | |
| self.avg = self.sum / self.count | |
| def accuracy(output, target, topk=(1,)): | |
| """Computes the accuracy over the k top predictions for the specified values of k""" | |
| with torch.no_grad(): | |
| maxk = max(topk) | |
| batch_size = target.size()[0] | |
| _, pred = output.topk(maxk, 1, True, True) | |
| pred = pred.t() | |
| correct = pred.eq(target.view(1, -1).expand_as(pred)) | |
| res = [] | |
| for k in topk: | |
| correct_k = correct[:k].view(-1).float().sum(0, keepdim=True) | |
| res.append(correct_k.mul_(100.0 / batch_size)) | |
| return res | |
| def update_lr(iteration, warmup_iter, total_iter, max_lr, min_lr) : | |
| if iteration < warmup_iter: | |
| current_lr = max_lr * iteration / warmup_iter | |
| else: | |
| current_lr = min_lr + (max_lr - min_lr) * 0.5 * \ | |
| (1. + math.cos(math.pi * (iteration - warmup_iter) / (total_iter - warmup_iter))) | |
| return current_lr | |
| def adjust_learning_rate(optimizer, iteration, warmup_iter, total_iter, max_lr, min_lr): | |
| """Decay the learning rate with half-cycle cosine after warmup""" | |
| current_lr = update_lr(iteration, warmup_iter, total_iter, max_lr, min_lr) | |
| for param_group in optimizer.param_groups: | |
| if "lr_scale" in param_group: | |
| param_group["lr"] = current_lr * param_group["lr_scale"] | |
| else: | |
| param_group["lr"] = current_lr | |
| return current_lr | |
| def get_2d_sincos_pos_embed(embed_dim, grid_size, cls_token=False): | |
| """ | |
| grid_size: int of the grid height and width | |
| return: | |
| pos_embed: [grid_size*grid_size, embed_dim] or [1+grid_size*grid_size, embed_dim] (w/ or w/o cls_token) | |
| """ | |
| grid_h = np.arange(grid_size, dtype=np.float32) | |
| grid_w = np.arange(grid_size, dtype=np.float32) | |
| grid = np.meshgrid(grid_w, grid_h) # here w goes first | |
| grid = np.stack(grid, axis=0) | |
| grid = grid.reshape([2, 1, grid_size, grid_size]) | |
| pos_embed = get_2d_sincos_pos_embed_from_grid(embed_dim, grid) | |
| if cls_token: | |
| pos_embed = np.concatenate([np.zeros([1, embed_dim]), pos_embed], axis=0) | |
| return pos_embed | |
| def get_2d_sincos_pos_embed_from_grid(embed_dim, grid): | |
| assert embed_dim % 2 == 0 | |
| # use half of dimensions to encode grid_h | |
| emb_h = get_1d_sincos_pos_embed_from_grid(embed_dim // 2, grid[0]) # (H*W, D/2) | |
| emb_w = get_1d_sincos_pos_embed_from_grid(embed_dim // 2, grid[1]) # (H*W, D/2) | |
| emb = np.concatenate([emb_h, emb_w], axis=1) # (H*W, D) | |
| return emb | |
| def get_1d_sincos_pos_embed_from_grid(embed_dim, pos): | |
| """ | |
| embed_dim: output dimension for each position | |
| pos: a list of positions to be encoded: size (M,) | |
| out: (M, D) | |
| """ | |
| assert embed_dim % 2 == 0 | |
| omega = np.arange(embed_dim // 2, dtype=float) | |
| omega /= embed_dim / 2. | |
| omega = 1. / 10000**omega # (D/2,) | |
| pos = pos.reshape(-1) # (M,) | |
| out = np.einsum('m,d->md', pos, omega) # (M, D/2), outer product | |
| emb_sin = np.sin(out) # (M, D/2) | |
| emb_cos = np.cos(out) # (M, D/2) | |
| emb = np.concatenate([emb_sin, emb_cos], axis=1) # (M, D) | |
| return emb | |
| # -------------------------------------------------------- | |
| # Interpolate position embeddings for high-resolution | |
| # References: | |
| # DeiT: https://github.com/facebookresearch/deit | |
| # -------------------------------------------------------- | |
| def interpolate_pos_embed(model, checkpoint_model): | |
| if 'pos_embed' in checkpoint_model: | |
| pos_embed_checkpoint = checkpoint_model['pos_embed'] | |
| embedding_size = pos_embed_checkpoint.shape[-1] | |
| num_patches = model.patch_embed.num_patches | |
| num_extra_tokens = model.pos_embed.shape[-2] - num_patches | |
| # height (== width) for the checkpoint position embedding | |
| orig_size = int((pos_embed_checkpoint.shape[-2] - num_extra_tokens) ** 0.5) | |
| # height (== width) for the new position embedding | |
| new_size = int(num_patches ** 0.5) | |
| # class_token and dist_token are kept unchanged | |
| if orig_size != new_size: | |
| print("Position interpolate from %dx%d to %dx%d" % (orig_size, orig_size, new_size, new_size)) | |
| extra_tokens = pos_embed_checkpoint[:, :num_extra_tokens] | |
| # only the position tokens are interpolated | |
| pos_tokens = pos_embed_checkpoint[:, num_extra_tokens:] | |
| pos_tokens = pos_tokens.reshape(-1, orig_size, orig_size, embedding_size).permute(0, 3, 1, 2) | |
| pos_tokens = torch.nn.functional.interpolate( | |
| pos_tokens, size=(new_size, new_size), mode='bicubic', align_corners=False) | |
| pos_tokens = pos_tokens.permute(0, 2, 3, 1).flatten(1, 2) | |
| new_pos_embed = torch.cat((extra_tokens, pos_tokens), dim=1) | |
| checkpoint_model['pos_embed'] = new_pos_embed | |
| def load_my_state_dict(net, state_dict): | |
| own_state = net.state_dict() | |
| for name, param in state_dict.items(): | |
| name = name.replace('module.','') | |
| if name not in own_state: | |
| continue | |
| if isinstance(param, Parameter): | |
| # backwards compatibility for serialized parameters | |
| param = param.data | |
| own_state[name].copy_(param) | |
| # Fix for non-interactive environments | |
| try: | |
| _, term_width = os.popen('stty size', 'r').read().split() | |
| term_width = int(term_width) | |
| except ValueError: | |
| term_width = 80 # Set a default value if the stty command fails | |
| TOTAL_BAR_LENGTH = 65. | |
| last_time = time.time() | |
| begin_time = last_time | |
| def progress_bar(current, total, msg=None): | |
| global last_time, begin_time | |
| if current == 0: | |
| begin_time = time.time() # reset for new bar. | |
| cur_len = int(TOTAL_BAR_LENGTH*current/total) | |
| rest_len = int(TOTAL_BAR_LENGTH - cur_len) - 1 | |
| sys.stdout.write(' [') | |
| for i in range(cur_len): | |
| sys.stdout.write('=') | |
| sys.stdout.write('>') | |
| for i in range(rest_len): | |
| sys.stdout.write('.') | |
| sys.stdout.write(']') | |
| cur_time = time.time() | |
| step_time = cur_time - last_time | |
| last_time = cur_time | |
| tot_time = cur_time - begin_time | |
| L = [] | |
| L.append(' Step: %s' % format_time(step_time)) | |
| L.append(' | Tot: %s' % format_time(tot_time)) | |
| if msg: | |
| L.append(' | ' + msg) | |
| msg = ''.join(L) | |
| sys.stdout.write(msg) | |
| for i in range(term_width-int(TOTAL_BAR_LENGTH)-len(msg)-3): | |
| sys.stdout.write(' ') | |
| # Go back to the center of the bar. | |
| for i in range(term_width-int(TOTAL_BAR_LENGTH/2)+2): | |
| sys.stdout.write('\b') | |
| sys.stdout.write(' %d/%d ' % (current+1, total)) | |
| if current < total-1: | |
| sys.stdout.write('\r') | |
| else: | |
| sys.stdout.write('\n') | |
| sys.stdout.flush() | |
| def format_time(seconds): | |
| days = int(seconds / 3600/24) | |
| seconds = seconds - days*3600*24 | |
| hours = int(seconds / 3600) | |
| seconds = seconds - hours*3600 | |
| minutes = int(seconds / 60) | |
| seconds = seconds - minutes*60 | |
| secondsf = int(seconds) | |
| seconds = seconds - secondsf | |
| millis = int(seconds*1000) | |
| f = '' | |
| i = 1 | |
| if days > 0: | |
| f += str(days) + 'D' | |
| i += 1 | |
| if hours > 0 and i <= 2: | |
| f += str(hours) + 'h' | |
| i += 1 | |
| if minutes > 0 and i <= 2: | |
| f += str(minutes) + 'm' | |
| i += 1 | |
| if secondsf > 0 and i <= 2: | |
| f += str(secondsf) + 's' | |
| i += 1 | |
| if millis > 0 and i <= 2: | |
| f += str(millis) + 'ms' | |
| i += 1 | |
| if f == '': | |
| f = '0ms' | |
| return f | |