Kaggle competition: I’m Something of a Painter Myself
<SOS> metric: MiFID Kaggle data source: i’m-something-of-a-painter-myself<import_modules>
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)
 AUTO = tf.data.experimental.AUTOTUNE
print(tf.__version__ )
from pandas import Series<import_modules>
GCS_PATH_MONET = KaggleDatasets().get_gcs_path('monet-tfrecords-extdata')
 GCS_PATH = KaggleDatasets().get_gcs_path('gan-getting-started')
 GCS_PATH_MONET, GCS_PATH
kfold = KFold(n_splits=10, random_state=22)
 xyz=[]
accuracy=[]
std=[]
classifiers=['Linear Svm','Radial Svm','Logistic Regression','KNN','Decision Tree','Naive Bayes','Random Forest']
models=[svm.SVC(kernel='linear'),svm.SVC(kernel='rbf'),LogisticRegression() ,KNeighborsClassifier(n_neighbors=9),DecisionTreeClassifier() ,GaussianNB() ,RandomForestClassifier(n_estimators=100)]
for i in models:
model = i
cv_result = cross_val_score(model,X,Y, cv = kfold,scoring = "accuracy")
 cv_result=cv_result
xyz.append(cv_result.mean())
 std.append(cv_result.std())
 accuracy.append(cv_result)
 new_models_dataframe2=pd.DataFrame({'CV Mean':xyz,'Std':std},index=classifiers)
 new_models_dataframe2<train_model>
