Kaggle competition: Generative Dog Images
<SOS> metric: PostProcessorKernel Kaggle data source: generative-dog-images<define_variables>
Conv2DTranspose, Conv2D, Flatten, Dropout, Embedding, ReLU
print(os.listdir(".. /input"))
image = glob.glob('.. /input/all-dogs/all-dogs/*')
 breed = glob.glob('.. /input/annotation/Annotation/*')
 annot = glob.glob('.. /input/annotation/Annotation/*/*')
 print(len(image), len(breed), len(annot))<define_variables>
tf.enable_eager_execution()
def get_image(annot):

 img_path = '.. /input/all-dogs/all-dogs/'
file = annot.split('/')
 img_class = file[-2]
img_filename = img_path+file[-1]+'.jpg'
return img_filename, img_class<define_variables>
image_width = 64
image_height = 64
image_channels = 3
image_sample_size = 10000
image_output_dir = '.. /output_images/'
image_input_dir = '.. /input/all-dogs/all-dogs/'
image_ann_dir = ".. /input/annotation/Annotation/"
OUT_DIR = Path('.. /output_images/')
 MODEL_PATH = '.. /input/dog-face-generation-competition-kid-metric-input/classify_image_graph_def.pb'
TRAIN_DIR = Path('.. /input/all-dogs/all-dogs' )
select_dogs = []
select_bbox = []<feature_engineering>
dog_breed_dict = {}
for annotation in os.listdir(image_ann_dir):
annotations = annotation.split('-')
 dog_breed_dict[annotations[0]] = annotations[1]
def get_class_dict(breed):
class_dict = {}
for i in range(len(breed)) :
class_ = breed[i].split('/')
 class_dict[class_[-1]] = i
return class_dict
class_dict = get_class_dict(breed )<prepare_x_and_y>
def load_cropped_images(dog_breed_dict=dog_breed_dict, image_ann_dir=image_ann_dir, sample_size=25000,
image_width=image_width, image_height=image_height, image_channels=image_channels):
curIdx = 0
breeds = []
dog_images_np = np.zeros(( sample_size,image_width,image_height,image_channels))
for breed_folder in os.listdir(image_ann_dir):
for dog_ann in tqdm(os.listdir(image_ann_dir + breed_folder)) :
try:
img = read_image(os.path.join(image_input_dir, dog_ann + '.jpg'))
except FileNotFoundError:
continue
tree = ET.parse(os.path.join(image_ann_dir + breed_folder, dog_ann))
root = tree.getroot()
size = root.find('size')
 width = int(size.find('width' ).text)
 height = int(size.find('height' ).text)
 objects = root.findall('object')
 for o in objects:
bndbox = o.find('bndbox')
 xmin = int(bndbox.find('xmin' ).text)
 ymin = int(bndbox.find('ymin' ).text)
 xmax = int(bndbox.find('xmax' ).text)
 ymax = int(bndbox.find('ymax' ).text)
 xmin = max(0, xmin - 4)
 xmax = min(width, xmax + 4)
 ymin = max(0, ymin - 4)
 ymax = min(height, ymax + 4)
 w = np.min(( xmax - xmin, ymax - ymin))
w = min(w, width, height)
 if w > xmax - xmin:
xmin = min(max(0, xmin - int(( w -(xmax - xmin)) /2)) , width - w)
 xmax = xmin + w
if w > ymax - ymin:
ymin = min(max(0, ymin - int(( w -(ymax - ymin)) /2)) , height - w)
 ymax = ymin + w
img_cropped = img[ymin:ymin+w, xmin:xmin+w, :]
if xmax - xmin > image_width:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_CUBIC
img_cropped = cv2.resize(img_cropped,(image_width, image_height),
interpolation=interpolation)
 dog_images_np[curIdx,:,:,:] = np.asarray(img_cropped)
 dog_breed_name = dog_breed_dict[dog_ann.split('_')[0]]
breeds.append(dog_breed_name)
 curIdx += 1
return dog_images_np, breeds
def get_cleaned_data(image, annot, class_dict):
select_dogs = []
select_bbox = []
select_labels = []
for i in range(len(image)) :
bbox = get_bbox(annot[i])
 bbox = bbox[0]
dog, dog_class = get_image(annot[i])
 if dog == '.. /input/all-dogs/all-dogs/n02105855_2933.jpg':
continue
im = Image.open(dog)
 xdiff = abs(bbox[2] - bbox[0])
 ydiff = abs(bbox[3] - bbox[1])
 if(( abs(ydiff - xdiff)/min(ydiff, xdiff)<=0.25)and(( xdiff>=64)and(ydiff>=64))):
select_dogs.append(dog)
 select_bbox.append(bbox)
 select_labels.append(class_dict[dog_class])
 return select_dogs, select_bbox, select_labels
select_dogs, select_bbox, select_labels = get_cleaned_data(image, annot, class_dict )<categorify>
start_time = time.time()
dog_images_np, breeds = load_cropped_images(sample_size=22125)
 est_time = round(time.time() - start_time)
 print("Feature loading time: {}.".format(str(datetime.timedelta(seconds=est_time))))
def prepare_labels(y):
values = np.array(y)
 onehot_encoder = OneHotEncoder(sparse=False, categories='auto')
 onehot_encoded = onehot_encoder.fit_transform(values.reshape(values.shape[0], 1))
y = onehot_encoded
return y
labels_encoded = prepare_labels(select_labels )<categorify>
dog_images_np =(dog_images_np - 127.5)/ 127.5
class dogs_Dataset(Dataset):
def __init__(self, datafolder, datatype='train', index=[], \
transform = transforms.Compose([transforms.RandomResizedCrop(128),transforms.ToTensor() ])) :
self.datafolder = datafolder
self.datatype = datatype
self.image_files_list = [select_dogs[i] for i in index]
self.bbox_list = [select_bbox[i] for i in index]
self.label_list = [labels_encoded[i] for i in index]
self.transform = transform
self.imgs = []
for idx in range(len(self.image_files_list)) :
img_name = self.image_files_list[idx]
bbox = self.bbox_list[idx]
img = Image.open(img_name)
 img = img.crop(bbox)
 self.imgs.append(img)
 def __len__(self):
return len(self.imgs)
 def __getitem__(self, idx):
image = self.imgs[idx]
image = self.transform(image)
 if self.datatype == 'train':
label = self.label_list[idx]
else:
label = torch.zeros(1)
 return image, label<define_variables>
