Kaggle competition: Natural Language Processing with Disaster Tweets
all_path = glob('.. /input/global-wheat-detection/test/*')
 a = {}
for row in range(len(all_path)) :
image_id = all_path[row].split("/")[-1].split(".")[0]
boxes,scores,labels = run_last_wbf(y[image_id],z[image_id])
 boxes =(boxes*1023)
 a[image_id] = [boxes,scores,labels]<categorify>
!pip install git+git://github.com/AndLen/simpletransformers.git --quiet
def format_prediction_string(boxes, scores):
pred_strings = []
for j in zip(scores, boxes):
pred_strings.append("{0:.4f} {1} {2} {3} {4}".format(j[0], j[1][0], j[1][1], j[1][2], j[1][3]))
return " ".join(pred_strings)
 results = []
for row in range(len(all_path)) :
image_id = all_path[row].split("/")[-1].split(".")[0]
boxes,scores,labels = run_last_wbf(w[image_id],g[image_id],l[image_id])
 boxes =(boxes*1023 ).astype(np.int32 ).clip(min=0, max=1023)
 boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
result = {'image_id': image_id,'PredictionString': format_prediction_string(boxes, scores)}
results.append(result )<save_to_csv>
import csv
import os
import torch
from transformers import pipeline
import gc
import seaborn as sns
from matplotlib import pyplot as plt
from scipy.special import softmax
from simpletransformers.classification import(ClassificationModel, ClassificationArgs)
 import sklearn
from sklearn.model_selection import train_test_split
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv',index=False)
 test_df<install_modules>
test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 training = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv" )
!pip install --no-deps ".. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl"
!pip install --no-deps ".. /input/resnest/resnest-0.0.5-py3-none-any.whl"<import_modules>
training["text"].isna().sum()
import torchvision
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
from torchvision.models.detection import FasterRCNN
from torchvision.models.detection.rpn import AnchorGenerator
from torch.utils.data.sampler import SequentialSampler
from torchvision.models.utils import load_state_dict_from_url
from torchvision.models.detection.backbone_utils import resnet_fpn_backbone
from torchvision.models.detection.backbone_utils import BackboneWithFPN
from torchvision.ops import misc as misc_nn_ops
from collections import OrderedDict
from torch.jit.annotations import Tuple, List, Dict, Optional
from resnest.torch import resnest101<categorify>
training_df = training[["text", "target"]]
training_df.columns = ["text", "labels"]
class DatasetRetriever(Dataset):
def __init__(self, marking, image_ids, transforms=None, test=False):
super().__init__()
self.image_ids = image_ids
self.marking = marking
self.transforms = transforms
self.test = test
def __getitem__(self, index: int):
image_id = self.image_ids[index]
if self.test or random.random() > 0.5:
image, boxes = self.load_image_and_boxes(index)
 else:
image, boxes = self.load_cutmix_image_and_boxes(index)
 labels = torch.ones(( boxes.shape[0],), dtype=torch.int64)
 target = {}
target['boxes'] = boxes
target['labels'] = labels
target['image_id'] = torch.tensor([index])
 if self.transforms:
for i in range(10):
sample = self.transforms(**{
'image': image,
'bboxes': target['boxes'],
'labels': labels
})
 if len(sample['bboxes'])> 0:
image = sample['image']
target['boxes'] = torch.stack(tuple(map(torch.tensor, zip(*sample['bboxes'])))).permute(1, 0)
 target['boxes'][:,[0,1,2,3]] = target['boxes'][:,[1,0,3,2]]
