Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<choose_model_class>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
input_path = Path('/kaggle/input/tabular-playground-series-jan-2021/' )
def _bn_relu(input):

 norm = BatchNormalization(axis=CHANNEL_AXIS )(input)
 return Activation("relu" )(norm)
 def _conv_bn_relu(**conv_params):

 filters = conv_params["filters"]
kernel_size = conv_params["kernel_size"]
strides = conv_params.setdefault("strides",(1, 1))
kernel_initializer = conv_params.setdefault("kernel_initializer", "he_normal")
 padding = conv_params.setdefault("padding", "same")
 kernel_regularizer = conv_params.setdefault("kernel_regularizer", l2(1.e-4))
def f(input):
conv = Conv2D(filters=filters, kernel_size=kernel_size,
strides=strides, padding=padding,
kernel_initializer=kernel_initializer,
kernel_regularizer=kernel_regularizer )(input)
 return _bn_relu(conv)
 return f
def _bn_relu_conv(**conv_params):

 filters = conv_params["filters"]
kernel_size = conv_params["kernel_size"]
strides = conv_params.setdefault("strides",(1, 1))
kernel_initializer = conv_params.setdefault("kernel_initializer", "he_normal")
 padding = conv_params.setdefault("padding", "same")
 kernel_regularizer = conv_params.setdefault("kernel_regularizer", l2(1.e-4))
def f(input):
activation = _bn_relu(input)
 return Conv2D(filters=filters, kernel_size=kernel_size,
strides=strides, padding=padding,
kernel_initializer=kernel_initializer,
kernel_regularizer=kernel_regularizer )(activation)
 return f
def _shortcut(input, residual):

 input_shape = K.int_shape(input)
 residual_shape = K.int_shape(residual)
 stride_width = int(round(input_shape[ROW_AXIS] / residual_shape[ROW_AXIS]))
stride_height = int(round(input_shape[COL_AXIS] / residual_shape[COL_AXIS]))
equal_channels = input_shape[CHANNEL_AXIS] == residual_shape[CHANNEL_AXIS]
shortcut = input
if stride_width > 1 or stride_height > 1 or not equal_channels:
shortcut = Conv2D(filters=residual_shape[CHANNEL_AXIS],
kernel_size=(1, 1),
strides=(stride_width, stride_height),
padding="valid",
kernel_initializer="he_normal",
kernel_regularizer=l2(0.0001))(input)
 return add([shortcut, residual])
 def basic_block(filters, init_strides=(1, 1), is_first_block_of_first_layer=False):

 def f(input):
if is_first_block_of_first_layer:
conv1 = Conv2D(filters=filters, kernel_size=(3, 3),
strides=init_strides,
padding="same",
kernel_initializer="he_normal",
kernel_regularizer=l2(1e-4))(input)
 else:
conv1 = _bn_relu_conv(filters=filters, kernel_size=(3, 3),
strides=init_strides )(input)
 residual = _bn_relu_conv(filters=filters, kernel_size=(3, 3))(conv1)
 return _shortcut(input, residual)
 return f
def _residual_block(block_function, filters, repetitions, is_first_layer=False):

 def f(input):
for i in range(repetitions):
init_strides =(1, 1)
 if i == 0 and not is_first_layer:
init_strides =(2, 2)
 input = block_function(filters=filters, init_strides=init_strides,
is_first_block_of_first_layer=(is_first_layer and i == 0))(input)
 return input
return f
def _handle_dim_ordering() :
global ROW_AXIS
global COL_AXIS
global CHANNEL_AXIS
if K.image_data_format() == 'channels_last':
ROW_AXIS = 1
COL_AXIS = 2
CHANNEL_AXIS = 3
else:
CHANNEL_AXIS = 1
ROW_AXIS = 2
COL_AXIS = 3
def _get_block(identifier):
if isinstance(identifier, six.string_types):
res = globals().get(identifier)
 if not res:
raise ValueError('Invalid {}'.format(identifier))
return res
return identifier
class ResnetBuilder(object):
@staticmethod
def build(input_shape, block_fn, repetitions,input_tensor):
_handle_dim_ordering()
if len(input_shape)!= 3:
raise Exception("Input shape should be a tuple(nb_channels, nb_rows, nb_cols)")
 if K.image_data_format() == 'channels_last':
input_shape =(input_shape[1], input_shape[2], input_shape[0])
 block_fn = _get_block(block_fn)
 if input_tensor is None:
img_input = Input(shape=input_shape)
 else:
if not K.is_keras_tensor(input_tensor):
img_input = Input(tensor=input_tensor, shape=input_shape)
 else:
img_input = input_tensor
conv1 = _conv_bn_relu(filters=64, kernel_size=(7, 7), strides=(2, 2))(img_input)
 pool1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding="same" )(conv1)
 block = pool1
filters = 64
for i, r in enumerate(repetitions):
block = _residual_block(block_fn, filters=filters, repetitions=r, is_first_layer=(i == 0))(block)
 filters *= 2
