Kaggle competition: Mechanisms of Action (MoA) Prediction
<SOS> metric: MeanColumnwiseLogLoss Kaggle data source: mechanisms-of-action-(moa)-prediction<categorify>
sys.path.append('.. /input/iterative-stratification/iterative-stratification-master/')
 %matplotlib inline
sns.set_style('ticks')
 sns.set_context("poster")
 sns.set_palette('colorblind')
 warnings.filterwarnings('ignore')
if PSEUDO or VALIDATE:
convertTrainLabel()<find_best_params>
plt.rcParams['figure.figsize'] =(20.0, 10.0 )
if VALIDATE and is_TEST:
all_predictions = validate()

 for score_threshold in tqdm(np.arange(0, 1, 0.01), total=np.arange(0, 1, 0.01 ).shape[0]):
final_score = calculate_final_score(all_predictions, best_iou_thr, best_skip_box_thr, score_threshold)
 if final_score > best_final_score:
best_final_score = final_score
best_score_threshold = score_threshold
print('-'*30)
 print(f'[Best Score Threshold]: {best_score_threshold}')
 print(f'[OOF Score]: {best_final_score:.4f}')
 print('-'*30)
<load_pretrained>
device =('cuda' if torch.cuda.is_available() else 'cpu' )
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)
 def detect() :
source = '.. /input/global-wheat-detection/test/'
weights = 'weights/best.pt'
if not os.path.exists(weights):
weights = WEIGHTS
imagenames = os.listdir(source)
 device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu')
 model = torch.load(weights, map_location=device)['model'].float()
model.to(device ).eval()
dataset = LoadImages(source, img_size=1024)
 results = []
fig, ax = plt.subplots(5, 2, figsize=(30, 70))
count = 0
for path, img, img0, vid_cap in dataset:
image_id = os.path.basename(path ).split('.')[0]
img = img.transpose(1,2,0)
 enboxes = []
enscores = []
if is_ROT:
for i in range(4):
img1 = TTAImage(img, i)
 boxes, scores = detect1Image(img1, img0, model, device, aug=False)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, *img.shape[:2])
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores = detect1Image(img, img0, model, device, aug=is_AUG)
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores, labels = run_wbf(enboxes, enscores, image_size=1024, iou_thr=best_iou_thr, skip_box_thr=best_skip_box_thr)
 boxes = boxes.astype(np.int32 ).clip(min=0, max=1024)
 boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
indices = scores >= best_score_threshold
boxes = boxes[indices]
scores = scores[indices]
if count<10:
img_ = cv2.imread(path)
 for box, score in zip(boxes,scores):