dog_features_tf = tf.cast(dog_images_np, 'float32' )
imgs, label = next(iter(train_loader))
imgs = imgs.numpy().transpose(0, 2, 3, 1 )<init_hyperparams>
sample_size = 22125
batch_size = 128
weight_init_std = 0.02
weight_init_mean = 0.0
leaky_relu_slope = 0.2
downsize_factor = 2
dropout_rate = 0.3
scale_factor = 4 ** downsize_factor
lr_initial_d = 0.0002
lr_initial_g = 0.0002
lr_decay_steps = 1000
noise_dim = 128
LR_G = 0.0005
LR_D = 0.0005
beta1 = 0.5
real_label = 0.8
fake_label = 0.0
nz = 128
device = torch.device("cuda" if torch.cuda.is_available() else "cpu" )<choose_model_class>
dog_features_data = tf.data.Dataset.from_tensor_slices(dog_features_tf ).shuffle(sample_size ).map(flip ).batch(batch_size )
netG = Generator(nz ).to(device)
 netD = Discriminator().to(device)
 criterion = nn.BCELoss()
optimizerD = optim.Adam(netD.parameters() , lr=LR_D, betas=(beta1, 0.999))
optimizerG = optim.Adam(netG.parameters() , lr=LR_G, betas=(beta1, 0.999))
fixed_noise = torch.randn(25, nz, 1, 1, device=device )<define_variables>
weight_initializer = tf.keras.initializers.TruncatedNormal(stddev=weight_init_std, mean=weight_init_mean,
seed=42 )
epochs = 780<init_hyperparams>
class DenseSN(Dense):
def build(self, input_shape):
assert len(input_shape)>= 2
input_dim = input_shape[-1]
self.kernel = self.add_weight(shape=(input_dim, self.units),
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
 if self.use_bias:
self.bias = self.add_weight(shape=(self.units,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
 else:
self.bias = None
self.u = self.add_weight(shape=tuple([1, self.kernel.shape.as_list() [-1]]),
initializer=initializers.RandomNormal(0, 1),
name='sn',
trainable=False)
 self.input_spec = InputSpec(min_ndim=2, axes={-1: input_dim})
 self.built = True
def call(self, inputs, training=None):
def _l2normalize(v, eps=1e-12):
return v /(K.sum(v ** 2)** 0.5 + eps)
 def power_iteration(W, u):
_u = u
_v = _l2normalize(K.dot(_u, K.transpose(W)))
 _u = _l2normalize(K.dot(_v, W))
return _u, _v
W_shape = self.kernel.shape.as_list()
W_reshaped = K.reshape(self.kernel, [-1, W_shape[-1]])
 _u, _v = power_iteration(W_reshaped, self.u)
 sigma=K.dot(_v, W_reshaped)
 sigma=K.dot(sigma, K.transpose(_u))
W_bar = W_reshaped / sigma
if training in {0, False}:
W_bar = K.reshape(W_bar, W_shape)
 else:
with tf.control_dependencies([self.u.assign(_u)]):
W_bar = K.reshape(W_bar, W_shape)
 output = K.dot(inputs, W_bar)
 if self.use_bias:
output = K.bias_add(output, self.bias, data_format='channels_last')
 if self.activation is not None:
output = self.activation(output)
 return output
class ConvSN2D(Conv2D):
def build(self, input_shape):
if self.data_format == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
if input_shape[channel_axis] is None:
raise ValueError('The channel dimension of the inputs '
'should be defined.Found `None`.')
 input_dim = input_shape[channel_axis]
kernel_shape = self.kernel_size +(input_dim, self.filters)
 self.kernel = self.add_weight(shape=kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
 if self.use_bias:
self.bias = self.add_weight(shape=(self.filters,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
 else:
self.bias = None
self.u = self.add_weight(shape=tuple([1, self.kernel.shape.as_list() [-1]]),
initializer=initializers.RandomNormal(0, 1),
name='sn',
trainable=False)
 self.input_spec = InputSpec(ndim=self.rank + 2,
axes={channel_axis: input_dim})
 self.built = True
def call(self, inputs, training=None):
def _l2normalize(v, eps=1e-12):
return v /(K.sum(v ** 2)** 0.5 + eps)
 def power_iteration(W, u):
_u = u
_v = _l2normalize(K.dot(_u, K.transpose(W)))
 _u = _l2normalize(K.dot(_v, W))
return _u, _v
W_shape = self.kernel.shape.as_list()
W_reshaped = K.reshape(self.kernel, [-1, W_shape[-1]])
 _u, _v = power_iteration(W_reshaped, self.u)
 sigma=K.dot(_v, W_reshaped)
 sigma=K.dot(sigma, K.transpose(_u))
W_bar = W_reshaped / sigma
if training in {0, False}:
W_bar = K.reshape(W_bar, W_shape)
 else:
with tf.control_dependencies([self.u.assign(_u)]):
W_bar = K.reshape(W_bar, W_shape)
 outputs = K.conv2d(
 inputs,
W_bar,
strides=self.strides,
padding=self.padding,
data_format=self.data_format,
dilation_rate=self.dilation_rate)
 if self.use_bias:
outputs = K.bias_add(
 outputs,
self.bias,
data_format=self.data_format)
 if self.activation is not None:
return self.activation(outputs)
 return outputs
class ConvSN2DTranspose(Conv2DTranspose):
def build(self, input_shape):
if len(input_shape)!= 4:
raise ValueError('Inputs should have rank ' +
str(4)+
'; Received input shape:', str(input_shape))
if self.data_format == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
if input_shape[channel_axis] is None:
raise ValueError('The channel dimension of the inputs '
'should be defined.Found `None`.')
 input_dim = input_shape[channel_axis]
kernel_shape = self.kernel_size +(self.filters, input_dim)
 self.kernel = self.add_weight(shape=kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
 if self.use_bias:
self.bias = self.add_weight(shape=(self.filters,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
 else:
self.bias = None
self.u = self.add_weight(shape=tuple([1, self.kernel.shape.as_list() [-1]]),
initializer=initializers.RandomNormal(0, 1),
name='sn',
trainable=False)
 self.input_spec = InputSpec(ndim=4, axes={channel_axis: input_dim})
 self.built = True
def call(self, inputs):
input_shape = K.shape(inputs)
 batch_size = input_shape[0]
if self.data_format == 'channels_first':
h_axis, w_axis = 2, 3
else:
h_axis, w_axis = 1, 2
height, width = input_shape[h_axis], input_shape[w_axis]
kernel_h, kernel_w = self.kernel_size
stride_h, stride_w = self.strides
if self.output_padding is None:
out_pad_h = out_pad_w = None
else:
out_pad_h, out_pad_w = self.output_padding
out_height = conv_utils.deconv_length(height,
stride_h, kernel_h,
self.padding,
out_pad_h)
 out_width = conv_utils.deconv_length(width,
stride_w, kernel_w,
self.padding,
out_pad_w)
 if self.data_format == 'channels_first':
output_shape =(batch_size, self.filters, out_height, out_width)
 else:
output_shape =(batch_size, out_height, out_width, self.filters)
 def _l2normalize(v, eps=1e-12):
return v /(K.sum(v ** 2)** 0.5 + eps)
 def power_iteration(W, u):
_u = u
_v = _l2normalize(K.dot(_u, K.transpose(W)))
 _u = _l2normalize(K.dot(_v, W))
return _u, _v
W_shape = self.kernel.shape.as_list()
W_reshaped = K.reshape(self.kernel, [-1, W_shape[-1]])
 _u, _v = power_iteration(W_reshaped, self.u)
 sigma=K.dot(_v, W_reshaped)
 sigma=K.dot(sigma, K.transpose(_u))
W_bar = W_reshaped / sigma
if training in {0, False}:
W_bar = K.reshape(W_bar, W_shape)
 else:
with tf.control_dependencies([self.u.assign(_u)]):
W_bar = K.reshape(W_bar, W_shape)
 self.kernel = W_bar
outputs = K.conv2d_transpose(
 inputs,
self.kernel,
output_shape,
self.strides,
padding=self.padding,
data_format=self.data_format)
 if self.use_bias:
outputs = K.bias_add(
 outputs,
self.bias,
data_format=self.data_format)
 if self.activation is not None:
return self.activation(outputs)
 return outputs
for epoch in range(epochs):
for ii,(real_images, train_labels)in enumerate(train_loader):
netD.zero_grad()
real_images = real_images.to(device)
 batch_size = real_images.size(0)
 labels = torch.full(( batch_size, 1), real_label, device=device)
 output = netD(real_images)
 errD_real = criterion(output, labels)
 errD_real.backward()
D_x = output.mean().item()
noise = torch.randn(batch_size, nz, 1, 1, device=device)
 fake = netG(noise)
 labels.fill_(fake_label)
 output = netD(fake.detach())
 errD_fake = criterion(output, labels)
 errD_fake.backward()
D_G_z1 = output.mean().item()
errD = errD_real + errD_fake
optimizerD.step()
netG.zero_grad()
labels.fill_(real_label)
 output = netD(fake)
 errG = criterion(output, labels)
 errG.backward()
D_G_z2 = output.mean().item()
optimizerG.step()
if(ii+1)%(len(train_loader)//2)== 0:
print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f D(x): %.4f D(G(z)) : %.4f / %.4f'
%(epoch + 1, epochs, ii+1, len(train_loader),
errD.item() , errG.item() , D_x, D_G_z1, D_G_z2)) 
<compute_test_metric>
def transposed_conv(model, out_channels, ksize, stride_size, ptype='same'):
model.add(Conv2DTranspose(out_channels,(ksize, ksize),
strides=(stride_size, stride_size), padding=ptype,
kernel_initializer=weight_initializer, use_bias=False))
model.add(BatchNormalization())
 model.add(ReLU())
 return model
def transposed_convSN(model, out_channels, ksize, stride_size, ptype='same'):
model.add(ConvSN2DTranspose(out_channels,(ksize, ksize),
strides=(stride_size, stride_size), padding=ptype,
kernel_initializer=weight_initializer, use_bias=False))
model.add(BatchNormalization())
 model.add(ReLU())
 return model
def convSN(model, out_channels, ksize, stride_size):
model.add(ConvSN2D(out_channels,(ksize, ksize), strides=(stride_size, stride_size), padding='same',
kernel_initializer=weight_initializer, use_bias=False))
model.add(BatchNormalization())
 model.add(LeakyReLU(alpha=leaky_relu_slope))
return model
def conv(model, out_channels, ksize, stride_size):
model.add(Conv2D(out_channels,(ksize, ksize), strides=(stride_size, stride_size), padding='same',
kernel_initializer=weight_initializer, use_bias=False))
model.add(BatchNormalization())
 model.add(LeakyReLU(alpha=leaky_relu_slope))
return model
def truncated_normal(size, threshold=1):
values = truncnorm.rvs(-threshold, threshold, size=size)
 return values<categorify>
print(image_height // scale_factor, image_width // scale_factor, 512 )
gen_z = torch.randn(32, nz, 1, 1, device=device)
 gen_images = netG(gen_z ).to("cpu" ).clone().detach()
gen_images = gen_images.numpy().transpose(0, 2, 3, 1 )<categorify>
def DogGenerator() :
model = Sequential()
model.add(Dense(image_width // scale_factor * image_height // scale_factor * 128,
input_shape=(noise_dim,), kernel_initializer=weight_initializer))
model.add(Reshape(( image_height // scale_factor, image_width // scale_factor, 128)))
 model = transposed_conv(model, 512, ksize=5, stride_size=1)
 model = transposed_conv(model, 256, ksize=5, stride_size=2)
 model = transposed_conv(model, 128, ksize=5, stride_size=2)
 model = transposed_conv(model, 64, ksize=5, stride_size=2)
 model = transposed_conv(model, 32, ksize=5, stride_size=2)
 model.add(Dense(3, activation='tanh', kernel_initializer=weight_initializer))
return model
if not os.path.exists('.. /output_images'):
os.mkdir('.. /output_images')
 im_batch_size = 50
n_images=10000
for i_batch in range(0, n_images, im_batch_size):
z = truncated_normal(( im_batch_size, nz, 1, 1), threshold=1)
 gen_z = torch.from_numpy(z ).float().to(device)
 gen_images =(netG(gen_z)+1.) /2.
 images = gen_images.to("cpu" ).clone().detach()
images = images.numpy().transpose(0, 2, 3, 1)
 for i_image in range(gen_images.size(0)) :
save_image(gen_images[i_image, :, :, :], os.path.join('.. /output_images', f'image_{i_batch+i_image:05d}.png'))
shutil.make_archive('images', 'zip', '.. /output_images' )<set_options>
def generate_latent_points(latent_dim, n_samples):
x_input = np.random.randn(latent_dim * n_samples)
 x_input = x_input.reshape(( n_samples, latent_dim))
return x_input
%matplotlib inline<import_modules>
def DogDiscriminator(spectral_normalization=True):
model = Sequential()
if spectral_normalization:
model.add(ConvSN2D(64,(5, 5), strides=(1,1), padding='same', use_bias=False,
input_shape=[image_height, image_width, image_channels],
kernel_initializer=weight_initializer))
model.add(LeakyReLU(alpha=leaky_relu_slope))
model = convSN(model, 64, ksize=5, stride_size=2)
 model = convSN(model, 128, ksize=5, stride_size=2)
 model = convSN(model, 256, ksize=5, stride_size=2)
 model.add(Dropout(dropout_rate))
model.add(Flatten())
 model.add(DenseSN(1, activation='sigmoid'))
else:
model.add(Conv2D(64,(4, 4), strides=(2,2), padding='same', use_bias=False,
input_shape=[image_height, image_width, image_channels],
kernel_initializer=weight_initializer))
model.add(LeakyReLU(alpha=leaky_relu_slope))
model = conv(model, 64, ksize=4, stride_size=2)
 model = conv(model, 128, ksize=4, stride_size=2)
 model = conv(model, 256, ksize=4, stride_size=2)
 model.add(Flatten())
 model.add(Dense(1, activation='sigmoid'))
return model
import copy
from itertools import chain
import io
import math
from multiprocessing import cpu_count
from pathlib import Path
from pdb import set_trace
import time
from threading import Thread
from xml.etree import ElementTree
import zipfile
from allennlp.training.learning_rate_schedulers import CosineWithRestarts
from imageio import imread
from joblib import Parallel, delayed
import numpy as np
import pandas as pd
import PIL.Image
import matplotlib.pyplot as plt
from tqdm import tqdm_notebook as tqdm
from scipy.stats import truncnorm
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader
import torchvision.datasets as dset
import torchvision.transforms as transforms
from torchvision.utils import make_grid, save_image<feature_engineering>
decision = dog_discriminator(generated_image)
 print(decision )
HOUR = 3600
class Watchdog:
def __init__(self, max_seconds=8.5 * HOUR):
self.start = time.time()
self.deadline = max_seconds
@property
def timeout(self):
return self.elapsed >= self.deadline
@property
def elapsed(self):
return time.time() - self.start
wd = Watchdog()
SEED = 1
np.random.seed(SEED)
 torch.manual_seed(SEED)
 ANNOTS = Path.cwd().parent/'input'/'annotation'/'Annotation'
IMAGES = Path.cwd().parent/'input'/'all-dogs'/'all-dogs'
try:
patched
except NameError:
patched = True
__print__ = print
def print(message):
log_message = datetime.now().strftime(f'[Kernel][%Y-%m-%d %H:%M:%S] {message}')
 os.system(f'echo "{log_message}"')
 __print__(message)
 class VisualStyle:

 def __init__(self, config, default=None):
if default is None:
default = plt.rcParams
self.default = default.copy()
self.config = config
def replace(self):
plt.rcParams = self.config
def override(self, extra=None):
plt.rcParams.update(self.config)
 if extra is not None:
plt.rcParams.update(extra)
 def restore(self):
plt.rcParams = self.default
def __enter__(self):
self.override()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.restore()
class NotebookStyle(VisualStyle):
def __init__(self):
super().__init__({
'figure.figsize':(11, 8),
'axes.titlesize': 20,
'axes.labelsize': 18,
'xtick.labelsize': 14,
'ytick.labelsize': 14,
'font.size': 16
})
 NotebookStyle().override()
print(f'Annotations: {ANNOTS}')
 print(f'Images: {IMAGES}' )<categorify>
def smooth_positive_labels(y):
return y - 0.3 +(np.random.random(y.shape)* 0.5)
 def smooth_negative_labels(y):
return y + np.random.random(y.shape)* 0.3
def parse_annotation(path):
root = ElementTree.parse(path ).getroot()
size = [int(root.find(f'size/{leaf}' ).text)
 for leaf in('width', 'height')]
bbox = [int(root.find(f'object/bndbox/{leaf}' ).text)
 for leaf in('xmin', 'ymin', 'xmax', 'ymax')]
breed = path.parent.name.split('-')[-1]
return {'path': str(path), 'name': path.name,
'breed': breed, 'size': size, 'bbox': bbox}
def enrich_with_image_paths(annotations, images_directory):
image_files = {x.stem: x for x in images_directory.iterdir() }
enriched_data = []
for annot in annotations:
image_path = image_files.get(annot['name'], None)
 if image_path is None:
print('Warning: image not found for annotation entry: %s.' % annot['path'])
 continue
annot['image'] = str(image_path)
 enriched_data.append(annot)
 return enriched_data
def load_annotations() :
return enrich_with_image_paths([
parse_annotation(path)
 for directory in ANNOTS.iterdir()
for path in directory.iterdir()
], IMAGES)
 def dog(annot):
img = imread(annot['image'])
 xmin, ymin, xmax, ymax = annot['bbox']
cropped = img[ymin:ymax, xmin:xmax]
return cropped
def chunks(seq, chunk_size=10):
n = len(seq)
 n_chunks = n // chunk_size + int(( n % chunk_size)!= 0)
 for i in range(n_chunks):
yield seq[i*chunk_size:(i+1)*chunk_size]
def resize(image, new_size):
return np.array(PIL.Image.fromarray(image ).resize(new_size))
def parallel(func, sequence, func_args=None, n_jobs=None):
with Parallel(n_jobs=n_jobs or cpu_count())as p:
func_args = func_args or {}
results = p(delayed(func )(item, **func_args)for item in sequence)
 return results
def load_single_image(annot, size):
cropped = dog(annot)
 resized = resize(cropped, size)
 return resized
def load_dogs_images(annots, size=(64, 64)) :
return np.stack(parallel(load_single_image, annots, func_args={'size': size}))
def as_pil_list(dataset):
return [PIL.Image.fromarray(image, 'RGB')for image in dataset]
class Normalizer:
def __init__(self, method='tanh', params=None):
assert method in('tanh', 'stats')
 self.method = method
self.params = params or {}
def transform(self, dataset):
dataset = dataset.float()
if self.method == 'tanh':
return(dataset - 127.5)/127.5
if self.method == 'stats':
mean = self.params.get('mean',(0.5, 0.5, 0.5))
std = self.params.get('std',(0.5, 0.5, 0.5))
return functional.normalize(dataset, mean, std)
 def inv_transform(self, dataset):
if self.method == 'tanh':
inv_dataset =(dataset + 1)*127.5
if self.method == 'stats':
mean = params.get('mean',(0.5, 0.5, 0.5))
std = params.get('std',(0.5, 0.5, 0.5))
mean, std = [torch.as_tensor(
 x, dtype=torch.float32, device=dataset.device)]
dataset.mul_(std[:, None, None] ).add_(mean[:, None, None])
 inv_dataset = dataset
return inv_dataset.long()<load_pretrained>
def noisy_labels(y, p_flip):
n_select = int(p_flip * int(y.shape[0]))
flip_ix = np.random.choice([i for i in range(int(y.shape[0])) ], size=n_select)
 op_list = []
for i in range(int(y.shape[0])) :
if i in flip_ix:
op_list.append(tf.subtract(1, y[i]))
else:
op_list.append(y[i])
 outputs = tf.stack(op_list)
 return outputs
print('Reading dogs images and annotations.')
 annots = load_annotations()
print(f'Total number of examples: {len(annots)}.')
 dogs = load_dogs_images(annots,(128, 128))
assert len(dogs)== len(annots)
 print(f'Dogs dataset shape: {dogs.shape}.')
 pils = as_pil_list(dogs)
 print(f'Numbers of PIL images: {len(pils)}')
 del dogs, annots<categorify>
generator_optimizer = tf.train.AdamOptimizer(learning_rate=lr_initial_g, beta1=0.5)
 discriminator_optimizer = tf.train.AdamOptimizer(learning_rate=lr_initial_d, beta1=0.5)
 cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=False )
class PILDataset:
def __init__(self, pil_images, transform=None):
self.pil_images = pil_images
self.tr = transform or(lambda x: x)
 def __getitem__(self, i):
if isinstance(i, int): return self.tr(self.pil_images[i])
 elif isinstance(i,(list, np.ndarray)) : return [self.tr(self.pil_images[ii])for ii in i]
elif isinstance(i, slice): return [self.tr(img)for img in self.pil_images[i]]
raise TypeError(f'unknown index type: {type(i)}')
 def __len__(self):
return len(self.pil_images)
 class RandomCropOfFive:
def __init__(self, size):
self.five_crop = transforms.FiveCrop(size)
 def __call__(self, x):
[idx] = np.random.randint(0, 4, 1)
 cropped = self.five_crop(x)[idx]
return cropped
def show_tensor(t, n_rows=4, denorm=False):
if denorm: t =(255 *(t + 1)/2)
 canvas = make_grid(t ).numpy().transpose(1, 2, 0 ).astype(np.uint8)
 f, ax = plt.subplots(1, 1)
 ax.imshow(canvas)
 ax.axis('off' )<normalization>
def discriminator_loss(real_output, fake_output, loss_func, apply_label_smoothing=True, label_noise=True):
if label_noise and apply_label_smoothing:
real_output_noise = noisy_labels(tf.ones_like(real_output), 0.05)
 fake_output_noise = noisy_labels(tf.zeros_like(fake_output), 0.05)
 real_output_smooth = smooth_positive_labels(real_output_noise)
 fake_output_smooth = smooth_negative_labels(fake_output_noise)
 if loss_func == 'gan':
real_loss = cross_entropy(tf.ones_like(real_output_smooth), real_output)
 fake_loss = cross_entropy(tf.zeros_like(fake_output_smooth), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output_smooth - tf.reduce_mean(fake_output_smooth)- tf.ones_like(real_output_smooth)))
 + tf.reduce_mean(tf.square(fake_output_smooth - tf.reduce_mean(real_output_smooth)+ tf.ones_like(fake_output_smooth)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output_smooth)
 avg_real_logit = tf.reduce_mean(real_output_smooth)
 D_r_tilde = tf.nn.sigmoid(real_output_smooth - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output_smooth - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_r_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_f_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output_smooth)-(real_output_smooth - tf.reduce_mean(fake_output_smooth))))
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output_smooth)+(fake_output_smooth - tf.reduce_mean(real_output_smooth))))
total_loss = real_loss + fake_loss
return total_loss
elif label_noise and not apply_label_smoothing:
real_output_noise = noisy_labels(tf.ones_like(real_output), 0.05)
 fake_output_noise = noisy_labels(tf.zeros_like(fake_output), 0.05)
 if loss_func == 'gan':
real_loss = cross_entropy(tf.ones_like(real_output_noise), real_output)
 fake_loss = cross_entropy(tf.zeros_like(fake_output_noise), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output_noise - tf.reduce_mean(fake_output_noise)- tf.ones_like(real_output_noise)))
 + tf.reduce_mean(tf.square(fake_output_noise - tf.reduce_mean(real_output_noise)+ tf.ones_like(fake_output_noise)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output_noise)
 avg_real_logit = tf.reduce_mean(real_output_noise)
 D_r_tilde = tf.nn.sigmoid(real_output_noise - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output_noise - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_r_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_f_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output_noise)-(real_output_noise - tf.reduce_mean(fake_output_noise))))
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output_noise)+(fake_output_noise - tf.reduce_mean(real_output_noise))))
total_loss = real_loss + fake_loss
return total_loss
elif apply_label_smoothing and not label_noise:
real_output_smooth = smooth_positive_labels(tf.ones_like(real_output))
fake_output_smooth = smooth_negative_labels(tf.zeros_like(fake_output))
if loss_func == 'gan':
real_loss = cross_entropy(tf.ones_like(real_output_smooth), real_output)
 fake_loss = cross_entropy(tf.zeros_like(fake_output_smooth), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output_smooth - tf.reduce_mean(fake_output_smooth)- tf.ones_like(real_output_smooth)))
 + tf.reduce_mean(tf.square(fake_output_smooth - tf.reduce_mean(real_output_smooth)+ tf.ones_like(fake_output_smooth)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output_smooth)
 avg_real_logit = tf.reduce_mean(real_output_smooth)
 D_r_tilde = tf.nn.sigmoid(real_output_smooth - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output_smooth - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_r_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_f_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output_smooth)-(real_output_smooth - tf.reduce_mean(fake_output_smooth))))
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output_smooth)+(fake_output_smooth - tf.reduce_mean(real_output_smooth))))
total_loss = real_loss + fake_loss
return total_loss
else:
if loss_func == 'gan':
real_loss = cross_entropy(tf.ones_like(real_output), real_output)
 fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output - tf.reduce_mean(fake_output)- tf.ones_like(real_output)))
 + tf.reduce_mean(tf.square(fake_output - tf.reduce_mean(real_output)+ tf.ones_like(fake_output)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output)
 avg_real_logit = tf.reduce_mean(real_output)
 D_r_tilde = tf.nn.sigmoid(real_output - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_r_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_f_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output)-(real_output - tf.reduce_mean(fake_output))))
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output)+(fake_output - tf.reduce_mean(real_output))))
total_loss = real_loss + fake_loss
return total_loss
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv')!= -1:
nn.init.normal_(m.weight.data, 0.0, 0.02)
 elif classname.find('BatchNorm')!= -1:
