Kaggle competition: Mechanisms of Action (MoA) Prediction
def flattener(pred):
str_pred = str([row for row in pred])
 str_pred = str_pred.replace(', ', '')
 str_pred = str_pred.replace('[[', '|')
 str_pred = str_pred.replace('][', '|')
 str_pred = str_pred.replace(']]', '|')
 return str_pred<concatenate>
sns.set_context("talk")
 style.use('fivethirtyeight')
 pd.options.display.max_columns = None
warnings.filterwarnings('ignore' )
example_grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
display(example_grid)
 print(flattener(example_grid))<load_pretrained>
N_STARTS = 12
N_SPLITS = 12
SEED = 42
VAR_THRESHOLD = 0.6
N_COMPONENTS = [360, 40]
DROPOUT = 0.24
POSTPROCESS = False
VERBOSE = 0
BATCH_SIZE = 128
EPOCHS = 160
LR = 0.001
NUM_NEURON = 1024
DECAY_FACTOR = 2
NN_NORM = 'batch'
NUM_HIDDEN_LAYER = 1
AF = 'mish'
DEBUG = False
if DEBUG:
N_STARTS = 1
VERBOSE = 2
print('DEBUG TRUE!!!' )
for output_id in submission.index:
task_id = output_id.split('_')[0]
pair_id = int(output_id.split('_')[1])
 f = str(test_path / str(task_id + '.json'))
with open(f, 'r')as read_file:
task = json.load(read_file)
 data = task['test'][pair_id]['input']
pred_1 = flattener(data)
 data = [[5 if i==0 else i for i in j] for j in data]
pred_2 = flattener(data)
 data = [[0 for i in j] for j in data]
pred_3 = flattener(data)
 pred = pred_1 + ' ' + pred_2 + ' ' + pred_3 + ' '
submission.loc[output_id, 'output'] = pred
submission.to_csv('submission.csv' )<import_modules>
%%time
print('loading train, test, targets, drugs')
 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')
 drug = pd.read_csv('.. /input/lish-moa/train_drug.csv')
 print('merging drug ID')
 train_features = pd.merge(train_features, drug, how='left', on='sig_id')
 print('no ctl')
 train_g = train_features['cp_type'] != 'ctl_vehicle'
control_g = test_features['cp_type'] == 'ctl_vehicle'
test_g = test_features['cp_type'] != 'ctl_vehicle'
test_features = test_features.loc[test_g, :].reset_index(drop=True)
 train_features = train_features.loc[train_g, :].reset_index(drop=True)
 train_targets = train_targets.loc[train_g, :].reset_index(drop=True)
 targets = [f for f in train_targets.columns.values.tolist() if 'sig_id' not in f]
print('loading non-targets')
 train_targets_non = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 train_targets_non = train_targets_non.loc[train_g, :].reset_index(drop=True)
 vari = train_targets_non.var().reset_index()
train_targets_non = train_targets_non[[f for f in train_targets_non.columns.values.tolist() if f in vari.loc[vari[0] > 0, 'index'].values.tolist() ]]
non_targets = [f for f in train_targets_non.columns.values.tolist() if 'sig_id' not in f]
ss = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )
import numpy as np
import pandas as pd
import os
import json
from pathlib import Path
import matplotlib.pyplot as plt
from matplotlib import colors 
<define_variables>
data_all = pd.concat([train_features, test_features], ignore_index=True)
 cols_numeric = [feat for feat in list(data_all.columns)if feat not in ['sig_id', 'drug_id', 'cp_type', 'cp_time', 'cp_dose']]
mask =(data_all[cols_numeric].var() >= VAR_THRESHOLD ).values
tmp = data_all[cols_numeric].loc[:, mask]
data_all = pd.concat([data_all[['sig_id', 'drug_id', 'cp_type', 'cp_time', 'cp_dose']], tmp], axis=1 )
data_path = Path('/kaggle/input/abstraction-and-reasoning-challenge/')
 training_path = data_path / 'training'
