Kaggle competition: Tweet Sentiment Extraction
model.load_weights('plant_best.h5' )<predict_on_test>
test_df = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/test.csv' )
ids = test.index
Test_set = test['image_id']
data_generator_test = TestDataGenerator(Test_set, img_dir, ids, batch_size, input_shape, augmentation = None)
 data_generator_test_Horizontal = TestDataGenerator(Test_set, img_dir, ids, batch_size, input_shape, augmentation = "HorizontalFlip")
 data_generator_test_Vertical = TestDataGenerator(Test_set, img_dir, ids, batch_size, input_shape, augmentation = "VerticalFlip")
 data_generator_test_VH = TestDataGenerator(Test_set, img_dir, ids, batch_size, input_shape, augmentation = "VH")
 preds_1 = model.predict_generator(data_generator_test, verbose = 1)
 preds_2 = model.predict_generator(data_generator_test_Horizontal, verbose = 1)
 preds_3 = model.predict_generator(data_generator_test_Vertical, verbose = 1)
 preds_4 = model.predict_generator(data_generator_test_VH, verbose = 1)
 for index, pred in enumerate(zip(preds_1,preds_2, preds_3, preds_4)) :
sample_submission["healthy"][index] =(pred[0][0] + pred[1][0] + pred[2][0] + pred[3][0])/4
sample_submission["multiple_diseases"][index] =(pred[0][1] + pred[1][1] + pred[2][1] + pred[3][1])/4
sample_submission["rust"][index] =(pred[0][2] + pred[1][2] + pred[2][2] + pred[3][2])/4
sample_submission["scab"][index] =(pred[0][3] + pred[1][3] + pred[2][3] + pred[3][3])/4<save_to_csv>
os.listdir('/kaggle/input')
 model_path = '.. /input/tf-roberta/'
weights_path = '.. /input/tfrobertaweights/tf_roBERTa-weights/'
sample_submission.to_csv("submission.csv", index=False )<import_modules>
weights_list = []
weights_list = list(os.listdir(weights_path))
!pip install efficientnet 
<define_variables>
weights_path_latest = '.. /input/v18robertaweights/'
weights_path_latest_list = []
for root, dirs, files in os.walk(weights_path_latest):
for file in files:
if file.endswith(".h5"):
weights_path_latest_list.append(file )
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 seed_everything(42 )<define_variables>
seed = 69
img_dir = ".. /input/plant-pathology-2020-fgvc7//images/"
path = ".. /input/plant-pathology-2020-fgvc7/"
input_shape =(512,512,3)
<load_from_csv>
print('TF version',tf.__version__ )
train = pd.read_csv(path + 'train.csv')
 test = pd.read_csv(path + 'test.csv')
 sample_submission = pd.read_csv(path + 'sample_submission.csv')
<choose_model_class>
MAX_LEN = 96
tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file=model_path+'vocab-roberta-base.json',
merges_file=model_path+'merges-roberta-base.txt',
lowercase=True,
add_prefix_space=True 
)
 sentiment_id = {'positive': 1313, 'negative': 2430, 'neutral': 7974}
gm_exp = tf.Variable(3.0, dtype = tf.float32)
 def generalized_mean_pool_2d(X):
pool =(tf.reduce_mean(tf.abs(X**(gm_exp)) ,
axis = [1, 2],
keepdims = False)+ 1.e-7)**(1./gm_exp)
 return pool
def create_model(input_shape):
input = Input(shape = input_shape)
 x_model = ResNet50(weights = 'imagenet', include_top = False, input_tensor = input, pooling = None,
classes = None)
 for layer in x_model.layers:
layer.trainable = True
lambda_layer = Lambda(generalized_mean_pool_2d)
 lambda_layer.trainable_weights.extend([gm_exp])
 x = lambda_layer(x_model.output)
 healthy = Dense(4, activation = 'softmax', name = 'plan_diseases' )(x)
 model = Model(inputs = x_model.input, outputs = healthy)
 return model 
<train_model>
ct = test_df.shape[0]
input_ids_t = np.ones(( ct,MAX_LEN),dtype='int32')
 attention_mask_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 token_type_ids_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 for k in range(test_df.shape[0]):
