Kaggle competition: Generative Dog Images
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 
<choose_model_class>
DIR = os.getcwd()
DIRimg = ".. /input/all-dogs/all-dogs"
DIRanno = ".. /input/annotation/Annotation"
DIRout = ".. /output_images"
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()<train_model>
all_fNames = os.listdir(DIRimg)
 x_train = np.zeros(( len(all_fNames),img_size,img_size,3))
j = 0
for i in tqdm(range(len(all_fNames))):
path = os.path.join(DIRimg, all_fNames[i])
 ok, img = loadImage(path)
 if ok:
x_train[j] = img
j += 1
print(j)
 x_train = x_train[:j] / 255 .
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<drop_column>
input = Input(( 10000,))
x = Dense(2048, activation='elu' )(input)
 x = Reshape(( 8,8,32))(x)
 x = Conv2D(128,(3, 3), activation='elu', padding='same' )(x)
 x = UpSampling2D(( 2, 2))(x)
 x = Conv2D(64,(3, 3), activation='elu', padding='same' )(x)
 x = UpSampling2D(( 2, 2))(x)
 x = Conv2D(32,(3, 3), activation='elu', padding='same' )(x)
 x = UpSampling2D(( 2, 2))(x)
 decoded = Conv2D(3,(3, 3), activation='sigmoid', padding='same' )(x)
 decoder = Model(input, decoded)
 decoder.compile(optimizer=Adam(lr=0.005), loss='binary_crossentropy')
 decoder.summary()
del train_X, train_y, imagesIn<choose_model_class>
ids = np.random.randint(0,len(x_train),10000)
 train_y = x_train[ids, :,:,:]
train_X = np.eye(10000 )
BadMemory = False
seed = Input(( 10000,))
generated = Dense(12288, activation='linear' )(seed)
 generator = Model(seed, [generated,Reshape(( 10000,))(seed)])
 generator.summary()<choose_model_class>
lr = 0.01
batch = 256; ep = 30; it = 10
d_loss = []
for k in range(10):
annealer = LearningRateScheduler(lambda x: lr)
 h = decoder.fit(train_X, train_y, epochs = ep, batch_size = batch,
callbacks=[annealer], verbose=0)
 d_loss.extend(h.history['loss'])
 print('Epoch',(k+1)*ep,'/',ep*it,' loss =',h.history['loss'][-1], '/ lr =',lr)
 if h.history['loss'][-1] / h.history['loss'][0] > 0.99: lr = max(lr/2., 0.0005)
 plt.plot(d_loss)
 plt.show()
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()<train_model>
del x_train, train_y, train_X
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<categorify>
def getDog(ids, mix_rate):
imgs = []
for id in ids:
xx = np.zeros(( 10000))
xx[id] = mix_rate
xx[np.random.randint(10000)] = 1.0 - mix_rate
imgs.append(decoder.predict(xx.reshape(( -1,10000)))[0].reshape(img_shape))
return imgs
<load_from_csv><EOS> 
shutil.make_archive('images', 'zip', DIRout )