evaluation_path = data_path / 'evaluation'
test_path = data_path / 'test'
training_tasks = sorted(os.listdir(training_path))
evaluation_tasks = sorted(os.listdir(evaluation_path))<load_from_disk>
def preprocess(df):
df = df.copy()
df.loc[:, 'cp_type'] = df.loc[:, 'cp_type'].map({'trt_cp': 0, 'ctl_vehicle': 1})
 df.loc[:, 'cp_dose'] = df.loc[:, 'cp_dose'].map({'D1': 0, 'D2': 1})
 df.loc[:, 'cp_time'] = df.loc[:, 'cp_time'].map({24: 0, 48: 1, 72: 2})
 df = pd.get_dummies(df, columns=['cp_time','cp_dose'])
 return df
data_all = preprocess(data_all )
task_file = str(training_path / '00d62c1b.json')
 with open(task_file, 'r')as f:
task = json.load(f)
 print(task.keys() )<define_variables>
train_features['drug_id'].value_counts()
n_train_pairs = len(task['train'])
 n_test_pairs = len(task['test'])
 print(f'task contains {n_train_pairs} training pairs')
 print(f'task contains {n_test_pairs} test pairs' )<load_from_csv>
def assign_folds(train, train_targets, targets, seed=SEED):
scored = train_targets.copy()
scored = pd.merge(scored, train[['sig_id', 'drug_id']], how='left', on='sig_id')
 vc = scored['drug_id'].value_counts()
vc1 = vc.loc[vc <= 18].index.sort_values()
vc2 = vc.loc[vc > 18].index.sort_values()
dct1 = {}; dct2 = {}
skf = MultilabelStratifiedKFold(n_splits=N_SPLITS, shuffle=True, random_state=seed)
 tmp = scored.groupby('drug_id')[targets].mean().loc[vc1]
for fold,(idxT,idxV)in enumerate(skf.split(tmp,tmp[targets])) :
dd = {k:fold for k in tmp.index[idxV].values}
dct1.update(dd)
 skf = MultilabelStratifiedKFold(n_splits=N_SPLITS, shuffle=True, random_state=seed)
 tmp = scored.loc[scored.drug_id.isin(vc2)].reset_index(drop=True)
 for fold,(idxT,idxV)in enumerate(skf.split(tmp,tmp[targets])) :
dd = {k:fold for k in tmp.sig_id[idxV].values}
dct2.update(dd)
 scored['fold'] = scored['drug_id'].map(dct1)
 scored.loc[scored.fold.isna() ,'fold'] =\
scored.loc[scored.fold.isna() ,'sig_id'].map(dct2)
 scored.fold = scored.fold.astype('int8')
 return scored['fold'].values
folds = assign_folds(train_features, train_targets, train_targets.columns.values[1:].tolist() , SEED)
 pd.DataFrame(folds ).value_counts()
submission = pd.read_csv(data_path / 'sample_submission.csv', index_col='output_id')
 display(submission.head() )<categorify>
n = 0
tr = np.where(folds != n)[0]
te = np.where(folds == n)[0]
print(tr )
def flattener(pred):
str_pred = str([row for row in pred])
 str_pred = str_pred.replace(', ', '')
 str_pred = str_pred.replace('[[', '|')
 str_pred = str_pred.replace('][', '|')
 str_pred = str_pred.replace(']]', '|')
 return str_pred<concatenate>
def target_category_maker(targets):
targets_category = {'agonist': [], 'antagonist': [], 'agent': [], 'others': []}
for t in targets:
if('_agonist' in t)|('_activator' in t)|('_stimulant' in t)|('_secretagogue' in t)|('_sensitizer' in t):
targets_category['agonist'].append(t)
 elif('_antagonist' in t)|('_inhibitor' in t)|('_blocker' in t):
targets_category['antagonist'].append(t)
 elif('_agent' in t)|('_medium' in t):
targets_category['agent'].append(t)
 else:
targets_category['others'].append(t)
 return targets_category
targets_category = target_category_maker(targets)
 non_targets_category = target_category_maker(non_targets )
example_grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
display(example_grid)
 print(flattener(example_grid))<load_pretrained>