nn.init.normal_(m.weight.data, 1.0, 0.02)
 nn.init.constant_(m.bias.data, 0)
 class SpectralNorm(nn.Module):
def __init__(self, module):
super().__init__()
self.module = nn.utils.spectral_norm(module)
 def forward(self, x):
return self.module(x)
 class PixelwiseNorm(nn.Module):
def __init__(self, alpha=1e-8):
super().__init__()
self.alpha = alpha
def forward(self, x):
y = x.pow(2.).mean(dim=1, keepdim=True ).add(self.alpha ).sqrt()
y = x / y
return y
class MinibatchStdDev(nn.Module):
def __init__(self, alpha=1e-8):
super().__init__()
self.alpha = alpha
def forward(self, x):
batch_size, _, height, width = x.shape
y = x - x.mean(dim=0, keepdim=True)
 y = y.pow(2.).mean(dim=0, keepdim=False ).add(self.alpha ).sqrt()
y = y.mean().view(1, 1, 1, 1)
 y = y.repeat(batch_size, 1, height, width)
 y = torch.cat([x, y], 1)
 return y
class Mixup:
def __init__(self, alpha=0.2):
self.alpha = alpha
def __call__(self, b1, b2):
assert b1.size(0)== b2.size(0)
 lam = np.random.beta(self.alpha, self.alpha, size=b1.size(0))