break
return image, target, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]
def load_image_and_boxes(self, index):
image_id = self.image_ids[index]
image = cv2.imread(f'{TRAIN_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
records = self.marking[self.marking['image_id'] == image_id]
boxes = records[['x', 'y', 'w', 'h']].values
boxes[:, 2] = boxes[:, 0] + boxes[:, 2]
boxes[:, 3] = boxes[:, 1] + boxes[:, 3]
return image, boxes
def load_cutmix_image_and_boxes(self, index, imsize=1024):

 w, h = imsize, imsize
s = imsize // 2
xc, yc = [int(random.uniform(imsize * 0.25, imsize * 0.75)) for _ in range(2)]
indexes = [index] + [random.randint(0, self.image_ids.shape[0] - 1)for _ in range(3)]
result_image = np.full(( imsize, imsize, 3), 1, dtype=np.float32)
 result_boxes = []
for i, index in enumerate(indexes):
image, boxes = self.load_image_and_boxes(index)
 if i == 0:
x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc
x1b, y1b, x2b, y2b = w -(x2a - x1a), h -(y2a - y1a), w, h
elif i == 1:
x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
x1b, y1b, x2b, y2b = 0, h -(y2a - y1a), min(w, x2a - x1a), h
elif i == 2:
x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
 x1b, y1b, x2b, y2b = w -(x2a - x1a), 0, max(xc, w), min(y2a - y1a, h)
 elif i == 3:
x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
 x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)
 result_image[y1a:y2a, x1a:x2a] = image[y1b:y2b, x1b:x2b]
padw = x1a - x1b
padh = y1a - y1b
boxes[:, 0] += padw
boxes[:, 1] += padh
boxes[:, 2] += padw
boxes[:, 3] += padh
result_boxes.append(boxes)
 result_boxes = np.concatenate(result_boxes, 0)
 np.clip(result_boxes[:, 0:], 0, 2 * s, out=result_boxes[:, 0:])
 result_boxes = result_boxes.astype(np.int32)
 result_boxes = result_boxes[np.where(( result_boxes[:,2]-result_boxes[:,0])*(result_boxes[:,3]-result_boxes[:,1])> 0)]
