Kaggle competition: TGS Salt Identification Challenge
<SOS> metric: IntersectionOverUnionObjectSegmentation Kaggle data source: tgs-salt-identification-challenge<filter>
imread, imshow, concatenate_images 
)
 Input,Dropout,BatchNormalization,
Activation,Add, LeakyReLU,
UpSampling2D, Reshape, GaussianNoise,
Wrapper, Dense 
)
 EarlyStopping, ModelCheckpoint, ReduceLROnPlateau 
)
 ImageDataGenerator, array_to_img, img_to_array,
load_img 
)
print(train.loc[train['trip_duration'] > 500000] )<categorify>
class DropConnect(Wrapper):
def __init__(self, layer, prob=1., **kwargs):
self.prob = prob
self.layer = layer
super(DropConnect, self ).__init__(layer, **kwargs)
 if 0.< self.prob < 1.:
self.uses_learning_phase = True
def build(self, input_shape):
if not self.layer.built:
self.layer.build(input_shape)
 self.layer.built = True
super(DropConnect, self ).build()
def compute_output_shape(self, input_shape):
return self.layer.compute_output_shape(input_shape)
 def call(self, x):
if 0.< self.prob < 1.:
self.layer.kernel = K.in_train_phase(K.dropout(self.layer.kernel, self.prob), self.layer.kernel)
 self.layer.bias = K.in_train_phase(K.dropout(self.layer.bias, self.prob), self.layer.bias)
 return self.layer.call(x )
le = LabelEncoder()
le.fit(train['store_and_fwd_flag'])
 train['store_and_fwd_flag'] = le.transform(train['store_and_fwd_flag'])
 test['store_and_fwd_flag'] = le.transform(test['store_and_fwd_flag'])
 train.head()<feature_engineering>
def set_random(number):
os.environ['PYTHONHASHSEED'] = '6072019'
np.random.seed(number)
 random.seed(number)
 session_conf = tf.ConfigProto(
 intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1 
)
 tf.set_random_seed(number)
 sess = tf.Session(graph=tf.get_default_graph() , config=session_conf)
 K.set_session(sess )
train['pickup_datetime'] = pd.to_datetime(train['pickup_datetime'])
 test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'])
 train['month'] = train['pickup_datetime'].dt.month
train['day'] = train['pickup_datetime'].dt.day
train['weekday'] = train['pickup_datetime'].dt.weekday
train['hour'] = train['pickup_datetime'].dt.hour
train['minute'] = train['pickup_datetime'].dt.minute
test['month'] = test['pickup_datetime'].dt.month
test['day'] = test['pickup_datetime'].dt.day
test['weekday'] = test['pickup_datetime'].dt.weekday
test['hour'] = test['pickup_datetime'].dt.hour
test['minute'] = test['pickup_datetime'].dt.minute
train.head() 
<prepare_x_and_y>
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 )
selection_train = ["passenger_count", "pickup_longitude", "pickup_latitude", "dropoff_longitude","dropoff_latitude","month", "day", "weekday", "hour", "minute"]
selection_test = ["passenger_count", "pickup_longitude", "pickup_latitude", "dropoff_longitude","dropoff_latitude", "month", "day", "weekday", "hour", "minute"]
y_train = train["trip_duration"]
X_train = train[selection_train]
X_test = test[selection_test]<normalization>
def swish(x):
return(K.sigmoid(x)* x )
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
 X_test_scaled = scaler.transform(X_test )<define_search_model>
def dropconnect(input_value, prob):
return tf.nn.dropout(input_value, keep_prob=prob)* prob
m1 = RandomForestRegressor() 
<feature_engineering>

 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 )
<train_model>
def predict_result(model,x_test,img_size_target):
x_test_reflect = np.array([np.fliplr(x)for x in x_test])
 preds_test1 = model.predict(x_test ).reshape(-1, img_size_target, img_size_target)
 preds_test2_refect = model.predict(x_test_reflect ).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
print("Start Training...")
 m1.fit(X_train, y_train)
<predict_on_test>
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 = Activation('swish' )(x)
 return x
def residual_block(blockInput, num_filters=16):
x = Activation('swish' )(blockInput)
 x = BatchNormalization()(x)
 x = convolution_block(x, num_filters,(3,3))
x = convolution_block(x, num_filters,(3,3), activation=False)
 x = Add()([x, blockInput])
 return x
