Kaggle competition: Generative Dog Images
<SOS> metric: PostProcessorKernel Kaggle data source: generative-dog-images<install_modules>
%matplotlib inline
!pip install lazypredict<load_from_csv>
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
data = pd.read_csv('.. /input/forest-cover-type-prediction/train.csv')
 data.head(5 )<prepare_x_and_y>
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}' )
class DistanceTransformer(BaseEstimator, TransformerMixin):
def __init__(self):
pass
def fit(self, X, y=None):
return self
def transform(self, X):
X = X.copy()
X['EuclideanDistanceHidroloy'] = np.around(
 np.sqrt(X['Horizontal_Distance_To_Hydrology'] **2 +
X['Vertical_Distance_To_Hydrology'] **2),
4)
 X['Elevation_Vertical_Hydro_Minus'] = X['Elevation'] - X['Vertical_Distance_To_Hydrology']
X['Elevation_Vertical_Hydro_Plus'] = X['Elevation'] + X['Vertical_Distance_To_Hydrology']
X['Elevation_Vertical_Hydro_Prod'] = X['Elevation'] * X['Vertical_Distance_To_Hydrology']
X['Elevation_Horizontal_Hydro_Minus'] = X['Elevation'] - X['Horizontal_Distance_To_Hydrology']
X['Elevation_Horizontal_Hydro_Plus'] = X['Elevation'] + X['Horizontal_Distance_To_Hydrology']
X['Elevation_Horizontal_Hydro_Prod'] = X['Elevation'] * X['Horizontal_Distance_To_Hydrology']
X['Elevation_Horizontal_Fire_Minus'] = X['Elevation'] - X['Horizontal_Distance_To_Fire_Points']
X['Elevation_Horizontal_Fire_Plus'] = X['Elevation'] + X['Horizontal_Distance_To_Fire_Points']
X['Elevation_Horizontal_Fire_Prod'] = X['Elevation'] * X['Horizontal_Distance_To_Fire_Points']
X['Elevation_Horizontal_Roadways_Minus'] = X['Elevation'] - X['Horizontal_Distance_To_Roadways']
X['Elevation_Horizontal_Roadways_Plus'] = X['Elevation'] + X['Horizontal_Distance_To_Roadways']
X['Elevation_Horizontal_Roadways_Prod'] = X['Elevation'] * X['Horizontal_Distance_To_Roadways']
X['Hidrology_Horizonal_Fire_Minus'] = X['Horizontal_Distance_To_Hydrology'] - X['Horizontal_Distance_To_Fire_Points']
X['Hidrology_Horizonal_Fire_Plus'] = X['Horizontal_Distance_To_Hydrology'] + X['Horizontal_Distance_To_Fire_Points']
X['Hidrology_Horizonal_Fire_Prod'] = X['Horizontal_Distance_To_Hydrology'] * X['Horizontal_Distance_To_Fire_Points']
X['Hidrology_Horizonal_Roadways_Minus'] = X['Horizontal_Distance_To_Hydrology'] - X['Horizontal_Distance_To_Roadways']
X['Hidrology_Horizonal_Roadways_Plus'] = X['Horizontal_Distance_To_Hydrology'] + X['Horizontal_Distance_To_Roadways']
X['Hidrology_Horizonal_Roadways_Prod'] = X['Horizontal_Distance_To_Hydrology'] * X['Horizontal_Distance_To_Roadways']
X['Hidrology_Horizonal_Vertical_Minus'] = X['Horizontal_Distance_To_Hydrology'] - X['Vertical_Distance_To_Hydrology']
X['Hidrology_Horizonal_Vertical_Plus'] = X['Horizontal_Distance_To_Hydrology'] + X['Vertical_Distance_To_Hydrology']
X['Hidrology_Horizonal_Vertical_Prod'] = X['Horizontal_Distance_To_Hydrology'] * X['Vertical_Distance_To_Hydrology']
X['Hidrology_Vertical_Fire_Minus'] = X['Vertical_Distance_To_Hydrology'] - X['Horizontal_Distance_To_Fire_Points']
X['Hidrology_Vertical_Fire_Plus'] = X['Vertical_Distance_To_Hydrology'] + X['Horizontal_Distance_To_Fire_Points']
X['Hidrology_Vertical_Fire_Prod'] = X['Vertical_Distance_To_Hydrology'] * X['Horizontal_Distance_To_Fire_Points']
X['Hidrology_Vertical_Fire_Minus'] = X['Vertical_Distance_To_Hydrology'] - X['Horizontal_Distance_To_Roadways']
X['Hidrology_Vertical_Fire_Plus'] = X['Vertical_Distance_To_Hydrology'] + X['Horizontal_Distance_To_Roadways']
X['Hidrology_Vertical_Fire_Prod'] = X['Vertical_Distance_To_Hydrology'] * X['Horizontal_Distance_To_Roadways']
X.drop(['Horizontal_Distance_To_Hydrology', 'Vertical_Distance_To_Hydrology', 'Horizontal_Distance_To_Roadways', 'Horizontal_Distance_To_Fire_Points'], axis=1, inplace=True)
 return X<train_model>
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()
class DropIdentifierFeatures(BaseEstimator, TransformerMixin):
def __init__(self):
pass
def fit(self, X, y=None):
return self
def transform(self, X):
X = X.copy()
X.drop('Id', axis=1, inplace=True)
 return X<categorify>
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
class FromDummiesToCategories(BaseEstimator, TransformerMixin):
def __init__(self, cols_to_operate, new_column_name):
self.cols_to_operate = cols_to_operate
self.new_column_name = new_column_name
def fit(self, X, y=None):
return self
def transform(self, X):
X = X.copy()
X1 = pd.DataFrame(X[self.cols_to_operate])
 serie = X1.columns[np.where(X1!=0)[1]]