cv2.rectangle(img_,(box[0], box[1]),(box[2]+box[0], box[3]+box[1]),(220, 0, 0), 2)
 cv2.putText(img_, '%.2f'%(score),(box[0], box[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5,(255,255,255), 2, cv2.LINE_AA)
 ax[count%5][count//5].imshow(img_)
 count+=1
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result)
 return results<save_to_csv>
params = {'device': device,
'n_comp_g': 450,
'n_comp_c': 45,
'var_thresh': 0.67,
'epochs': 25,
'batch_size': 128,
'lr': 1e-3,
'weight_decay': 1e-5,
'n_folds': 7,
'early_stopping_steps': 10,
'early_stop': False,
'in_size': None,
'out_size': None,
'hidden_size': 1500}
results = detect()
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()<define_variables>
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )
DIR_INPUT = '/kaggle/input'
DIR_TRAIN = f'{DIR_INPUT}/global-wheat-detection/train'
DIR_TEST = f'{DIR_INPUT}/global-wheat-detection/test'<feature_engineering>
g_features = [col for col in train_features.columns if col.startswith('g-')]
c_features = [col for col in train_features.columns if col.startswith('c-')]
g_c_features = g_features + c_features
train_df = pd.read_csv(f'{DIR_INPUT}/global-wheat-detection/train.csv')
 train_df.shape
train_df['x'] = -1
train_df['y'] = -1
train_df['w'] = -1
train_df['h'] = -1
def expand_bbox(x):
r = np.array(re.findall("([0-9]+[.]?[0-9]*)", x))
if len(r)== 0:
r = [-1, -1, -1, -1]
return r
train_df[['x', 'y', 'w', 'h']] = np.stack(train_df['bbox'].apply(lambda x: expand_bbox(x)))
 train_df.drop(columns=['bbox'], inplace=True)
 train_df['x'] = train_df['x'].astype(np.float)
 train_df['y'] = train_df['y'].astype(np.float)
 train_df['w'] = train_df['w'].astype(np.float)
 train_df['h'] = train_df['h'].astype(np.float)
 image_ids = train_df['image_id'].unique()
valid_ids = image_ids[-1:]
train_ids = image_ids
valid_df = train_df[train_df['image_id'].isin(valid_ids)]
train_df = train_df[train_df['image_id'].isin(train_ids)]<load_pretrained>
transformer = QuantileTransformer(n_quantiles=100,random_state=0, output_distribution="normal" )
def load_image(self, index):
image_id = self.image_ids[index]
imgpath = f'{DIR_INPUT}/global-wheat-detection/train'
img = cv2.imread(f'{imgpath}/{image_id}.jpg', cv2.IMREAD_COLOR)
 assert img is not None, 'Image Not Found ' + imgpath
h0, w0 = img.shape[:2]
return img,(h0, w0), img.shape[:2] 
<categorify>
trans_train_features = transformer.fit_transform(train_features[g_c_features])
 trans_test_features = transformer.transform(test_features[g_c_features])
 trans_train_df = pd.DataFrame(trans_train_features, columns = g_c_features)
 trans_test_df = pd.DataFrame(trans_test_features, columns = g_c_features)
 train_features = pd.concat([train_features.drop(columns=g_c_features), trans_train_df], axis=1)
 test_features = pd.concat([test_features.drop(columns=g_c_features), trans_test_df], axis=1 )
def load_mosaic(self, index):
labels4 = []
s = self.img_size
xc, yc = [int(random.uniform(s * 0.5, s * 1.5)) for _ in range(2)]
indices = [index] + [random.randint(0, len(self.labels)- 1)for _ in range(3)]
for i, index in enumerate(indices):
img, _,(h, w)= load_image(self, index)
 if i == 0:
img4 = np.full(( s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8)
 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)
 img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b]
padw = x1a - x1b
padh = y1a - y1b
x = self.labels[index]
labels = x.copy()
if x.size > 0:
labels[:, 1] = w *(x[:, 1] - x[:, 3] / 2)+ padw
labels[:, 2] = h *(x[:, 2] - x[:, 4] / 2)+ padh
labels[:, 3] = w *(x[:, 1] + x[:, 3] / 2)+ padw
labels[:, 4] = h *(x[:, 2] + x[:, 4] / 2)+ padh
labels4.append(labels)
 if len(labels4):
labels4 = np.concatenate(labels4, 0)
 np.clip(labels4[:, 1:], 0, 2 * s, out=labels4[:, 1:])
 img4, labels4 = random_affine(img4, labels4,
degrees=1.98 * 2,
translate=0.05 * 2,
scale=0.05 * 2,
shear=0.641 * 2,
border=-s // 2)
 return img4, labels4 
<categorify>
def transfrom_all_data(transformer, train, test, feature_list):
data = pd.concat([train[feature_list], test[feature_list]], axis=0 ).reset_index(drop=True)
 n = train.shape[0]
data_trans = transformer.fit_transform(data)
 train_trans = data_trans[:n, :]
test_trans = data_trans[n:, :]
return train_trans, test_trans
def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5):
r = np.random.uniform(-1, 1, 3)* [hgain, sgain, vgain] + 1
hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
dtype = img.dtype
x = np.arange(0, 256, dtype=np.int16)
 lut_hue =(( x * r[0])% 180 ).astype(dtype)
 lut_sat = np.clip(x * r[1], 0, 255 ).astype(dtype)
 lut_val = np.clip(x * r[2], 0, 255 ).astype(dtype)
 img_hsv = cv2.merge(( cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val)) ).astype(dtype)
 cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img )<set_options>
def make_pca_features(n_comp, train, test, feature_list, name, normalize=False, scaler=None):
pca = PCA(n_comp)
 train_pca, test_pca = transfrom_all_data(pca, train, test, feature_list)
 if normalize and scaler is not None:
train_pca = scaler.fit_transform(train_pca)
 test_pca = scaler.transform(test_pca)
 for i in range(n_comp):
train['{0}_{1}'.format(name, i)] = train_pca[:, i]
test['{0}_{1}'.format(name, i)] = test_pca[:, i]
return train, test
def letterbox(img, new_shape=(416, 416), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):
shape = img.shape[:2]
if isinstance(new_shape, int):
new_shape =(new_shape, new_shape)
 r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
 if not scaleup:
