Kaggle competition: TGS Salt Identification Challenge
<SOS> metric: IntersectionOverUnionObjectSegmentation Kaggle data source: tgs-salt-identification-challenge<count_values>
plt.style.use('seaborn-white')
 sns.set_style("white")
train['Title'] = train['Name'].str.extract(pat = '([a-zA-Z]+)\.', expand = False)
 train.Title.value_counts()<categorify>
img_size_ori = 101
img_size_target = 128
def upsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_target, img_size_target), mode='constant', preserve_range=True)
 def downsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_ori, img_size_ori), mode='constant', preserve_range=True )
train['Title'] = train['Title'].replace(['Mlle', 'Ms'], 'Miss')
 train['Title'] = train['Title'].replace('Mme', 'Mrs')
 train['Title'] = train['Title'].replace(['Mlle', 'Mme', 'Ms', 'Don', 'Rev', 'Lady', 'Sir', 'Major', 'Col', 'Capt', 'Countess', 'Jonkheer', 'Dr'], 'Other' )<count_values>
train_df = pd.read_csv(".. /input/tgs-salt-identification-challenge/train.csv", index_col="id", usecols=[0])
 val_ids = pd.read_csv('.. /input/validation-ids/validation_ids.csv')
 train_df = train_df.drop(val_ids.iloc[:,0].values)
 no_salt_ids = pd.read_csv(".. /input/pseudolabel-gen/no_salt_ids.csv", index_col="id", usecols=[0])
 no_salt_ids = no_salt_ids.sample(2340)
 some_salt_ids = pd.read_csv(".. /input/pseudolabel-gen/some_salt_ids.csv", index_col="id")
 some_salt_ids = some_salt_ids.sample(3660)
 depths_df = pd.read_csv(".. /input/tgs-salt-identification-challenge/depths.csv", index_col="id")
 train_df = train_df.join(depths_df)
 test_df = depths_df[~depths_df.index.isin(train_df.index)]
test['Title'] = test['Name'].str.extract(pat = '([a-zA-Z]+)\.', expand = False)
 test.Title.value_counts()<feature_engineering>