X[self.new_column_name] = serie
X.drop(self.cols_to_operate, axis=1, inplace=True)
 return X<categorify>
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' )
class AspectTransformer(BaseEstimator, TransformerMixin):
def __init__(self, column, new_name):
self.column = column
self.new_name = new_name
def fit(self, X, y=None):
return self
def transform(self, X):
X = X.copy()
X[self.new_name] = X[self.column].copy().apply(self._reclassify_aspect)
 X.drop(self.column, axis=1, inplace=True)
 return X
def _reclassify_aspect(self, x):
if x<0:
return 'Flat'
if 0 >= x < 45:
return 'North'
if 45 >= x < 90:
return 'North_East'
if 90 >= x < 135:
return 'East'
if 135 >= x < 180:
return 'South_East'
if 180 >= x < 225:
return 'South'
if 225 >= x < 270:
return 'South_West'
if 270 >= x < 315:
return 'West'
if 315 >= x <360:
return 'North_West'
if 360 >= x :
return 'North_West'<split>
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
sss = StratifiedShuffleSplit(n_splits=5, test_size=0.2, random_state=42)
 X = data.drop('Cover_Type', axis=1 ).copy()
y = data['Cover_Type'].copy()
for train_index, test_index in sss.split(X, y):
X_train, X_test = X.iloc[train_index], X.iloc[test_index]
y_train, y_test = y.iloc[train_index], y.iloc[test_index]<define_variables>
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 )
soil_columns = [x for x in X.columns if x.startswith('Soil_Type')]
wilder_columns = [x for x in X.columns if x.startswith('Wilder')]<import_modules>
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 )
from feature_engine.discretisation import EqualWidthDiscretiser, EqualFrequencyDiscretiser
from feature_engine.selection import DropConstantFeatures, DropDuplicateFeatures, DropCorrelatedFeatures, SelectBySingleFeaturePerformance, RecursiveFeatureElimination, SmartCorrelatedSelection, DropFeatures
from feature_engine.encoding import RareLabelEncoder, OneHotEncoder, OrdinalEncoder
from feature_engine.outliers import Winsorizer
from feature_engine.creation import MathematicalCombination
from sklearn.pipeline import Pipeline<categorify>
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))
pipeline_list_1 = [ 
('dropuniquefeatures', DropIdentifierFeatures()), 
('aspect', AspectTransformer(column='Aspect', new_name='Orientation')) , 
('soil_columns_dummies', FromDummiesToCategories(cols_to_operate=soil_columns, new_column_name='Soil_Type')) , 
('wilder_columns_dummies', FromDummiesToCategories(cols_to_operate=wilder_columns, new_column_name='Wilderness')) , 
('soil_rare', RareLabelEncoder(tol=0.05, variables=['Soil_Type'])) , 
('dp', DropConstantFeatures(tol=0.99)) , 
('dd', DropDuplicateFeatures()), 
('dt', DistanceTransformer()), 
('one_hot',OneHotEncoder(variables=['Soil_Type', 'Wilderness', 'Orientation'])) , 
('dteq', EqualFrequencyDiscretiser(q=10, variables=['EuclideanDistanceHidroloy',
'Hillshade_9am', 'Hillshade_Noon', 'Hillshade_3pm'])) , 
('dcf', DropCorrelatedFeatures(variables=None, method='pearson', threshold=0.80)) ,
]
pipeline_1 = Pipeline(pipeline_list_1)
 X_train_pipe_1 = pipeline_1.fit_transform(X_train)
 X_train_pipe_1.head()<categorify>
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 )
X_test_pipe_1 = pipeline_1.transform(X_test)
 X_test_pipe_1.head()<categorify>
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 )
pipeline_list_2 = [ 
('dropuniquefeatures', DropIdentifierFeatures()), 
('aspect', AspectTransformer(column='Aspect', new_name='Orientation')) , 
('soil_columns_dummies', FromDummiesToCategories(cols_to_operate=soil_columns, new_column_name='Soil_Type')) , 
('wilder_columns_dummies', FromDummiesToCategories(cols_to_operate=wilder_columns, new_column_name='Wilderness')) , 
('soil_rare', RareLabelEncoder(tol=0.05, variables=['Soil_Type'])) , 
('dp', DropConstantFeatures(tol=0.99)) , 
('dd', DropDuplicateFeatures()), 
('dt', DistanceTransformer()), 
('one_hot',OneHotEncoder(variables=['Soil_Type', 'Wilderness', 'Orientation'])) , 
('winds', Winsorizer(variables=['EuclideanDistanceHidroloy', 'Elevation',
'Hillshade_9am', 'Hillshade_Noon', 'Hillshade_3pm'])) , 
('dcf', DropCorrelatedFeatures(variables=None, method='pearson', threshold=0.80)) ,
]
pipeline_2 = Pipeline(pipeline_list_2)
 X_train_pipe_2 = pipeline_2.fit_transform(X_train)
 X_train_pipe_2.head()<categorify>
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
pipeline_list_3 = [ 
('dropuniquefeatures', DropIdentifierFeatures()), 
('aspect', AspectTransformer(column='Aspect', new_name='Orientation')) , 
('soil_columns_dummies', FromDummiesToCategories(cols_to_operate=soil_columns, new_column_name='Soil_Type')) , 
('wilder_columns_dummies', FromDummiesToCategories(cols_to_operate=wilder_columns, new_column_name='Wilderness')) , 
('soil_rare', RareLabelEncoder(tol=0.05, variables=['Soil_Type'])) , 
('dp', DropConstantFeatures(tol=0.99)) , 
('dt', DistanceTransformer()), 
('one_hot',OneHotEncoder(variables=['Soil_Type', 'Wilderness', 'Orientation'])) , 
('winds', Winsorizer(variables=['EuclideanDistanceHidroloy', 'Elevation',
'Hillshade_9am', 'Hillshade_Noon', 'Hillshade_3pm'])) , 
('dd', SmartCorrelatedSelection()),
]
pipeline_3 = Pipeline(pipeline_list_3)
 X_train_pipe_3 = pipeline_3.fit_transform(X_train)
 X_train_pipe_3.head()<categorify>
