Kaggle competition: Generative Dog Images
<SOS> metric: PostProcessorKernel Kaggle data source: generative-dog-images<define_variables>
import shutil
import os
import random
from time import time
import matplotlib.pyplot as plt
import numpy as np
import torch
from torch.nn.utils import spectral_norm
from torch.utils.data import Dataset
import torchvision
import xml.etree.ElementTree as ET
from tqdm import tqdm_notebook as tqdm
from scipy.stats import truncnorm
cat_params = {'learning_rate': 0.02,
'depth': 5,
'l2_leaf_reg': 10,
'bootstrap_type': 'Bernoulli',
'od_type': 'Iter',
'od_wait': 50,
'random_seed': 11,
'allow_writing_files': False}
forcast_catc = np.zeros(len(xtest))
validation_pred_catc = np.zeros(len(xtrain))
scores_catc = []
valid_catc = np.zeros(len(label))
for fold_,(xtrn_indx, xvad_indx)in enumerate(folds.split(xtrain, label)) :
x_train, x_valid = xtrain[xtrn_indx], xtrain[xvad_indx]
y_train, y_valid = label[xtrn_indx], label[xvad_indx]
clf_catc = CatBoostClassifier(iterations=400, **cat_params)
 clf_catc.fit(x_train, y_train)
 pred_valid = clf_catc.predict(x_valid ).reshape(-1,)
 score = accuracy_score(y_valid, pred_valid)
 Pred_Real = clf_catc.predict_proba(xtest)[:, 1]
validation_pred_catc[xvad_indx] +=(pred_valid ).reshape(-1,)
 scores_catc.append(accuracy_score(y_valid, pred_valid))
forcast_catc += Pred_Real
valid_catc[xvad_indx] +=(y_valid)
 print(c_report(valid_catc[xvad_indx], validation_pred_catc[xvad_indx]))
print(c_matrix(valid_catc[xvad_indx], validation_pred_catc[xvad_indx]))
print('accuracy is: {}, std: {}.'.format(np.mean(scores_catc), np.std(scores_catc)) )<save_to_csv>
CHANNEL = {'RGB': 3, 'BW': 1}
IMG_CHANNEL = CHANNEL['RGB']
DATA_LOCATION = '.. /input/all-dogs/all-dogs/'
BATCH_SIZE = 32
LATENT_DIM = 128
CONV_DEPTHS_G = 32
CONV_DEPTHS_D = 48
LEARNING_RATE_G = 0.0003
LEARNING_RATE_D = 0.0001
BETA_1 = 0.5
T0_interval = 200
EPOCHS = 600
NUM_CLASSES = 10
TIME_FOR_TRAIN = 25000
sample_submission['target'] = forcast_lr4
sample_submission.to_csv('Forcasting.csv', index=False)
 sample_submission.head(20 )<load_from_csv>
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
train = pd.read_csv('.. /input/train.csv')
 train.head()<data_type_conversions>
class DataGenerator(Dataset):
def __init__(self, directory, transform=None, n_samples=np.inf):
self.directory = directory
self.transform = transform
self.n_samples = n_samples
self.samples = self._load_subfolders_images(directory)
 if len(self.samples)== 0:
raise RuntimeError("Found 0 files in subfolders of: {}".format(directory))
def _load_subfolders_images(self, root):
IMG_EXTENSIONS =(
 '.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif', '.tiff', '.webp')
 def is_valid_file(x):
return torchvision.datasets.folder.has_file_allowed_extension(x, IMG_EXTENSIONS)
 required_transforms = torchvision.transforms.Compose([
torchvision.transforms.Resize(64),
torchvision.transforms.CenterCrop(64),
])
 imgs = []
paths = []
for root, _, fnames in sorted(os.walk(root)) :
for fname in sorted(fnames)[:min(self.n_samples, 999999999999999)]:
path = os.path.join(root, fname)
 paths.append(path)
 for path in paths:
if is_valid_file(path):
img = torchvision.datasets.folder.default_loader(path)
 annotation_basename = os.path.splitext(os.path.basename(path)) [0]
