Kaggle competition: I’m Something of a Painter Myself
<SOS> metric: MiFID Kaggle data source: i’m-something-of-a-painter-myself<drop_column>
def seed_everything(seed=0):
random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
SEED = 0
seed_everything(SEED)
 %matplotlib inline
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Device:', tpu.master())
 tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 except:
strategy = tf.distribute.get_strategy()
print('Number of replicas:', strategy.num_replicas_in_sync)
 AUTOTUNE = tf.data.experimental.AUTOTUNE
tf.__version__
all['Deck'] = all['Cabin'].apply(lambda x: x[0])
 all['Deck'] = all['Deck'].replace(['T'], 'A')
 all.drop(['Cabin'], inplace=True, axis=1 )<feature_engineering>
GCS_PATH = KaggleDatasets().get_gcs_path("gan-getting-started")
 GCS_PATH_1367 = KaggleDatasets().get_gcs_path("monet-tfrecords-256x256")
 MONET_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/monet_tfrec/*.tfrec'))
PHOTO_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/photo_tfrec/*.tfrec'))
MONET_FILENAMES_1367 = tf.io.gfile.glob(str(GCS_PATH_1367 + '/mon*.tfrec'))
m=MONET_FILENAMES_1367
p=PHOTO_FILENAMES
PRETRAIN_FILENAMES=[m[0],p[0],m[1],p[1],m[2],p[2],m[3],p[3],m[0],p[4],m[1],p[5],m[2],p[6],m[3],p[7],m[0],p[8],m[1],p[9],m[2],p[10],m[3],p[11],m[0],p[12],m[1],p[13],m[2],p[14],m[3],p[15],m[0],p[16],m[1],p[17],m[2],p[18],m[4],p[19]]
PRETRAIN_LABELS=[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*274+[0]*352+[1]*271+[0]*350
all.loc[all['Deck'] == 'A', 'Deck'] = 0
all.loc[all['Deck'] == 'B', 'Deck'] = 1
all.loc[all['Deck'] == 'C', 'Deck'] = 2
all.loc[all['Deck'] == 'D', 'Deck'] = 3
all.loc[all['Deck'] == 'E', 'Deck'] = 4
all.loc[all['Deck'] == 'F', 'Deck'] = 5
all.loc[all['Deck'] == 'G', 'Deck'] = 6
all.loc[all['Deck'] == 'N', 'Deck'] = 7<feature_engineering>
IMAGE_SIZE = [256, 256]
def decode_image(image):
image = tf.image.decode_jpeg(image, channels=3)
 image =(tf.cast(image, tf.float32)/ 127.5)- 1
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_tfrecord(example):
tfrecord_format = {
"image": tf.io.FixedLenFeature([], tf.string)
 }
example = tf.io.parse_single_example(example, tfrecord_format)
 image = decode_image(example['image'])
 return image
all.loc[all['Embarked'] == 'S', 'Embarked'] = 0
all.loc[all['Embarked'] == 'Q', 'Embarked'] = 1
all.loc[all['Embarked'] == 'C', 'Embarked'] = 2<count_values>
def data_augment_color(image):
image = tf.image.random_flip_left_right(image)
 image =(image + 1)/ 2
image = tf.image.random_saturation(image, 0.7, 1.2)
 image = tf.clip_by_value(image, 0, 1)
 image =(image - 0.5)* 2
return image
def pretrain_data_augment(image):
image = tf.image.resize(image, [286, 286])
 image = tf.image.random_crop(image, size=[256, 256, 3])
 return image
def to3030(label):
return np.ones(( 30,30,1),dtype="float32")*label
all['Title'].value_counts()<count_values>
def data_augment(image):
p_rotate = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_spatial = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_crop = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 if p_crop >.5:
image = tf.image.resize(image, [286, 286])
 image = tf.image.random_crop(image, size=[256, 256, 3])
 if p_crop >.9:
image = tf.image.resize(image, [300, 300])
 image = tf.image.random_crop(image, size=[256, 256, 3])
 if p_rotate >.9:
image = tf.image.rot90(image, k=3)
 elif p_rotate >.7:
image = tf.image.rot90(image, k=2)
 elif p_rotate >.5:
image = tf.image.rot90(image, k=1)
 if p_spatial >.6:
image = tf.image.random_flip_left_right(image)
 image = tf.image.random_flip_up_down(image)
 if p_spatial >.9:
image = tf.image.transpose(image)
 return image
all['Title'] = all['Title'].replace(['Miss', 'Mrs','Ms', 'Mlle', 'Lady', 'Mme', 'the Countess', 'Dona'], 'Miss/Mrs/Ms')
 all['Title'] = all['Title'].replace(['Dr', 'Col', 'Major', 'Jonkheer', 'Capt', 'Sir', 'Don', 'Rev'], 'Dr/Military/Noble/Clergy')
 all['Title'].value_counts()<feature_engineering>
BATCH_SIZE = 1
def load_dataset(filenames):
dataset = tf.data.TFRecordDataset(filenames)
 dataset = dataset.map(read_tfrecord, num_parallel_calls=AUTOTUNE)
 return dataset
photo_ds = load_dataset(PHOTO_FILENAMES)
 monet_ds = load_dataset(MONET_FILENAMES)
 monet_ds = monet_ds.repeat()
photo_ds = photo_ds.repeat()
gan_ds = tf.data.Dataset.zip(( monet_ds, photo_ds)).batch(BATCH_SIZE ).prefetch(AUTOTUNE)
 fast_photo_ds = load_dataset(PHOTO_FILENAMES ).batch(32*strategy.num_replicas_in_sync ).prefetch(32)
 monet_ds_fid = load_dataset(MONET_FILENAMES ).batch(32*strategy.num_replicas_in_sync ).prefetch(32)
 pretrain_ds = load_dataset(PRETRAIN_FILENAMES)
 pretrain_labels=tf.data.Dataset.from_tensor_slices(np.array(PRETRAIN_LABELS,dtype="float32"))
pretrain_labels=pretrain_labels.map(to3030,num_parallel_calls=AUTOTUNE)
 pretrain_trans=pretrain_ds.map(pretrain_data_augment, num_parallel_calls=AUTOTUNE)
 pretrain_ds=tf.data.Dataset.zip(( pretrain_ds, pretrain_trans,pretrain_labels))
val_pretrain_ds=pretrain_ds.skip(11894 ).batch(32, drop_remainder=True ).prefetch(2)
 pretrain_ds=pretrain_ds.take(11894 ).shuffle(400 ).batch(32, drop_remainder=True ).prefetch(2 )
all.loc[all['Title'] == 'Mr', 'Title'] = 0
all.loc[all['Title'] == 'Miss/Mrs/Ms', 'Title'] = 1
all.loc[all['Title'] == 'Master', 'Title'] = 2
all.loc[all['Title'] == 'Dr/Military/Noble/Clergy', 'Title'] = 3<feature_engineering>
with strategy.scope() :
inception_model = tf.keras.applications.InceptionV3(input_shape=(256,256,3),pooling="avg",include_top=False)
 inception_model.trainable = False
def calculate_activation_statistics_mod(images,fid_model):
act=fid_model.predict(images)
 mu = np.mean(act, axis=0)
 sigma = np.cov(act, rowvar=False)
 return mu, sigma
myFID_mu2, myFID_sigma2 = calculate_activation_statistics_mod(monet_ds_fid,inception_model )
list1 = all[(all['Title'] != 0)&(all['Survived'] == 0)]['Family'].tolist()
list2 = all[(all['Title'] == 0)&(all['Survived'] == 1)]['Family'].tolist()
all['Family_with_FC_dead'] = 0
all['Family_with_M_alive'] = 0
all.loc[all['Family'].isin(list1), 'Family_with_FC_dead'] = 1
all.loc[all['Family'].isin(list2), 'Family_with_M_alive'] = 1
all.drop(['Family'], inplace=True, axis=1 )<feature_engineering>
def calculate_frechet_distance(mu1,sigma1,mu2,sigma2):
fid_epsilon = 1e-14
mu1 = np.atleast_1d(mu1)
 mu2 = np.atleast_1d(mu2)
 sigma1 = np.atleast_2d(sigma1)
 sigma2 = np.atleast_2d(sigma2)
 assert mu1.shape == mu2.shape, 'Training and test mean vectors have different lengths'
assert sigma1.shape == sigma2.shape, 'Training and test covariances have different dimensions'
covmean, _ = linalg.sqrtm(sigma1.dot(sigma2), disp=False)
 if not np.isfinite(covmean ).all() :
msg = f'fid calculation produces singular product; adding {fid_epsilon} to diagonal of cov estimates'
warnings.warn(msg)
 offset = np.eye(sigma1.shape[0])* fid_epsilon
covmean = linalg.sqrtm(( sigma1 + offset ).dot(sigma2 + offset))
if np.iscomplexobj(covmean):
if not np.allclose(np.diagonal(covmean ).imag, 0, atol=1e-3):
m = np.max(np.abs(covmean.imag))
raise ValueError(f'Imaginary component {m}')
 covmean = covmean.real
tr_covmean = np.trace(covmean)
 return(mu1 - mu2 ).dot(mu1 - mu2)+ np.trace(sigma1)+ np.trace(sigma2)- 2 * tr_covmean
def FID(images,gen_model,inception_model=inception_model,myFID_mu2=myFID_mu2, myFID_sigma2=myFID_sigma2):
with strategy.scope() :
inp = layers.Input(shape=[256, 256, 3], name='input_image')
 x = gen_model(inp)
 x=inception_model(x)
 fid_model = tf.keras.Model(inputs=inp, outputs=x)
 mu1, sigma1 = calculate_activation_statistics_mod(images,fid_model)
 fid_value = calculate_frechet_distance(mu1, sigma1,myFID_mu2, myFID_sigma2)
 return fid_value
all.loc[all['FamilySizeGrouped'] == 'Alone', 'FamilySizeGrouped'] = 0
all.loc[all['FamilySizeGrouped'] == 'Small', 'FamilySizeGrouped'] = 1
all.loc[all['FamilySizeGrouped'] == 'Medium', 'FamilySizeGrouped'] = 2
all.loc[all['FamilySizeGrouped'] == 'Large', 'FamilySizeGrouped'] = 3<choose_model_class>
OUTPUT_CHANNELS = 3
def downsample(filters, size, apply_instancenorm=True):
initializer = tf.random_normal_initializer(0., 0.02)
 gamma_init = keras.initializers.RandomNormal(mean=0.0, stddev=0.02)
 result = keras.Sequential()
result.add(layers.Conv2D(filters, size, strides=2, padding='same',
kernel_initializer=initializer, use_bias=False))
if apply_instancenorm:
result.add(tfa.layers.InstanceNormalization(gamma_initializer=gamma_init))
result.add(layers.LeakyReLU())
 return result
train_y = train['Survived']
train_x = train.drop(['Survived', 'PassengerId'], axis=1)
 model = LogisticRegression()
model.fit(train_x, train_y)
 pred = model.predict(train_x)
 metrics.accuracy_score(pred, train_y )<save_to_csv>
def upsample(filters, size, apply_dropout=False):
initializer = tf.random_normal_initializer(0., 0.02)
 gamma_init = keras.initializers.RandomNormal(mean=0.0, stddev=0.02)
 result = keras.Sequential()
result.add(layers.Conv2DTranspose(filters, size, strides=2,
padding='same',
kernel_initializer=initializer,
use_bias=False))
result.add(tfa.layers.InstanceNormalization(gamma_initializer=gamma_init))
if apply_dropout:
result.add(layers.Dropout(0.5))
result.add(layers.ReLU())
 return result
test_x = test.drop('PassengerId', axis=1)
 timestamp = int(round(time.time() * 1000))
pred = model.predict(test_x)
 output = pd.DataFrame({"PassengerId":test.PassengerId , "Survived" : pred})
 output = output.astype(int)
 output.to_csv("submission_" + str(timestamp)+ ".csv",index = False )<set_options>
def Generator() :
inputs = layers.Input(shape=[256,256,3])
 down_stack = [
downsample(64, 4, apply_instancenorm=False),
downsample(128, 4),
downsample(256, 4),
downsample(512, 4),
downsample(512, 4),
downsample(512, 4),
downsample(512, 4),
downsample(512, 4),
]
up_stack = [
upsample(512, 4, apply_dropout=True),
upsample(512, 4, apply_dropout=True),
upsample(512, 4, apply_dropout=True),
upsample(512, 4),
upsample(256, 4),
upsample(128, 4),
upsample(64, 4),
]
initializer = tf.random_normal_initializer(0., 0.02)
 last = layers.Conv2DTranspose(OUTPUT_CHANNELS, 4,
strides=2,
padding='same',
kernel_initializer=initializer,
activation='tanh')
 x = inputs
skips = []
for down in down_stack:
x = down(x)
 skips.append(x)
 skips = reversed(skips[:-1])
 for up, skip in zip(up_stack, skips):
x = up(x)
 x = layers.Concatenate()([x, skip])
 x = last(x)
 return keras.Model(inputs=inputs, outputs=x )
plt.style.use('seaborn')
 sns.set(font_scale=2.5)
 warnings.filterwarnings('ignore')
 %matplotlib inline<load_from_csv>
def Discriminator() :
initializer = tf.random_normal_initializer(0., 0.02)
 gamma_init = keras.initializers.RandomNormal(mean=0.0, stddev=0.02)
 inp = layers.Input(shape=[256, 256, 3], name='input_image')
 x = inp
down1 = downsample(64, 4, False )(x)
 down2 = downsample(128, 4 )(down1)
 down3 = downsample(256, 4 )(down2)
 zero_pad1 = layers.ZeroPadding2D()(down3)
 conv = layers.Conv2D(512, 4, strides=1,
kernel_initializer=initializer,
use_bias=False )(zero_pad1)
 norm1 = tfa.layers.InstanceNormalization(gamma_initializer=gamma_init )(conv)
 leaky_relu = layers.LeakyReLU()(norm1)
 zero_pad2 = layers.ZeroPadding2D()(leaky_relu)
 last = layers.Conv2D(1, 4, strides=1,
kernel_initializer=initializer )(zero_pad2)
 return tf.keras.Model(inputs=inp, outputs=last)
df_train=pd.read_csv('.. /input/titanic/train.csv')
 df_test=pd.read_csv('.. /input/titanic/test.csv' )<count_missing_values>
with strategy.scope() :
monet_generator = Generator()
photo_generator = Generator()
monet_discriminator = Discriminator()
photo_discriminator = Discriminator()
for col in df_train.columns:
msg='columns: {:>10}\t Percent of NaN value: {: 2f}%'.format(col,100*(df_train[col].isnull().sum() /df_train[col].shape[0]))
print(msg )<count_missing_values>
class CycleGan(keras.Model):
def __init__(
 self,
monet_generator,
photo_generator,
monet_discriminator,
photo_discriminator,
monet_expert,
lambda_cycle=10,
lambda_id=3, 
):
super(CycleGan, self ).__init__()
self.m_gen = monet_generator
self.p_gen = photo_generator
self.m_disc = monet_discriminator
self.p_disc = photo_discriminator
self.monet_expert = monet_expert
self.lambda_cycle = lambda_cycle
self.lambda_id = lambda_id
def compile(
 self,
m_gen_optimizer,
p_gen_optimizer,
m_disc_optimizer,
p_disc_optimizer,
gen_loss_fn,
disc_loss_fn,
cycle_loss_fn,
identity_loss_fn,
expert_loss_fn, 
):
super(CycleGan, self ).compile()
self.m_gen_optimizer = m_gen_optimizer
self.p_gen_optimizer = p_gen_optimizer
self.m_disc_optimizer = m_disc_optimizer
self.p_disc_optimizer = p_disc_optimizer
self.gen_loss_fn = gen_loss_fn
self.disc_loss_fn = disc_loss_fn
self.cycle_loss_fn = cycle_loss_fn
self.identity_loss_fn = identity_loss_fn
self.expert_loss_fn = expert_loss
def train_step(self, batch_data):
real_monet, real_photo = batch_data
with tf.GradientTape(persistent=True)as tape:
fake_monet = self.m_gen(real_photo, training=True)
 fake_photo = self.p_gen(real_monet, training=True)
 same_monet = self.m_gen(real_monet, training=True)
 same_photo = self.p_gen(real_photo, training=True)
 disc_real_monet = self.m_disc(real_monet, training=True)
 disc_real_photo = self.p_disc(real_photo, training=True)
 disc_fake_monet = self.m_disc(fake_monet, training=True)
 disc_fake_photo = self.p_disc(fake_photo, training=True)
 monet_disc_loss = self.disc_loss_fn(disc_real_monet, disc_fake_monet)
 photo_disc_loss = self.disc_loss_fn(disc_real_photo, disc_fake_photo)
 monet_expert_loss=self.expert_loss_fn(self.monet_expert(fake_monet, training=False),tf.ones_like(fake_monet))
photo_expert_loss=self.expert_loss_fn(self.monet_expert(fake_photo, training=False),tf.zeros_like(fake_photo))
cycled_monet = self.m_gen(fake_photo, training=True)
 cycled_photo = self.p_gen(fake_monet, training=True)
 monet_gen_loss = self.gen_loss_fn(disc_fake_monet)
 photo_gen_loss = self.gen_loss_fn(disc_fake_photo)
 cycle_loss_mpm = self.cycle_loss_fn(real_monet, cycled_monet, self.lambda_cycle)
 cycle_loss_pmp = self.cycle_loss_fn(real_photo, cycled_photo, self.lambda_cycle)
 total_cycle_loss = cycle_loss_mpm + cycle_loss_pmp
total_monet_gen_loss = monet_gen_loss + total_cycle_loss + self.identity_loss_fn(real_monet, same_monet, self.lambda_id)+ monet_expert_loss
total_photo_gen_loss = photo_gen_loss + total_cycle_loss + self.identity_loss_fn(real_photo, same_photo, self.lambda_id)+ photo_expert_loss
monet_discriminator_gradients = tape.gradient(monet_disc_loss,
self.m_disc.trainable_variables)
 photo_discriminator_gradients = tape.gradient(photo_disc_loss,
self.p_disc.trainable_variables)
 monet_generator_gradients = tape.gradient(total_monet_gen_loss,
self.m_gen.trainable_variables)
 photo_generator_gradients = tape.gradient(total_photo_gen_loss,
self.p_gen.trainable_variables)
 self.m_gen_optimizer.apply_gradients(zip(monet_generator_gradients,
self.m_gen.trainable_variables))
self.p_gen_optimizer.apply_gradients(zip(photo_generator_gradients,
self.p_gen.trainable_variables))
self.m_disc_optimizer.apply_gradients(zip(monet_discriminator_gradients,
self.m_disc.trainable_variables))
self.p_disc_optimizer.apply_gradients(zip(photo_discriminator_gradients,
self.p_disc.trainable_variables))
return {
"monet_disc_loss": monet_disc_loss,
"photo_disc_loss": photo_disc_loss,
"disc_real_monet": disc_real_monet,
"disc_fake_monet": disc_fake_monet,
"disc_real_photo": disc_real_photo,
"disc_fake_photo": disc_fake_photo,
"monet_expert_loss": monet_expert_loss,
"photo_expert_loss": photo_expert_loss,
}
for col in df_test.columns:
msg='columns: {:>10}\t Percent of NaN value: {: 2f}%'.format(col,100*(df_test[col].isnull().sum() /df_test[col].shape[0]))
print(msg )<groupby>
class Pretrain_CycleGan(keras.Model):
def __init__(
 self,
monet_generator,
photo_generator,
monet_discriminator,
photo_discriminator,
monet_expert,
lambda_cycle=25, 
):
super(Pretrain_CycleGan, self ).__init__()
self.m_gen = monet_generator
self.p_gen = photo_generator
self.m_disc = monet_discriminator
self.p_disc = photo_discriminator
self.m_expert = monet_expert
self.lambda_cycle = lambda_cycle
def compile(
 self,
m_gen_optimizer,
p_gen_optimizer,
m_disc_optimizer,
p_disc_optimizer,
m_expert_optimizer,
gen_loss_fn,
disc_loss_fn,
expert_loss_fn, 
):
super(Pretrain_CycleGan, self ).compile()
self.m_gen_optimizer = m_gen_optimizer
self.p_gen_optimizer = p_gen_optimizer
self.m_disc_optimizer = m_disc_optimizer
self.p_disc_optimizer = p_disc_optimizer
self.m_expert_optimizer = m_expert_optimizer
self.expert_loss_fn = expert_loss
self.gen_loss_fn = gen_loss_fn
self.disc_loss_fn = disc_loss_fn
def train_step(self, batch_data):
real_image, transformed_image, label_is_monet = batch_data
with tf.GradientTape(persistent=True)as tape:
gen_monet = self.m_gen(real_image, training=True)
 gen_photo = self.p_gen(real_image, training=True)
 disc_real_monet = self.m_disc(real_image, training=True)
 disc_real_photo = self.p_disc(real_image, training=True)
 monet_exp_loss=self.expert_loss_fn(self.m_expert(real_image, training=True),label_is_monet)
 monet_gen_loss = self.gen_loss_fn(gen_monet,transformed_image,1)
 photo_gen_loss = self.gen_loss_fn(gen_photo,transformed_image,1)
 monet_disc_loss = self.disc_loss_fn(disc_real_monet, tf.ones_like(disc_real_monet)*label_is_monet)
 photo_disc_loss = self.disc_loss_fn(disc_real_photo, tf.ones_like(disc_real_photo)*(1-label_is_monet))
monet_generator_gradients = tape.gradient(monet_gen_loss,
self.m_gen.trainable_variables)
 photo_generator_gradients = tape.gradient(photo_gen_loss,
self.p_gen.trainable_variables)
 monet_discriminator_gradients = tape.gradient(monet_disc_loss,
self.m_disc.trainable_variables)
 photo_discriminator_gradients = tape.gradient(photo_disc_loss,
self.p_disc.trainable_variables)
 monet_expert_gradients = tape.gradient(monet_exp_loss,
self.m_expert.trainable_variables)
 self.m_gen_optimizer.apply_gradients(zip(monet_generator_gradients,
self.m_gen.trainable_variables))
self.p_gen_optimizer.apply_gradients(zip(photo_generator_gradients,
self.p_gen.trainable_variables))
self.m_disc_optimizer.apply_gradients(zip(monet_discriminator_gradients,
self.m_disc.trainable_variables))
self.p_disc_optimizer.apply_gradients(zip(photo_discriminator_gradients,
self.p_disc.trainable_variables))
self.m_expert_optimizer.apply_gradients(zip(monet_expert_gradients,
self.m_expert.trainable_variables))
return {
"monet_gen_loss": monet_gen_loss,
"photo_gen_loss": photo_gen_loss,
"monet_disc_loss": monet_disc_loss,
"photo_disc_loss": photo_disc_loss,
"monet_exp_loss" : monet_exp_loss
}
def test_step(self, batch_data):
real_image, transformed_image, label_is_monet = batch_data
gen_monet = self.m_gen(real_image, training=True)
 gen_photo = self.p_gen(real_image, training=True)
 disc_real_monet = self.m_disc(real_image, training=True)
 disc_real_photo = self.p_disc(real_image, training=True)
 monet_exp_loss=self.expert_loss_fn(self.m_expert(real_image, training=True),label_is_monet)
 monet_gen_loss = self.gen_loss_fn(gen_monet,transformed_image,1)
 photo_gen_loss = self.gen_loss_fn(gen_photo,transformed_image,1)
 monet_disc_loss = self.disc_loss_fn(disc_real_monet, label_is_monet)
 photo_disc_loss = self.disc_loss_fn(disc_real_photo, 1-label_is_monet)
 return {
"monet_gen_loss": monet_gen_loss,
"photo_gen_loss": photo_gen_loss,
"monet_disc_loss": monet_disc_loss,
"photo_disc_loss": photo_disc_loss,
"monet_exp_loss" : monet_exp_loss
}
df_train[['Pclass','Survived']].groupby(['Pclass'],as_index=True ).count()<import_modules>
with strategy.scope() :
def discriminator_loss(real, generated):
real_loss = tf.square(tf.ones_like(real)- real)
 generated_loss = tf.square(generated)
 total_disc_loss = real_loss + generated_loss
return total_disc_loss * 0.5
<train_model>
with strategy.scope() :
def generator_loss(generated):
return tf.square(tf.ones_like(generated)- generated )
<feature_engineering>
with strategy.scope() :
def calc_cycle_loss(real_image, cycled_image, LAMBDA):
loss1 = tf.reduce_mean(tf.keras.losses.Huber(0.5,reduction=tf.keras.losses.Reduction.NONE )(inception_model(real_image), inception_model(cycled_image)))
 return LAMBDA * loss1
df_train['FamilySize']=df_train['SibSp']+df_train['Parch']+1
df_test['FamilySize'] = df_test['SibSp'] + df_test['Parch'] + 1<feature_engineering>
with strategy.scope() :
def identity_loss(real_image, translated_image, LAMBDA):
loss = tf.reduce_mean(tf.keras.losses.Huber(0.5,reduction=tf.keras.losses.Reduction.NONE )(tf.nn.avg_pool2d(real_image, ksize=32, strides=16, padding="VALID"), tf.nn.avg_pool2d(translated_image, ksize=32, strides=16, padding="VALID")))
 return LAMBDA * loss
df_test.loc[df_test.Fare.isnull() , 'Fare'] = df_test['Fare'].mean()<feature_engineering>
with strategy.scope() :
def pretrain_identity_loss(real_image, translated_image, LAMBDA):
loss = tf.reduce_mean(tf.abs(real_image - translated_image))
return LAMBDA * loss
df_train['Fare']=df_train['Fare'].map(lambda i : np.log(i)if i>0 else 0)
 df_test['Fare']=df_test['Fare'].map(lambda i : np.log(i)if i>0 else 0 )<count_missing_values>
with strategy.scope() :
def pretrain_disc_loss(image,label):
return tf.square(label - image)
 def expert_loss(generated,label):
return tf.keras.losses.BinaryCrossentropy(from_logits=True,reduction=tf.keras.losses.Reduction.SUM )(tf.reduce_mean(label,axis=[1,2,3]),tf.reduce_mean(generated,axis=[1,2,3]))
df_train.isnull().sum()<feature_engineering>
with strategy.scope() :
monet_generator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)
 photo_generator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)
 monet_discriminator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)
 photo_discriminator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)
 monet_expert_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)
 pt_monet_generator = Generator()
pt_photo_generator = Generator()
pt_monet_discriminator = Discriminator()
pt_photo_discriminator = Discriminator()
pt_monet_expert = Discriminator()
monet_expert = Discriminator()
df_train['Initial']= df_train.Name.str.extract('([A-Za-z]+)\.')
<feature_engineering>
with strategy.scope() :
cycle_gan_model = CycleGan(
 monet_generator, photo_generator, monet_discriminator, photo_discriminator,monet_expert 
)
 cycle_gan_model.compile(
 m_gen_optimizer = monet_generator_optimizer,
p_gen_optimizer = photo_generator_optimizer,
m_disc_optimizer = monet_discriminator_optimizer,
p_disc_optimizer = photo_discriminator_optimizer,
gen_loss_fn = generator_loss,
disc_loss_fn = discriminator_loss,
cycle_loss_fn = calc_cycle_loss,
identity_loss_fn = identity_loss,
expert_loss_fn = expert_loss,
)
df_test['Initial']= df_test.Name.str.extract('([A-Za-z]+)\.' )<categorify>
with strategy.scope() :
pretrain_cycle_gan_model = Pretrain_CycleGan(pt_monet_generator, pt_photo_generator, pt_monet_discriminator, pt_photo_discriminator,pt_monet_expert)
 pretrain_cycle_gan_model.compile(
 m_gen_optimizer = monet_generator_optimizer,
p_gen_optimizer = photo_generator_optimizer,
m_disc_optimizer = monet_discriminator_optimizer,
p_disc_optimizer = photo_discriminator_optimizer,
m_expert_optimizer = monet_expert_optimizer,
gen_loss_fn = pretrain_identity_loss,
disc_loss_fn = pretrain_disc_loss,
expert_loss_fn = expert_loss,
)
df_train['Initial'].replace(['Capt','Col','Countess','Don','Dr','Jonkheer','Lady','Major','Master','Miss','Mlle','Mme','Mr','Mrs','Ms','Rev','Sir','Dona'],
['Mr','Mr','Other','Mr','Mr','Other','Miss','Mr','Master','Miss','Miss','Mrs','Mr','Mrs','Miss','Other','Other','Other'],inplace=True )<categorify>
hist=pretrain_cycle_gan_model.fit(pretrain_ds,validation_data=val_pretrain_ds, epochs=33 ).history
df_test['Initial'].replace(['Capt','Col','Countess','Don','Dr','Jonkheer','Lady','Major','Master','Miss','Mlle','Mme','Mr','Mrs','Ms','Rev','Sir','Dona'],
['Mr','Mr','Other','Mr','Mr','Other','Miss','Mr','Master','Miss','Miss','Mrs','Mr','Mrs','Miss','Other','Other','Other'],inplace=True )<sort_values>
pretrain_cycle_gan_model.save_weights('premonet.h5' )
df_train.groupby(['Initial'] ).mean().sort_values(by='Survived',ascending=False )<concatenate>
cycle_gan_model.built = True
cycle_gan_model.load_weights('premonet.h5' )
df_all=pd.concat([df_train,df_test] )<drop_column>
! rm premonet.h5
disc_m_loss1=[]
disc_p_loss1=[]
df_all.reset_index(drop=True )<groupby>
%%time
fids=[]
best_fid=999999999
for epoch in range(1,48):
print("Epoch = ",epoch)
 hist=cycle_gan_model.fit(gan_ds,steps_per_epoch=1500, epochs=1 ).history
if epoch>35:
cur_fid=FID(fast_photo_ds,monet_generator)
 fids.append(cur_fid)
 print("After epoch
".format(epoch,cur_fid))
if epoch>42:
best_fid=cur_fid
monet_generator.save('monet_generator_'+str(epoch)+'.h5')
df_all.groupby('Initial' ).mean()<feature_engineering>
_, ax = plt.subplots(5, 3, figsize=(32, 32))
for i, img in enumerate(photo_ds.batch(1 ).take(5)) :
prediction = monet_generator(img, training=False)
 cycledphoto = photo_generator(prediction, training=False)
 prediction =(prediction * 127.5 + 127.5)[0].numpy().astype(np.uint8)
 cycledphoto =(cycledphoto * 127.5 + 127.5)[0].numpy().astype(np.uint8)
 img =(img[0] * 127.5 + 127.5 ).numpy().astype(np.uint8)
 ax[i, 0].imshow(img)
 ax[i, 1].imshow(prediction)
 ax[i, 2].imshow(cycledphoto)
 ax[i, 0].set_title("Input Photo")
 ax[i, 1].set_title("Monet-esque")
 ax[i, 2].set_title("Cycled Photo")
 ax[i, 0].axis("off")
 ax[i, 1].axis("off")
 ax[i, 2].axis("off")
 plt.show()
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Mr'),'Age']=33
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Mrs'),'Age']=37
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Miss'),'Age']=22
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Master'),'Age']=5
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Mr'),'Age']=33
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Mr'),'Age']=33
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Mrs'),'Age']=37
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Miss'),'Age']=22
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Master'),'Age']=5
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Mr'),'Age']=33<count_missing_values>
_, ax = plt.subplots(5, 3, figsize=(32, 32))
for i, img in enumerate(monet_ds.batch(1 ).take(5)) :
prediction = photo_generator(img, training=False)
 cycledphoto = monet_generator(prediction, training=False)
 prediction =(prediction * 127.5 + 127.5)[0].numpy().astype(np.uint8)
 cycledphoto =(cycledphoto * 127.5 + 127.5)[0].numpy().astype(np.uint8)
 img =(img[0] * 127.5 + 127.5 ).numpy().astype(np.uint8)
 ax[i, 0].imshow(img)
 ax[i, 1].imshow(prediction)
 ax[i, 2].imshow(cycledphoto)
 ax[i, 0].set_title("Input Monet")
 ax[i, 1].set_title("Generated Photo")
 ax[i, 2].set_title("Cycled Monet")
 ax[i, 0].axis("off")
 ax[i, 1].axis("off")
 ax[i, 2].axis("off")
 plt.show()
df_train.isnull().sum()<count_missing_values>
%%time
i = 1
for img in fast_photo_ds:
prediction = monet_generator(img, training=False ).numpy()
prediction =(prediction * 127.5 + 127.5 ).astype(np.uint8)
 for pred in prediction:
im = PIL.Image.fromarray(pred)
 im.save(".. /images/" + str(i)+ ".jpg")
 i += 1
<data_type_conversions><EOS> 
shutil.make_archive("/kaggle/working/images", 'zip', "/kaggle/images" )