Kaggle competition: Global Wheat Detection
<SOS> metric: custom metric Kaggle data source: global-wheat-detection<compute_train_metric>
TIME_LIMIT = 6.9*60*60
Origin = False
p_HF = True
p_ROT = False
eimg_size = 1024
pesudo_thres = 0.4
pesudo_iou = 0.35
pesudo_score_threshold = 0
img_size = 1024
NMS_IOU_THR = 0.6
NMS_CONF_THR = 0.35
best_iou_thr = 0.35
best_skip_box_thr = 0.35
best_final_score = 0
best_score_threshold = 0.1
EPO = 8
WEIGHTS = ".. /input/yolov4-pytorch/last_trainF5_onlyfix.weights"
PESUDO_WEIGHTS = '.. /input/effdet-cutmix-augmix-result/best-checkpoint_d7.bin'
CONFIG = '.. /input/yolov4-pytorch/cd53s_self_1class.cfg'
OPTIMIZER = '.. /input/yolov4-pytorch/last_optimizer.pth'
is_TEST = len(os.listdir('.. /input/global-wheat-detection/test/')) >11
is_AUG = False
is_ROT = True
VALIDATE = False
PSEUDO = True
Yolov4 = True
Effdet = True
GET_PESUDO = True
csv_file = '.. /input/yolov4-pytorch/newtrain.csv'
marking = pd.read_csv(csv_file)
 bboxs = np.stack(marking['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))
 for i, column in enumerate(['x', 'y', 'w', 'h']):
marking[column] = bboxs[:,i]
marking.drop(columns=['bbox'], inplace=True)
 max_label_numbers = 0
pesudo_result = []
with torch.no_grad() :
test_df = update_stage1_test_preds(test_df )<load_pretrained>
!pip install --no-deps '.. /input/timm-package/timm-0.1.26-py3-none-any.whl' > /dev/null
!pip install --no-deps '.. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl' > /dev/null
sys.path.insert(0, ".. /input/timm-efficientdet-pytorch")
 sys.path.insert(0, ".. /input/omegaconf")
device = torch.device(CFG['device'])
 model = RSNAClassifier().to(device)
 test_pred_dfs = []
model.load_state_dict(torch.load(f"{CFG['model_path']}/model_{CFG['tag']}"))
test_pred_df = inference(model, device, test_df, CFG['test_img_path'])
 test_pred_dfs.append(test_pred_df )<save_to_csv>
def convertTrainLabel(max_label_numbers):
df = pd.read_csv(csv_file)
 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['x1'] = df['x'] + df['w']
df['y1'] = df['y'] + df['h']
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','x1','y1','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')) :
os.makedirs('convertor', exist_ok=True)
 sh.copy(".. /input/global-wheat-detection/{}/{}.jpg".format(source,name),'convertor/{}.jpg'.format(name))
if name in val_index:
path2save = 'convertor/val.txt'
else:
path2save = 'convertor/train.txt'
with open(path2save, 'a')as f:
f.write(f'{name}.jpg')
 row = mini[['x','y','x1','y1','classes']].astype(int ).values
if row.shape[0]>max_label_numbers:
max_label_numbers = row.shape[0]
row = row.astype(str)
 for j in range(len(row)) :
text = ' '+','.join(row[j])
 f.write(text)
 f.write('
')
 return max_label_numbers
test_pred_df.to_csv('submission_raw.csv' )<groupby>
def run_wbf(boxes, scores, image_size=1024, 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 )
ids = []
labels = []
gp_mean = test_pred_df.loc[:, ['StudyInstanceUID']+CFG['exam_target_cols']].groupby('StudyInstanceUID', sort=False ).mean()
for col in CFG['exam_target_cols']:
ids += [[patient+'_'+col for patient in gp_mean.index]]
labels += [gp_mean[col].values]
ids += [test_pred_df.SOPInstanceUID.values]
labels += [test_pred_df[CFG['image_target_cols']].values[:,0]]
ids = np.concatenate(ids)
 labels = np.concatenate(labels )<create_dataframe>
@jit(nopython=True)
 def calculate_iou(gt, pr, form='pascal_voc')-> float:

 if form == 'coco':
gt = gt.copy()
pr = pr.copy()
gt[2] = gt[0] + gt[2]
gt[3] = gt[1] + gt[3]
pr[2] = pr[0] + pr[2]
pr[3] = pr[1] + pr[3]
dx = min(gt[2], pr[2])- max(gt[0], pr[0])+ 1
if dx < 0:
return 0.0
dy = min(gt[3], pr[3])- max(gt[1], pr[1])+ 1
if dy < 0:
return 0.0
overlap_area = dx * dy
union_area =(
(gt[2] - gt[0] + 1)*(gt[3] - gt[1] + 1)+ 
(pr[2] - pr[0] + 1)*(pr[3] - pr[1] + 1)-
overlap_area 
)
 return overlap_area / union_area
@jit(nopython=True)
 def find_best_match(gts, pred, pred_idx, threshold = 0.5, form = 'pascal_voc', ious=None)-> int:

 best_match_iou = -np.inf
best_match_idx = -1
for gt_idx in range(len(gts)) :
if gts[gt_idx][0] < 0:
continue
iou = -1 if ious is None else ious[gt_idx][pred_idx]
if iou < 0:
iou = calculate_iou(gts[gt_idx], pred, form=form)
 if ious is not None:
ious[gt_idx][pred_idx] = iou
if iou < threshold:
continue
if iou > best_match_iou:
best_match_iou = iou
best_match_idx = gt_idx
return best_match_idx
@jit(nopython=True)
 def calculate_precision(gts, preds, threshold = 0.5, form = 'coco', ious=None)-> float:

 n = len(preds)
 tp = 0
fp = 0
for pred_idx in range(n):
best_match_gt_idx = find_best_match(gts, preds[pred_idx], pred_idx,
threshold=threshold, form=form, ious=ious)
 if best_match_gt_idx >= 0:
tp += 1
gts[best_match_gt_idx] = -1
else:
fp += 1
fn =(gts.sum(axis=1)> 0 ).sum()
return tp /(tp + fp + fn)
 @jit(nopython=True)
 def calculate_image_precision(gts, preds, thresholds =(0.5,), form = 'coco')-> float:

 n_threshold = len(thresholds)
 image_precision = 0.0
ious = np.ones(( len(gts), len(preds)))* -1
for threshold in thresholds:
precision_at_threshold = calculate_precision(gts.copy() , preds, threshold=threshold,
form=form, ious=ious)
 image_precision += precision_at_threshold / n_threshold
return image_precision
iou_thresholds = numba.typed.List()
for x in [0.5, 0.55, 0.6, 0.65, 0.7, 0.75]:
iou_thresholds.append(x)
 def validate() :

 use_cuda = True
truth = {}
DATA_ROOT_PATH = '.. /input/global-wheat-detection/train'
with open('convertor/val.txt', 'r', encoding='utf-8')as f:
for line in f.readlines() :
data = line.split(" ")
 truth[data[0]] = []
for i in data[1:]:
truth[data[0]].append([int(j)for j in i.split(',')])
 image_ids = [image_id.split('.')[0] for image_id in list(truth.keys())]
weights = 'checkpoints/last.pt'
Effdet = False
if not os.path.exists(weights):
Effdet = True
model = Darknet(CONFIG)
 model.load_weights(WEIGHTS)
 print('OLD WEIGHT')
 else:
model = torch.load(weights)
 if use_cuda:
model.cuda()
if Effdet:
net = get_valnet_file(PESUDO_WEIGHTS)
 model.eval()
num_classes = model.num_classes
class_names = 'wheat'
results = []
for image_id in image_ids:
enboxes = []
enscores = []
img = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg')
 img = cv2.resize(img,(model.width, model.height))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 if Yolov4:
if is_ROT:
for i in range(4):
img1 = TTAImage(img, i)
 img1 = img1.transpose(2, 0, 1)
 img1 = torch.from_numpy(img1 ).float().div(255.0 ).unsqueeze(0)
 img1 = img1.cuda()
img1 = torch.autograd.Variable(img1)
 with torch.no_grad() :
output = model(img1)
 boxes = utils.post_processing(img1, NMS_CONF_THR, NMS_CONF_THR, output)
 det = np.array(boxes)
 boxes, scores = process_det(0, det, NMS_CONF_THR)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, *img.shape[:2])
 enboxes.append(boxes)
 enscores.append(scores)
 with torch.no_grad() :
img1 = img.transpose(2, 0, 1)
 img1 = torch.from_numpy(img1 ).float().div(255.0 ).unsqueeze(0)
 img1 = img1.cuda()
img1 = torch.autograd.Variable(img1)
 output = model(img1)
 boxes = utils.post_processing(img1, NMS_CONF_THR, NMS_CONF_THR, output)
 det = np.array(boxes)
 boxes, scores = process_det(0, det, NMS_CONF_THR)
 enboxes.append(boxes)
 enscores.append(scores)
 if Effdet:
img1 = img.transpose(2, 0, 1)
 img1 = torch.from_numpy(img1 ).cuda()
img1 = img1.float()
img1 /= 255.0
if p_HF:
with torch.no_grad() :
imgs = torch.stack(( img1,)).float().cuda()
for tta_transform in tta_transforms:
det = net(tta_transform.batch_augment(imgs.clone()), torch.tensor([1]*imgs.shape[0] ).float().cuda())
 for i in range(imgs.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > pesudo_thres)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes = tta_transform.deaugment_boxes(boxes.copy())
 enboxes.append(boxes)
 enscores.append(scores)
 records = marking[marking['image_id'] == image_id]
gtboxes = records[['x', 'y', 'w', 'h']].values
gtboxes = gtboxes.astype(np.int32 ).clip(min=0, max=1024)
 gtboxes[:, 2] = gtboxes[:, 0] + gtboxes[:, 2]
gtboxes[:, 3] = gtboxes[:, 1] + gtboxes[:, 3]
result = {
'image_id': image_id,
'pred_enboxes': enboxes,
'pred_enscores': enscores,
'gt_boxes': gtboxes,
}
results.append(result)
 return results
def calculate_final_score(all_predictions, iou_thr, skip_box_thr, score_threshold):
final_scores = []
for i in range(len(all_predictions)) :
gt_boxes = all_predictions[i]['gt_boxes'].copy()
enboxes = all_predictions[i]['pred_enboxes'].copy()
enscores = all_predictions[i]['pred_enscores'].copy()
image_id = all_predictions[i]['image_id']
pred_boxes, scores, labels = run_wbf(enboxes, enscores, image_size=1024, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 pred_boxes = pred_boxes.astype(np.int32 ).clip(min=0, max=1024)
 indexes = np.where(scores>score_threshold)
 pred_boxes = pred_boxes[indexes]
scores = scores[indexes]
image_precision = calculate_image_precision(gt_boxes, pred_boxes,thresholds=iou_thresholds,form='pascal_voc')
 final_scores.append(image_precision)
 return np.mean(final_scores)
 def show_result(sample_id, preds, gt_boxes):
sample = cv2.imread(f'.. /input/global-wheat-detection/train/{sample_id}.jpg', cv2.IMREAD_COLOR)
 sample = cv2.cvtColor(sample, cv2.COLOR_BGR2RGB)
 fig, ax = plt.subplots(1, 1, figsize=(16, 8))
for pred_box in preds:
cv2.rectangle(
 sample, 
(pred_box[0], pred_box[1]), 
(pred_box[2], pred_box[3]), 
(220, 0, 0), 2 
)
 for gt_box in gt_boxes:
cv2.rectangle(
 sample, 
(gt_box[0], gt_box[1]), 
(gt_box[2], gt_box[3]), 
(0, 0, 220), 2 
)
 ax.set_axis_off()
ax.imshow(sample)
 ax.set_title("RED: Predicted | BLUE - Ground-truth" )
assert len(ids)== len(labels)
 submission = pd.DataFrame()
submission['id'] = ids
submission['label'] = labels<concatenate>
def log(text):
print(text)
 def optimize(space, all_predictions, n_calls=10):
@use_named_args(space)
 def score(**params):
log('-'*10)
 log(params)
 final_score = calculate_final_score(all_predictions, **params)
 log(f'final_score = {final_score}')
 log('-'*10)
 return -final_score
return gp_minimize(func=score, dimensions=space, n_calls=n_calls )
if not DEBUG:
submission = pd.concat([public_sub,
submission],
).reset_index(drop=True )<save_to_csv>
img_size = 1024
def get_valid_transforms() :
return A.Compose([
A.Resize(height=img_size, width=img_size, p=1.0),
ToTensorV2(p=1.0),
], p=1.0)
 DATA_ROOT_PATH = '.. /input/global-wheat-detection/test'
class DatasetRetriever_test(Dataset):
def __init__(self, image_ids, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
def __getitem__(self, index: int):
image_id = self.image_ids[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
im0 = image
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id, im0
def __len__(self)-> int:
return self.image_ids.shape[0]
dataset = DatasetRetriever_test(
 image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),
transforms=get_valid_transforms() 
)
 data_loader = DataLoader(
 dataset,
batch_size=2,
shuffle=False,
num_workers=4,
drop_last=False
)
print(submission.head(3))
print(submission.tail(3))
print(submission.shape)
 submission.to_csv('submission.csv', index=False )<import_modules>
class BaseWheatTTA:

 image_size = img_size
def augment(self, image):
raise NotImplementedError
def batch_augment(self, images):
raise NotImplementedError
def deaugment_boxes(self, boxes):
raise NotImplementedError
class TTAHorizontalFlip(BaseWheatTTA):

 def augment(self, image):
return image.flip(1)
 def batch_augment(self, images):
return images.flip(2)
 def deaugment_boxes(self, boxes):
boxes[:, [1,3]] = self.image_size - boxes[:, [3,1]]
return boxes
class TTAVerticalFlip(BaseWheatTTA):

 def augment(self, image):
return image.flip(2)
 def batch_augment(self, images):
return images.flip(3)
 def deaugment_boxes(self, boxes):
boxes[:, [0,2]] = self.image_size - boxes[:, [2,0]]
return boxes
class TTARotate90(BaseWheatTTA):

 def augment(self, image):
return torch.rot90(image, 1,(1, 2))
def batch_augment(self, images):
return torch.rot90(images, 1,(2, 3))
def deaugment_boxes(self, boxes):
res_boxes = boxes.copy()
res_boxes[:, [0,2]] = self.image_size - boxes[:, [1,3]]
res_boxes[:, [1,3]] = boxes[:, [2,0]]
return res_boxes
class TTACompose(BaseWheatTTA):

 def __init__(self, transforms):
self.transforms = transforms
def augment(self, image):
for transform in self.transforms:
image = transform.augment(image)
 return image
def batch_augment(self, images):
for transform in self.transforms:
images = transform.batch_augment(images)
 return images
def prepare_boxes(self, boxes):
result_boxes = boxes.copy()
result_boxes[:,0] = np.min(boxes[:, [0,2]], axis=1)
 result_boxes[:,2] = np.max(boxes[:, [0,2]], axis=1)
 result_boxes[:,1] = np.min(boxes[:, [1,3]], axis=1)
 result_boxes[:,3] = np.max(boxes[:, [1,3]], axis=1)
 return result_boxes
def deaugment_boxes(self, boxes):
for transform in self.transforms[::-1]:
boxes = transform.deaugment_boxes(boxes)
 return self.prepare_boxes(boxes)
 tta_transforms = []
for tta_combination in product([TTAHorizontalFlip() , None],
[TTAVerticalFlip() , None],
[TTARotate90() , None]):
tta_transforms.append(TTACompose([tta_transform for tta_transform in tta_combination if tta_transform]))
package_path = '.. /input/efficientnet-pytorch-07/efficientnet_pytorch-0.7.0'
bash_commands = [
'cp.. /input/gdcm-conda-install/gdcm.tar.',
'tar -xvzf gdcm.tar',
'conda install --offline./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2'
]
for bashCommand in bash_commands:
process = subprocess.Popen(bashCommand.split() , stdout=subprocess.PIPE)
 output, error = process.communicate()
do_full = False
TTA_STEPS=3
CFG = {
'train': False,
'train_img_path': '.. /input/rsna-str-pulmonary-embolism-detection/train',
'test_img_path': '.. /input/rsna-str-pulmonary-embolism-detection/test',
'cv_fold_path': '.. /input/stratified-validation-strategy/rsna_train_splits_fold_20.csv',
'train_path': '.. /input/rsna-str-pulmonary-embolism-detection/train.csv',
'test_path': '.. /input/rsna-str-pulmonary-embolism-detection/test.csv',
'image_target_cols': [
'pe_present_on_image',
],
'exam_target_cols': [
'negative_exam_for_pe',
'rv_lv_ratio_gte_1',
'rv_lv_ratio_lt_1',
'leftsided_pe',
'chronic_pe',
'rightsided_pe',
'acute_and_chronic_pe',
'central_pe',
'indeterminate'
],
'img_num': 200,
'img_size': 256,
'lr': 0.0005,
'epochs': 2,
'device': 'cuda',
'train_bs': 2,
'accum_iter': 8,
'verbose_step': 1,
'num_workers': 4,
'efbnet': 'efficientnet-b0',
'train_folds': [np.arange(0,16),
np.concatenate([np.arange(0,12), np.arange(16,20)]),
np.concatenate([np.arange(0,8), np.arange(12,20)]),
np.concatenate([np.arange(0,4), np.arange(8,20)]),
np.arange(4,20),
],
'valid_folds': [np.arange(16,20),
np.arange(12,16),
np.arange(8,12),
np.arange(4,8),
np.arange(0,4)
 ],
'model_path': '.. /input/kh-rsna-model',
'tag': 'efb0_stage2_multilabel'
}
SEED = 42321
def seed_everything(seed):
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
torch.backends.cudnn.benchmark = True
def seed_everything(seed):
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
torch.backends.cudnn.benchmark = True
def window(img, WL=50, WW=350):
upper, lower = WL+WW//2, WL-WW//2
X = np.clip(img.copy() , lower, upper)
 X = X - np.min(X)
 X = X / np.max(X)
 return X
def get_img(path, transforms, opencv=False):
d = pydicom.read_file(path)


 img =(d.pixel_array * d.RescaleSlope)+ d.RescaleIntercept
r = window(img, -600, 1500)
 g = window(img, 100, 700)
 b = window(img, 40, 400)
 res = np.concatenate([r[:, :, np.newaxis],
g[:, :, np.newaxis],
b[:, :, np.newaxis]], axis=-1)
 if opencv:
res = transforms(image=(res*255.0 ).astype('uint8')) ['image']
res = torch.div(res, 255.)
else:
res = transforms(image=res)['image']
return res
def tta_augmentation() :
transforms = [
albu.Resize(256, 256),
albu.VerticalFlip(p=0.25),
albu.RandomRotate90(p=0.5),
albu.CLAHE(p=0.3),
albu.RandomBrightnessContrast(p=0.3),
albu.HueSaturationValue(p=0.3),
ToTensorV2(p=1.) ,
]
return albu.Compose(transforms)
 def get_valid_transforms() :
return albu.Compose([
albu.Resize(256, 256),
ToTensorV2(p=1.) ,
], p=1.)
class RSNADatasetStage1(Dataset):
def __init__(
 self, df, label_smoothing, data_root,
image_subsampling=True, transforms=None, output_label=True, opencv=False 
):
super().__init__()
self.df = df.reset_index(drop=True ).copy()
self.label_smoothing = label_smoothing
self.transforms = transforms
self.data_root = data_root
self.output_label = output_label
self.opencv = opencv
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index: int):
if self.output_label:
target = self.df[CFG['image_target_cols']].values[index]
path = "{}/{}/{}/{}.dcm".format(self.data_root,
self.df.iloc[index]['StudyInstanceUID'],
self.df.iloc[index]['SeriesInstanceUID'],
self.df.iloc[index]['SOPInstanceUID'])
 img = get_img(path, self.transforms, self.opencv)
 return img
class RSNADataset(Dataset):
def __init__(
 self, df, label_smoothing, data_root,
image_subsampling=True, transforms=None, output_label=True 
):
super().__init__()
self.df = df
self.patients = self.df['StudyInstanceUID'].unique()
self.image_subsampling = image_subsampling
self.label_smoothing = label_smoothing
self.transforms = transforms
self.data_root = data_root
self.output_label = output_label
def get_patients(self):
return self.patients
def __len__(self):
return len(self.patients)
 def __getitem__(self, index: int):
patient = self.patients[index]
df_ = self.df.loc[self.df.StudyInstanceUID == patient]
per_image_feats = get_stage1_columns()
if self.image_subsampling:
img_num = min(CFG['img_num'], df_.shape[0])
 img_ix = np.random.choice(np.arange(df_.shape[0]), replace=False, size=img_num)
 imgs = np.zeros(( CFG['img_num'],), np.float32)
 per_image_preds = np.zeros(( CFG['img_num'], len(per_image_feats)) , np.float32)
 locs = np.zeros(( CFG['img_num'],), np.float32)
 image_masks = np.zeros(( CFG['img_num'],), np.float32)
 image_masks[:img_num] = 1.
 if self.output_label:
exam_label = df_[CFG['exam_target_cols']].values[0]
image_labels = np.zeros(( CFG['img_num'], len(CFG['image_target_cols'])) , np.float32)
 else:
img_num = df_.shape[0]
img_ix = np.arange(df_.shape[0])
 imgs = np.zeros(( img_num,), np.float32)
 per_image_preds = np.zeros(( img_num, len(per_image_feats)) , np.float32)
 locs = np.zeros(( img_num,), np.float32)
 image_masks = np.zeros(( img_num,), np.float32)
 image_masks[:img_num] = 1.
 if self.output_label:
exam_label = df_[CFG['exam_target_cols']].values[0]
image_labels = np.zeros(( img_num, len(CFG['image_target_cols'])) , np.float32)
 for i, im_ix in enumerate(img_ix):
path = "{}/{}/{}/{}.dcm".format(self.data_root,
df_['StudyInstanceUID'].values[im_ix],
df_['SeriesInstanceUID'].values[im_ix],
df_['SOPInstanceUID'].values[im_ix])
 d = pydicom.read_file(path)
 locs[i] = d.ImagePositionPatient[2]
per_image_preds[i,:] = df_[per_image_feats].values[im_ix,:]
if self.output_label == True:
image_labels[i] = df_[CFG['image_target_cols']].values[im_ix]
seq_ix = np.argsort(locs)

locs = locs[seq_ix]
locs[1:img_num] = locs[1:img_num]-locs[0:img_num-1]
locs[0] = 0
per_image_preds = per_image_preds[seq_ix]
if self.output_label == True:
image_labels = image_labels[seq_ix]
image_labels = np.clip(image_labels, self.label_smoothing, 1 - self.label_smoothing)
 exam_label = np.clip(exam_label, self.label_smoothing, 1 - self.label_smoothing)
 return imgs, per_image_preds, locs, image_labels, exam_label, image_masks
else:
return imgs, per_image_preds, locs, img_num, index, seq_ix
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout 
)
 class RNSAImageFeatureExtractor(nn.Module):
def __init__(self):
super().__init__()
self.cnn_model = EfficientNet.from_name(CFG['efbnet'])
 self.pooling = nn.AdaptiveAvgPool2d(1)
 def get_dim(self):
return self.cnn_model._fc.in_features
def forward(self, x):
feats = self.cnn_model.extract_features(x)
 return self.pooling(feats ).view(x.shape[0], -1)
 class RSNAImgClassifierSingle(nn.Module):
def __init__(self):
super().__init__()
self.cnn_model = RNSAImageFeatureExtractor()
self.image_predictors = nn.Linear(self.cnn_model.get_dim() , 1)
 def forward(self, imgs):
imgs_embdes = self.cnn_model(imgs)
 image_preds = self.image_predictors(imgs_embdes)
 return image_preds
class RSNAImgClassifier(nn.Module):
def __init__(self):
super().__init__()
self.cnn_model = RNSAImageFeatureExtractor()
self.image_predictors = nn.Linear(self.cnn_model.get_dim() , 9)
 def forward(self, imgs):
imgs_embdes = self.cnn_model(imgs)
 image_preds = self.image_predictors(imgs_embdes)
 return image_preds
def post_process(exam_pred, image_pred):
rv_lv_ratio_lt_1_ix = CFG['exam_target_cols'].index('rv_lv_ratio_lt_1')
 rv_lv_ratio_gte_1_ix = CFG['exam_target_cols'].index('rv_lv_ratio_gte_1')
 central_pe_ix = CFG['exam_target_cols'].index('central_pe')
 rightsided_pe_ix = CFG['exam_target_cols'].index('rightsided_pe')
 leftsided_pe_ix = CFG['exam_target_cols'].index('leftsided_pe')
 acute_and_chronic_pe_ix = CFG['exam_target_cols'].index('acute_and_chronic_pe')
 chronic_pe_ix = CFG['exam_target_cols'].index('chronic_pe')
 negative_exam_for_pe_ix = CFG['exam_target_cols'].index('negative_exam_for_pe')
 indeterminate_ix = CFG['exam_target_cols'].index('indeterminate')
 has_pe_image = torch.max(image_pred, 1)[0][0] > 0
rv_lv_ratios = exam_pred[:, [rv_lv_ratio_lt_1_ix, rv_lv_ratio_gte_1_ix]]
rv_lv_ratios_1_a = nn.functional.softmax(rv_lv_ratios, dim=1)
 rv_lv_ratios_1_a = torch.log(rv_lv_ratios_1_a/(1-rv_lv_ratios_1_a))
exam_pred[:, [rv_lv_ratio_lt_1_ix, rv_lv_ratio_gte_1_ix]] = torch.where(has_pe_image, rv_lv_ratios_1_a, rv_lv_ratios)
 crl_pe = exam_pred[:, [central_pe_ix, rightsided_pe_ix, leftsided_pe_ix]]
has_no_pe = torch.max(crl_pe ,1)[0] <= 0
max_val = torch.max(crl_pe, 1)[0]
crl_pe_1_b = torch.where(crl_pe==max_val, 0.0001-crl_pe+crl_pe, crl_pe)
 exam_pred[:, [central_pe_ix, rightsided_pe_ix, leftsided_pe_ix]] = torch.where(has_pe_image*has_no_pe, crl_pe_1_b, crl_pe)
 ac_pe = exam_pred[:, [acute_and_chronic_pe_ix, chronic_pe_ix]]
both_ac_ch = torch.min(ac_pe ,1)[0] > 0
ac_pe_1_c = nn.functional.softmax(ac_pe, dim=1)
 ac_pe_1_c = torch.log(ac_pe_1_c/(1-ac_pe_1_c))
exam_pred[:, [acute_and_chronic_pe_ix, chronic_pe_ix]] = torch.where(has_pe_image*both_ac_ch, ac_pe_1_c, ac_pe)
 neg_ind = exam_pred[:, [negative_exam_for_pe_ix, indeterminate_ix]]
neg_ind_1d = torch.clamp(neg_ind, max=0)
 exam_pred[:, [negative_exam_for_pe_ix, indeterminate_ix]] = torch.where(has_pe_image, neg_ind_1d, neg_ind)
 ne_inde = exam_pred[:, [negative_exam_for_pe_ix, indeterminate_ix]]
ne_inde_2_a = nn.functional.softmax(ne_inde, dim=1)
 ne_inde_2_a = torch.log(ne_inde_2_a/(1-ne_inde_2_a))
exam_pred[:, [negative_exam_for_pe_ix, indeterminate_ix]] = torch.where(~has_pe_image, ne_inde_2_a, ne_inde)
 all_other_exam_labels = exam_pred[:, [rv_lv_ratio_lt_1_ix, rv_lv_ratio_gte_1_ix,
central_pe_ix, rightsided_pe_ix, leftsided_pe_ix,
acute_and_chronic_pe_ix, chronic_pe_ix]]
all_other_exam_labels_2_b = torch.clamp(all_other_exam_labels, max=0)
 exam_pred[:, [rv_lv_ratio_lt_1_ix, rv_lv_ratio_gte_1_ix,
central_pe_ix, rightsided_pe_ix, leftsided_pe_ix,
acute_and_chronic_pe_ix, chronic_pe_ix]] = torch.where(~has_pe_image, all_other_exam_labels_2_b, all_other_exam_labels)
 return exam_pred, image_pred
def check_label_consistency(checking_df):
df = checking_df.copy()
print(df.shape)
 df['positive_images_in_exam'] = df['StudyInstanceUID'].map(df.groupby(['StudyInstanceUID'] ).pe_present_on_image.max())
 df_pos = df.loc[df.positive_images_in_exam > 0.5]
df_neg = df.loc[df.positive_images_in_exam <= 0.5]
rule1a = df_pos.loc[(( df_pos.rv_lv_ratio_lt_1 > 0.5)& 
(df_pos.rv_lv_ratio_gte_1 > 0.5)) | 
(( df_pos.rv_lv_ratio_lt_1 <= 0.5)& 
(df_pos.rv_lv_ratio_gte_1 <= 0.5)) ].reset_index(drop = True)
 rule1a['broken_rule'] = '1a'
rule1b = df_pos.loc[(df_pos.central_pe <= 0.5)& 
(df_pos.rightsided_pe <= 0.5)& 
(df_pos.leftsided_pe <= 0.5)].reset_index(drop = True)
 rule1b['broken_rule'] = '1b'
rule1c = df_pos.loc[(df_pos.acute_and_chronic_pe > 0.5)& 
(df_pos.chronic_pe > 0.5)].reset_index(drop = True)
 rule1c['broken_rule'] = '1c'
rule1d = df_pos.loc[(df_pos.indeterminate > 0.5)| 
(df_pos.negative_exam_for_pe > 0.5)].reset_index(drop = True)
 rule1d['broken_rule'] = '1d'
rule2a = df_neg.loc[(( df_neg.indeterminate > 0.5)& 
(df_neg.negative_exam_for_pe > 0.5)) | 
(( df_neg.indeterminate <= 0.5)& 
(df_neg.negative_exam_for_pe <= 0.5)) ].reset_index(drop = True)
 rule2a['broken_rule'] = '2a'
rule2b = df_neg.loc[(df_neg.rv_lv_ratio_lt_1 > 0.5)| 
(df_neg.rv_lv_ratio_gte_1 > 0.5)| 
(df_neg.central_pe > 0.5)| 
(df_neg.rightsided_pe > 0.5)| 
(df_neg.leftsided_pe > 0.5)| 
(df_neg.acute_and_chronic_pe > 0.5)| 
(df_neg.chronic_pe > 0.5)].reset_index(drop = True)
 rule2b['broken_rule'] = '2b'
errors = pd.concat([rule1a, rule1b, rule1c, rule1d, rule2a, rule2b], axis = 0)
 print('label in-consistency counts:', errors.shape)
 if errors.shape[0] > 0:
print(errors.broken_rule.value_counts())
 print(errors)
 assert False
class TimeDistributed(nn.Module):
def __init__(self, module, batch_first=True):
super(TimeDistributed, self ).__init__()
self.module = module
self.batch_first = batch_first
def forward(self, x):

 x_size= x.size()
c_in = x.contiguous().view(x_size[0] * x_size[1], *x_size[2:])
 c_out = self.module(c_in)
 r_in = c_out.view(x_size[0], x_size[1], -1)
 if self.batch_first is False:
r_in = r_in.permute(1, 0, 2)
 return r_in
def inference(model, device, df, root_path):
model.eval()
t = time.time()
ds = RSNADataset(df[0], 0.0, root_path, image_subsampling=False, transforms=None, output_label=False)
 dataloader = torch.utils.data.DataLoader(
 ds,
batch_size=1,
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=True, 
)
 patients = ds.get_patients()
res_dfs = []
for step,(imgs, per_image_preds, locs, img_num, index, seq_ix)in enumerate(dataloader):
imgs = imgs.to(device ).float()
per_image_preds = per_image_preds.to(device ).float()
locs = locs.to(device ).float()
index = index.detach().numpy() [0]
seq_ix = seq_ix.detach().numpy() [0,:]
patient_filt =(df[0].StudyInstanceUID == patients[index])
 patient_df = pd.DataFrame()
for n, i in enumerate(df):
patient_df['SOPInstanceUID'] = i.loc[patient_filt, 'SOPInstanceUID'].values[seq_ix]
patient_df['SeriesInstanceUID'] = i.loc[patient_filt, 'SeriesInstanceUID'].values
patient_df['StudyInstanceUID'] = patients[index]
for c in CFG['image_target_cols']+CFG['exam_target_cols']:
patient_df[c] = 0.0
if n==0:
image_preds, exam_pred = model(per_image_preds, locs)
 else:
a,b = model(per_image_preds, locs)
 image_preds+=a
exam_pred+=b
del a,b
image_preds, exam_pred = torch.true_divide(image_preds, n+1), torch.true_divide(exam_pred, n+1)
 exam_pred = torch.sigmoid(exam_pred ).cpu().detach().numpy()
image_preds = torch.sigmoid(image_preds ).cpu().detach().numpy()
patient_df[CFG['exam_target_cols']] = exam_pred[0]
patient_df[CFG['image_target_cols']] = image_preds[0,:]
res_dfs += [patient_df]
if(( step + 1)% CFG['verbose_step'] == 0)or(( step + 1)== len(dataloader)) :
print(
 f'Inference Step {step+1}/{len(dataloader)}, ' + \
f'time: {(time.time() - t):.4f}', end='\r' if(step + 1)!= len(dataloader)else '
' 
)
 res_dfs = pd.concat(res_dfs, axis=0 ).reset_index(drop=True)
 res_dfs = df[0][['SOPInstanceUID', 'SeriesInstanceUID', 'StudyInstanceUID']].merge(res_dfs, on=['SOPInstanceUID', 'SeriesInstanceUID', 'StudyInstanceUID'], how='left')
 assert res_dfs.shape[0] == df[0].shape[0]
return res_dfs
STAGE1_CFGS = [
{
'tag': 'efb0_stage1',
'model_constructor': RSNAImgClassifierSingle,
'dataset_constructor': RSNADatasetStage1,
'output_len': 1
},
{
'tag': 'efb0_stage1_multilabel',
'model_constructor': RSNAImgClassifier,
'dataset_constructor': RSNADatasetStage1,
'output_len': 9
},
]
STAGE1_CFGS_TAG = 'efb0-stage1-single-multi-label'
def get_stage1_columns() :
new_feats = []
for cfg in STAGE1_CFGS:
for i in range(cfg['output_len']):
f = cfg['tag']+'_'+str(i)
 new_feats += [f]
return new_feats
def update_stage1_test_preds(df):
new_feats = get_stage1_columns()
df.loc[:,new_feats] = 0
df_list=[]
for n, i in enumerate(range(TTA_STEPS)) :
if n==0:
test_ds = RSNADatasetStage1(df, 0.0, CFG['test_img_path'], image_subsampling=False, transforms=get_valid_transforms() , output_label=False, opencv=False)
 else:
test_ds = RSNADatasetStage1(df, 0.0, CFG['test_img_path'], image_subsampling=False, transforms=tta_augmentation() , output_label=False, opencv=True)
 test_loader = torch.utils.data.DataLoader(
 test_ds,
batch_size=256,
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False,
sampler=SequentialSampler(test_ds)
)
 image_preds_all_list = []
models = []
for cfg in STAGE1_CFGS:
device = torch.device(CFG['device'])
 model = cfg['model_constructor']().to(device)
 model.load_state_dict(torch.load('{}/model_{}'.format(CFG['model_path'], cfg['tag'])))
 model.eval()
models.append(model)
 image_preds_all = []
for step, imgs in enumerate(tqdm(test_loader)) :
imgs = imgs.to(device ).float()
for model in models:
image_preds = model(imgs)
 image_preds_all += [image_preds.cpu().detach().numpy() ]
del test_loader
torch.cuda.empty_cache()
image_preds_all_image = np.concatenate(image_preds_all[::2], axis=0)
 image_preds_all_exam = np.concatenate(image_preds_all[1::2], axis=0)
 image_preds_all = np.concatenate([image_preds_all_image, image_preds_all_exam], axis=1)
 print(np.array(new_feats ).shape)
 print(np.array(image_preds_all ).shape)
 temp = df.copy()
temp.loc[:, new_feats] = image_preds_all
df_list.append(temp)
 del temp
return df_list
class RSNAClassifier(nn.Module):
def __init__(self, hidden_size=64):
super().__init__()
self.gru = nn.GRU(len(get_stage1_columns())+1, hidden_size, bidirectional=True, batch_first=True, num_layers=2)
 self.image_predictors = TimeDistributed(nn.Linear(hidden_size*2, 1))
self.exam_predictor = nn.Linear(hidden_size*2*2, 9)
 def forward(self, img_preds, locs):
embeds = torch.cat([img_preds, locs.view(locs.shape[0], locs.shape[1], 1)], dim=2)
 embeds, _ = self.gru(embeds)
 image_preds = self.image_predictors(embeds)
 avg_pool = torch.mean(embeds, 1)
 max_pool, _ = torch.max(embeds, 1)
 conc = torch.cat([avg_pool, max_pool], 1)
 exam_pred = self.exam_predictor(conc)
 return image_preds, exam_pred
if __name__ == '__main__':
seed_everything(SEED)
 if path.exists('.. /input/rsna-str-pulmonary-embolism-detection/train')and not do_full:
test_df = pd.read_csv(CFG['test_path'] ).head(1000)
 else:
test_df = pd.read_csv(CFG['test_path'])
 with torch.no_grad() :
test_df = update_stage1_test_preds(test_df)
 device = torch.device(CFG['device'])
 model = RSNAClassifier().to(device)
 model.load_state_dict(torch.load('{}/model_{}'.format(CFG['model_path'], CFG['tag'])))
 test_pred_df = inference(model, device, test_df, CFG['test_img_path'])
 test_pred_df.to_csv('kh_submission_raw.csv')
 ids = []
labels = []
gp_mean = test_pred_df.loc[:, ['StudyInstanceUID']+CFG['exam_target_cols']].groupby('StudyInstanceUID', sort=False ).mean()
for col in CFG['exam_target_cols']:
ids += [[patient+'_'+col for patient in gp_mean.index]]
labels += [gp_mean[col].values]
ids += [test_pred_df.SOPInstanceUID.values]
labels += [test_pred_df[CFG['image_target_cols']].values[:,0]]
ids = np.concatenate(ids)
 labels = np.concatenate(labels)
 assert len(ids)== len(labels)
 submission = pd.DataFrame()
submission['id'] = ids
submission['label'] = labels
print(submission.head())
 submission.to_csv('submission.csv', index=False )<load_from_zip>
model_struct = 'tf_efficientdet_d7'
def get_valnet_file(Get_Path):
config = get_efficientdet_config(model_struct)
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size = img_size
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
checkpoint = torch.load(Get_Path)
 net.load_state_dict(checkpoint['model_state_dict'])
 del checkpoint
net = DetBenchEval(net, config)
 net.eval() ;
return net.cuda()
def makePseudolabel() :

 use_cuda = True
max_label_numbers = 0
DATA_ROOT_PATH = '.. /input/global-wheat-detection/test'
image_ids = [path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]
model = Darknet(CONFIG)
 model.load_weights(WEIGHTS)
 if use_cuda:
model.cuda()
if Effdet:
net = get_valnet_file(PESUDO_WEIGHTS)
 model.eval()
num_classes = model.num_classes
class_names = 'wheat'
count = 0
pesudo_result = []
for image_id in image_ids:
enboxes = []
enscores = []
try:
img = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg')
 img = cv2.resize(img,(model.width, model.height))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 if Yolov4:
if is_ROT:
for i in range(4):
img1 = TTAImage(img, i)
 img1 = img1.transpose(2, 0, 1)
 img1 = torch.from_numpy(img1 ).float().div(255.0 ).unsqueeze(0)
 img1 = img1.cuda()
img1 = torch.autograd.Variable(img1)
 with torch.no_grad() :
output = model(img1)
 boxes = utils.post_processing(img1, NMS_CONF_THR, NMS_CONF_THR, output)
 det = np.array(boxes)
 if det.size !=0:
boxes, scores = process_det(0, det, NMS_CONF_THR)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, *img.shape[:2])
 enboxes.append(boxes)
 enscores.append(scores)
 with torch.no_grad() :
img1 = img.transpose(2, 0, 1)
 img1 = torch.from_numpy(img1 ).float().div(255.0 ).unsqueeze(0)
 img1 = img1.cuda()
img1 = torch.autograd.Variable(img1)
 output = model(img1)
 boxes = utils.post_processing(img1, NMS_CONF_THR, NMS_CONF_THR, output)
 det = np.array(boxes)
 if det.size !=0:
boxes, scores = process_det(0, det, NMS_CONF_THR)
 enboxes.append(boxes)
 enscores.append(scores)
 if Effdet:
img1 = img.transpose(2, 0, 1)
 img1 = torch.from_numpy(img1 ).cuda()
img1 = img1.float()
img1 /= 255.0
if p_HF:
with torch.no_grad() :
imgs = torch.stack(( img1,)).float().cuda()
for tta_transform in tta_transforms:
det = net(tta_transform.batch_augment(imgs.clone()), torch.tensor([1]*imgs.shape[0] ).float().cuda())
 for i in range(imgs.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > pesudo_thres)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes = tta_transform.deaugment_boxes(boxes.copy())
 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)
 indices = scores >= best_score_threshold
