Kaggle competition: Invasive Species Monitoring
<SOS> metric: AUC Kaggle data source: invasive-species-monitoring<string_transform>
import numpy as np
import pandas as pd
import os
normal_sentence = "covid-19 sucks like hell!"
tokenize_sentence = tokenizer.tokenize(normal_sentence)
 print("Tokens for BERT: ", tokenize_sentence)
 bert_input_sentence = ["[CLS]"] + tokenize_sentence + ["[SEP]"]
print("Token Ids for BERT: ", tokenizer.convert_tokens_to_ids(bert_input_sentence))<categorify>
train = pd.read_csv(".. /input/invasive-species-monitoring/train_labels.csv")
 train["name"] = train["name"].apply(lambda x : ".. /input/invasive-species-monitoring/train/" + str(x)+ ".jpg")
 train["invasive"] = train["invasive"].astype("str" )
def bert_encode(texts, max_length=128):
all_token_ids = []
all_input_ids = []
all_segment_ids = []
for sentence in texts:
tokenize_sentence = tokenizer.tokenize(sentence)[
:max_length
]
bert_input_sentence = ["[CLS]"] + tokenize_sentence + ["[SEP]"]
unused_sentence_length = max_length - len(
 bert_input_sentence 
)
 unused_length_array = [0] * unused_sentence_length
token_ids =(
 tokenizer.convert_tokens_to_ids(bert_input_sentence)+ unused_length_array 
)
 bert_input_ids =([1] * len(bert_input_sentence)) + unused_length_array
bert_segment_ids = [0] * max_length
all_token_ids.append(token_ids)
 all_input_ids.append(bert_input_ids)
 all_segment_ids.append(bert_segment_ids)
 return(
 np.array(all_token_ids),
np.array(all_input_ids),
np.array(all_segment_ids),
)<categorify>
train_image_gen = ImageDataGenerator(rescale = 1/255, validation_split=0.022, rotation_range=270, horizontal_flip=True)
 batch_size = 6
image_size = 800
train_generator = train_image_gen.flow_from_dataframe(dataframe=train, batch_size=batch_size, target_size=(image_size,image_size), x_col="name", y_col="invasive", subset="training", class_mode="binary")
 val_generator = train_image_gen.flow_from_dataframe(dataframe=train, batch_size=batch_size, target_size=(image_size,image_size), x_col="name", y_col="invasive", subset="validation", class_mode="binary" )
MAX_SEQUENCE_LENGTH = 128
def build_model(hp):
input_word_ids = keras.Input(
 shape=(MAX_SEQUENCE_LENGTH,), dtype=tf.int32, name="input_word_ids" 
)
 input_mask = keras.Input(
 shape=(MAX_SEQUENCE_LENGTH,), dtype=tf.int32, name="input_mask" 
)
 segment_ids = keras.Input(
 shape=(MAX_SEQUENCE_LENGTH,), dtype=tf.int32, name="segment_ids" 
)
 pooled_output, sequence_output = bert_layer(
 [input_word_ids, input_mask, segment_ids] 
)
 clf_output = sequence_output[:, 0, :]
neural_inputs = keras.layers.Dense(
 units=hp.Int(
 "units_hidden_1", min_value=32, max_value=256, step=32, default=128 
),
activation="elu",
kernel_initializer="he_normal",
)(clf_output)
 neural_inputs = keras.layers.Dropout(
 rate=hp.Float("dropout_1", min_value=0.1, max_value=0.9, default=0.2, step=0.1)
 )(neural_inputs)
 neural_inputs = keras.layers.Dense(
 units=hp.Int(
 "units_hidden_2", min_value=32, max_value=256, step=32, default=64 
),
activation="elu",
kernel_initializer="he_normal",
)(neural_inputs)
 neural_inputs = keras.layers.Dropout(
 rate=hp.Float("dropout_2", min_value=0.1, max_value=0.9, default=0.2, step=0.1)
 )(neural_inputs)
 out = keras.layers.Dense(1, activation="sigmoid", )(neural_inputs)
 model = keras.models.Model(
 inputs=[input_word_ids, input_mask, segment_ids], outputs=out 
)
 model.compile(
 keras.optimizers.Adam(hp.Choice("learning_rate", [5e-5, 3e-5, 1e-5])) ,
loss=keras.losses.binary_crossentropy,
metrics=["accuracy"], 
)
 return model<choose_model_class>
model = Sequential()
model.add(InceptionResNetV2(weights="imagenet", include_top=False, pooling="avg", input_shape=(image_size, image_size, 3)))
 model.add(Dropout(0.5))
model.add(Dense(1, activation="sigmoid"))
tuner = Hyperband(
 build_model,
objective="val_accuracy",
max_epochs=3,
project_name=".. /output/kaggle/working/hyperparameter/hyperband",
)<split>
model.compile(optimizer=Adam(lr=0.0003, decay=2e-7), loss='binary_crossentropy',metrics=['accuracy'] )
x_train, x_val, y_train, y_val = train_test_split(
 df.clean_text, df.target, train_size=0.85
)<categorify>
modelcheck = ModelCheckpoint("best.h5", save_best_only=True)
 model.fit_generator(train_generator, steps_per_epoch=int(np.ceil(train_generator.n/batch_size)) , validation_data = val_generator, validation_steps = int(np.ceil(val_generator.n/batch_size)) , epochs=6, callbacks=[modelcheck] )
<data_type_conversions><EOS> 
sub = pd.read_csv(".. /input/invasive-species-monitoring/sample_submission.csv")
 test = pd.DataFrame({"name": sub["name"].apply(lambda x : ".. /input/invasive-species-monitoring/test/" + str(x)+ ".jpg")})
 test_gen = ImageDataGenerator(rescale = 1./255)
 test_generator = test_gen.flow_from_dataframe(dataframe = test, shuffle=False, x_col = "name", y_col = None, class_mode=None, batch_size = batch_size, target_size =(image_size, image_size))
model.load_weights("best.h5")
 preds = model.predict_generator(test_generator, steps=int(np.ceil(test_generator.n/batch_size)) , workers = 2)
 sub["invasive"] = preds
sub.to_csv("submission.csv", index=False )