r = min(r, 1.0)
 ratio = r, r
new_unpad = int(round(shape[1] * r)) , int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]
if auto:
dw, dh = np.mod(dw, 64), np.mod(dh, 64)
 elif scaleFill:
dw, dh = 0.0, 0.0
new_unpad = new_shape
ratio = new_shape[0] / shape[1], new_shape[1] / shape[0]
dw /= 2
dh /= 2
if shape[::-1] != new_unpad:
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
 top, bottom = int(round(dh - 0.1)) , int(round(dh + 0.1))
left, right = int(round(dw - 0.1)) , int(round(dw + 0.1))
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
 return img, ratio,(dw, dh )<set_options>
def preprocess(data):
data['cp_time'] = data['cp_time'].map({24:0, 48:1, 72:2})
 data['cp_dose'] = data['cp_dose'].map({'D1':0, 'D2':1})
 return data
device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu' )<create_dataframe>
train_features, test_features = make_pca_features(params['n_comp_g'], train_features, test_features, g_features, 'g_pca' )
def get_train_transform() :
return A.Compose([
ToTensorV2(p=1.0)
 ], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})
 def get_valid_transform() :
return A.Compose([
ToTensorV2(p=1.0)
 ], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})
 def collate_fn(batch):
return tuple(zip(*batch))
train_dataset = WheatDataset(train_df, DIR_TRAIN, get_train_transform())
 valid_dataset = WheatDataset(valid_df, DIR_TRAIN, get_valid_transform())
 indices = torch.randperm(len(train_dataset)).tolist()
train_data_loader = DataLoader(
 train_dataset,
batch_size=16,
shuffle=True,
num_workers=4,
collate_fn=collate_fn 
)
 valid_data_loader = DataLoader(
 valid_dataset,
batch_size=4,
shuffle=False,
num_workers=4,
collate_fn=collate_fn 
)
 fig, ax = plt.subplots(15, 1, figsize=(160, 80))
images, targets = next(iter(train_data_loader))
for i in range(15):
boxes = targets[i]
sample = images[i]
for box in boxes:
cv2.rectangle(sample, 
(int(box[1]), int(box[2])) , 
(int(box[3]), int(box[4])) ,
220, 3)
 ax[i].imshow(sample)
 plt.show()<load_from_csv>
train_features, test_features = make_pca_features(params['n_comp_c'], train_features, test_features, c_features, 'c_pca' )
WEIGHTS_FILE = '/kaggle/input/yoloaugment/yoloaug_transfer_crmaug_epoch_9.pth'
test_df = pd.read_csv(f'{DIR_INPUT}/global-wheat-detection/sample_submission.csv' )<feature_engineering>
train_thresh, test_thresh = transfrom_all_data(var_thresh, train_features, test_features, to_thresh )
class WheatTestDataset(Dataset):
def __init__(self, dataframe, image_dir, transforms=None):
super().__init__()
self.image_ids = dataframe['image_id'].unique()
self.df = dataframe
self.image_dir = image_dir
self.transforms = transforms
def __getitem__(self, index: int):
image_id = self.image_ids[index]
records = self.df[self.df['image_id'] == image_id]
image = cv2.imread(f'{self.image_dir}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
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]<categorify>
train_features = pd.concat([train_features[cat_features], pd.DataFrame(train_thresh)], axis=1)
 test_features = pd.concat([test_features[cat_features], pd.DataFrame(test_thresh)], axis=1 )
def get_test_transform() :
return A.Compose([
ToTensorV2(p=1.0)
 ] )<choose_model_class>
train_mask = train_features['cp_type'] != 'ctl_vehicle'
train_sig_ids = train_features.loc[train_mask]['sig_id']
train = train_features.loc[train_mask].reset_index(drop=True)
 test_mask = test_features['cp_type'] != 'ctl_vehicle'
test_sig_ids = test_features.loc[test_mask]['sig_id']
test = test_features.loc[test_mask].reset_index(drop=True)
 train_target_sigids = train_targets[['sig_id']]
y_true = train_targets.copy()
train_targets = train_targets[train_targets['sig_id'].isin(train_sig_ids)].reset_index(drop=True)
 train_targets.drop(columns=['sig_id'], inplace=True)
 train_targets.reset_index(drop=True, inplace=True )
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False, pretrained_backbone=False)
 num_classes = 2