boxes = boxes[indices]
scores = scores[indices]
lineo = ''
for box in boxes:
x, y, x1, y1 = box
lineo += ' %d,%d,%d,%d,0'%(x, y, x1, y1)
 os.makedirs('convertor', exist_ok=True)
 if len(lineo)>0:
with open('convertor/train.txt', 'a')as f:
f.write(f'{image_id}.jpg')
 f.write(lineo)
 f.write('
')
 sh.copy(".. /input/global-wheat-detection/test/{}.jpg".format(image_id),'convertor/{}.jpg'.format(image_id))
result = {
'image_id': image_id,
'box': enboxes,
'score': enscores
}
if boxes.shape[0]>max_label_numbers:
max_label_numbers = boxes.shape[0]
pesudo_result.append(result)
 print(path,image_id)
 except:
boxes = []
scores = []
print('one error')
 return pesudo_result, max_label_numbers
!cp.. /input/gdcm-conda-install/gdcm.tar.
 !tar -xvzf gdcm.tar
!conda install --offline./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2<set_options>
if is_TEST:
if PSEUDO:
remain_times = 60
!python train.py -TRAIN_EPOCHS {EPO} -cfgfile {CONFIG} -op {OPTIMIZER} -dir /kaggle/working/convertor -pretrained {WEIGHTS} -train_label_path convertor/train.txt -val_label_path convertor/val.txt -optimizer radam -iou-type ciou -l 0.0001 -g 0 -classes 1 -maxboxes {max_label_numbers+10} -time_limit {remain_times}
else:
pass
else:
pass
warnings.filterwarnings('ignore')
 pd.set_option('display.max_columns', 100 )<define_variables>
if VALIDATE and is_TEST:
all_predictions = validate()

 for score_threshold in tqdm(np.arange(0.1, 0.25, 0.01), total=np.arange(0.1, 0.25, 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)
col_index = 'SOPInstanceUID'
col_groupby = 'StudyInstanceUID'
df_test_path = ".. /input/rsna-str-pulmonary-embolism-detection/test.csv"
df_train_path = ".. /input/rsna-str-pulmonary-embolism-detection/train.csv"
df_sub_path = ".. /input/rsna-str-pulmonary-embolism-detection/sample_submission.csv"
test_image_path = ".. /input/rsna-str-pulmonary-embolism-detection/test/"
train_image_path = ".. /input/rsna-str-pulmonary-embolism-detection/train/"
LEN_PUBLIC = 146853
DEBUG = False
num_cpu = multiprocessing.cpu_count()
num_features1_1 = 1408
weight_dir1_1 = ".. /input/rsna2020-1/201024_5_b2_1loss_512_fp16_bs80_lr1e3/201024_5_b2_1loss_512_fp16_bs80_lr1e3"
weight_dir2_1 = ".. /input/rsna2020-1/201026_3_2ndNNs_features_pool_flip_b0b2/201026_3_2ndNNs_features_pool_flip_b0b2"
do_full = False
BATCH_SIZE = 64
NUM_FOLD = 5<define_variables>
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_cv2(pesudo_result):

 use_cuda = True
DATA_ROOT_PATH = '.. /input/global-wheat-detection/test'
image_ids = [path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]
weights = 'checkpoints/last.pt'
Effdet = False
if not os.path.exists(weights):
Effdet = True
model = Darknet(CONFIG)
 model.load_weights(WEIGHTS)
 print('OLD WEIGHT')
 else:
model = torch.load(weights)
 if(len(pesudo_result)>0)and(GET_PESUDO==True):
print('get pesudo label')
 if use_cuda:
model.cuda()
if Effdet:
net = get_valnet_file(PESUDO_WEIGHTS)
 model.eval()
num_classes = model.num_classes
class_names = 'wheat'
results = []
fig, ax = plt.subplots(5, 2, figsize=(30, 70))
count = 0
for image_id in image_ids:
enboxes = []
enscores = []
try:
img = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg')
 h, w = img.shape[:2]
img = cv2.resize(img,(model.width, model.height))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 if Yolov4:
if is_ROT:
for i in range(4):
img1 = TTAImage(img, i)
 with torch.no_grad() :
img1 = img1.transpose(2, 0, 1)
 img1 = torch.from_numpy(img1 ).float().unsqueeze(0 ).cuda()
img1 /= 255.0
img1 = torch.autograd.Variable(img1)
 output = model(img1)
 boxes = utils.post_processing(img1, NMS_CONF_THR, NMS_CONF_THR, output)
 det = np.array(boxes)
 boxes, scores = process_det(0, det, NMS_CONF_THR)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, *img.shape[:2])
 enboxes.append(boxes)
 enscores.append(scores)
 with torch.no_grad() :
img1 = img.transpose(2, 0, 1)
 img1 = torch.from_numpy(img1 ).float().unsqueeze(0 ).cuda()
img1 /= 255.0
img1 = torch.autograd.Variable(img1)
 output = model(img1)
 boxes = utils.post_processing(img1, NMS_CONF_THR, NMS_CONF_THR, output)
 det = np.array(boxes)
 boxes, scores = process_det(0, det, NMS_CONF_THR)
 enboxes.append(boxes)
 enscores.append(scores)
 if Effdet:
img1 = img.transpose(2, 0, 1)
 img1 = torch.from_numpy(img1 ).cuda()
img1 = img1.float()
img1 /= 255.0
if p_HF:
with torch.no_grad() :
imgs = torch.stack(( img1,)).float().cuda()
for tta_transform in tta_transforms:
det = net(tta_transform.batch_augment(imgs.clone()), torch.tensor([1]*imgs.shape[0] ).float().cuda())
 for i in range(imgs.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > pesudo_thres)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes = tta_transform.deaugment_boxes(boxes.copy())
 enboxes.append(boxes)
 enscores.append(scores)
 if(len(pesudo_result)>0)and(GET_PESUDO==True):
if next(( inx for(inx,dic)in enumerate(pesudo_result)if dic['image_id']==image_id), None)is not None:
enboxes.extend(pesudo_result[next(( inx for(inx,dic)in enumerate(pesudo_result)if dic['image_id']==image_id), None)]['box'])
 enscores.extend(pesudo_result[next(( inx for(inx,dic)in enumerate(pesudo_result)if dic['image_id']==image_id), None)]['score'])
 boxes, scores, labels = run_wbf(enboxes, enscores, image_size=1024, iou_thr=best_iou_thr, skip_box_thr=best_skip_box_thr)
 boxes =(boxes/1024*h ).astype(np.int32 ).clip(min=0, max=h)
 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)and(is_TEST==False):
img_ = img
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
except:
boxes = []
scores = []
print('one error')
 pass
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result)
 return results
<load_from_csv><EOS> 
results = detect_cv2(pesudo_result)
 test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()