lam = torch.from_numpy(lam ).float().to(b1.device)
 lam = lam.view(-1, 1, 1, 1)
 return lam*b1 +(1 - lam)*b2<define_search_model>
def generator_loss(real_output, fake_output, loss_func, apply_label_smoothing=True):
if apply_label_smoothing:
fake_output_smooth = smooth_negative_labels(tf.ones_like(fake_output))
if loss_func == 'gan':
return cross_entropy(tf.ones_like(fake_output_smooth), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output - tf.reduce_mean(fake_output_smooth)+ tf.ones_like(real_output)))
 + tf.reduce_mean(tf.square(fake_output_smooth - tf.reduce_mean(real_output)- tf.ones_like(fake_output_smooth)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output_smooth)
 avg_real_logit = tf.reduce_mean(real_output)
 D_r_tilde = tf.nn.sigmoid(real_output - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output_smooth - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_f_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_r_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output_smooth)-(fake_output_smooth - tf.reduce_mean(real_output))))
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output)+(real_output - tf.reduce_mean(fake_output_smooth))))
loss = fake_loss + real_loss
return loss
else:
if loss_func == 'gan':
return cross_entropy(tf.ones_like(fake_output), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output - tf.reduce_mean(fake_output)+ tf.ones_like(real_output)))
 + tf.reduce_mean(tf.square(fake_output - tf.reduce_mean(real_output)- tf.ones_like(fake_output)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output)
 avg_real_logit = tf.reduce_mean(real_output)
 D_r_tilde = tf.nn.sigmoid(real_output - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_f_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_r_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output)-(fake_output - tf.reduce_mean(real_output))))
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output)+(real_output - tf.reduce_mean(fake_output))))
loss = fake_loss + real_loss
return loss
class DCGAN_D(nn.Module):
def __init__(self):
super().__init__()
self.main = nn.Sequential(
 SpectralNorm(nn.Conv2d(3, 64, 4, 2, 1, bias=False)) ,
nn.LeakyReLU(0.2, inplace=True),
SpectralNorm(nn.Conv2d(64, 128, 4, 2, 1, bias=False)) ,
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
SpectralNorm(nn.Conv2d(128, 256, 4, 2, 1, bias=False)) ,
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2, inplace=True),
SpectralNorm(nn.Conv2d(256, 512, 4, 2, 1, bias=False)) ,
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2, inplace=True),
SpectralNorm(nn.Conv2d(512, 1024, 4, 2, 1, bias=False)) ,
nn.BatchNorm2d(1024),
nn.LeakyReLU(0.2, inplace=True),
MinibatchStdDev() ,
SpectralNorm(nn.Conv2d(1024 + 1, 1, 4, 2, 1, bias=False)) ,
nn.Sigmoid() 
)
 self.main.apply(weights_init)
 def forward(self, x):
