Kaggle competition: Statoil/C-CORE Iceberg Classifier Challenge
import re
import tensorflow as tf
import tensorflow_text
import string
import tensorflow_hub as hub
import matplotlib.pyplot as plt<feature_engineering>
train_df = pd.read_json('.. /input/train.json')
 test_df = pd.read_json('.. /input/test.json')
 train_df.head()
puncts = re.compile('[%s]' % re.escape(string.punctuation))
def preprocess(df):
df['text'] = re.sub(r'&[a-zA-Z0-9]+;?', "", df['text'])
 df['text'] = re.sub("'ld", ' would', df['text'])
 df['text'] = re.sub("'d", " had", df['text'])
 df['text'] = re.sub("'ve", " have", df['text'])
 df['text'] = re.sub("'m", " am", df['text'])
 df['text'] = re.sub("n't", " not", df['text'])
 df['text'] = re.sub("won't", "would not", df['text'])
 df['text'] = re.sub(r'https?:\/\/(www\.) ?[-a-zA-Z0-9@:%._\+~
df['text'] = re.sub("@[A-Za-z0-9_]+","",df['text'])
 df['text'] = re.sub('
df['text'] = puncts.sub("", df['text'])
 df['text'] = df['text'].encode('ascii', 'ignore' ).decode('ascii')
 df['text'] = re.sub('[0-9]+[a-zA-Z]+', '<UNIT>', df['text'])
 df['text'] = re.sub('[0-9]+', '<NUMBER>', df['text'])
 df['text'] = df['text'].lower()
df['text'] = re.sub(' u ', ' you ', df['text'])
 return df<categorify>
X_band_1=np.array([np.array(band ).astype(np.float32 ).reshape(75, 75)for band in train_df["band_1"]])
 X_band_2=np.array([np.array(band ).astype(np.float32 ).reshape(75, 75)for band in train_df["band_2"]] )
if not os.path.exists('/kaggle/input/albert-temp/final_version.h5'):
text_input = tf.keras.layers.Input(shape=() , dtype=tf.string)
 preprocessor = hub.KerasLayer(
 "http://tfhub.dev/tensorflow/albert_en_preprocess/2")
 encoder_inputs = preprocessor(text_input)
 encoder = hub.KerasLayer(
 "https://tfhub.dev/tensorflow/albert_en_base/2",
trainable=True)
 outputs = encoder(encoder_inputs)
 pooled_output = outputs["pooled_output"]
batch_norm = tf.keras.layers.BatchNormalization()(pooled_output)
 dense1 = tf.keras.layers.Dense(512, activation='relu' )(batch_norm)
 drop = tf.keras.layers.Dropout(0.1 )(dense1)
 final = tf.keras.layers.Dense(1, activation='sigmoid' )(drop)
 model = tf.keras.Model(inputs=text_input, outputs=final)
 model.compile(optimizer=tf.keras.optimizers.Adam(2e-5), loss='binary_crossentropy')
 model.summary()
else:
print('Loading from previous version')
 model = tf.keras.models.load_model('/kaggle/input/albert-temp/final_version.h5', custom_objects={'KerasLayer':hub.KerasLayer})
 model.summary()<train_model>
X_band = np.zeros([1604,75,75,3])
 for t in range(1604):
X_band[t,:,:,0] = X_band_1[t]
X_band[t,:,:,1] = X_band_2[t]
X_band[t,:,:,2] =(X_band_1[t]+X_band_2[t])/2
<load_from_csv>
K.set_image_data_format('channels_last')
test_data = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv', usecols=['id','text'] )<feature_engineering>
def Iceberg_model(input_shape):
X_in = Input(input_shape)
 X = Conv2D(16,kernel_size=(5,5),input_shape=(75,75,3))(X_in)
 X = BatchNormalization()(X)
 X = Activation('relu' )(X)
 X = MaxPooling2D(pool_size=(2,2))(X)
 X = Conv2D(32,kernel_size=(5,5))(X)
 X = BatchNormalization()(X)
 X = Activation('relu' )(X)
 X = MaxPooling2D(pool_size=(2,2))(X)
 X = Conv2D(64,kernel_size=(5,5))(X)
 X = BatchNormalization()(X)
 X = Activation('relu' )(X)
 X = MaxPooling2D(pool_size=(2,2))(X)
 X = Flatten()(X)
 X = Dense(128 )(X)
 X = Activation('relu' )(X)
 X = Dense(64 )(X)
 X = Activation('relu' )(X)
 X = Dense(1 )(X)
 X = Activation('sigmoid' )(X)
 model = Model(inputs=X_in,outputs=X,name='Iceberg_model')
 return model
test_data = test_data.apply(preprocess, axis=1)
 test_data.head()<predict_on_test>
IcebergModel = Iceberg_model(( 75,75,3))
preds = model.predict(test_data['text'].tolist() )<statistical_test>
IcebergModel.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'] )
def apply_threshold(x, thresh=0.5):
ret = []
for i in x:
if i[0] >= thresh:
ret.append(1)
 else:
ret.append(0)
 return ret<groupby>
target = train_df['is_iceberg'].values
IcebergModel.fit(x=X_band,y=target,epochs=20,batch_size=128 )
preds = apply_threshold(preds )<save_to_csv>
IcebergModel.evaluate(x=X_band,y=target )
submission = pd.DataFrame({'id':test_data['id'].tolist() , 'target':preds})
 submission.to_csv('submission.csv', index=False )<load_from_url>
pred_label = IcebergModel.predict(x=X_band)
 pred_label[pred_label>0.5]=1
pred_label[pred_label<=0.5]=0
print(classification_report(target,pred_label))
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<install_modules>
X_band_test_1=np.array([np.array(band ).astype(np.float32 ).reshape(75, 75)for band in test_df["band_1"]])
 X_band_test_2=np.array([np.array(band ).astype(np.float32 ).reshape(75, 75)for band in test_df["band_2"]])
 X_test = np.zeros([8424,75,75,3])
 for t in range(8424):
X_test[t,:,:,0] = X_band_test_1[t]
X_test[t,:,:,1] = X_band_test_2[t]
X_test[t,:,:,2] =(X_band_test_1[t]+X_band_test_2[t])/2
!pip3 -q install contractions
!pip3 -q install nb_black
!pip -q install beautifulsoup4<set_options>
pred = IcebergModel.predict(x=X_test)
 sub_df = pd.DataFrame()
sub_df['id'] = test_df['id']
sub_df['is_iceberg'] = pred
sub_df.to_csv('output.csv',index=False )