in_features = model.roi_heads.box_predictor.cls_score.in_features
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
 model.load_state_dict(torch.load(WEIGHTS_FILE))
model.eval()
x = model.to(device )<create_dataframe>
params['in_size'] = train.shape[1] - 2
params['out_size'] = train_targets.shape[1]
def collate_fn(batch):
return tuple(zip(*batch))
test_dataset = WheatTestDataset(test_df, DIR_TEST, get_test_transform())
 test_data_loader = DataLoader(
 test_dataset,
batch_size=4,
shuffle=False,
num_workers=4,
drop_last=False,
collate_fn=collate_fn 
)
 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 )<categorify>
mskf = MultilabelStratifiedKFold(n_splits=params['n_folds'] )
detection_threshold = 0.5
results = []
for images, image_ids in test_data_loader:
images = list(image.to(device)for image in images)
 outputs = model(images)
 for i, image in enumerate(images):
boxes = outputs[i]['boxes'].data.cpu().numpy()
scores = outputs[i]['scores'].data.cpu().numpy()
boxes = boxes[scores >= detection_threshold].astype(np.int32)
 scores = scores[scores >= detection_threshold]
image_id = image_ids[i]
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 )<prepare_output>
folds = train.copy()
for f,(t_idx, v_idx)in enumerate(mskf.split(X=train, y=train_targets)) :
folds.loc[v_idx, 'kfold'] = int(f)
 folds['kfold'] = folds['kfold'].astype(int )
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.head()<save_to_csv>
class TabularDataset:
def __init__(self, X, y):
self.X = X
self.y = y
def __len__(self):
return(self.X.shape[0])
 def __getitem__(self, i):
X_i = torch.tensor(self.X[i, :], dtype=torch.float)
 y_i = torch.tensor(self.y[i, :], dtype=torch.float)
 return X_i, y_i
class TabularDatasetTest:
def __init__(self, X):
self.X = X
def __len__(self):
return(self.X.shape[0])
 def __getitem__(self, i):
X_i = torch.tensor(self.X[i, :], dtype=torch.float)
 return X_i
test_df.to_csv('submission.csv', index=False )<load_from_csv>
def train_func(model, optimizer, scheduler, loss_func, dataloader, device):
train_loss = 0
model.train()
for inputs, labels in dataloader:
optimizer.zero_grad()
inputs, labels = inputs.to(device), labels.to(device)
 outputs = model(inputs)
 loss = loss_func(outputs, labels)
 loss.backward()
optimizer.step()
scheduler.step()
train_loss += loss.item()
train_loss /= len(dataloader)
 return train_loss
train = pd.read_csv('.. /input/covid19-global-forecasting-week-1/train.csv')
 test = pd.read_csv('.. /input/covid19-global-forecasting-week-1/test.csv')
 submission = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/submission.csv')
<set_options>
def valid_func(model, loss_func, dataloader, device):
model.eval()
valid_loss = 0
valid_preds = []
for inputs, labels in dataloader:
inputs, labels = inputs.to(device), labels.to(device)
 outputs = model(inputs)
 loss = loss_func(outputs, labels)
 valid_loss += loss.item()
valid_preds.append(outputs.sigmoid().detach().cpu().numpy())
 valid_loss /= len(dataloader)
 valid_preds = np.concatenate(valid_preds)
 return valid_loss, valid_preds
%matplotlib inline
sns.set()<import_modules>
def inference_fn(model, dataloader, device):
model.eval()
preds = []
for data in dataloader:
inputs = data.to(device)
 with torch.no_grad() :
outputs = model(inputs)
 preds.append(outputs.sigmoid().detach().cpu().numpy())
 preds = np.concatenate(preds)
 return preds
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_log_error
from sklearn.preprocessing import LabelEncoder<feature_engineering>
class SmoothBCEwLogits(_WeightedLoss):
def __init__(self, weight=None, reduction='mean', smoothing=0.0):
super().__init__(weight=weight, reduction=reduction)
 self.smoothing = smoothing