return self.main(x )<define_search_model>
checkpoint_dir = '/training_checkpoints'
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
 checkpoint = tf.train.Checkpoint(generator_optimizer=generator_optimizer,
discriminator_optimizer=discriminator_optimizer,
generator=dog_generator,
discriminator=dog_discriminator )
class DCGAN_G(nn.Module):
def __init__(self, nz):
super().__init__()
self.nz = nz
self.main = nn.Sequential(
 SpectralNorm(nn.ConvTranspose2d(nz, 1024, 4, 1, 0, bias=False)) ,
nn.BatchNorm2d(1024),
nn.LeakyReLU(inplace=True),
SpectralNorm(nn.ConvTranspose2d(1024, 512, 4, 2, 1, bias=False)) ,
nn.BatchNorm2d(512),
nn.LeakyReLU(inplace=True),
SpectralNorm(nn.ConvTranspose2d(512, 256, 4, 2, 1, bias=False)) ,
nn.BatchNorm2d(256),
nn.LeakyReLU(inplace=True),
SpectralNorm(nn.ConvTranspose2d(256, 128, 4, 2, 1, bias=False)) ,
nn.BatchNorm2d(128),
nn.LeakyReLU(inplace=True),
SpectralNorm(nn.ConvTranspose2d(128, 64, 4, 2, 1, bias=False)) ,
nn.BatchNorm2d(64),
nn.LeakyReLU(inplace=True),
PixelwiseNorm() ,
SpectralNorm(nn.ConvTranspose2d(64, 3, 3, 1, 1, bias=False)) ,
nn.Tanh() 
)
 self.main.apply(weights_init)
 def forward(self, x):