annotation_dirname = next(
 dirname for dirname in os.listdir('.. /input/annotation/Annotation/')if
dirname.startswith(annotation_basename.split('_')[0]))
annotation_filename = os.path.join('.. /input/annotation/Annotation/',
annotation_dirname, annotation_basename)
 tree = ET.parse(annotation_filename)
 root = tree.getroot()
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)
 w = np.min(( xmax - xmin, ymax - ymin))
bbox =(xmin, ymin, xmin + w, ymin + w)
 object_img = required_transforms(img.crop(bbox))
imgs.append(object_img)
 return imgs
def __getitem__(self, index):
sample = self.samples[index]
if self.transform is not None:
sample = self.transform(sample)
 return np.asarray(sample)
 def __len__(self):
return len(self.samples)
 transform = torchvision.transforms.Compose([torchvision.transforms.RandomHorizontalFlip(p=0.1),
torchvision.transforms.ToTensor() ,
torchvision.transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5)) ])
 data_gen = DataGenerator(DATA_LOCATION, transform=transform, n_samples=25000)
 train_loader = torch.utils.data.DataLoader(data_gen, shuffle=True, batch_size=BATCH_SIZE, num_workers=4 )
train.pop('id')
 target = train.pop('target' ).astype(int)
 test = pd.read_csv('.. /input/test.csv')
 ids = test.pop('id' )<define_variables>
class PixelwiseNorm(torch.nn.Module):
def __init__(self):
super(PixelwiseNorm, self ).__init__()
def forward(self, x, alpha=1e-8):

 y = x.pow(2.).mean(dim=1, keepdim=True ).add(alpha ).sqrt()
y = x / y
return y
class Generator(torch.nn.Module):
def __init__(self, latent_dim, nfeats, nchannels):
super(Generator, self ).__init__()
self.conv1 = spectral_norm(torch.nn.ConvTranspose2d(latent_dim, nfeats * 8, 4, 1, 0, bias=False))
self.conv2 = spectral_norm(torch.nn.ConvTranspose2d(nfeats * 8, nfeats * 8, 4, 2, 1, bias=False))
self.conv3 = spectral_norm(torch.nn.ConvTranspose2d(nfeats * 8, nfeats * 4, 4, 2, 1, bias=False))
self.conv4 = spectral_norm(torch.nn.ConvTranspose2d(nfeats * 4, nfeats * 2, 4, 2, 1, bias=False))
self.conv5 = spectral_norm(torch.nn.ConvTranspose2d(nfeats * 2, nfeats, 4, 2, 1, bias=False))
self.conv6 = spectral_norm(torch.nn.ConvTranspose2d(nfeats, nchannels, 3, 1, 1, bias=False))
self.pixnorm = PixelwiseNorm()
def forward(self, x):
x = torch.nn.functional.leaky_relu(self.conv1(x))
x = self.pixnorm(x)
 x = torch.nn.functional.leaky_relu(self.conv2(x))
x = torch.nn.Dropout(0.05 )(x)
 x = self.pixnorm(x)
 x = torch.nn.functional.leaky_relu(self.conv3(x))
x = self.pixnorm(x)
 x = torch.nn.functional.leaky_relu(self.conv4(x))
x = torch.nn.Dropout(0.05 )(x)
 x = self.pixnorm(x)
 x = torch.nn.functional.leaky_relu(self.conv5(x))
x = self.pixnorm(x)
 x = torch.tanh(self.conv6(x))
return x
code =<define_variables>
class MinibatchStdDev(torch.nn.Module):

 def __init__(self):

 super(MinibatchStdDev, self ).__init__()
def forward(self, x, alpha=1e-8):

 batch_size, _, height, width = x.shape
y = x - x.mean(dim=0, keepdim=True)
 y = torch.sqrt(y.pow(2.).mean(dim=0, keepdim=False)+ alpha)
 y = y.mean().view(1, 1, 1, 1)
 y = y.repeat(batch_size, 1, height, width)
 y = torch.cat([x, y], 1)
 return y
data = {
'N': 250,
'N2': 19750,
'K': 300,
'y': target,
'X': train,
'new_X': test,
}
sm = ps.StanModel(model_code=code)
 fit = sm.sampling(data=data, seed=1234)
 ex = fit.extract(permuted=True)
 target = np.mean(ex['y_pred'], axis=0)
 df = pd.DataFrame({'id': ids, 'target': target})
 df[['id', 'target']].to_csv('submission.csv', index=False )<set_options>