train_df["images"] = [np.array(load_img(".. /input/tgs-salt-identification-challenge/train/images/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]
train_df["masks"] = [np.array(load_img(".. /input/tgs-salt-identification-challenge/train/masks/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]
test['Title'] = test['Title'].replace('Ms', 'Miss')
 test['Title'] = test['Title'].replace(['Rev', 'Col', 'Dona', 'Dr'], 'Other' )<count_values>
def rle_decode(rle_mask):

 if str(rle_mask)==str(np.nan):
return np.zeros(( 101,101))
s = rle_mask.split()
starts, lengths = [np.asarray(x, dtype=int)for x in(s[0:][::2], s[1:][::2])]
starts -= 1
ends = starts + lengths
img = np.zeros(101*101, dtype=np.uint8)
 for lo, hi in zip(starts, ends):
img[lo:hi] = 1
return img.reshape(101,101 )
train['Cabin_Code'] = train.Cabin.str[0]
train.Cabin_Code.value_counts()<count_values>
train_df_temp = pd.DataFrame()
train_df_temp['id'] = no_salt_ids.index
train_df_temp = train_df_temp.set_index('id')
 train_df_temp = train_df_temp.join(depths_df)
 train_df_temp["images"] = [np.array(load_img(".. /input/tgs-salt-identification-challenge/test/images/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(no_salt_ids.index)]
train_df_temp["masks"] = [np.zeros(( img_size_ori,img_size_ori)) for idx in tqdm_notebook(no_salt_ids.index)]
train_df = train_df.append(train_df_temp)
 del train_df_temp
test['Cabin_Code'] = test.Cabin.str[0]
test.Cabin_Code.value_counts()<data_type_conversions>
train_df_temp = pd.DataFrame()
train_df_temp['id'] = some_salt_ids.index
train_df_temp = train_df_temp.set_index('id')
 train_df_temp = train_df_temp.join(depths_df)
 train_df_temp["images"] = [np.array(load_img(".. /input/tgs-salt-identification-challenge/test/images/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(some_salt_ids.index)]
train_df_temp["masks"] = [np.fliplr(np.rot90(rle_decode(some_salt_ids.loc[idx,'rle_mask']),-1)) for idx in tqdm_notebook(some_salt_ids.index)]
train_df = train_df.append(train_df_temp)
 del train_df_temp
gc.collect()
train['Ticket_Numeric'] = train.Ticket.str.isnumeric().astype('uint8')
 test['Ticket_Numeric'] = test.Ticket.str.isnumeric().astype('uint8' )<create_dataframe>
train_df["coverage"] = train_df.masks.map(np.sum)/ pow(img_size_ori, 2)
 def cov_to_class(val):
for i in range(0, 11):
if val * 10 <= i :
return i
train_df["coverage_class"] = train_df.coverage.map(cov_to_class )
pd.pivot_table(train,index='Survived',columns='Ticket_Numeric',values='Name', aggfunc='count', fill_value=0 )<feature_engineering>
ids_valid = val_ids.iloc[:,0].values
temp_df = pd.DataFrame()
temp_df["images"] = [np.array(load_img(".. /input/tgs-salt-identification-challenge/train/images/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(ids_valid)]
temp_df["masks"] = [np.array(load_img(".. /input/tgs-salt-identification-challenge/train/masks/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(ids_valid)]
x_valid = np.array(temp_df.images.map(upsample ).tolist() ).reshape(-1, img_size_target, img_size_target, 1)
 y_valid = np.array(temp_df.masks.map(upsample ).tolist() ).reshape(-1, img_size_target, img_size_target, 1)
 del temp_df
gc.collect()
train['Ticket_Alphabets'] = train.Ticket.str.extract('([A-Za-z./0-9]+\)', expand = False )<count_values>
ids_train,x_train,y_train = train_df.index.values,\
np.array(train_df.images.map(upsample ).tolist() ).reshape(-1, img_size_target, img_size_target, 1), \
np.array(train_df.masks.map(upsample ).tolist() ).reshape(-1, img_size_target, img_size_target, 1 )
train.Ticket_Alphabets.value_counts()<feature_engineering>
def get_iou_vector(A, B):
batch_size = A.shape[0]
metric = 0.0
for batch in range(batch_size):
t, p = A[batch], B[batch]
true = np.sum(t)
 pred = np.sum(p)
 if true == 0:
metric +=(pred == 0)
 continue
intersection = np.sum(t * p)
 union = true + pred - intersection
iou = intersection / union
iou = np.floor(max(0,(iou - 0.45)*20)) / 10
metric += iou
metric /= batch_size
return metric
def my_iou_metric(label, pred):
return tf.py_func(get_iou_vector, [label, pred > 0.5], tf.float64 )
train['Ticket_Alphabets'] = train.Ticket_Alphabets.str.lower().str.extract('([a-z.]+)')
 train['Ticket_Alphabets'] = train.Ticket_Alphabets.str.replace('.', '', regex = True )<count_values>
def dice_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
 y_pred = K.cast(y_pred, 'float32')
 y_pred_f = K.cast(K.greater(K.flatten(y_pred), 0.5), 'float32')
 intersection = y_true_f * y_pred_f
score = 2.* K.sum(intersection)/(K.sum(y_true_f)+ K.sum(y_pred_f))
return score
def dice_loss(y_true, y_pred):
smooth = 1.
 y_true_f = K.flatten(y_true)
 y_pred_f = K.flatten(y_pred)
 intersection = y_true_f * y_pred_f
score =(2.* K.sum(intersection)+ smooth)/(K.sum(y_true_f)+ K.sum(y_pred_f)+ smooth)
 return 1.- score
def bce_dice_loss(y_true, y_pred):
return binary_crossentropy(y_true, y_pred)+ dice_loss(y_true, y_pred)
 def bce_logdice_loss(y_true, y_pred):
return binary_crossentropy(y_true, y_pred)- K.log(1.- dice_loss(y_true, y_pred))
train.Ticket_Alphabets.value_counts()<feature_engineering>
class SnapshotCallbackBuilder:
def __init__(self, nb_epochs, nb_snapshots, init_lr=0.1):
self.T = nb_epochs
self.M = nb_snapshots
self.alpha_zero = init_lr
def get_callbacks(self, model_prefix='Model'):
callback_list = [
callbacks.ModelCheckpoint("./keras.model",monitor='val_my_iou_metric',
mode = 'max', save_best_only=True, verbose=1),
swa,
callbacks.LearningRateScheduler(schedule=self._cosine_anneal_schedule)
 ]
return callback_list
def _cosine_anneal_schedule(self, t):
cos_inner = np.pi *(t %(self.T // self.M))
cos_inner /= self.T // self.M
cos_out = np.cos(cos_inner)+ 1
return float(self.alpha_zero / 2 * cos_out )
train.loc[(train['Title']=='Mr')&(train['Age'].isna()), 'Age'] = train.loc[(train['Title']=='Mr'), 'Age'].mean(skipna = True)
 train.loc[(train['Title']=='Mrs')&(train['Age'].isna()), 'Age'] = train.loc[(train['Title']=='Mrs'), 'Age'].mean(skipna = True)
 train.loc[(train['Title']=='Miss')&(train['Age'].isna()), 'Age'] = train.loc[(train['Title']=='Miss'), 'Age'].mean(skipna = True)
 train.loc[(train['Title']=='Master')&(train['Age'].isna()), 'Age'] = train.loc[(train['Title']=='Master'), 'Age'].mean(skipna = True)
 train.loc[(train['Title']=='Other')&(train['Age'].isna()), 'Age'] = train.loc[(train['Title']=='Other'), 'Age'].mean(skipna = True )<data_type_conversions>
def convolution_block(x, filters, size, strides=(1,1), padding='same', activation=True):
x = Conv2D(filters, size, strides=strides, padding=padding )(x)
 x = BatchNormalization()(x)
 if activation == True:
x = LeakyReLU(alpha=0.1 )(x)
 return x
def residual_block(blockInput, num_filters=16):
x = LeakyReLU(alpha=0.1 )(blockInput)
 x = BatchNormalization()(x)
 blockInput = BatchNormalization()(blockInput)
 x = convolution_block(x, num_filters,(3,3))
x = convolution_block(x, num_filters,(3,3), activation=False)
 x = Add()([x, blockInput])
 return x
train['Embarked'] = train['Embarked'].fillna(train['Embarked'].mode().iloc[0] )<feature_engineering>
def UXception(input_shape=(None, None, 3)) :
backbone = Xception(input_shape=input_shape,weights='imagenet',include_top=False)
 input = backbone.input
start_neurons = 16
conv4 = backbone.layers[121].output
conv4 = LeakyReLU(alpha=0.1 )(conv4)
 pool4 = MaxPooling2D(( 2, 2))(conv4)
 pool4 = Dropout(0.1 )(pool4)
 convm = Conv2D(start_neurons * 32,(3, 3), activation=None, padding="same" )(pool4)
 convm = residual_block(convm,start_neurons * 32)
 convm = residual_block(convm,start_neurons * 32)
 convm = LeakyReLU(alpha=0.1 )(convm)
 deconv4 = Conv2DTranspose(start_neurons * 16,(3, 3), strides=(2, 2), padding="same" )(convm)
 uconv4 = concatenate([deconv4, conv4])
 uconv4 = Dropout(0.1 )(uconv4)
 uconv4 = Conv2D(start_neurons * 16,(3, 3), activation=None, padding="same" )(uconv4)
 uconv4 = residual_block(uconv4,start_neurons * 16)
 uconv4 = residual_block(uconv4,start_neurons * 16)
 uconv4 = LeakyReLU(alpha=0.1 )(uconv4)
 deconv3 = Conv2DTranspose(start_neurons * 8,(3, 3), strides=(2, 2), padding="same" )(uconv4)
 conv3 = backbone.layers[31].output
uconv3 = concatenate([deconv3, conv3])
 uconv3 = Dropout(0.1 )(uconv3)
 uconv3 = Conv2D(start_neurons * 8,(3, 3), activation=None, padding="same" )(uconv3)
 uconv3 = residual_block(uconv3,start_neurons * 8)
 uconv3 = residual_block(uconv3,start_neurons * 8)
 uconv3 = LeakyReLU(alpha=0.1 )(uconv3)
 deconv2 = Conv2DTranspose(start_neurons * 4,(3, 3), strides=(2, 2), padding="same" )(uconv3)
 conv2 = backbone.layers[21].output
conv2 = ZeroPadding2D(((1,0),(1,0)) )(conv2)
 uconv2 = concatenate([deconv2, conv2])
 uconv2 = Dropout(0.1 )(uconv2)
 uconv2 = Conv2D(start_neurons * 4,(3, 3), activation=None, padding="same" )(uconv2)
 uconv2 = residual_block(uconv2,start_neurons * 4)
 uconv2 = residual_block(uconv2,start_neurons * 4)
 uconv2 = LeakyReLU(alpha=0.1 )(uconv2)
 deconv1 = Conv2DTranspose(start_neurons * 2,(3, 3), strides=(2, 2), padding="same" )(uconv2)
 conv1 = backbone.layers[11].output
conv1 = ZeroPadding2D(((3,0),(3,0)) )(conv1)
 uconv1 = concatenate([deconv1, conv1])
 uconv1 = Dropout(0.1 )(uconv1)
 uconv1 = Conv2D(start_neurons * 2,(3, 3), activation=None, padding="same" )(uconv1)
 uconv1 = residual_block(uconv1,start_neurons * 2)
 uconv1 = residual_block(uconv1,start_neurons * 2)
 uconv1 = LeakyReLU(alpha=0.1 )(uconv1)
 uconv0 = Conv2DTranspose(start_neurons * 1,(3, 3), strides=(2, 2), padding="same" )(uconv1)
 uconv0 = Dropout(0.1 )(uconv0)
 uconv0 = Conv2D(start_neurons * 1,(3, 3), activation=None, padding="same" )(uconv0)
 uconv0 = residual_block(uconv0,start_neurons * 1)
 uconv0 = residual_block(uconv0,start_neurons * 1)
 uconv0 = LeakyReLU(alpha=0.1 )(uconv0)
 uconv0 = Dropout(0.1/2 )(uconv0)
 output_layer = Conv2D(1,(1,1), padding="same", activation="sigmoid" )(uconv0)
 model = Model(input, output_layer)
 model.name = 'u-xception'
return model
test.loc[(test['Title']=='Mr')&(test['Age'].isna()), 'Age'] = train.loc[(train['Title']=='Mr'), 'Age'].mean(skipna = True)
 test.loc[(test['Title']=='Mrs')&(test['Age'].isna()), 'Age'] = train.loc[(train['Title']=='Mrs'), 'Age'].mean(skipna = True)
 test.loc[(test['Title']=='Miss')&(test['Age'].isna()), 'Age'] = train.loc[(train['Title']=='Miss'), 'Age'].mean(skipna = True)
 test.loc[(test['Title']=='Master')&(test['Age'].isna()), 'Age'] = train.loc[(train['Title']=='Master'), 'Age'].mean(skipna = True)
 test.loc[(test['Title']=='Other')&(test['Age'].isna()), 'Age'] = train.loc[(train['Title']=='Other'), 'Age'].mean(skipna = True )<feature_engineering>
K.clear_session()
model = UXception(input_shape=(img_size_target,img_size_target,3))
test['Fare'] = test['Fare'].fillna(train['Fare'].median() )<categorify>
def l2_norm(grad):
norm = K.sqrt(K.sum(K.square(grad)))+ K.epsilon()
return norm
class OptimizerWrapper(optimizers.Optimizer):
def __init__(self, optimizer):
self.optimizer = optimizers.get(optimizer)
 self._optimizer_get_gradients = self.optimizer.get_gradients
def get_gradients(self, loss, params):
grads = self._optimizer_get_gradients(loss, params)
 return grads
@interfaces.legacy_get_updates_support
def get_updates(self, loss, params):
self.optimizer.get_gradients = self.get_gradients
self.optimizer.get_updates(loss, params)
 self.optimizer.get_gradients = self._optimizer_get_gradients
return self.updates
def set_weights(self, weights):
self.optimizer.set_weights(weights)
 def get_weights(self):
return self.optimizer.get_weights()
def get_config(self):
config = {'optimizer_name': self.optimizer.__class__.__name__.lower() }
optimizer_config = {'optimizer_config': self.optimizer.get_config() }
return dict(list(optimizer_config.items())+ list(config.items()))
@property
def weights(self):
return self.optimizer.weights
@property
def updates(self):
return self.optimizer.updates
@classmethod
def from_config(cls, config):
raise NotImplementedError
@classmethod
def set_normalization_function(cls, name, func):
global _NORMS
_NORMS[name] = func
@classmethod
def get_normalization_functions(cls):
global _NORMS
return sorted(list(_NORMS.keys()))
class NormalizedOptimizer(OptimizerWrapper):
def __init__(self, optimizer, normalization='l2'):
super(NormalizedOptimizer, self ).__init__(optimizer)
 if normalization not in _NORMS:
raise ValueError('`normalization` must be one of %s.
 '
'Provided was "%s".' %(str(sorted(list(_NORMS.keys()))), normalization))
self.normalization = normalization
self.normalization_fn = _NORMS[normalization]
self.lr = K.variable(1e-3, name='lr')
 def get_gradients(self, loss, params):
grads = super(NormalizedOptimizer, self ).get_gradients(loss, params)
 grads = [grad / self.normalization_fn(grad)for grad in grads]
return grads
def get_config(self):
config = {'normalization': self.normalization}
base_config = super(NormalizedOptimizer, self ).get_config()
return dict(list(base_config.items())+ list(config.items()))
@classmethod
def from_config(cls, config):
optimizer_config = {'class_name': config['optimizer_name'],
'config': config['optimizer_config']}
optimizer = optimizers.get(optimizer_config)
 normalization = config['normalization']
return cls(optimizer, normalization=normalization)
 _NORMS = {
'l2': l2_norm,
}
get_custom_objects().update({'NormalizedOptimizer': NormalizedOptimizer} )
train = pd.get_dummies(data = train, columns = ['Sex', 'Embarked'], drop_first = True)
 train = pd.get_dummies(data = train, columns=['Cabin_Code'] )<categorify>
class SWA(keras.callbacks.Callback):
def __init__(self, filepath, swa_epoch):
super(SWA, self ).__init__()
self.filepath = filepath
self.swa_epoch = swa_epoch
def on_train_begin(self, logs=None):
self.nb_epoch = self.params['epochs']
print('Stochastic weight averaging selected for last {} epochs.' 
.format(self.nb_epoch - self.swa_epoch))
def on_epoch_end(self, epoch, logs=None):
if epoch == self.swa_epoch:
self.swa_weights = self.model.get_weights()
elif epoch > self.swa_epoch:
for i in range(len(self.swa_weights)) :
self.swa_weights[i] =(self.swa_weights[i] * 
(epoch - self.swa_epoch)+ self.model.get_weights() [i])/(( epoch - self.swa_epoch)+ 1)
 else:
pass
def on_train_end(self, logs=None):
self.model.set_weights(self.swa_weights)
 print('Final model parameters set to stochastic weight average.')
 self.model.save_weights(self.filepath)
 print('Final stochastic averaged weights saved to file.' )
test = pd.get_dummies(data = test, columns = ['Sex', 'Embarked'], drop_first = True)
 test = pd.get_dummies(data = test, columns=['Cabin_Code'])
 test['Cabin_Code_T'] = 0 
<prepare_x_and_y>
sgd = SGD(0.01, momentum=0.9, nesterov=True)
 sgd = NormalizedOptimizer(sgd, normalization='l2' )
sc = StandardScaler()
train[['Age', 'Fare']] = pd.DataFrame(sc.fit_transform(train[['Age', 'Fare']]))
test[['Age', 'Fare']] = pd.DataFrame(sc.transform(test[['Age', 'Fare']]))<drop_column>
model.compile(loss=bce_dice_loss, optimizer=sgd, metrics=[my_iou_metric] )
train = train.drop(columns = ['Cabin', 'Ticket', 'PassengerId', 'Name', 'Title', 'Ticket_Alphabets'] )<drop_column>
x_train = np.append(x_train, [np.fliplr(x)for x in x_train[0:3200]], axis=0)
 y_train = np.append(y_train, [np.fliplr(x)for x in y_train[0:3200]], axis=0)
 x_train = np.repeat(x_train,3,axis=3)
 x_valid = np.repeat(x_valid,3,axis=3 )
test = test.drop(columns = ['Cabin', 'Ticket', 'PassengerId', 'Name', 'Title'] )<prepare_x_and_y>
epochs = 40
snapshot = SnapshotCallbackBuilder(nb_epochs=epochs,nb_snapshots=1,init_lr=1e-3)
 batch_size = 32
swa = SWA('./keras_swa.model',35)
 history = model.fit(x_train, y_train,
validation_data=[x_valid, y_valid],
epochs=epochs,
batch_size=batch_size,
callbacks=snapshot.get_callbacks() ,shuffle=True,verbose=2 )
X = train.drop(columns = ['Survived'])
 y = train['Survived']<choose_model_class>
del ids_train, x_train, y_train,some_salt_ids,depths_df
gc.collect()
classifier_lr = LogisticRegression(penalty = 'l2', random_state=0 )<choose_model_class>
try:
print('using swa weight model')
 model.load_weights('./keras_swa.model')
 except:
model.load_weights('./keras.model' )
classifier_nb = GaussianNB()<choose_model_class>
def predict_result(model,x_test,img_size_target,batch_size):
x_test_reflect = np.array([np.fliplr(x)for x in x_test])
 preds_test1 = model.predict([x_test],batch_size=batch_size ).reshape(-1, img_size_target, img_size_target)
 preds_test2_refect = model.predict([x_test_reflect],batch_size=batch_size ).reshape(-1, img_size_target, img_size_target)
 preds_test2 = np.array([ np.fliplr(x)for x in preds_test2_refect])
 preds_avg =(preds_test1 +preds_test2)/2
return preds_avg
classifier_svm = SVC(C=1, kernel='rbf', gamma = 'auto' )<choose_model_class>
preds_valid = predict_result(model,x_valid,img_size_target,batch_size)
 preds_valid = np.array([downsample(x)for x in preds_valid])
 y_valid_ori = np.array([downsample(x)for x in y_valid] )
classifier_rf = RandomForestClassifier(n_estimators=100, criterion = 'entropy', min_samples_split=10, random_state=0 )<compute_train_metric>
thresholds = np.linspace(0.3, 0.7, 31)
 ious = np.array([iou_metric_batch(y_valid_ori, np.int32(preds_valid > threshold)) for threshold in tqdm_notebook(thresholds)] )
lr = cross_val_score(estimator = classifier_lr, X = X, y = y, cv = 10)
 print(lr )<compute_train_metric>
del x_valid, y_valid,preds_valid,y_valid_ori,train_df
gc.collect()
nb = cross_val_score(estimator = classifier_nb, X = X, y = y, cv = 10)
 print(nb )<compute_train_metric>
train_df = pd.read_csv(".. /input/tgs-salt-identification-challenge/train.csv", index_col="id", usecols=[0])
 depths_df = pd.read_csv(".. /input/tgs-salt-identification-challenge/depths.csv", index_col="id")
 train_df = train_df.join(depths_df)
 test_df = depths_df[~depths_df.index.isin(train_df.index)]
svm = cross_val_score(estimator = classifier_svm, X = X, y = y, cv = 10)
 print(svm )<compute_train_metric>
batch_size = 500
preds_test = []
i = 0
while i < test_df.shape[0]:
index_val = test_df.index[i:i+batch_size]
x_test = np.array([upsample(np.array(load_img(".. /input/tgs-salt-identification-challenge/test/images/{}.png".format(idx), grayscale=True)))/ 255 for idx in(index_val)] ).reshape(-1, img_size_target, img_size_target, 1)
 x_test = np.repeat(x_test,3,axis=3)
 preds_test_temp = predict_result(model,x_test,img_size_target,32)
 if i==0:
preds_test = preds_test_temp
else:
preds_test = np.concatenate([preds_test,preds_test_temp],axis=0)
 if i%2000==0:
print('Images Processed:',i)
 i += batch_size
print('Done!' )
rf = cross_val_score(estimator = classifier_rf, X = X, y = y, cv = 10)
 print(rf )<define_variables>

 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 )
model = ['Logistic Regression', 'Naive Bayes', 'SVM', 'Random Forest']
accuracy_mean = [np.mean(lr), np.mean(nb), np.mean(svm), np.mean(rf)]
accuracy_std = [np.std(lr), np.std(nb), np.std(svm), np.std(rf)]<create_dataframe>
t1 = time.time()
pred_dict = {idx: rle_encode(np.round(downsample(preds_test[i])> threshold_best)) for i, idx in enumerate(tqdm_notebook(test_df.index.values)) }
t2 = time.time()
print(f"Usedtime = {t2-t1} s" )
<predict_on_test><EOS> 
sub = pd.DataFrame.from_dict(pred_dict,orient='index')
 sub.index.names = ['id']
sub.columns = ['rle_mask']
sub.to_csv('swa_xce_submission.csv' )