return self.main(x )<create_dataframe>
EPOCHS = 320
num_examples_to_generate = 16
seed = tf.random.normal([num_examples_to_generate, noise_dim] )
bs = 16
nz = 128
lr_d = 0.0005
lr_g = 0.0005
beta_1 = 0.5
use_adam = True
mixup = Mixup(0.2)
 dataset = PILDataset(pils, transform=transforms.Compose([
transforms.Resize(70),
RandomCropOfFive(64),
transforms.RandomHorizontalFlip() ,
transforms.RandomRotation(3),
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
]))
netD = DCGAN_D().cuda()
netG = DCGAN_G(nz ).cuda()
if use_adam:
optimizerD = optim.Adam(netD.parameters() , lr=lr_d, betas=(beta_1, 0.999))
optimizerG = optim.Adam(netG.parameters() , lr=lr_g, betas=(beta_1, 0.999))
else:
optimizerD = optim.RMSprop(netD.parameters() , lr=lr_d)
 optimizerG = optim.RMSprop(netG.parameters() , lr=lr_g)
 schedD = CosineWithRestarts(optimizerD, eta_min=lr_d*0.1, t_initial=1000, t_mul=math.sqrt(2))
schedG = CosineWithRestarts(optimizerG, eta_min=lr_g*0.1, t_initial=1000, t_mul=math.sqrt(2))<statistical_test>
def train_step(images, G_loss, D_loss, loss_type='gan'):
noise = tf.random.normal([batch_size, noise_dim])
 with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