self.weight = weight
self.reduction = reduction
@staticmethod
def _smooth(targets:torch.Tensor, n_labels:int, smoothing=0.0):
assert 0 <= smoothing < 1
with torch.no_grad() :
targets = targets *(1.0 - smoothing)+ 0.5 * smoothing
return targets
def forward(self, inputs, targets):
targets = SmoothBCEwLogits._smooth(targets, inputs.size(-1),
self.smoothing)
 loss = F.binary_cross_entropy_with_logits(inputs, targets,self.weight)
 if self.reduction == 'sum':
loss = loss.sum()
elif self.reduction == 'mean':
loss = loss.mean()
return loss
train['month'] = train['Date'].str.extract(r'[0-9]+[-]([0-9]+)[-]')
 train['day'] = train['Date'].str.extract(r'[0-9]+[-][0-9]+[-]([0-9]+)')
 train = train.drop('Date', axis = 1)
 test['month'] = test['Date'].str.extract(r'[0-9]+[-]([0-9]+)[-]')
 test['day'] = test['Date'].str.extract(r'[0-9]+[-][0-9]+[-]([0-9]+)')
 test = test.drop('Date', axis = 1)
<categorify>
class Model(nn.Module):
def __init__(self, num_features, num_targets, hidden_size):
super(Model, self ).__init__()
self.batch_norm1 = nn.BatchNorm1d(num_features)
 self.dense1 = nn.utils.weight_norm(nn.Linear(num_features, hidden_size))
self.batch_norm2 = nn.BatchNorm1d(hidden_size)
 self.dropout2 = nn.Dropout(0.25)
 self.dense2 = nn.utils.weight_norm(nn.Linear(hidden_size, hidden_size))
self.batch_norm3 = nn.BatchNorm1d(hidden_size)
 self.dropout3 = nn.Dropout(0.25)
 self.dense3 = nn.utils.weight_norm(nn.Linear(hidden_size, num_targets))
def forward(self, x):
x = self.batch_norm1(x)
 x = F.leaky_relu(self.dense1(x))
x = self.batch_norm2(x)
 x = self.dropout2(x)
 x = F.leaky_relu(self.dense2(x))
x = self.batch_norm3(x)
 x = self.dropout3(x)
 x = self.dense3(x)
 return x
province = LabelEncoder()
train['Province/State'] = train['Province/State'].fillna('N')
 test['Province/State'] = test['Province/State'].fillna('N')
 province.fit(train['Province/State'].unique())
 train['Province/State'] = province.transform(train['Province/State'])
 test['Province/State'] = province.transform(test['Province/State'])
 country = LabelEncoder()
country.fit(train['Country/Region'])
 train['Country/Region'] = country.transform(train['Country/Region'])
 test['Country/Region'] = country.transform(test['Country/Region'])
<split>
def run_training(fold, seed):
seed_everything(seed)
 train = preprocess(folds.drop(columns = ['sig_id', 'cp_type']))
train_mask = train['kfold'] != fold
valid_idc = train.loc[~train_mask].index
X_train = train.loc[train_mask].reset_index(drop=True)
 y_train = train_targets.loc[train_mask].reset_index(drop=True)
 X_val = train.loc[~train_mask].reset_index(drop=True)
 y_val = train_targets.loc[~train_mask].reset_index(drop=True)
 X_train.drop(columns=['kfold'], inplace=True)
 X_val.drop(columns=['kfold'], inplace=True)
 test_ = preprocess(test.drop(columns = ['sig_id', 'cp_type']))
train_ds = TabularDataset(X_train.values, y_train.values)
 valid_ds = TabularDataset(X_val.values, y_val.values)
 test_ds = TabularDatasetTest(test_.values)
 train_dl = DataLoader(train_ds, batch_size=params['batch_size'], shuffle=True)
 valid_dl = DataLoader(valid_ds, batch_size=params['batch_size'], shuffle=False)
 test_dl = DataLoader(test_ds, batch_size=params['batch_size'], shuffle=False)
 model = Model(num_features=params['in_size'], num_targets=params['out_size'],
hidden_size=params['hidden_size'])
 model.to(params['device'])
 optimizer = torch.optim.Adam(model.parameters() , lr=params['lr'], weight_decay=params['weight_decay'])
 scheduler = optim.lr_scheduler.OneCycleLR(optimizer=optimizer, pct_start=0.1, div_factor=1e3,
max_lr=1e-2, epochs=params['epochs'], steps_per_epoch=len(train_dl))
loss_fn = nn.BCEWithLogitsLoss()
loss_tr = SmoothBCEwLogits(smoothing=0.001)
 early_stopping_steps = params['early_stopping_steps']