g_feats = [f for f in data_all.columns.values.tolist() if f.startswith('g-')]
c_feats = [f for f in data_all.columns.values.tolist() if f.startswith('c-')]
cp_feats = [f for f in data_all.columns.values.tolist() if f.startswith('cp_')]
print(len(g_feats), len(c_feats), len(cp_feats))
for output_id in submission.index:
task_id = output_id.split('_')[0]
pair_id = int(output_id.split('_')[1])
 f = str(test_path / str(task_id + '.json'))
with open(f, 'r')as read_file:
task = json.load(read_file)
 data = task['test'][pair_id]['input']
pred_1 = flattener(data)
 data = [[5 if i==0 else i for i in j] for j in data]
pred_2 = flattener(data)
 data = [[0 for i in j] for j in data]
pred_3 = flattener(data)
 pred = pred_1 + ' ' + pred_2 + ' ' + pred_3 + ' '
submission.loc[output_id, 'output'] = pred
submission.to_csv('submission.csv' )<import_modules>
%%time
def add_stats_feats(df, feat_list, n):
df[f'{n}stats-mean'] = df[feat_list].mean(axis=1)
 df[f'{n}stats-std'] = df[feat_list].std(axis=1)
 df[f'{n}stats-skew'] = df[feat_list].skew(axis=1)
 df[f'{n}stats-kurt'] = df[feat_list].kurt(axis=1)
 df[f'{n}stats-mad'] = df[feat_list].mad(axis=1)
 return df
data_all = add_stats_feats(data_all, g_feats, 'g')
 data_all = add_stats_feats(data_all, c_feats, 'c')
 data_all = add_stats_feats(data_all, g_feats+c_feats, 'gc' )
import numpy as np
import pandas as pd
import os
from tqdm.auto import tqdm
import shutil as sh<install_modules>
%%time
scaler = StandardScaler()
data_all[g_feats+c_feats] = scaler.fit_transform(data_all[g_feats+c_feats])
 def dim_reducer(data_all, feats, n_components=100):
trans = PCA(n_components=n_components, random_state=SEED)
 train_dist = trans.fit_transform(data_all[feats].values)
 return train_dist
train_g = dim_reducer(data_all, g_feats, n_components=N_COMPONENTS[0])
 train_c = dim_reducer(data_all, c_feats, n_components=N_COMPONENTS[1])
 for i in range(train_g.shape[1]):