class Discriminator(torch.nn.Module):
def __init__(self, nchannels, nfeats):
super(Discriminator, self ).__init__()
self.conv1 = torch.nn.Conv2d(nchannels, nfeats, 4, 2, 1, bias=False)
 self.conv2 = spectral_norm(torch.nn.Conv2d(nfeats, nfeats * 2, 4, 2, 1, bias=False))
self.bn2 = torch.nn.BatchNorm2d(nfeats * 2)
 self.conv3 = spectral_norm(torch.nn.Conv2d(nfeats * 2, nfeats * 4, 4, 2, 1, bias=False))
self.bn3 = torch.nn.BatchNorm2d(nfeats * 4)
 self.conv4 = spectral_norm(torch.nn.Conv2d(nfeats * 4, nfeats * 8, 4, 2, 1, bias=False))
self.bn4 = torch.nn.MaxPool2d(2)
 self.batch_discriminator = MinibatchStdDev()
self.conv5 = spectral_norm(torch.nn.Conv2d(nfeats * 8 + 1, 1, 2, 1, 0, bias=False))
def forward(self, x):
x = torch.nn.functional.leaky_relu(self.conv1(x), 0.2)
 x = torch.nn.functional.leaky_relu(self.bn2(self.conv2(x)) , 0.2)
 x = torch.nn.functional.leaky_relu(self.bn3(self.conv3(x)) , 0.2)
 x = torch.nn.functional.leaky_relu(self.bn4(self.conv4(x)) , 0.2)
 x = self.batch_discriminator(x)
 x = torch.sigmoid(self.conv5(x))
return x.view(-1, 1)
seed(639)
 set_random_seed(5944)
<load_from_csv>
def truncated_normal(size, threshold=1):
values = truncnorm.rvs(-threshold, threshold, size=size)
 return values
%time
float_data = pd.read_csv(".. /input/train.csv", dtype={"acoustic_data": np.float32, "time_to_failure": np.float32} ).values<feature_engineering>
def train_module(epochs):
step = 0
start = time()
for epoch in range(epochs):
for ii,(real_images)in enumerate(train_loader):
end = time()
if(end - start)> TIME_FOR_TRAIN:
break
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)+ np.random.uniform(-0.1, 0.1)
 output = netD(real_images)
 errD_real = criterion(output, labels)
 errD_real.backward()
D_x = output.mean().item()
noise = torch.randn(batch_size, LATENT_DIM, 1, 1, device=device)
 fake = netG(noise)
 labels.fill_(fake_label)+ np.random.uniform(0, 0.2)
 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 step % 500 == 0:
print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f D(x): %.4f D(G(z)) : %.4f / %.4f'
%(epoch + 1, EPOCHS, ii, len(train_loader),
errD.item() , errG.item() , D_x, D_G_z1, D_G_z2))
valid_image = netG(fixed_noise)
 step += 1
lr_schedulerG.step(epoch)
 lr_schedulerD.step(epoch)
 if epoch % 5 == 0:
show_generated_img()
print(end - start)
 if(end - start)> TIME_FOR_TRAIN:
break
<define_variables><EOS> 
netG = Generator(LATENT_DIM, CONV_DEPTHS_G, IMG_CHANNEL ).to(device)
 netD = Discriminator(3, CONV_DEPTHS_D ).to(device)
 criterion = torch.nn.BCELoss()
optimizerD = torch.optim.Adam(netD.parameters() , lr=LEARNING_RATE_G, betas=(BETA_1, 0.999))
optimizerG = torch.optim.Adam(netG.parameters() , lr=LEARNING_RATE_D, betas=(BETA_1, 0.999))
lr_schedulerG = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizerG,
T_0=EPOCHS // T0_interval, eta_min=0.00005)
 lr_schedulerD = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizerD,
T_0=EPOCHS // T0_interval, eta_min=0.00005)
 fixed_noise = torch.randn(25, LATENT_DIM, 1, 1, device=device)
 real_label = 0.8
fake_label = 0.15
batch_size = train_loader.batch_size
train_module(EPOCHS )