Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<set_options>
sns.set_style("darkgrid")
 %matplotlib inline
warnings.filterwarnings("ignore" )
def ImageGenerator() :
return ImageDataGenerator(
 rotation_range=20,
zoom_range=0.15,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.15,
horizontal_flip=True,
fill_mode="nearest" )<choose_model_class>
%%time
train = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/train.csv')
 test = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/test.csv')
 sample_sub = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/sample_submission.csv')
 org_len = len(train.drop('id',axis=1))
class CNNLSTM:
def __init__(self, shape, cnn):
self.shape = shape
self.model_input = keras.layers.Input(shape =(self.shape[0], self.shape[1], self.shape[2]), ragged = True, dtype = 'float32')
 self.cnn = cnn(weights = "imagenet", include_top = False, input_tensor = self.model_input)
 for layer in self.cnn.layers[: len(self.cnn.layers)// 2 ]:
layer.trainable = True
for layer in self.cnn.layers[len(self.cnn.layers)// 2 :]:
layer.trainable = False
self.model = 0
self.save_path = "./cnnlstm.h5"
self.__build_model()
def __build_model(self):
x = keras.layers.Flatten()(self.cnn.output)
 x = keras.layers.Dense(1024, activation ="selu", kernel_initializer = "lecun_normal" )(x)
 x = keras.layers.Dropout(0.1 )(x)
 x = keras.layers.Dense(512, activation = "selu", kernel_initializer = "lecun_normal" )(x)
 x = keras.layers.Dropout(0.1 )(x)
 x = keras.layers.Dense(256, activation = "selu", kernel_initializer = "lecun_normal" )(x)
 x = keras.layers.Dropout(0.1 )(x)
 x = keras.layers.Dense(128, activation = "selu", kernel_initializer = "lecun_normal" )(x)
 x = keras.layers.Dropout(0.1 )(x)
 x = keras.layers.Dense(64, activation = "selu", kernel_initializer = "lecun_normal" )(x)
 x = keras.layers.Dropout(0.1 )(x)
 x = keras.layers.Dense(32, activation = "selu", kernel_initializer = "lecun_normal" )(x)
 x = keras.layers.Dropout(0.1 )(x)
 output = keras.layers.Dense(12, activation = "softmax" )(x)
 self.model = keras.models.Model(inputs = self.model_input,
outputs = output, name = "CNNLSTM")
 def plot_loss_acc(self):
plt.plot(self.model.history.history['loss'], label='train')
 plt.plot(self.model.history.history['val_loss'], label='test')
 plt.xlabel("Epochs")
 plt.ylabel("Loss")
 plt.title(f"{self.name} Loss vs Epochs")
 plt.legend()
plt.show()
plt.plot(self.model.history.history['accuracy'], label='train')
 plt.plot(self.model.history.history['val_accuracy'], label='test')
 plt.xlabel("Epochs")
 plt.ylabel("Accuracy")
 plt.title(f"{self.name} ACCURACY vs Epochs")
 plt.legend()
plt.show()
def scoring(self):
y_pred = self.model.predict(x_test)
 cm = confusion_matrix(y_test, y_pred)
 print(classification_report(y_test,y_pred))
df_cm = pd.DataFrame(cm)
 plt.figure(figsize =(10,7))
sn.heatmap(df_cm, annot=True)
 def compile_and_run(self, lr = 1e-4, epochs = 100):
optimizer = keras.optimizers.Adam(learning_rate = lr)
 checkpoint = keras.callbacks.ModelCheckpoint(self.save_path, save_best_only = True)
 self.model.compile(loss="categorical_crossentropy", optimizer = optimizer, metrics = ["accuracy"])
 self.model.fit(x = ImageGenerator().flow(x_train,y_train), epochs = epochs,
validation_data =(x_test, y_test),shuffle=True, workers = -1,
batch_size = 32, callbacks = [checkpoint])
<choose_model_class>
print("Train data set dtypes:
")
 print(f"Shape : {train.shape}")
 print(f"{train.dtypes.value_counts() }")
 print('*'*30)
 print("Test data set dtypes:
")
 print(f"Shape : {test.shape}")
 print(f"{test.dtypes.value_counts() }" )
cnnlstm = CNNLSTM(( size, size, 3), keras.applications.resnet.ResNet101)
 cnnlstm.compile_and_run()<normalization>
profile = pandas_profiling.ProfileReport(train,minimal=True)
 profile.to_file(output_file="output.html")
 profile
def __preprocessImage(image):
image = np.array(image, dtype = "float32")
 image = cv2.resize(image,(size, size))
image = np.reshape(image,(size, size, 3))
image /= 255.0
return np.array(image, dtype = "float32")
 x_data_test = []
def __extractImages(path):
global x_data_test
image = cv2.imread(path)
 x_data_test.append(__preprocessImage(image))
x_path = ".. /input/plant-seedlings-classification/test"
images = os.listdir(x_path)
 for image in images:
__extractImages(x_path + "/" + image)
 x_data_test = np.array(x_data_test, dtype = "float32" )<save_to_csv>
train_cont = train.drop('id',axis=1 )
classes = os.listdir(".. /input/plant-seedlings-classification/train")
 predictions = cnnlstm.model.predict(x_data_test)
 df = pd.DataFrame()
df['file'] = images
df['species'] = [classes[np.argmax(pred)] for pred in predictions]
df.index = df['file']
df = df.drop('file', axis = 1)
 df.to_csv("./submission.csv" )<import_modules>
train_cont['new'] = train_cont['cont2']*train_cont['cont3']*train_cont['cont6']*train_cont['cont7']*train_cont['cont11']*train_cont['cont12']
test['new'] = test['cont2']*test['cont3']*test['cont6']*test['cont7']*test['cont11']*test['cont12']
warnings.filterwarnings("ignore")
<load_from_csv>
train_cont['new1'] = train_cont['cont9']*train_cont['cont10']*train_cont['cont1']
test['new1'] = test['cont9']*test['cont10']*test['cont1']
<define_variables>
features = train_cont.drop('target',axis=1 ).columns
train_cont['mean'] = train_cont[features].mean(axis=1)
 test['mean'] = test[features].mean(axis=1 )
<load_from_csv>
low_cont = ['target', 'cont10', 'cont9', 'cont7']
up_cont = ['cont10']
n999 = [ np.percentile(train_cont[i],99.9)for i in train_cont[up_cont]]
n001 = [ np.percentile(train_cont[i],0.1)for i in train_cont[low_cont]]
<categorify>
for i, j in enumerate(low_cont):
train_cont = train_cont[train_cont[j] > n001[i]]
gc.collect()
for i, j in enumerate(up_cont):
train_cont = train_cont[train_cont[j] < n999[i]]
gc.collect()
<choose_model_class>
X_train = train_cont.drop('target',axis=1)
 y_train = train_cont['target']
<categorify>
def objective(trial):
params = {
'n_estimators': trial.suggest_int('n_estimators', 350, 1000),
'max_depth': trial.suggest_int('max_depth', 6, 13),
'learning_rate': trial.suggest_uniform('learning_rate', 0.009, 0.10),
'subsample': trial.suggest_uniform('subsample', 0.50, 1),
'colsample_bytree': trial.suggest_uniform('colsample_bytree', 0.50, 1),
'gamma': trial.suggest_int('gamma', 0, 0.05),
'objective':'reg:squarederror',
'eval_metric' : 'rmse',
'tree_method':'gpu_hist',
}
clf = xgb.XGBRegressor(**params)
 rmse_scores = []
X_train_k = X_train.values
y_train_k = y_train.values
skf = KFold(n_splits=3,shuffle=True)
 for train_idx, valid_idx in skf.split(X_train_k,y_train_k):
train_data = X_train_k[train_idx, :], y_train_k[train_idx]
valid_data = X_train_k[valid_idx, :], y_train_k[valid_idx]
clf.fit(X_train_k[train_idx, :], y_train_k[train_idx])
 pred = clf.predict(X_train_k[valid_idx, :])
 rmse = np.sqrt(mean_squared_error(y_train_k[valid_idx],pred))
rmse_scores.append(rmse)
 print(f'Trial done: Accuracy values on folds: {rmse_scores}')
 return np.average(rmse_scores )
img_size = 600
def auto_select_accelerator() :
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print("Running on TPU:", tpu.master())
 except ValueError:
strategy = tf.distribute.get_strategy()
print(f"Running on {strategy.num_replicas_in_sync} replicas")
 return strategy
def build_decoder(with_labels=True, target_size=(img_size, img_size), ext='jpg'):
def decode(path):
file_bytes = tf.io.read_file(path)
 if ext == 'png':
img = tf.image.decode_png(file_bytes, channels=3)
 elif ext in ['jpg', 'jpeg']:
img = tf.image.decode_jpeg(file_bytes, channels=3)
 else:
raise ValueError("Image extension not supported")
 img = tf.cast(img, tf.float32)/ 255.0
img = tf.image.resize(img, target_size)
 return img
def decode_with_labels(path, label):
return decode(path), label
return decode_with_labels if with_labels else decode
def build_augmenter(with_labels=True):
def augment(img):
img = tf.image.random_flip_left_right(img)
 img = tf.image.random_flip_up_down(img)
 img = tf.image.random_saturation(img, 0.8, 1.2)
 img = tf.image.random_brightness(img, 0.1)
 img = tf.image.random_contrast(img, 0.9, 1.2)
 return img
def augment_with_labels(img, label):
return augment(img), label
return augment_with_labels if with_labels else augment
def build_dataset(paths, labels=None, bsize=32, cache=True,
decode_fn=None, augment_fn=None,
augment=True, repeat=True, shuffle=1024,
cache_dir=""):
if cache_dir != "" and cache is True:
os.makedirs(cache_dir, exist_ok=True)
 if decode_fn is None:
decode_fn = build_decoder(labels is not None)
 if augment_fn is None:
augment_fn = build_augmenter(labels is not None)
 AUTO = tf.data.experimental.AUTOTUNE
slices = paths if labels is None else(paths, labels)
 dset = tf.data.Dataset.from_tensor_slices(slices)
 dset = dset.map(decode_fn, num_parallel_calls=AUTO)
 dset = dset.cache(cache_dir)if cache else dset
dset = dset.map(augment_fn, num_parallel_calls=AUTO)if augment else dset
dset = dset.repeat() if repeat else dset
dset = dset.shuffle(shuffle)if shuffle else dset
dset = dset.batch(bsize ).prefetch(AUTO)
 return dset
COMPETITION_NAME = "ranzcr-clip-catheter-line-classification"
strategy = auto_select_accelerator()
batch_size = strategy.num_replicas_in_sync * 16
print('batch size', batch_size)
 with strategy.scope() :
model = tf.keras.models.load_model('.. /input/ranzcr-tpu-weights/model.h5', custom_objects={'FixedDropout': tf.keras.layers.Dropout})
 test_decoder = build_decoder(with_labels=False, target_size=(img_size, img_size))
load_dir = '/kaggle/input/ranzcr-clip-catheter-line-classification/'
df_sub = pd.read_csv(load_dir + 'sample_submission.csv')
 test_paths = load_dir + "test/" + df_sub['StudyInstanceUID'] + '.jpg'
dtest = build_dataset(
 test_paths, bsize=batch_size, repeat=False,
shuffle=False, augment=False, cache=False,
decode_fn=test_decoder 
)
 y_preds = model.predict(dtest, verbose=1)
 df_sub.iloc[:, 1:] = y_preds
display(df_sub)
 df_sub.to_csv('submission.csv',index=False )<install_modules>
n_trials = 5
FIT_XGB = True
if FIT_XGB:
study = optuna.create_study(direction="minimize")
 study.optimize(objective, n_trials=n_trials)
 print("Number of finished trials: {}".format(len(study.trials)))
 print("Best trial:")
 trial = study.best_trial
print(" Value: {}".format(trial.value))
print(" Params: ")
 for key, value in trial.params.items() :
print(" {}: {}".format(key, value))
!pip install /kaggle/input/kerasapplications -q
!pip install /kaggle/input/efficientnet-keras-source-code/ -q --no-deps<import_modules>
best_param = {'n_estimators': 751, 'max_depth': 10,
'learning_rate': 0.019789645280696613,
'subsample': 0.8730019407814834,
'colsample_bytree': 0.6012295369579667,'gamma':0}
best_param['objective'] ='reg:squarederror'
best_param['tree_method'] ='gpu_hist'
best_param['eval_metric'] ='rmse'
import os
import efficientnet.tfkeras as efn
import numpy as np
import pandas as pd
import tensorflow as tf<categorify>
model = xgb.XGBRegressor(**best_param)
 model.fit(X_train,y_train )
def auto_select_accelerator() :
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print("Running on TPU:", tpu.master())
 except ValueError:
strategy = tf.distribute.get_strategy()
print(f"Running on {strategy.num_replicas_in_sync} replicas")
 return strategy
def build_decoder(with_labels=True, target_size=(512, 512), ext='jpg'):
def decode(path):
file_bytes = tf.io.read_file(path)
 if ext == 'png':
img = tf.image.decode_png(file_bytes, channels=3)
 elif ext in ['jpg', 'jpeg']:
img = tf.image.decode_jpeg(file_bytes, channels=3)
 else:
raise ValueError("Image extension not supported")
 img = tf.cast(img, tf.float32)/ 255.0
img = tf.image.resize(img, target_size)
 return img
def decode_with_labels(path, label):
return decode(path), label
return decode_with_labels if with_labels else decode
def build_augmenter(with_labels=True):
def augment(img):
img = tf.image.random_flip_left_right(img)
 img = tf.image.random_flip_up_down(img)
 return img
def augment_with_labels(img, label):
return augment(img), label
return augment_with_labels if with_labels else augment
def build_dataset(paths, labels=None, bsize=32, cache=True,
decode_fn=None, augment_fn=None,
augment=True, repeat=True, shuffle=1024,
cache_dir=""):
if cache_dir != "" and cache is True:
os.makedirs(cache_dir, exist_ok=True)
 if decode_fn is None:
decode_fn = build_decoder(labels is not None)
 if augment_fn is None:
augment_fn = build_augmenter(labels is not None)
 AUTO = tf.data.experimental.AUTOTUNE
slices = paths if labels is None else(paths, labels)
 dset = tf.data.Dataset.from_tensor_slices(slices)
 dset = dset.map(decode_fn, num_parallel_calls=AUTO)
 dset = dset.cache(cache_dir)if cache else dset
dset = dset.map(augment_fn, num_parallel_calls=AUTO)if augment else dset
dset = dset.repeat() if repeat else dset
dset = dset.shuffle(shuffle)if shuffle else dset
dset = dset.batch(bsize ).prefetch(AUTO)
 return dset<define_variables>
predictions_final = model.predict(test.drop('id',axis=1))
COMPETITION_NAME = "ranzcr-clip-catheter-line-classification"
strategy = auto_select_accelerator()
BATCH_SIZE = strategy.num_replicas_in_sync * 16<load_from_csv>
submission = pd.DataFrame({
"id": test["id"],
"target":predictions_final
})
 submission.to_csv('my_submission.csv', index=False )