def __init__( self, num_cutouts = 128, loss_coef = 100, image_size = 512, bilinear = False, max_classes = None, class_temperature = 2., experimental_resample = False, ema_decay = 0.99, center_bias = False, larger_clip = False ): super().__init__() self.loss_coef = loss_coef self.image_size = image_size self.num_cutouts = num_cutouts self.experimental_resample = experimental_resample self.center_bias = center_bias self.interpolation_settings = {'mode': 'bilinear', 'align_corners': False} if bilinear else {'mode': 'nearest'} model_name = 'ViT-B/32' if not larger_clip else 'ViT-L/14' self.perceptor, self.normalize_image = load(model_name, jit = False) self.model = Model( image_size = image_size, max_classes = max_classes, class_temperature = class_temperature, ema_decay = ema_decay )
offset_y = int(random.gauss(mu=center, sigma=std)) # resample uniformly if over boundaries offset_x = random.randint(min_offset, max_offset) if ( offset_x > max_offset or offset_x < min_offset) else offset_x offset_y = random.randint(min_offset, max_offset) if ( offset_y > max_offset or offset_y < min_offset) else offset_y else: offset_x = random.randint(min_offset, max_offset) offset_y = random.randint(min_offset, max_offset) cutout = image[:, :, offset_x:offset_x + size, offset_y:offset_y + size] return cutout # load clip perceptor, normalize_image = load('ViT-B/32', jit=False) # load biggan class Latents(torch.nn.Module): def __init__(self, num_latents=15, num_classes=1000, z_dim=128, max_classes=None, class_temperature=2.): super().__init__() self.normu = torch.nn.Parameter( torch.zeros(num_latents, z_dim).normal_(std=1)) self.cls = torch.nn.Parameter(
topk_tensors = [] for i in range(k): is_last = i == (k - 1) values, indices = (x / temperature).softmax(dim=-1).topk(1, dim=-1) topks = torch.zeros_like(x).scatter_(-1, indices, values) topk_tensors.append(topks) if not is_last: x = x.scatter(-1, indices, float('-inf')) topks = torch.cat(topk_tensors, dim=-1) return topks.reshape(n, k, dim).sum(dim = 1) # load clip perceptor, preprocess = load() # load biggan class Latents(torch.nn.Module): def __init__( self, num_latents = 32, max_classes = None, class_temperature = 2. ): super().__init__() self.normu = torch.nn.Parameter(torch.zeros(num_latents, 128).normal_(std = 1)) self.cls = torch.nn.Parameter(torch.zeros(num_latents, 1000).normal_(mean = -3.9, std = .3)) self.register_buffer('thresh_lat', torch.tensor(1))
offset_x = (random.randint(min_offset, max_offset) if (offset_x > max_offset or offset_x < min_offset) else offset_x) offset_y = (random.randint(min_offset, max_offset) if (offset_y > max_offset or offset_y < min_offset) else offset_y) else: offset_x = random.randint(min_offset, max_offset) offset_y = random.randint(min_offset, max_offset) cutout = image[:, :, offset_x:offset_x + size, offset_y:offset_y + size] return cutout # load clip perceptor, normalize_image = load("ViT-B/32", jit=False) # load biggan class Latents(torch.nn.Module): def __init__( self, num_latents=15, num_classes=1000, z_dim=128, max_classes=None, class_temperature=2.0, ): super().__init__() self.normu = torch.nn.Parameter(