Kaggle competition: Generative Dog Images
<SOS> metric: PostProcessorKernel Kaggle data source: generative-dog-images<prepare_output>
import numpy as np
import pandas as pd
import os
Submission['Id']=test_data['Id']
Submission.set_index('Id',inplace=True )<save_to_csv>
ComputeLB=True
DogsOnly = True
ROOT = '.. /input/'
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:
x = np.random.choice(np.arange(20579),10000)
 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]
sz = np.min(( w,h))
a=0; b=0
if w<h: b =(h-sz)//2
else: a =(w-sz)//2
img = img.crop(( 0+a, 0+b, sz+a, sz+b))
img = img.resize(( 64,64), Image.ANTIALIAS)
 imagesIn[idxIn,:,:,:] = np.asarray(img)
 namesIn.append(IMAGES[x[k]])
 if idxIn%1000==0: print(idxIn)
 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()
Submission.to_csv('Submission_first_time.csv' )<load_from_csv>
from keras.models import Model
from keras.layers import Input,Dense,Conv2D,Reshape,Flatten,concatenate,UpSampling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler
from keras.optimizers import SGD,Adam
train_data=pd.read_csv('/kaggle/input/forest-cover-type-prediction/train.csv')
 test_data=pd.read_csv('/kaggle/input/forest-cover-type-prediction/test.csv' )<prepare_x_and_y>
dog=Input(( 12288,))
dogName=Input(( 10000,))
x=Dense(12288,activation="sigmoid" )(dogName)
 x=Reshape(( 2,12288,1))(concatenate([dog,x]))
x=Conv2D(1,(2,1),use_bias=False,name="conv" )(x)
 discriminated=Flatten()(x)
 discriminator=Model([dog,dogName],discriminated)
 discriminator.get_layer("conv" ).trainable=False
discriminator.get_layer("conv" ).set_weights([np.array([[[[-1.0]]],[[[1.0]]]])])
 discriminator.compile(optimizer="adam",loss="binary_crossentropy")
 discriminator.summary()
X=train_data.drop(labels=['Id','Cover_Type'],axis=1)
 y=train_data['Cover_Type']<split>
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):
annealer=LearningRateScheduler(lambda x:lr)
 h = discriminator.fit([zeros,train_X], train_y, epochs = 10, batch_size=256, callbacks=[annealer], verbose=0)
 print('Epoch',(k+1)*10,'/30 - loss =',h.history['loss'][-1])
 if h.history['loss'][-1]<0.533: lr = 0.1
x_train,x_val,y_train,y_val=train_test_split(X,y,random_state=40 )<train_model>
del train_X, train_y, imagesIn
rfc=RandomForestClassifier(n_estimators=70)
 rfc.fit(x_train,y_train )<compute_test_metric>
BadMemory = False
seed = Input(( 10000,))
generated = Dense(12288, activation='linear' )(seed)
 generator = Model(seed, [generated,Reshape(( 10000,))(seed)])
 generator.summary()
rfc.score(x_val,y_val )<predict_on_test>
discriminator.trainable=False
gan_input = Input(shape=(10000,))
x = generator(gan_input)
 gan_output = discriminator(x)
 gan = Model(gan_input, gan_output)
 gan.get_layer("model_1" ).get_layer('conv' ).set_weights([np.array([[[[-1 ]]],[[[255.]]]])])
 gan.compile(optimizer=Adam(5), loss='mean_squared_error')
 gan.summary()
predict=rfc.predict(test_data.drop(labels=['Id'],axis=1))<prepare_output>
train = np.zeros(( 10000,10000))
for i in range(10000): train[i,i] = 1
zeros = np.zeros(( 10000,12288))
ep = 1; it = 9
if BadMemory: lr = 0.005
else: lr = 5.
 for k in range(it):
annealer = LearningRateScheduler(lambda x: lr)
 h = gan.fit(train, zeros, epochs = ep, batch_size=256, callbacks=[annealer], verbose=0)
 print('Epoch',(k+1),'/'+str(it)+' - loss =',h.history['loss'][-1])
 plt.figure(figsize=(15,3))
for j in range(5):
xx = np.zeros(( 10000))
xx[np.random.randint(10000)] = 1
plt.subplot(1,5,j+1)
 img = generator.predict(xx.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()
if BadMemory:
ep *= 2
if ep>=32: lr = 0.001
if ep>256: ep = 256
else:
if h.history['loss'][-1] < 25: lr = 1.
 if h.history['loss'][-1] < 1.5: lr = 0.5
submission=pd.DataFrame(data=predict,columns=['Cover_Type'])
 submission['Id']=test_data['Id']
submission.set_index('Id',inplace=True )<save_to_csv>
class DogGenerator:
index = 0
def getDog(self,seed):
xx = np.zeros(( 10000))
xx[self.index] = 0.70
xx[np.random.randint(10000)] = 0.30
img = generator.predict(xx.reshape(( -1,10000)))[0].reshape(( 64,64,3))
self.index =(self.index+1)%10000
return Image.fromarray(img.astype('uint8'))