y_pred = m1.predict(X_test)
<train_model>
def build_model(input_layer, start_neurons, DropoutRatio = 0.5):
prob_dc = 1 -(DropoutRatio/4)
 conv1 = DropConnect(Conv2D(start_neurons * 1,(3, 3), activation=None, padding="same"), prob =prob_dc )(input_layer)
 conv1 = residual_block(conv1,start_neurons * 1)
 conv1 = residual_block(conv1,start_neurons * 1)
 conv1 = Activation('swish' )(conv1)
 pool1 = MaxPooling2D(( 2, 2))(conv1)
 conv2 = Conv2D(start_neurons * 2,(3, 3), activation=None, padding="same" )(pool1)
 conv2 = residual_block(conv2,start_neurons * 2)
 conv2 = residual_block(conv2,start_neurons * 2)
 conv2 = Activation('swish' )(conv2)
 pool2 = MaxPooling2D(( 2, 2))(conv2)
 pool2 = Dropout(DropoutRatio/2 )(pool2)
 conv3 = Conv2D(start_neurons * 4,(3, 3), activation=None, padding="same" )(pool2)
 conv3 = residual_block(conv3,start_neurons * 4)
 conv3 = residual_block(conv3,start_neurons * 4)
 conv3 = Activation('swish' )(conv3)
 pool3 = MaxPooling2D(( 2, 2))(conv3)
 pool3 = Dropout(DropoutRatio/2 )(pool3)
 conv4 = Conv2D(start_neurons * 8,(3, 3), activation=None, padding="same" )(pool3)
 conv4 = residual_block(conv4,start_neurons * 8)
 conv4 = residual_block(conv4,start_neurons * 8)
 conv4 = Activation('swish' )(conv4)
 pool4 = MaxPooling2D(( 2, 2))(conv4)
 pool4 = Dropout(DropoutRatio/2 )(pool4)
 convm = Conv2D(start_neurons * 16,(3, 3), activation=None, padding="same" )(pool4)
 convm = residual_block(convm,start_neurons * 16)
 convm = residual_block(convm,start_neurons * 16)
 convm = Activation('swish' )(convm)
 deconv4 = Conv2DTranspose(start_neurons * 8,(3, 3), strides=(2, 2), padding="same" )(convm)
 uconv4 = concatenate([deconv4, conv4])
 uconv4 = Dropout(DropoutRatio/2 )(uconv4)
 uconv4 = Conv2D(start_neurons * 8,(3, 3), activation=None, padding="same" )(uconv4)
 uconv4 = residual_block(uconv4,start_neurons * 8)
 uconv4 = residual_block(uconv4,start_neurons * 8)
 uconv4 = Activation('swish' )(uconv4)
 deconv3 = Conv2DTranspose(start_neurons * 4,(3, 3), strides=(2, 2), padding="valid" )(uconv4)
 uconv3 = concatenate([deconv3, conv3])
 uconv3 = Dropout(DropoutRatio/2 )(uconv3)
 uconv3 = Conv2D(start_neurons * 4,(3, 3), activation=None, padding="same" )(uconv3)
 uconv3 = residual_block(uconv3,start_neurons * 4)
 uconv3 = residual_block(uconv3,start_neurons * 4)
 uconv3 = Activation('swish' )(uconv3)
 deconv2 = Conv2DTranspose(start_neurons * 2,(3, 3), strides=(2, 2), padding="same" )(uconv3)
 uconv2 = concatenate([deconv2, conv2])
 uconv2 = Dropout(DropoutRatio/2 )(uconv2)
 uconv2 = Conv2D(start_neurons * 2,(3, 3), activation=None, padding="same" )(uconv2)
 uconv2 = residual_block(uconv2,start_neurons * 2)
 uconv2 = residual_block(uconv2,start_neurons * 2)
 uconv2 = Activation('swish' )(uconv2)
 deconv1 = Conv2DTranspose(start_neurons * 1,(3, 3), strides=(2, 2), padding="valid" )(uconv2)
 uconv1 = concatenate([deconv1, conv1])
 uconv1 = Dropout(DropoutRatio/2 )(uconv1)
 uconv1 = Conv2D(start_neurons * 1,(3, 3), activation=None, padding="same" )(uconv1)
 uconv1 = residual_block(uconv1,start_neurons * 1)
 uconv1 = residual_block(uconv1,start_neurons * 1)
 uconv1 = Activation('swish' )(uconv1)
 prob = 1 -(DropoutRatio/4)
 uconv1 = Lambda(dropconnect,arguments={"prob":prob} )(uconv1)
 output_layer = Conv2D(1,(1,1), padding="same", activation="sigmoid" )(uconv1)
 return output_layer