MONET_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/monet_tfrec/*.tfrec'))
print('Monet TFRecord Files:', len(MONET_FILENAMES))
PHOTO_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/photo_tfrec/*.tfrec'))
print('Photo TFRecord Files:', len(PHOTO_FILENAMES))
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 n_monet_samples = count_data_items(MONET_FILENAMES)
 n_photo_samples = count_data_items(PHOTO_FILENAMES)
 BATCH_SIZE = 4
EPOCHS_NUM = 30
print(f'Monet TFRecord files: {len(MONET_FILENAMES)}')
 print(f'Monet image files: {n_monet_samples}')
 print(f'Photo TFRecord files: {len(PHOTO_FILENAMES)}')
 print(f'Photo image files: {n_photo_samples}')
 print(f"Batch_size: {BATCH_SIZE}")
 print(f"Epochs number: {EPOCHS_NUM}" )
model=svm.SVC(kernel='rbf',C=1,gamma=0.1)
 model.fit(train_X,train_Y )<load_from_csv>
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_name": tf.io.FixedLenFeature([], tf.string),
"image": tf.io.FixedLenFeature([], tf.string),
"target": tf.io.FixedLenFeature([], tf.string)
 }
example = tf.io.parse_single_example(example, tfrecord_format)
 image = decode_image(example['image'])
 return image
submission = pd.read_csv('.. /input/titanic/gender_submission.csv')
 submission.head() 
<predict_on_test>
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
def load_dataset(filenames):
dataset = tf.data.TFRecordDataset(filenames)
 dataset = dataset.map(read_tfrecord, num_parallel_calls=AUTO)
 return dataset
def get_gan_dataset(monet_files, photo_files, augment=None, repeat=True, shuffle=True, batch_size=1):
monet_ds = load_dataset(monet_files)
 photo_ds = load_dataset(photo_files)
 if augment:
monet_ds = monet_ds.map(augment, num_parallel_calls=AUTO)
 photo_ds = photo_ds.map(augment, num_parallel_calls=AUTO)
 if repeat:
monet_ds = monet_ds.repeat()
photo_ds = photo_ds.repeat()
if shuffle:
monet_ds = monet_ds.shuffle(2048)
 photo_ds = photo_ds.shuffle(2048)
 monet_ds = monet_ds.batch(batch_size, drop_remainder=True)
 photo_ds = photo_ds.batch(batch_size, drop_remainder=True)
 monet_ds = monet_ds.cache()
photo_ds = photo_ds.cache()
monet_ds = monet_ds.prefetch(AUTO)
 photo_ds = photo_ds.prefetch(AUTO)
 gan_ds = tf.data.Dataset.zip(( monet_ds, photo_ds))
return gan_ds
prediction_final=model.predict(df_test )<prepare_output>
full_dataset = get_gan_dataset(MONET_FILENAMES, PHOTO_FILENAMES, augment=data_augment, repeat=True, shuffle=True, batch_size=BATCH_SIZE )
submission['Survived']=prediction_final<save_to_csv>
example_monet , example_photo = next(iter(full_dataset))
submission.to_csv('./myfirstsubmission.csv',index=False )<import_modules>
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
import os
import json
import numpy as np
import pandas as pd
import keras
from keras.callbacks import Callback
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score
from sklearn.model_selection import train_test_split<load_pretrained>
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
x_train = np.load('.. /input/reducing-image-sizes-to-32x32/X_train.npy')
 x_test = np.load('.. /input/reducing-image-sizes-to-32x32/X_test.npy')
 y_train = np.load('.. /input/reducing-image-sizes-to-32x32/y_train.npy')
 print('x_train shape:', x_train.shape)
 print(x_train.shape[0], 'train samples')
 print(x_test.shape[0], 'test samples' )<data_type_conversions>
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 )
x_train = x_train.astype('float32')
 x_test = x_test.astype('float32')
 x_train /= 255.
 x_test /= 255 .<compute_train_metric>
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 )
class Metrics(Callback):
def on_train_begin(self, logs={}):
self.val_f1s = []
self.val_recalls = []
self.val_precisions = []
def on_epoch_end(self, epoch, logs={}):
X_val, y_val = self.validation_data[:2]
y_pred = self.model.predict(X_val)
 y_pred_cat = keras.utils.to_categorical(
 y_pred.argmax(axis=1),
num_classes=num_classes 
)
 _val_f1 = f1_score(y_val, y_pred_cat, average='macro')
 _val_recall = recall_score(y_val, y_pred_cat, average='macro')
 _val_precision = precision_score(y_val, y_pred_cat, average='macro')
 self.val_f1s.append(_val_f1)
 self.val_recalls.append(_val_recall)
 self.val_precisions.append(_val_precision)
 print(( f"val_f1: {_val_f1:.4f}"
f" — val_precision: {_val_precision:.4f}"
f" — val_recall: {_val_recall:.4f}"))
return<define_variables>
with strategy.scope() :
monet_generator = Generator()
photo_generator = Generator()
monet_discriminator = Discriminator()
photo_discriminator = Discriminator()
batch_size = 64
num_classes = 14
epochs = 30
val_split = 0.1
save_dir = os.path.join(os.getcwd() , 'models')
 model_name = 'keras_cnn_model.h5'<choose_model_class>
class CycleGan(keras.Model):
def __init__(
 self,
monet_generator,
photo_generator,
monet_discriminator,
photo_discriminator,
lambda_cycle=10, 
):
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.lambda_cycle = lambda_cycle
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 
):
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
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)
 cycled_photo = self.p_gen(fake_monet, training=True)
 fake_photo = self.p_gen(real_monet, training=True)
 cycled_monet = self.m_gen(fake_photo, 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_gen_loss = self.gen_loss_fn(disc_fake_monet)
 photo_gen_loss = self.gen_loss_fn(disc_fake_photo)
 total_cycle_loss = self.cycle_loss_fn(real_monet, cycled_monet, self.lambda_cycle)+ self.cycle_loss_fn(real_photo, cycled_photo, self.lambda_cycle)
 total_monet_gen_loss = monet_gen_loss + total_cycle_loss + self.identity_loss_fn(real_monet, same_monet, self.lambda_cycle)
 total_photo_gen_loss = photo_gen_loss + total_cycle_loss + self.identity_loss_fn(real_photo, same_photo, self.lambda_cycle)
 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_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)
 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)
 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_gen_loss": total_monet_gen_loss,
"photo_gen_loss": total_photo_gen_loss,
"monet_disc_loss": monet_disc_loss,
"photo_disc_loss": photo_disc_loss
}
model = Sequential()
model.add(Conv2D(32,(3, 3), padding='same',
input_shape=x_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3)))
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64,(3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3)))
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))<train_model>
with strategy.scope() :
def discriminator_loss(real, generated):
real_loss = tf.keras.losses.BinaryCrossentropy(from_logits=True, reduction=tf.keras.losses.Reduction.NONE )(tf.ones_like(real), real)
 generated_loss = tf.keras.losses.BinaryCrossentropy(from_logits=True, reduction=tf.keras.losses.Reduction.NONE )(tf.zeros_like(generated), generated)
 total_disc_loss = real_loss + generated_loss
return total_disc_loss * 0.5
f1_metrics = Metrics()
model.compile(
 loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'] 
)
 hist = model.fit(
 x_train,
y_train,
batch_size=batch_size,
epochs=epochs,
callbacks=[f1_metrics],
validation_split=val_split,
shuffle=True
)<train_model>
with strategy.scope() :
def generator_loss(generated):
return tf.keras.losses.BinaryCrossentropy(from_logits=True,
reduction=tf.keras.losses.Reduction.NONE )(tf.ones_like(generated), generated )
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
 model_path = os.path.join(save_dir, model_name)
 model.save(model_path)
 print('Saved trained model at %s ' % model_path )<load_from_csv>
with strategy.scope() :
def calc_cycle_loss(real_image, cycled_image, LAMBDA):
loss1 = tf.reduce_mean(tf.abs(real_image - cycled_image))
return LAMBDA * loss1
y_test = model.predict(x_test)
 submission_df = pd.read_csv('.. /input/iwildcam-2019-fgvc6/sample_submission.csv')
 submission_df['Predicted'] = y_test.argmax(axis=1)
 print(submission_df.shape)
 submission_df.head()<save_to_csv>
with strategy.scope() :
def identity_loss(real_image, same_image, LAMBDA):
loss = tf.reduce_mean(tf.abs(real_image - same_image))
return LAMBDA * 0.5 * loss
submission_df.to_csv('submission.csv',index=False)
 history_df.to_csv('history.csv', index=False)
 with open('history.json', 'w')as f:
json.dump(hist.history, f )<import_modules>
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 )
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score<load_from_csv>
with strategy.scope() :
cycle_gan_model = CycleGan(
 monet_generator, photo_generator, monet_discriminator, photo_discriminator 
)
 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
)
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 y = train.Survived
passengerid = test.PassengerId
titanic = train.append(test, ignore_index = True )<split>
cycle_gan_model.fit(
 full_dataset,
epochs=EPOCHS_NUM,
steps_per_epoch=(max(n_monet_samples, n_photo_samples)//BATCH_SIZE),
)
train_index = len(train)
 test_index = len(titanic)- len(test )<feature_engineering>
def predict_and_save(input_ds, generator_model, output_path):
i = 1
for img in input_ds:
prediction = generator_model(img, training=False)[0].numpy()
prediction =(prediction * 127.5 + 127.5 ).astype(np.uint8)
 im = PIL.Image.fromarray(prediction)
 im.save(f'{output_path}{str(i)}.jpg')
 i += 1
titanic['Title'] = titanic.Name.apply(lambda x: x.split(',')[1].split('.')[0].strip() )<count_values>
os.makedirs('.. /images/')
 predict_and_save(load_dataset(PHOTO_FILENAMES ).batch(1), monet_generator, '.. /images/' )
<define_variables><EOS> 
shutil.make_archive('/kaggle/working/images/', 'zip', '.. /images')
 print(f"Generated samples: {len([name for name in os.listdir('.. /images/')if os.path.isfile(os.path.join('.. /images/', name)) ])}" )