avgG = train(bs=16 )
pipeline_list_4 = [ 
('dropuniquefeatures', DropIdentifierFeatures()), 
('dropwild_soil',DropFeatures(soil_columns + wilder_columns)) , 
('aspect', AspectTransformer(column='Aspect', new_name='Orientation')) , 
('dp', DropConstantFeatures(tol=0.99)) , 
('dt', DistanceTransformer()), 
('one_hot',OneHotEncoder(variables=['Orientation'])) , 
('winds', Winsorizer(variables=[
'EuclideanDistanceHidroloy', 'Elevation', 'Hillshade_9am', 'Hillshade_Noon', 'Hillshade_3pm'])) , 
('dd', SmartCorrelatedSelection()),
]
pipeline_4 = Pipeline(pipeline_list_4)
 X_train_pipe_4 = pipeline_4.fit_transform(X_train)
 X_train_pipe_4.head()<categorify>
print('Final model images generation.')
 print('Creating archive to write the images.')
 arch = zipfile.ZipFile('images.zip', 'w')
 img_no = 0
for batch in range(100):
t_noise = torch.from_numpy(truncated_normal(( 100, nz, 1, 1)) ).float().cuda()
images = avgG(t_noise ).detach().cpu()
images = images.mul(0.5 ).add(0.5)
 images =(255 * images.numpy() ).astype(np.uint8)
 images = images.transpose(0, 2, 3, 1)
 for image in images:
buf = io.BytesIO()
PIL.Image.fromarray(image ).save(buf, format='png')
 buf.seek(0)
 arch.writestr(f'{img_no}.png', buf.getvalue())
 img_no += 1
arch.close()
print('Saving is done!' )