generated_images = dog_generator(noise, training=True)
 real_output = dog_discriminator(images, training=True)
 fake_output = dog_discriminator(generated_images, training=True)
 gen_loss = generator_loss(real_output, fake_output, loss_type, apply_label_smoothing=True)
 disc_loss = discriminator_loss(real_output, fake_output, loss_type,
apply_label_smoothing=True, label_noise=True)
 G_loss.append(gen_loss.numpy())
 D_loss.append(disc_loss.numpy())
 gradients_of_generator = gen_tape.gradient(gen_loss, dog_generator.trainable_variables)
 gradients_of_discriminator = disc_tape.gradient(disc_loss, dog_discriminator.trainable_variables)
 generator_optimizer.apply_gradients(zip(gradients_of_generator, dog_generator.trainable_variables))
discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, dog_discriminator.trainable_variables))
def truncated_normal(size, threshold=1):
return truncnorm.rvs(-threshold, threshold, size=size)
 def sample(dataset, batch_size):
idx = np.random.randint(0, len(dataset), batch_size)
 return torch.stack(dataset[idx] ).cuda()
def smooth_positive(labels):
jitter = torch.from_numpy(np.random.uniform(0.05, 0.1, len(labels)) ).float().to(labels.device)
 jitter = jitter.view(labels.size())
 return(labels - jitter)
 def exp_mov_avg(acc_net, curr_net, alpha=0.999, global_step=999):
alpha = min(1 - 1 /(global_step + 1), alpha)
 for ema_param, param in zip(acc_net.parameters() , curr_net.parameters()):
ema_param.data.mul_(alpha ).add_(1 - alpha, param.data )<compute_train_metric>
class KernelEvalException(Exception):
pass
@dataclass
class MiFIDEvaluator(object):
model_path: str
train_images_path: str
feature_path: str = None
imsize: int = 64
output_layer: str = 'Pretrained_Net/pool_3:0'
input_layer: str = 'Pretrained_Net/ExpandDims:0'
output_shape: int = 2048
cosine_distance_eps: float = 0.1
batch_size: int = 50
fid_epsilon: float = 1e-14
def __post_init__(self):
tf.reset_default_graph()
self.create_model_graph()
with tf.Session() as sess:
if self.feature_path is None:
self.mu2, self.sigma2, self.features2 = self._handle_path_memorization(
 self.train_images_path, sess, is_checksize=False, is_check_png=False)
 else:
with np.load(self.feature_path)as f:
self.mu2, self.sigma2, self.features2 = f['m'], f['s'], f['features']
def create_model_graph(self):
with tf.gfile.FastGFile(self.model_path, 'rb')as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
 _ = tf.import_graph_def(graph_def, name='Pretrained_Net')
 def img_read_checks(self, filename, is_checksize=False, is_check_png=False):
im = Image.open(str(filename))
if is_checksize and im.size !=(self.imsize, self.imsize):
raise KernelEvalException(f'The images are not of size {check_imsize}')
 if is_check_png and im.format != 'PNG':
raise KernelEvalException('Only PNG images should be submitted.')
 if self.imsize is None:
return im
else:
return im.resize(( self.imsize, self.imsize), Image.ANTIALIAS)
 def _get_model_layer(self, sess):
layer = sess.graph.get_tensor_by_name(self.output_layer)
 ops = layer.graph.get_operations()
for op_idx, op in enumerate(ops):
for o in op.outputs:
shape = o.get_shape()
if shape._dims != []:
shape = [s.value for s in shape]
new_shape = []
for j, s in enumerate(shape):
if s == 1 and j == 0:
new_shape.append(None)
 else:
new_shape.append(s)
 o.__dict__['_shape_val'] = tf.TensorShape(new_shape)
 return layer
def get_activations(self, images, sess):
inception_layer = self._get_model_layer(sess)
 n_images = images.shape[0]
if self.batch_size > n_images:
warnings.warn('batch size is bigger than the data size.setting batch size to data size')
 self.batch_size = n_images
n_batches = n_images // self.batch_size + 1
pred_arr = np.empty(( n_images, self.output_shape))
for i in range(n_batches):
start = i * self.batch_size
if start + self.batch_size < n_images:
end = start + self.batch_size
else:
end = n_images
batch = images[start:end]
pred = sess.run(inception_layer, {self.input_layer: batch})
 pred_arr[start:end] = pred.reshape(-1, self.output_shape)
 return pred_arr
def calculate_activation_statistics(self, images, sess):
act = self.get_activations(images, sess)
 mu = np.mean(act, axis=0)
 sigma = np.cov(act, rowvar=False)
 return mu, sigma, act
def _handle_path_memorization(self, path, sess, is_checksize, is_check_png):
path = Path(path)
 files = list(path.glob('*.jpg')) + list(path.glob('*.png'))
x = np.array([np.array(self.img_read_checks(fn, is_checksize, is_check_png)) for fn in files])
 m, s, features = self.calculate_activation_statistics(x, sess)
 del x
return m, s, features
def calculate_frechet_distance(self, mu1, sigma1):
mu1 = np.atleast_1d(mu1)
 mu2 = np.atleast_1d(self.mu2)
 sigma1 = np.atleast_2d(sigma1)
 sigma2 = np.atleast_2d(self.sigma2)
 assert mu1.shape == mu2.shape, 'Training and test mean vectors have different lengths'
assert sigma1.shape == sigma2.shape, 'Training and test covariances have different dimensions'
covmean, _ = linalg.sqrtm(sigma1.dot(sigma2), disp=False)
 if not np.isfinite(covmean ).all() :
msg = f'fid calculation produces singular product; adding {self.eps} to diagonal of cov estimates'
warnings.warn(msg)
 offset = np.eye(sigma1.shape[0])* self.eps
covmean = linalg.sqrtm(( sigma1 + offset ).dot(sigma2 + offset))
if np.iscomplexobj(covmean):
if not np.allclose(np.diagonal(covmean ).imag, 0, atol=1e-3):
m = np.max(np.abs(covmean.imag))
raise ValueError(f'Imaginary component {m}')
 covmean = covmean.real