text1 = " "+" ".join(test_df.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 s_tok = sentiment_id[test_df.loc[k,'sentiment']]
input_ids_t[k,:len(enc.ids)+5] = [0] + enc.ids + [2,2] + [s_tok] + [2]
attention_mask_t[k,:len(enc.ids)+5] = 1
def _read(path):
img = cv2.imread(path)
 return img
class TrainDataGenerator(tf.keras.utils.Sequence):
def __init__(self , X_set, Y_set, img_dir, ids, batch_size = 2, img_size =(512,512,3), augmentation = False, GridMask = False):
self.X = X_set
self.Y = Y_set
self.batch_size = batch_size
self.ids = ids
self.img_size = img_size
self.img_dir = img_dir
self.augmentation = augmentation
self.GridMask = GridMask
self.on_epoch_end()
self.x_indexed = X_set[self.ids]
def __len__(self):
return int(ceil(len(self.ids)/self.batch_size))
def __getitem__(self, index):
indices = self.ids[index*self.batch_size:(index+1)*self.batch_size]
X, Y = self.__generator__(indices)
 return X, Y
def on_epoch_end(self):
self.indices = np.arange(len(self.ids))
def __generator__(self, indices):
X = np.empty(( self.batch_size, *self.img_size))
Y = np.empty(( self.batch_size, 4))
for i, index in enumerate(indices):
ID = self.X[index]
image = _read(self.img_dir+ID+".jpg")
 image = cv2.resize(image,(512,512))
if self.augmentation == True:
aug = strong_aug(p=1.0)
 image = augment(aug, image)
 elif self.GridMask == True:
image = augment(transforms_train, image)
 X[i,] = image/255.
 Y[i,] = self.Y.loc[index].values
return X, Y
class TestDataGenerator(tf.keras.utils.Sequence):
def __init__(self, Test_set, img_dir, ids, batch_size = 3, img_size =(512,512,3), augmentation = None):
self.X = Test_set
self.img_dir = img_dir
self.ids = ids
self.batch_size = batch_size
self.img_size = img_size
self.augmentation = augmentation
def __len__(self):
return int(ceil(len(self.ids)/self.batch_size))
def __getitem__(self, index):
indices = self.ids[index*self.batch_size:(index+1)*self.batch_size]
X = self.__generator__(indices)
 return X
def __generator__(self, indices):
X = np.empty(( self.batch_size, *self.img_size))
for i, index in enumerate(indices):
ID = self.X[index]
image = _read(self.img_dir+ID+".jpg")
 image = cv2.resize(image,(512,512))
if self.augmentation is not None:
if self.augmentation == "HorizontalFlip":
image = augment(HorizontalFlip(p=1), image)
 elif self.augmentation == "VerticalFlip":
image = augment(VerticalFlip(p=1), image)
 elif self.augmentation == 'VH':
image = VH_augment(image)
 X[i,] = image/255.
 return X 
<split>
def build_model() :
ids = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 att = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 tok = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 config = RobertaConfig.from_pretrained(model_path+'config-roberta-base.json')
 bert_model = TFRobertaModel.from_pretrained(model_path+'pretrained-roberta-base.h5',config=config)
 x = bert_model(ids,attention_mask=att,token_type_ids=tok)
 x1 = tf.keras.layers.Dropout(0.15 )(x[0])
 x1 = tf.keras.layers.Conv1D(512,2, padding='same' )(x1)
 x1 = tf.keras.layers.Conv1D(256, 2,padding='same' )(x1)
 x1 = tf.keras.layers.Dense(1 )(x1)
 x1 = tf.keras.layers.Flatten()(x1)
 x1 = tf.keras.layers.Activation('softmax' )(x1)
 x2 = tf.keras.layers.Dropout(0.15 )(x[0])
 x2 = tf.keras.layers.Conv1D(512,2, padding='same' )(x2)
 x2 = tf.keras.layers.Conv1D(256, 2,padding='same' )(x2)
 x2 = tf.keras.layers.Dense(1 )(x2)
 x2 = tf.keras.layers.Flatten()(x2)
 x2 = tf.keras.layers.Activation('softmax' )(x2)
 model = tf.keras.models.Model(inputs=[ids, att, tok], outputs=[x1,x2])
 optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5)
 model.compile(loss='categorical_crossentropy', optimizer=optimizer)
 return model
tgt_cols = ['healthy', 'multiple_diseases', 'rust' , 'scab']
train_df = train[tgt_cols]
Y = pd.get_dummies(train_df)
 X = train['image_id']
X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size = 0.15, random_state = seed)
 ids_train = np.array(X_train.index)
 ids_test = X_test.index
del train
del X
del Y<choose_model_class>
preds_start = np.zeros(( input_ids_t.shape[0],MAX_LEN))
preds_end = np.zeros(( input_ids_t.shape[0],MAX_LEN))
for i, fl in enumerate(weights_path_latest_list):
K.clear_session()
model = build_model()
print('Loading model...')
 model.load_weights(weights_path_latest + weights_path_latest_list[i])
 print(f'Predicting Test...using weights...{weights_path_latest_list[i]}')
 preds = model.predict([input_ids_t,attention_mask_t,token_type_ids_t],verbose=1)
 preds_start += preds[0]/len(weights_path_latest_list)
 preds_end += preds[1]/len(weights_path_latest_list )
model = create_model(input_shape)
 model.compile(optimizer = Adam(lr = 0.00016),
loss = 'categorical_crossentropy',
metrics = ['categorical_accuracy'] )<choose_model_class>
submission = []
for i in range(input_ids_t.shape[0]):
a = np.argmax(preds_start[i,])
 b = np.argmax(preds_end[i,])
 if a>b:
st = test_df.loc[i,'text']
else:
text1 = " "+" ".join(test_df.loc[i,'text'].split())
 enc = tokenizer.encode(text1)
 st = tokenizer.decode(enc.ids[a-1:b])
 submission.append(st)
best_w = ModelCheckpoint('plant_best.h5',
monitor='val_loss',
verbose=0,
save_best_only=True,
save_weights_only=True,
mode='auto',
period=1)
 last_w = ModelCheckpoint('plant_last.h5',
monitor='val_loss',
verbose=0,
save_best_only=False,
mode='auto',
period=1)
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
patience=2, min_lr=0.000001, verbose=1,cooldown=1)
 callbacks = [best_w, last_w, reduce_lr] 
<train_model>
test_df['selected_text'] = submission
pd.set_option('max_colwidth', 50)
<set_options><EOS> 
submission_df.to_csv('submission.csv',index=False)
 print('Submitted Successfully.....' )