Kaggle competition: Generative Dog Images
<SOS> metric: PostProcessorKernel Kaggle data source: generative-dog-images<prepare_x_and_y>
import numpy as np
import pandas as pd
import keras
from keras.layers import Input, Dense, Reshape, Flatten, Dropout,Concatenate
from keras.backend import random_normal,ones_like,zeros_like,mean
from keras.backend import get_session
from keras.layers import BatchNormalization, Activation, ZeroPadding2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import UpSampling2D, Conv2D
from keras.models import Sequential, Model
from keras.optimizers import Adam
from keras.layers import concatenate
from keras.initializers import TruncatedNormal
from keras.callbacks import LearningRateScheduler, EarlyStopping, History
from PIL import Image
import warnings
import os
import time
from glob import glob
import datetime
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np, pandas as pd, os
import xml.etree.ElementTree as ET
import matplotlib.pyplot as plt, zipfile
from PIL import Image
from glob import glob
data = pd.read_csv(".. /input/train.csv")
 X = data.iloc[:,2:].values
y = data.iloc[:,1:2].values<split>
ComputeLB = False
DogsOnly = False
ROOT = '.. /input/generative-dog-images/'
if not ComputeLB: ROOT = '.. /input/'
IMAGES = os.listdir(ROOT + 'all-dogs/all-dogs/')
 breeds = os.listdir(ROOT + 'annotation/Annotation/')
 idxIn = 0; namesIn = []
imagesIn = np.zeros(( 25000,64,64,3))
if DogsOnly:
for breed in breeds:
for dog in os.listdir(ROOT+'annotation/Annotation/'+breed):
try: img = Image.open(ROOT+'all-dogs/all-dogs/'+dog+'.jpg')
 except: continue
tree = ET.parse(ROOT+'annotation/Annotation/'+breed+'/'+dog)
 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))
img2 = img.crop(( xmin, ymin, xmin+w, ymin+w))
img2 = img2.resize(( 64,64), Image.ANTIALIAS)
 imagesIn[idxIn,:,:,:] = np.asarray(img2)
 namesIn.append(breed)
 idxIn += 1
idx = np.arange(idxIn)
 np.random.shuffle(idx)
 imagesIn = imagesIn[idx,:,:,:]
namesIn = np.array(namesIn)[idx]
else:
IMAGES = np.sort(IMAGES)
 np.random.seed(810)
 x = np.random.choice(np.arange(20579),10000)
 np.random.seed(None)
 for k in range(len(x)) :
img = Image.open(ROOT + 'all-dogs/all-dogs/' + IMAGES[x[k]])
 w = img.size[0]; h = img.size[1];
if(k%2==0)|(k%3==0):
w2 = 100; h2 = int(h/(w/100))
a = 18; b = 0
else:
a=0; b=0
if w<h:
w2 = 64; h2 = int(( 64/w)*h)
 b =(h2-64)//2
else:
h2 = 64; w2 = int(( 64/h)*w)
 a =(w2-64)//2
img = img.resize(( w2,h2), Image.ANTIALIAS)
 img = img.crop(( 0+a, 0+b, 64+a, 64+b))
imagesIn[idxIn,:,:,:] = np.asarray(img)
 namesIn.append(IMAGES[x[k]])
 idxIn += 1
x = np.random.randint(0,idxIn,25)
 for k in range(5):
plt.figure(figsize=(15,3))
for j in range(5):
plt.subplot(1,5,j+1)
 img = Image.fromarray(imagesIn[x[k*5+j],:,:,:].astype('uint8'))
plt.axis('off')
 if not DogsOnly: plt.title(namesIn[x[k*5+j]],fontsize=11)
 else: plt.title(namesIn[x[k*5+j]].split('-')[1],fontsize=11)
 plt.imshow(img)
 plt.show()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42, stratify = y )<train_on_grid>
IMG_SIZE = Input(( 12288,))
IMG_SIZE_2 = Input(( 10000,))
NOISE_SIZE = 10000
BATCH_SIZE = 64
clf_gini = DecisionTreeClassifier(max_depth=1, random_state = 100)
 clf_gini.fit(X, y)
 scores = cross_val_score(clf_gini, X, y, cv=5)
 scores<train_on_grid>
def discriminatorFunction() :
input_layer = Dense(12288, activation='sigmoid' )(IMG_SIZE_2)
 input_layer = Reshape(( 2,12288,1))(concatenate([IMG_SIZE,input_layer]))
discriminator = Conv2D(filters = 1, kernel_size=[2,1],use_bias=False, name = 'layer_1' )(input_layer)
 out = Flatten()(discriminator)
 return out
clf = LogisticRegression(solver='liblinear',C= 0.1, max_iter=10000)
 clf.fit(X, y)
 scores = cross_val_score(clf, X, y, cv=5)
 scores<load_from_csv>
print("Discriminator")
 model = discriminatorFunction()