data_all[f'g-pca{i+1}'] = train_g[:, i]
for i in range(train_c.shape[1]):
data_all[f'c-pca{i+1}'] = train_c[:, i]
!cp -r.. /input/yolov5train/* .<install_modules>
feats = data_all.columns.values.tolist()
drops = ['sig_id', 'cp_type', 'drug_id']
feats = [f for f in feats if f not in drops]
print('{:,} features'.format(len(feats)))
 print(feats )
!pip install --no-deps '.. /input/weightedboxesfusion/' > /dev/null<feature_engineering>
%%time
t_feats = [f for f in feats if 'cp_' not in f]
pt = GaussRankScaler()
data_all[t_feats] = pt.fit_transform(data_all[t_feats])
 scaler = StandardScaler()
data_all[t_feats] = scaler.fit_transform(data_all[t_feats] )
def convertTrainLabel() :
df = pd.read_csv('.. /input/global-wheat-detection/train.csv')
 bboxs = np.stack(df['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))
 for i, column in enumerate(['x', 'y', 'w', 'h']):
df[column] = bboxs[:,i]
df.drop(columns=['bbox'], inplace=True)
 df['x_center'] = df['x'] + df['w']/2
df['y_center'] = df['y'] + df['h']/2
df['classes'] = 0
df = df[['image_id','x', 'y', 'w', 'h','x_center','y_center','classes']]
index = list(set(df.image_id))
source = 'train'
if True:
for fold in [0]:
val_index = index[len(index)*fold//5:len(index)*(fold+1)//5]
for name,mini in tqdm(df.groupby('image_id')) :
if name in val_index:
path2save = 'val2017/'
else:
path2save = 'train2017/'
if not os.path.exists('convertor/fold{}/labels/'.format(fold)+path2save):
os.makedirs('convertor/fold{}/labels/'.format(fold)+path2save)
 with open('convertor/fold{}/labels/'.format(fold)+path2save+name+".txt", 'w+')as f:
row = mini[['classes','x_center','y_center','w','h']].astype(float ).values
row = row/1024
row = row.astype(str)
 for j in range(len(row)) :
text = ' '.join(row[j])
 f.write(text)
 f.write("
")
 if not os.path.exists('convertor/fold{}/images/{}'.format(fold,path2save)) :
os.makedirs('convertor/fold{}/images/{}'.format(fold,path2save))
sh.copy(".. /input/global-wheat-detection/{}/{}.jpg".format(source,name),'convertor/fold{}/images/{}/{}.jpg'.format(fold,path2save,name)) 
<compute_train_metric>
train = data_all.iloc[:len(train_features)]
test = data_all.iloc[len(train_features):]
del data_all
gc.collect()
def run_wbf(boxes, scores, image_size=1023, iou_thr=0.5, skip_box_thr=0.7, weights=None):
labels = [np.zeros(score.shape[0])for score in scores]
boxes = [box/(image_size)for box in boxes]
boxes, scores, labels = weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = boxes*(image_size)
 return boxes, scores, labels
def TTAImage(image, index):
image1 = image.copy()
if index==0:
rotated_image = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)
 return rotated_image
elif index==1:
rotated_image2 = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)
 rotated_image2 = cv2.rotate(rotated_image2, cv2.ROTATE_90_CLOCKWISE)
 return rotated_image2
elif index==2:
rotated_image3 = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)
 rotated_image3 = cv2.rotate(rotated_image3, cv2.ROTATE_90_CLOCKWISE)
 rotated_image3 = cv2.rotate(rotated_image3, cv2.ROTATE_90_CLOCKWISE)
 return rotated_image3
elif index == 3:
return image1
def rotBoxes90(boxes, im_w, im_h):
ret_boxes =[]
for box in boxes:
x1, y1, x2, y2 = box
x1, y1, x2, y2 = x1-im_w//2, im_h//2 - y1, x2-im_w//2, im_h//2 - y2
x1, y1, x2, y2 = y1, -x1, y2, -x2
x1, y1, x2, y2 = int(x1+im_w//2), int(im_h//2 - y1), int(x2+im_w//2), int(im_h//2 - y2)
 x1a, y1a, x2a, y2a = min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2)
 ret_boxes.append([x1a, y1a, x2a, y2a])
 return np.array(ret_boxes)
 def detect1Image(im0, imgsz, model, device, conf_thres, iou_thres):
img = letterbox(im0, new_shape=imgsz)[0]
img = img[:, :, ::-1].transpose(2, 0, 1)
 img = np.ascontiguousarray(img)
 img = torch.from_numpy(img ).to(device)
 img = img.float()
img /= 255.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
 pred = model(img, augment=False)[0]
pred = non_max_suppression(pred, conf_thres, iou_thres)
 boxes = []
scores = []
for i, det in enumerate(pred):
if det is not None and len(det):
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape ).round()
for *xyxy, conf, cls in det:
boxes.append([int(xyxy[0]), int(xyxy[1]), int(xyxy[2]), int(xyxy[3])])
 scores.append(conf)
 return np.array(boxes), np.array(scores )<load_pretrained>
def seed_everything(seed : int)-> NoReturn :
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed)
 seed_everything(SEED )
def makePseudolabel() :
source = '.. /input/global-wheat-detection/test/'
weights = '.. /input/bestweight/best_yolov5x_fold0.pt'
imgsz = 1024
conf_thres = 0.5
iou_thres = 0.6
is_TTA = True
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=imgsz)
 path2save = 'train2017/'
if not os.path.exists('convertor/fold0/labels/'+path2save):
os.makedirs('convertor/fold0/labels/'+path2save)
 if not os.path.exists('convertor/fold0/images/{}'.format(path2save)) :
os.makedirs('convertor/fold0/images/{}'.format(path2save))
for name in imagenames:
image_id = name.split('.')[0]
im01 = cv2.imread('%s/%s.jpg'%(source,image_id))
if im01.shape[0]!=1024 or im01.shape[1]!=1024:
continue
assert im01 is not None, 'Image Not Found '
im_w, im_h = im01.shape[:2]
if is_TTA:
enboxes = []
enscores = []
for i in range(4):
im0 = TTAImage(im01, i)
 boxes, scores = detect1Image(im0, imgsz, model, device, conf_thres, iou_thres)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, im_w, im_h)
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores, labels = run_wbf(enboxes, enscores, image_size = im_w, iou_thr=0.6, skip_box_thr=0.43)
 boxes = boxes.astype(np.int32 ).clip(min=0, max=im_w)
 else:
boxes, scores = detect1Image(im01, imgsz, model, device, conf_thres, iou_thres)
 boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
boxes = boxes[scores >= 0.05].astype(np.int32)
 scores = scores[scores >=float(0.05)]
lineo = ''
for box in boxes:
x1, y1, w, h = box
xc, yc, w, h =(x1+w/2)/1024,(y1+h/2)/1024, w/1024, h/1024
lineo += '0 %f %f %f %f
'%(xc, yc, w, h)
 fileo = open('convertor/fold0/labels/'+path2save+image_id+".txt", 'w+')
 fileo.write(lineo)
 fileo.close()
sh.copy(".. /input/global-wheat-detection/test/{}.jpg".format(image_id),'convertor/fold0/images/{}/{}.jpg'.format(path2save,image_id)) 
<load_pretrained>
p_min = 0.001
p_max = 0.999
def logloss(y_true, y_pred):
tf.dtypes.cast(y_true, tf.float32)
 tf.dtypes.cast(y_pred, tf.float32)
 y_pred = tf.clip_by_value(y_pred,p_min,p_max)
 return -K.mean(y_true*K.log(y_pred)+(1-y_true)*K.log(1-y_pred))
def create_model(num_feats):

 inp = layers.Input(shape=(num_feats,), name="inp")
 x = layers.Dense(NUM_NEURON, activation=AF )(inp)
 if NN_NORM == 'layer':
x = layers.LayerNormalization()(x)
 elif NN_NORM == 'batch':
x = layers.BatchNormalization()(x)
 x = layers.Dropout(DROPOUT )(x)
 for i in range(NUM_HIDDEN_LAYER):
x = layers.Dense(NUM_NEURON // DECAY_FACTOR, activation=AF )(inp)
 if NN_NORM == 'layer':
x = layers.LayerNormalization()(x)
 elif NN_NORM == 'batch':
x = layers.BatchNormalization()(x)
 x = layers.Dropout(DROPOUT )(x)
 preds = layers.Dense(206, activation='sigmoid' )(x)
 model = models.Model(inp, preds)
 opt = optimizers.Adam(lr=LR)
 loss = tf.keras.losses.BinaryCrossentropy(label_smoothing=0.001)
 model.compile(loss=loss, optimizer=opt, metrics=logloss)
 return model
if len(os.listdir('.. /input/global-wheat-detection/test/')) <11:
pass
else:
!python train.py --img 1024 --batch 4 --epochs 10 --data.. /input/configyolo5/wheat0.yaml --cfg.. /input/yolov5/v5/v5/models/yolov5x.yaml --weights.. /input/bestweight/best_yolov5x_fold0.pt
!rm -rf convertor<define_variables>
def metric(y_true, y_pred, targets):
metrics = []
for _target in targets:
metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target].astype(float), labels=[0,1]))
return np.mean(metrics )
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 )<load_pretrained>
def fit_nfold(train, test, ss, train_targets, feats, targets, seed=SEED, n_splits=N_SPLITS):
res = train_targets.copy()
ss.loc[:, targets] = 0
res.loc[:, targets] = 0
folds = assign_folds(train, train_targets, targets, seed=seed)
 historys = dict()
for n in range(n_splits):
tr = np.where(folds != n)[0]
te = np.where(folds == n)[0]
print(f"======{train_targets.values[tr].shape}========{train_targets.values[te].shape}=====")
 print(f'Seed: {seed} => Fold: {n}')
 if DEBUG:
if n > 0:
print(f'Skip fold{n}')
 continue
model = create_model(len(feats))
checkpoint_path = f'repeat{seed}_fold{n}.hdf5'
reduce_lr_loss = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, min_lr=1e-5, patience=4, verbose=VERBOSE, mode='min')
 cb_checkpt = callbacks.ModelCheckpoint(checkpoint_path, monitor = 'val_loss', verbose = 2, save_best_only = True,
save_weights_only = True, mode = 'min')
 early = callbacks.EarlyStopping(monitor="val_loss", mode="min", restore_best_weights=True, patience=8, verbose = VERBOSE)
 nn_callbacks = [reduce_lr_loss, cb_checkpt, early]
x_train = train[feats].values[tr]
x_val = train[feats].values[te]
x_test = test[feats].values
y_train = train_targets[targets].values[tr]
y_val = train_targets[targets].values[te]
history = model.fit(x_train, y_train,
validation_data=(x_val, y_val),
epochs=EPOCHS, batch_size=BATCH_SIZE,
callbacks=nn_callbacks, verbose=VERBOSE 
)
 historys[f'history_{n}'] = history
model.load_weights(checkpoint_path)
 test_predict = model.predict(x_test)
 val_predict = model.predict(x_val)
 res.loc[te, targets] = val_predict
ss.loc[test_g, targets] += test_predict / n_splits
print(f'OOF Metric For SEED {seed} => FOLD {n} : {metric(train_targets.loc[te, targets], pd.DataFrame(val_predict, columns=targets), targets)}')
 print('+-' * 10)
 print(f'OOF Metric: {metric(train_targets[targets], res[targets], targets)}')
 return ss, res, historys
def detect() :
source = '.. /input/global-wheat-detection/test/'
weights = 'weights/best.pt'
if not os.path.exists(weights):
weights = '.. /input/bestweight/best_yolov5x_fold0.pt'
imgsz = 1024
conf_thres = 0.5
iou_thres = 0.6
is_TTA = True
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=imgsz)
 results = []
fig, ax = plt.subplots(5, 2, figsize=(30, 70))
count = 0
for name in imagenames:
image_id = name.split('.')[0]
im01 = cv2.imread('%s/%s.jpg'%(source,image_id))
assert im01 is not None, 'Image Not Found '
im_w, im_h = im01.shape[:2]
if is_TTA:
enboxes = []
enscores = []
for i in range(4):
im0 = TTAImage(im01, i)
 boxes, scores = detect1Image(im0, imgsz, model, device, conf_thres, iou_thres)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, im_w, im_h)
 if 1:
enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores = detect1Image(im01, imgsz, model, device, conf_thres, iou_thres)
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores, labels = run_wbf(enboxes, enscores, image_size = im_w, iou_thr=0.6, skip_box_thr=0.5)
 boxes = boxes.astype(np.int32 ).clip(min=0, max=im_w)
 else:
boxes, scores = detect1Image(im01, imgsz, model, device, conf_thres, iou_thres)
 boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
boxes = boxes[scores >= 0.05].astype(np.int32)
 scores = scores[scores >=float(0.05)]
if count<10:
for box, score in zip(boxes,scores):
cv2.rectangle(im0, 
(box[0], box[1]), 
(box[2]+box[0], box[3]+box[1]), 
(220, 0, 0), 2)
 cv2.putText(im0, '%.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(im0)
 count+=1
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result)
 return results<save_to_csv>
res = train_targets.copy()
res.loc[:, targets] = 0
ss.loc[:, targets] = 0
for s in range(N_STARTS):
ss_tmp = ss.copy()
ss_, res_, historys = fit_nfold(train, test, ss_tmp, train_targets, feats, targets, seed=SEED+s**2, n_splits=N_SPLITS)
 ss.loc[:, targets] += ss_[targets].values / N_STARTS
res.loc[:, targets] += res_[targets].values / N_STARTS
results = detect()
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('./submission.csv', index=False)
 test_df.head()<set_options>
print(f'OOF Metric: {metric(train_targets, res, targets)}')
 if POSTPROCESS:
print('post-process...')
 ss.iloc[:,1:] = np.clip(ss.values[:, 1:], p_min, p_max)
 ss.iloc[control_g, 1:] = 0
ss.to_csv('submission.csv', index=False)
 print(ss.shape)
 ss.head()