return result_image, result_boxes<data_type_conversions>
class TestDatasetRetriever(Dataset):
def __init__(self, image_ids, path, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
self.path = path
def __getitem__(self, index: int):
image_id = self.image_ids[index]
image = cv2.imread(f'{self.path}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]<feature_engineering>
gc.collect()
torch.cuda.empty_cache()
def get_df_folds(marking):
df_folds = marking[['image_id']].copy()
df_folds.loc[:, 'bbox_count'] = 1
df_folds = df_folds.groupby('image_id' ).count()
df_folds.loc[:, 'source'] = marking[['image_id', 'source']].groupby('image_id' ).min() ['source']
df_folds.loc[:, 'stratify_group'] = np.char.add(
 df_folds['source'].values.astype(str),
df_folds['bbox_count'].apply(lambda x: f'_{x // 15}' ).values.astype(str)
)
 df_folds.loc[:, 'fold'] = 0
return df_folds<choose_model_class>
model_args = ClassificationArgs(num_train_epochs=2,
overwrite_output_dir=True)
 model_args.manual_seed = 42
model_args.best_model_dir = "/kaggle/working/best_model"
model_args.output_dir = "/kaggle/temp/output"
model_args.normalization = True
model_args.reprocess_input_data = True
model_args.train_batch_size = 80
model_args.eval_batch_size = 80
model_args.early_stopping_metric = "mcc"
model_args.early_stopping_metric_minimize = False
model_args.use_early_stopping = True
model_args.early_stopping_consider_epochs = True
model_args.early_stopping_patience = 1
model = ClassificationModel(model_type='bertweet',
model_name='vinai/bertweet-base',
args = model_args,
num_labels = 2 )
class CrossEntropyLabelSmooth(torch.nn.Module):

 def __init__(self, num_classes, epsilon=0.1, use_gpu=True):
super(CrossEntropyLabelSmooth, self ).__init__()
self.num_classes = num_classes
self.epsilon = epsilon
self.use_gpu = use_gpu
self.logsoftmax = nn.LogSoftmax(dim=1)
 def forward(self, inputs, targets):

 log_probs = self.logsoftmax(inputs)
 targets = torch.zeros(log_probs.size() ).scatter_(1, targets.unsqueeze(1 ).data.cpu() , 1)
 if self.use_gpu: targets = targets.cuda()
targets =(1 - self.epsilon)* targets + self.epsilon / self.num_classes
loss =(- targets * log_probs ).mean(0 ).sum()
return loss<compute_train_metric>
model.train_model(shuffled_training,
acc=sklearn.metrics.accuracy_score,
f1=sklearn.metrics.f1_score )
def fastrcnn_loss(class_logits, box_regression, labels, regression_targets):

 labels = torch.cat(labels, dim=0)
 regression_targets = torch.cat(regression_targets, dim=0)
 labal_smooth_loss = CrossEntropyLabelSmooth(2)
 classification_loss = labal_smooth_loss(class_logits, labels)
 sampled_pos_inds_subset = torch.nonzero(labels > 0 ).squeeze(1)
 labels_pos = labels[sampled_pos_inds_subset]
N, num_classes = class_logits.shape
box_regression = box_regression.reshape(N, -1, 4)
 box_loss = det_utils.smooth_l1_loss(
 box_regression[sampled_pos_inds_subset, labels_pos],
regression_targets[sampled_pos_inds_subset],
beta=1 / 9,
size_average=False, 
)
 box_loss = box_loss / labels.numel()
return classification_loss, box_loss<choose_model_class>
result, model_outputs, wrong_predictions = model.eval_model(shuffled_training,
acc=sklearn.metrics.accuracy_score,
f1=sklearn.metrics.f1_score )
def resnest_fpn_backbone(pretrained, norm_layer=misc_nn_ops.FrozenBatchNorm2d, trainable_layers=3):
backbone = resnest101(pretrained=pretrained)
 assert trainable_layers <= 5 and trainable_layers >= 0
layers_to_train = ['layer4', 'layer3', 'layer2', 'layer1', 'conv1'][:trainable_layers]
for name, parameter in backbone.named_parameters() :
if all([not name.startswith(layer)for layer in layers_to_train]):
parameter.requires_grad_(False)
 return_layers = {'layer1': '0', 'layer2': '1', 'layer3': '2', 'layer4': '3'}
in_channels_stage2 = backbone.inplanes // 8
in_channels_list = [
in_channels_stage2,
in_channels_stage2 * 2,
in_channels_stage2 * 4,
in_channels_stage2 * 8,
]
out_channels = 256
return BackboneWithFPN(backbone, return_layers, in_channels_list, out_channels )<choose_model_class>
result
class WheatDetector(torch.nn.Module):
def __init__(self, trainable_layers=3, **kwargs):
super(WheatDetector, self ).__init__()
backbone = resnest_fpn_backbone(pretrained=False)
 self.base = FasterRCNN(backbone, num_classes = 2, **kwargs)
 self.base.roi_heads.fastrcnn_loss = fastrcnn_loss
def forward(self, images, targets=None):
return self.base(images, targets )<load_pretrained>
predictions, raw_outputs = model.predict(test["text"].to_list() )
def load_res_net(path, cfg):
model = build_model(cfg)
 checkpoint = torch.load(path)
 model.load_state_dict(checkpoint['model_state_dict'])
 model.eval() ;
return model.cuda()<choose_model_class>
mypreds = pd.DataFrame(test[["id"]])
 mypreds["target"] = predictions
def get_net(level):
config = get_efficientdet_config(f'tf_efficientdet_d{level}')
 net = EfficientDet(config, pretrained_backbone=False)
 if level == 5:
checkpoint = torch.load('.. /input/efficientdet/efficientdet_d5-ef44aea8.pth')
 elif level == 7:
checkpoint = torch.load('.. /input/efficientdet/efficientdet_d7-f05bf714.pth')
 net.load_state_dict(checkpoint)
 config.num_classes = 1
config.image_size = 512
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
net = DetBenchTrain(net, config)
 return net
def load_net_eval(checkpoint_path, level):
config = get_efficientdet_config(f'tf_efficientdet_d{level}')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size = 512
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
checkpoint = torch.load(checkpoint_path)
 net.load_state_dict(checkpoint["model_state_dict"])
 del checkpoint
gc.collect()
net = DetBenchEval(net, config)
 net.eval() ;
return net.cuda() 
<normalization>
mypreds.to_csv("submission.csv", index=False )