block = _bn_relu(block)
 model = Model(inputs=img_input, outputs=block)
 return model
@staticmethod
def build_resnet_34(input_shape,input_tensor):
return ResnetBuilder.build(input_shape, basic_block, [3, 4, 6, 3],input_tensor )<compute_train_metric>
train = pd.read_csv(input_path / 'train.csv', index_col='id')
 print(train.shape)
 train.head()
def get_iou_vector(A, B):
batch_size = A.shape[0]
metric = []
for batch in range(batch_size):
t, p = A[batch], B[batch]
intersection = np.logical_and(t, p)
 union = np.logical_or(t, p)
 iou =(np.sum(intersection > 0)+ 1e-10)/(np.sum(union > 0)+ 1e-10)
 thresholds = np.arange(0.5, 1, 0.05)
 s = []
for thresh in thresholds:
s.append(iou > thresh)
 metric.append(np.mean(s))
return np.mean(metric)
 def my_iou_metric(label, pred):
return tf.py_function(get_iou_vector, [label, pred > 0.3], tf.float64)
 def my_iou_metric_2(label, pred):
return tf.py_function(get_iou_vector, [label, pred > 0.1], tf.float64 )<choose_model_class>
test = pd.read_csv(input_path / 'test.csv', index_col='id')
 print(test.shape)
 test.head()
def UResNet34(input_shape=(128, 128, 1), classes=1, decoder_filters=16, decoder_block_type='upsampling',
encoder_weights="imagenet", input_tensor=None, activation='sigmoid', **kwargs):
backbone = ResnetBuilder.build_resnet_34(input_shape=input_shape,input_tensor=input_tensor)
 input_layer = backbone.input
output_layer = build_model(input_layer, 16,0.5)
 model = Model(input_layer, output_layer)
 model.compile(loss="binary_crossentropy", optimizer='adam', metrics=[my_iou_metric])
 return model<choose_model_class>
train.isnull().sum()
model1 = UResNet34(input_shape =(1,img_size,img_size))
model1.summary()<choose_model_class>
trainNorm = train.copy()
for feature_name in train.columns:
mean_value = train[feature_name].mean()
std_value = train[feature_name].std()
trainNorm[feature_name] =(train[feature_name] - mean_value)/ std_value
trainNorm.head()
early_stopping = EarlyStopping(monitor='my_iou_metric', mode = 'max',patience=10, verbose=1)
 model_checkpoint = ModelCheckpoint(model_path,monitor='my_iou_metric',
mode = 'max', save_best_only=True, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='my_iou_metric', mode = 'max',factor=0.5, patience=5, min_lr=0.0001, verbose=1)
 history = model1.fit(x_train, y_train,
validation_data=(x_valid, y_valid),
epochs=epochs,
batch_size=batch_size,
callbacks=[model_checkpoint,reduce_lr],
verbose=1 )<choose_model_class>
testNorm = test.copy()
for feature_name in test.columns:
mean_value = test[feature_name].mean()
std_value = test[feature_name].std()
testNorm[feature_name] =(test[feature_name] - mean_value)/ std_value
testNorm.head()
model1 = load_model(model_path,custom_objects={'my_iou_metric': my_iou_metric})
 input_x = model1.layers[0].input
output_layer = model1.layers[-1].input
model = Model(input_x, output_layer)
 model.compile(loss=lovasz_loss, optimizer='adam', metrics=[my_iou_metric_2])
 model.summary()<choose_model_class>
X_train, X_test, y_train, y_test = train_test_split(trainNorm, target, test_size=0.2, random_state=7)
 print('X_train: ', X_train.shape)
 print('X_test: ', X_test.shape)
 print('y_train: ', y_train.shape)
 print('y_test: ', y_test.shape )
early_stopping = EarlyStopping(monitor='val_my_iou_metric_2', mode = 'max',patience=20, verbose=1)
 model_checkpoint = ModelCheckpoint(model_path,monitor='val_my_iou_metric_2',
mode = 'max', save_best_only=True, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='val_my_iou_metric_2', mode = 'max',factor=0.5, patience=5, min_lr=0.0001, verbose=1)
 history = model.fit(x_train, y_train,
validation_data=(x_valid, y_valid),
epochs=epochs,
batch_size=batch_size,
callbacks=[model_checkpoint,reduce_lr,early_stopping],
verbose=1 )<predict_on_test>
def rmse_cv(model,X,y):
rmse = np.sqrt(-cross_val_score(model, X, y, scoring="neg_mean_squared_error", cv=5))
return rmse
def predict_result(model,x_test,img_size):
x_test_reflect = np.array([np.fliplr(x)for x in x_test])
 preds_test = model.predict(x_test ).reshape(-1, img_size, img_size)
 preds_test2_refect = model.predict(x_test_reflect ).reshape(-1, img_size, img_size)
 preds_test += np.array([ np.fliplr(x)for x in preds_test2_refect])
 return preds_test<predict_on_test>