tr_covmean = np.trace(covmean)
 return(mu1 - mu2 ).dot(mu1 - mu2)+ np.trace(sigma1)+ np.trace(sigma2)- 2 * tr_covmean
@staticmethod
def normalize_rows(x):
return np.nan_to_num(x / np.linalg.norm(x, ord=2, axis=1, keepdims=True))
def cosine_distance(self, features1):
features1_nozero = features1[np.sum(features1, axis=1)!= 0]
features2_nozero = self.features2[np.sum(self.features2, axis=1)!= 0]
norm_f1 = self.normalize_rows(features1_nozero)
 norm_f2 = self.normalize_rows(features2_nozero)
 d = 1.0 - np.abs(np.matmul(norm_f1, norm_f2.T))
mean_min_d = np.mean(np.min(d, axis=1))
return mean_min_d
def calculate_kid_given_paths(self, user_images_unzipped_path):
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
 m1, s1, features1 = self._handle_path_memorization(
 user_images_unzipped_path, sess, is_checksize=True, is_check_png=True)
 fid_value = self.calculate_frechet_distance(m1, s1)
 distance = self.cosine_distance(features1)
 return fid_value, distance
def distance_thresholding(self, d):
if d < self.cosine_distance_eps:
return d
else:
return 1
def evaluate(self, user_images_unzipped_path):
fid_value, distance = self.calculate_kid_given_paths(user_images_unzipped_path)
 distance = self.distance_thresholding(distance)
 return fid_value, distance, fid_value /(distance + self.fid_epsilon )
def ema(avg_net, curr_net, alpha=0.999):
for avg_param, curr_param in zip(avg_net.parameters() , curr_net.parameters()):
avg_param.data.mul_(alpha ).add_(1 - alpha, curr_param.data )<choose_model_class>
def zip_images(filename='images.zip'):
z = zipfile.PyZipFile(filename, mode='w')
 for k in range(image_sample_size):
generated_image = dog_generator(tf.random.normal([1, noise_dim]), training=False)
 f = str(k)+'.png'
img = np.array(generated_image)
 img =(img[0, :, :, :] + 1.) / 2.
 img = Image.fromarray(( 255*img ).astype('uint8' ).reshape(( image_height,image_width,image_channels)))
 img.save(f,'PNG')
 z.write(f)
 os.remove(f)
 z.close()
print('Saved final images for submission.')
 def save_images(directory=OUT_DIR):
for k in range(image_sample_size):
generated_image = dog_generator(tf.random.normal([1, noise_dim]), training=False)
 f = str(k)+'.png'
f = os.path.join(directory, f)
 img = np.array(generated_image)
 img =(img[0, :, :, :] + 1.) / 2.
 img = Image.fromarray(( 255*img ).astype('uint8' ).reshape(( image_height,image_width,image_channels)))
 img.save(f,'PNG')
 print('Saved temporary images for evaluation.' )
def train(bs=32):
print('Starting training loop...')
 epoch = 0
real_label = 1
fake_label = 0
loss_fn = nn.BCELoss()
n = len(dataset)
 n_batches = n // bs
avg_model = type(netG )(netG.nz ).cuda()
avg_model.load_state_dict(netG.state_dict())
 while True:
idx1 = np.random.permutation(n)
 idx2 = np.random.permutation(n)
 for i in range(n_batches):
if wd.timeout: return avg_model
epoch += 1
batch1 = torch.stack(dataset[idx1[i*bs:(i+1)*bs]] ).float().cuda()
batch2 = torch.stack(dataset[idx2[i*bs:(i+1)*bs]] ).float().cuda()
mixed = mixup(batch1, batch2)
 netD.zero_grad()
x_real = mixed.cuda()
batch_size = x_real.size(0)
 labels = torch.full(( batch_size, 1), real_label ).cuda()
labels = smooth_positive(labels)
 output = netD(x_real ).view(-1, 1)
 errD_real = loss_fn(output, labels)
 errD_real.backward()
d_x = output.mean().item()
noise = torch.from_numpy(truncated_normal(( batch_size, nz, 1, 1)) ).float().cuda()
x_fake = netG(noise)
 labels.fill_(fake_label)
 output = netD(x_fake.detach() ).view(-1, 1)
 errD_fake = loss_fn(output, labels)
 errD_fake.backward()
d_g_z1 = output.mean().item()
errD = errD_real + errD_fake
optimizerD.step()
netG.zero_grad()
labels.fill_(real_label)
 output = netD(x_fake ).view(-1, 1)
 errG = loss_fn(output, labels)
 errG.backward()
d_g_z2 = output.mean().item()
optimizerG.step()
if epoch % 150 == 0:
print(f'[{epoch:06d}][{i+1:03d}] '
f'lr_d: {schedD.get_values() [0]:.6f}, '
f'lr_g: {schedG.get_values() [0]:.6f} | '
f'loss_d: {errD.item() :.4f}, '
f'loss_g: {errG.item() :.4f} | '
f'D(x): {d_x:.4f}, D(G(z)) : {d_g_z1:.4f}/{d_g_z2:.4f}')
 schedD.step()
schedG.step()
ema(avg_model, netG)
 return avg_model<train_model>
def train(dataset, epochs):
G_loss = []
D_loss = []
for epoch in tqdm(range(epochs)) :
start = time.time()
new_lr_d = lr_initial_d
new_lr_g = lr_initial_g
global_step = 0
for image_batch in dataset:
train_step(image_batch, G_loss, D_loss)
 global_step = global_step + 1
if(epoch + 1)% 50 == 0:
plot_loss(G_loss, D_loss, epoch + 1)
 generate_and_save_images(dog_generator, epoch + 1, seed)

if(epoch + 1)% 200 == 0:
print('Reducing learning rate.. ')
 new_lr_d = tf.train.cosine_decay(new_lr_d, min(global_step, lr_decay_steps), lr_decay_steps)
 new_lr_g = tf.train.cosine_decay(new_lr_g, min(global_step, lr_decay_steps), lr_decay_steps)
 generator_optimizer = tf.train.AdamOptimizer(learning_rate=new_lr_d, beta1=0.5)
 discriminator_optimizer = tf.train.AdamOptimizer(learning_rate=new_lr_g, beta1=0.5)
 G_loss = []
D_loss = []
print('Epoch: {} computed for {} sec'.format(epoch + 1, time.time() - start))
generate_and_save_images(dog_generator, epochs, seed)
 checkpoint.save(file_prefix = checkpoint_prefix)

print('Final epoch.' )
avgG = train(bs=16 )<normalization>
train(dog_features_data, EPOCHS )
<import_modules><EOS> 
zip_images()