model_discriminator = Model([IMG_SIZE,IMG_SIZE_2], model)
 model_discriminator.get_layer('layer_1' ).trainable = False
model_discriminator.get_layer('layer_1' ).set_weights([np.array([[[[-1.0 ]]],[[[1.0]]]])])
 model_discriminator.summary()
model_discriminator.compile(optimizer='adam', loss='binary_crossentropy' )
test = pd.read_csv(".. /input/test.csv")
 test_data =test.iloc[:,1:]<save_to_csv>
def GeneratorFunction(noise_shape=(NOISE_SIZE,)) :
input_layer = Input(noise_shape)
 generated = Dense(12288, activation='linear' )(input_layer)
 model = Model(inputs=input_layer,outputs = [generated,Reshape(( 10000,))(input_layer)])
 model.summary()
return model
ans= clf_gini.predict_proba(test_data.values)
 submit = pd.read_csv('.. /input/sample_submission.csv')
 submit['target'] = ans[:,1]
submit.to_csv('submit.csv', index = False )<load_from_csv>
print("Generator")
 model_generator = GeneratorFunction(noise_shape=(NOISE_SIZE,))
train = pd.read_csv(".. /input/train.csv" ).drop('id', axis=1)
 y_train = train['target']
X_train = train.drop('target', axis=1)
 X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size = 0.3, random_state = 40 )<train_model>
train_y =(imagesIn[:10000,:,:,:]/255.).reshape(( -1,12288))
train_X = np.zeros(( 10000,10000))
for i in range(10000): train_X[i,i] = 1
zeros = np.zeros(( 10000,12288))
lr = 0.5
for k in range(5):
LR_Scheduler = LearningRateScheduler(lambda x: lr)
 h = model_discriminator.fit([zeros,train_X], train_y, epochs = 100,batch_size = BATCH_SIZE, callbacks=[LR_Scheduler], verbose=0)
 print('Epoch',(k+1)*10,'/50 - loss =',h.history['loss'][-1])
 if h.history['loss'][-1]<0.533: lr = 0.1
<train_on_grid>
del train_X, train_y, imagesIn
def fit_and_evaluate(model):
model.fit(X_train, y_train)
 model_pred = model.predict(X_test)
 model_auroc = roc_auc_score(y_test, model_pred)
 return model_auroc<train_model>
model_discriminator.trainable = False
z = Input(shape=(NOISE_SIZE,))
img = model_generator(z)
 real = model_discriminator(img)
 gan = Model(z, real)
 gan.get_layer('model_1' ).get_layer('layer_1' ).set_weights([np.array([[[[-1 ]]],[[[255.]]]])])
 gan.compile(optimizer=Adam(5), loss='mean_squared_error')
 print("Model created based on Discriminator and Generator")
 gan.summary()
gradient_boosted = GradientBoostingRegressor(random_state=60)
 gradient_boosted_auroc = fit_and_evaluate(gradient_boosted)
 print('Gradient Boosted Regression Performance on the test set: AUROC = %0.4f' % gradient_boosted_auroc )<train_model>
train = np.zeros(( 10000,10000))
for i in range(10000): train[i,i] = 1
zeros = np.zeros(( 10000,12288))
Steps_per_epoch = 50
print("Training begins...Total steps per epoch: {}".format(Steps_per_epoch))
lr = LinearRegression()
lr_auroc = fit_and_evaluate(lr)
 print('Linear Regression Performance on the test set: AUROC = %0.4f' % lr_auroc )<train_on_grid>
warnings.filterwarnings("ignore")
 lr = 5.
 for step in range(Steps_per_epoch):
LR_Scheduler = LearningRateScheduler(lambda x: lr)
 h = gan.fit(train, zeros, epochs = 1, batch_size=256, callbacks=[LR_Scheduler], verbose=0)
 if(step<10)|(step%5==4):
print("Step: {}/{} [G loss: {:.4f}]".format(
(step+1)*10, Steps_per_epoch*10, h.history['loss'][-1]))
if h.history['loss'][-1] < 25: lr = 1.
 if h.history['loss'][-1] < 1.5: lr = 0.01
if step<10:
plt.figure(figsize=(15,3))
for j in range(5):
zz = np.zeros(( 10000))
zz[np.random.randint(10000)] = 1
plt.subplot(1,5,j+1)
 img = model_generator.predict(zz.reshape(( -1,10000)))[0].reshape(( -1,64,64,3))
img = Image.fromarray(( img ).astype('uint8' ).reshape(( 64,64,3)))
 plt.axis('off')
 plt.imshow(img)
 plt.show()
<train_model><EOS> 
class DogGenerator:
index = 0
def getDog(self,seed):
xx = np.zeros(( 10000))
xx[self.index] = 0.999
xx[np.random.randint(10000)] = 0.001
img = model_generator.predict(xx.reshape(( -1,10000)))[0].reshape(( 64,64,3))
self.index =(self.index+1)%10000
return Image.fromarray(img.astype('uint8'))