models = [LinearRegression() , Ridge() , Lasso() , ElasticNet() , SGDRegressor() , BayesianRidge() ,
cb.CatBoostRegressor() , RandomForestRegressor() , ]
names = ["LR", "Ridge", "Lasso", "ElasticNet", "SGD","BayesianRidge", "catboost","RandomForestRegressor"]
model = load_model(model_path,custom_objects={'my_iou_metric_2': my_iou_metric_2, 'lovasz_loss': lovasz_loss})
 preds_valid = predict_result(model,x_valid,img_size )<compute_train_metric>
%%time
ModScores = {}
for name, model in zip(names, models):
score = rmse_cv(model, X_train, y_train)
 ModScores[name] = score.mean()
print("{}: {:.2f}".format(name,score.mean()))
print("_"*100)
 for key, value in sorted(ModScores.items() , key = itemgetter(1), reverse = False):
print(key, round(value,3))
thresholds_ori = np.linspace(0.3, 0.7, 31)
 thresholds = np.log(thresholds_ori/(1-thresholds_ori))
ious = np.array([iou_metric_batch(y_valid, preds_valid > threshold)for threshold in tqdm(thresholds)])
 print(ious )<categorify>
model = cb.CatBoostRegressor()
model.fit(X_train, y_train)
 final_predictions = model.predict(X_test)
 final_mse = mean_squared_error(y_test, final_predictions)
 final_rmse = np.sqrt(final_mse)
 print("RMSE on X_test ", round(final_rmse, 4))
def rle_encode(im):

 pixels = im.flatten(order = 'F')
 pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs )<predict_on_test>
final_predictions = model.predict(testNorm)
 final_predictions.shape
subm = pd.read_csv(input_path / 'sample_submission.csv')
 print(subm.shape)
 subm.head()
id_col=subm.id
subm=id_col.to_frame()
subm['target'] = final_predictions
subm.set_index('id',inplace=True)
 subm.head()
subm.to_csv('CBSubmission.csv')
 LoadSub = pd.read_csv('CBSubmission.csv')
 LoadSub.set_index('id',inplace=True)
 LoadSub.head()
preds_test = predict_result(model,x_test,img_size)
 t1 = time.time()
pred_dict = {idx: rle_encode(np.round(preds_test[i])> threshold_best)for i, idx in enumerate(tqdm(test.id.values)) }
t2 = time.time()
print(f"Usedtime = {t2-t1} s" )<save_to_csv>
model = models.Sequential()
model.add(layers.Dense(512, activation='relu', input_shape=(X_train.shape[1],)))
 model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dropout(0.2))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(1))
model.summary()
model.compile(optimizer=optimizers.Adam() , loss='mse', metrics=['mae'])
 print("model compiled" )
submit = pd.DataFrame.from_dict(pred_dict,orient='index')
 submit.index.names = ['id']
submit.columns = ['rle_mask']
submit.to_csv(submission_file )<load_from_csv>
history = model.fit(X_train, y_train,
validation_split=0.2,
verbose=1,
epochs=10 )
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )<prepare_x_and_y>
final_predictions = model.predict(X_test)
 final_mse = mean_squared_error(y_test, final_predictions)
 final_rmse = np.sqrt(final_mse)
 print("RMSE on X_test ", round(final_rmse, 4))
x_train = train.drop('label', axis=1)/255.0
y_label = train['label'].values
x_test = test/255.0<define_variables>
final_predictions = model.predict(testNorm)
 final_predictions.shape
datagen = ImageDataGenerator(
 rotation_range=15,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
)<import_modules>
subm = pd.read_csv(input_path / 'sample_submission.csv')
 print(subm.shape)
 subm.head()
import tensorflow as tf<choose_model_class>
id_col=subm.id
subm=id_col.to_frame()
subm['target'] = final_predictions
subm.set_index('id',inplace=True)
 subm.head()
class ResidualUnit(tf.keras.layers.Layer):
def __init__(self, filters, strides=1, activation='relu', **kwargs):
super().__init__(**kwargs)
 self.activation = tf.keras.activations.get(activation)
 self.main_layers = [
tf.keras.layers.Conv2D(filters, 3, strides=strides, padding='SAME', use_bias=False),
tf.keras.layers.BatchNormalization() ,
self.activation,
tf.keras.layers.Conv2D(filters, 3, strides=1, padding='SAME', use_bias=False),
tf.keras.layers.BatchNormalization() ,
self.activation,
tf.keras.layers.Conv2D(filters, 3, strides=1, padding='SAME', use_bias=False),
tf.keras.layers.BatchNormalization() ,
]
self.skip_layers = []
if strides > 1:
self.skip_layers = [
tf.keras.layers.Conv2D(filters, 1, strides=strides, padding='SAME', use_bias=False),
tf.keras.layers.BatchNormalization() ,
]
def call(self, inputs):
Z = inputs
for layer in self.main_layers:
Z=layer(Z)
 skip_Z = inputs
for layer in self.skip_layers:
skip_Z = layer(skip_Z)
 return self.activation(Z +skip_Z )<choose_model_class>
subm.to_csv('NNSubmission.csv' )