filename = 'finalized_model.sav'
joblib.dump(m1, filename)
<prepare_output>
set_random(1537315160)
 img_size_ori = 101
img_size_target = 101
DATA_DIR = ".. /input/tgsdata/"
x_train2 = np.load(".. /input/intermediatetgs/x_train2.npy")
 x_valid = np.load(".. /input/intermediatetgs/x_valid.npy")
 y_train2 = np.load(".. /input/intermediatetgs/y_train2.npy")
 y_valid = np.load(".. /input/intermediatetgs/y_valid.npy")
 print("Train Data:")
 print("X: ",x_train2.shape,"Y: ",y_train2.shape)
 print("Validation Data:")
 print("X: ",x_valid.shape,"Y: ",y_valid.shape)
 get_custom_objects().update({"swish": swish} )
my_submission = pd.DataFrame({'id': test.id, 'trip_duration': np.expm1(y_pred)})
 my_submission.head()<save_to_csv>
input_layer = Input(( img_size_target, img_size_target, 2))
output_layer = build_model(input_layer, 16,0.6)
 model = Model(input_layer, output_layer)
 adam_opt = optimizers.Adam(lr=0.001,
beta_1=0.9,beta_2=0.999,
epsilon=None,
amsgrad=False,clipnorm=1.0 
)
 model.compile(loss="binary_crossentropy", optimizer=adam_opt, metrics=[my_iou_metric])
my_submission.to_csv('submission.csv', index=False)
 print("Submission done !" )<set_options>
early_stopping = EarlyStopping(monitor='val_my_iou_metric', mode = 'max',
patience=35, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='val_my_iou_metric', mode = 'max',factor=0.97,
patience=4, min_lr=0, verbose=1)
 model_checkpoint = ModelCheckpoint("./unet_best1.model",monitor='val_my_iou_metric',
mode = 'max', save_best_only=True, verbose=1)
 epochs = 200
batch_size = 32
history = model.fit(x_train2,
y_train2,
validation_data=[x_valid, y_valid],
epochs=epochs,
shuffle=True,
batch_size=batch_size,
callbacks=[early_stopping, model_checkpoint, reduce_lr],
verbose=1
)
%matplotlib inline<load_from_csv>
preds_valid = predict_result(model,x_valid,img_size_target)
 preds_valid2 = np.array([downsample(x)for x in preds_valid])
 y_valid2 = np.array([downsample(x)for x in y_valid] )
test = pd.read_csv('.. /input/test.csv')
 train = pd.read_csv('.. /input/train.csv' )<count_missing_values>
thresholds = np.linspace(0.3, 0.7, 31)
 ious = np.array([iou_metric_batch(y_valid2, np.int32(preds_valid2 > threshold)) for threshold in tqdm_notebook(thresholds)])
 threshold_best_index = np.argmax(ious)
 iou_best = ious[threshold_best_index]
threshold_best = thresholds[threshold_best_index]
train.isna().sum()<count_duplicates>
train.duplicated().sum()<count_missing_values>
x_test = np.load(".. /input/intermediatetgs/x_test.npy" )
train.isnull().sum()<count_missing_values>
preds_test = predict_result(model,x_test,img_size_target )
train.isnull().sum()<filter>
with open(".. /input/intermediatetgs/test_index.obj", "rb")as f:
indexes = _pickle.load(f )
train = train[(train['log_trip_duration'] > 3.5)]
train = train[(train['log_trip_duration'] < 9)]<filter>
t1 = time.time()
pred_dict = {idx: rle_encode(np.round(downsample(preds_test[i])> threshold_best)) for i, idx in enumerate(tqdm_notebook(indexes)) }
t2 = time.time()
print(f"Usedtime = {t2-t1} s" )
train = train.loc[train['pickup_longitude']> -74.02]
train = train.loc[train['pickup_latitude']< 40.77]<filter>
sub = pd.DataFrame.from_dict(pred_dict,orient="index")
 sub.index.names = ["id"]
sub.columns = ["rle_mask"]
sub.to_csv("submission4.csv" )
<filter><EOS>