early_step = 0
oof = np.zeros(( train.shape[0], params['out_size']))
best_loss = np.inf
for epoch in range(params['epochs']):
train_loss = train_func(model, optimizer,scheduler, loss_tr, train_dl, params['device'])
 print(f"FOLD: {fold}, EPOCH: {epoch}, train_loss: {train_loss}")
 valid_loss, valid_preds = valid_func(model, loss_fn, valid_dl, params['device'])
 print(f"FOLD: {fold}, EPOCH: {epoch}, valid_loss: {valid_loss}")
 if valid_loss < best_loss:
best_loss = valid_loss
oof[valid_idc] = valid_preds
torch.save(model.state_dict() , f"FOLD{fold}_.pth")
 elif(params['early_stop'] == True):
early_step += 1
if(early_step >= early_stopping_steps):
break
model = Model(num_features=params['in_size'], num_targets=params['out_size'],
hidden_size=params['hidden_size'])
 model.load_state_dict(torch.load(f"FOLD{fold}_.pth"))
model.to(params['device'])
 predictions = np.zeros(( test.shape[0], params['out_size']))
predictions = inference_fn(model, test_dl, params['device'])
 return oof, predictions
X = train.drop(['Id', 'ConfirmedCases', 'Fatalities'], axis = 1)
 y1 = train.ConfirmedCases
y2 = train.Fatalities
test = test.drop('ForecastId', axis = 1)
 x1_tr, x1_vld, y1_tr, y1_vld = train_test_split(X, y1, test_size = 0.2, random_state = 200)
 x2_tr, x2_vld, y2_tr, y2_vld = train_test_split(X, y2, test_size = 0.2, random_state = 200)
<train_model>
def run_k_fold(n_folds, seed):
oof = np.zeros(( train.shape[0], params['out_size']))
predictions = np.zeros(( test.shape[0], params['out_size']))
for fold in range(n_folds):
oof_, pred_ = run_training(fold, seed)
 predictions += pred_ / n_folds
oof += oof_
return oof, predictions
tr1 = DecisionTreeRegressor()
tr2 = DecisionTreeRegressor()
tr1.fit(x1_tr, y1_tr)
 tr2.fit(x2_tr, y2_tr )<compute_train_metric>
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(seed=42 )
tr1_pred = tr1.predict(x1_vld)
 print('tr1 score : ', np.sqrt(mean_squared_log_error(y1_vld, tr1_pred)))
 tr2_pred = tr2.predict(x2_vld)
 print('tr2 score : ', np.sqrt(mean_squared_log_error(y2_vld, tr2_pred)) )<predict_on_test>
seeds = [0, 1, 2, 3, 4, 5, 6]
oof = np.zeros(( train.shape[0], params['out_size']))
predictions = np.zeros(( test.shape[0], params['out_size']))
for seed in seeds:
oof_, predictions_ = run_k_fold(params['n_folds'], seed)
 oof += oof_ / len(seeds)
 predictions += predictions_ / len(seeds )
confirmed_pred = tr1.predict(test)
 fatalities_pred = tr2.predict(test )<save_to_csv>
valid_results = pd.concat([train_target_sigids[train_target_sigids['sig_id'].isin(train_sig_ids)].reset_index(drop=True), pd.DataFrame(oof)], axis=1 )
submission['ConfirmedCases'] = confirmed_pred
submission['Fatalities'] = fatalities_pred
submission.to_csv('submission.csv', index=False)
<import_modules>
test_results = pd.concat([test[['sig_id']], pd.DataFrame(predictions, columns = sample_submission.columns[1:])], axis=1 )
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split<load_from_csv>
valid_full = train_target_sigids.merge(valid_results, on='sig_id', how='left' ).fillna(0 )
df = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/train.csv')
 test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/test.csv')
 submission = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/submission.csv' )<define_variables>
y_true = y_true.drop(columns=['sig_id'] ).values
y_pred = valid_full.drop(columns=['sig_id'] ).values
score = 0
for i in range(y_true.shape[1]):
score_ = log_loss(y_true[:, i], y_pred[:, i])
 score += score_ / y_true.shape[1]
print("CV log_loss: ", score )
ids = submission['ForecastId']<define_variables>
sub = sample_submission[['sig_id']].merge(test_results, on='sig_id', how='left' ).fillna(0)
 sub.to_csv('submission.csv', index=False )