Kaggle competition: Global Wheat Detection
test_dir = ".. /input/cassava-leaf-disease-classification/test_images"<categorify>
%load_ext autoreload
%autoreload 2
def get_preds_model_list_norm_inds(image_dir, model_obj_list, TTA=True, aug_num=5, normalize_indices=None):

 preds = []
img_ids = []
for i in glob.glob(image_dir+"/*.jpg"):
print(i)
 image = Image.open(i)
 image = image.resize(( IMAGE_SIZE, IMAGE_SIZE))
aug_imgs = [seq(image=np.array(image)) *(( 1-g)+g/255)for j in range(aug_num)for g in normalize_indices]
avg_pred = np.concatenate([mod.predict(np.expand_dims(aug_imgs[i], axis = 0)) for i in range(aug_num)for mod in model_obj_list] ).mean(0)
 preds.append(np.argmax(avg_pred))
img_ids.append(i.replace(image_dir+"/", ""))
return(pd.DataFrame({'image_id':img_ids,
'label':preds}))<train_model>
sys.path.insert(0, ".. /input/weightedboxesfusion/")
 %matplotlib inline
gc.enable()
np.set_printoptions(suppress=True)
 print('PyTorch version', torch.__version__)
 SEED = 1120
image_width = 1024
image_height = 1024
batch=64
subdivisions=32
burn_in = 0
prob_threhold = 0.5
iou_thr = 0.55
skip_box_thr = 0.1
model_image_sizes = [1024, 1024, 1024, 1024, 1024]
dataset_folder = ".. /input/global-wheat-detection/"
train_image_folder = ".. /input/global-wheat-detection/train"
test_image_folder = ".. /input/global-wheat-detection/test"
meta_file = b"data/wheat.data"
model_cfg_files = [
b"./yolov4-v3-20000iter-fold2/yolov4-mish-416-wheat-v3-fold2.cfg",
]
model_weights_files = [
b"./yolov4-v3-20000iter-fold2/yolov4-mish-416-wheat-v3-fold2_best.weights",
]
USE_OPTIMIZE = len(os.listdir(test_image_folder)) == 10
if USE_OPTIMIZE:
iteration_offset = 0
pseudo_iterations = 10
else:
iteration_offset = 0
pseudo_iterations = 1000
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 = False
seed_everything(SEED )
def get_preds_model_list(image_dir, model_obj_list, TTA=True, aug_num=5, normalize=True):
preds = []
img_ids = []
for i in glob.glob(image_dir+"/*.jpg"):
print(i)
 image = Image.open(i)
 image = image.resize(( IMAGE_SIZE, IMAGE_SIZE))
if(TTA):
if normalize:
aug_imgs = [seq(image=np.array(image)) /255 for j in range(aug_num)]
else:
aug_imgs = [seq(image=np.array(image)) for j in range(aug_num)]
avg_pred = np.concatenate([mod.predict(np.expand_dims(aug_imgs[i], axis = 0)) for i in range(aug_num)for mod in model_obj_list] ).mean(0)
 else:
image = image/255
avg_pred = np.concatenate([mod.predict(image)for mod in model_obj_list] ).mean(0)
 preds.append(np.argmax(avg_pred))
img_ids.append(i.replace(image_dir+"/", ""))
return(pd.DataFrame({'image_id':img_ids,
'label':preds}))<predict_on_test>
bad_images = [
"41c0123cc",
"a1321ca95",
"2cc75e9f5",
"42e6efaaa",
"409a8490c",
"d067ac2b1",
"d60e832a5",
"893938464",
]
bad_boxes = [
3687, 117344, 173, 113947, 52868, 2159, 2169, 121633, 121634, 147504,
118211, 52727, 147552
]
def get_preds(image_dir, model_obj, normalize=True):
preds = []
img_ids = []
for i in glob.glob(image_dir+"/*.jpg"):
image = Image.open(i)
 image = image.resize(( IMAGE_SIZE, IMAGE_SIZE))
image = np.expand_dims(image, axis = 0)
 if normalize:
image = image/255
preds.append(np.argmax(model_obj.predict(image)))
 img_ids.append(i.replace(image_dir+"/", ""))
return(pd.DataFrame({'image_id':img_ids,
'label':preds}))<save_to_csv>
marking_df = pd.read_csv(f"{dataset_folder}/train.csv")
 marking_df['bbox'] = marking_df.bbox.fillna('[0,0,0,0]')
 bboxs = np.stack(
 marking_df['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))
 for i, column in enumerate(['x', 'y', 'w', 'h']):
marking_df[column] = bboxs[:, i]
marking_df.drop(columns=['bbox'], inplace=True)
 marking_df["id"] = marking_df.index.tolist()
marking_df['x_center'] = marking_df['x'] + marking_df['w'] / 2
marking_df['y_center'] = marking_df['y'] + marking_df['h'] / 2
marking_df['classes'] = 0
predict_df = get_preds_model_list(test_dir, mod_lst, normalize=False, aug_num=5)
 predict_df.to_csv("submission.csv", index=False )<install_modules>
n_folds = 5
skf = StratifiedKFold(n_splits=n_folds, shuffle=True, random_state=SEED)
 skf
pip install --quiet /kaggle/input/kerasapplications<install_modules>
df_folds = dataset_df[['image_id']].copy()
df_folds.loc[:, 'bbox_count'] = 1
df_folds = df_folds.groupby('image_id' ).count()
df_folds.loc[:, 'source'] = dataset_df[['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
for fold_number,(train_index, val_index)in enumerate(
 skf.split(X=df_folds.index, y=df_folds['stratify_group'])) :
df_folds.loc[df_folds.iloc[val_index].index, 'fold'] = fold_number
df_folds = df_folds.reset_index()
pip install --quiet /kaggle/input/efficientnet-git<import_modules>
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 process_detection_result(bboxes, resize_width, prob_threhold=0.5):
scores = np.zeros(( len(bboxes)))
 numpy_bboxes = np.zeros(( len(bboxes), 4))
for i, bbox in enumerate(bboxes):
scores[i] = bbox[1]
cord = bbox[2]
x1 = cord[0] - cord[2] / 2
y1 = cord[1] - cord[3] / 2
x2 = cord[0] + cord[2] / 2
y2 = cord[1] + cord[3] / 2
x1 /= resize_width
y1 /= resize_width
x2 /= resize_width
y2 /= resize_width
x1 *= image_width
y1 *= image_width
x2 *= image_width
y2 *= image_width
x1 = int(round(max(min(x1, image_width - 1), 0)))
 y1 = int(round(max(min(y1, image_width - 1), 0)))
 x2 = int(round(max(min(x2, image_width - 1), 0)))
 y2 = int(round(max(min(y2, image_width - 1), 0)))
 numpy_bboxes[i, :] = np.array([x1, y1, x2, y2])
 indexes = np.where(scores > prob_threhold)
 numpy_bboxes = numpy_bboxes[indexes]
scores = scores[indexes]
return scores, numpy_bboxes
import efficientnet.tfkeras as efn<set_options>
class BaseWheatTTA:

 image_size = image_width
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):
image = np.fliplr(image)
 return image
def batch_augment(self, images):
images = np.fliplr(images)
 return images
def deaugment_boxes(self, boxes):
boxes[:, [0, 2]] = self.image_size - boxes[:, [2, 0]]
return boxes
class TTAVerticalFlip(BaseWheatTTA):

 def augment(self, image):
image = np.flipud(image)
 return image
def batch_augment(self, images):
images = np.flipud(images)
 return images
def deaugment_boxes(self, boxes):
boxes[:, [3, 1]] = self.image_size - boxes[:, [1, 3]]
return boxes
class TTARotate90(BaseWheatTTA):

 def augment(self, image):
image = np.rot90(image, k=1, axes=(0, 1))
return image
def batch_augment(self, images):
images = np.rot90(images, k=1, axes=(1, 2))
return images
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 TTARotate180(BaseWheatTTA):
def augment(self, image):
tmp = np.rot90(image, k=1, axes=(0, 1))
tmp = np.rot90(tmp, k=1, axes=(0, 1))
return tmp
def batch_augment(self, images):
tmp = np.rot90(images, k=1, axes=(1, 2))
tmp = np.rot90(tmp, k=1, axes=(1, 2))
return tmp
def deaugment_boxes(self, boxes):
tmp = TTARotate90().deaugment_boxes(boxes)
 return TTARotate90().deaugment_boxes(tmp)
 class TTARotate270(BaseWheatTTA):
def augment(self, image):
tmp = TTARotate180().augment(image)
 tmp = np.rot90(tmp, k=1, axes=(0, 1))
return tmp
def batch_augment(self, images):
tmp = TTARotate180().batch_augment(images)
 tmp = np.rot90(tmp, k=1, axes=(1, 2))
return tmp
def deaugment_boxes(self, boxes):
tmp = TTARotate180().deaugment_boxes(boxes)
 return TTARotate90().deaugment_boxes(tmp)
 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 )
warnings.simplefilter("ignore")
<train_model>
print('Train images: %d' %len(os.listdir(os.path.join(WORK_DIR, "train_images"))))<load_from_disk>
def run_wbf(preds, image_size=image_width,
iou_thr=0.55, skip_box_thr=0.7, weights=None):
boxes = [(p['boxes'] /(image_size-1)).tolist() for p in preds]
scores = [p['scores'].tolist() for p in preds]
labels = [np.ones(p['scores'].shape[0] ).astype(int ).tolist() for p in preds]
boxes, scores, labels = ensemble_boxes_wbf.weighted_boxes_fusion(
 boxes, scores, labels, weights=weights, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = boxes*(image_size-1)
 return boxes, scores, labels
with open(os.path.join(WORK_DIR, "label_num_to_disease_map.json")) as file:
print(json.dumps(json.loads(file.read()), indent=4))<load_from_csv>
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
]))
train_labels = pd.read_csv(os.path.join(WORK_DIR, "train.csv"))
print(train_labels.head() )<load_from_csv>
def detect(net, meta, darknet_image, target_image, prob_threshold=.5):
dn.copy_image_from_bytes(darknet_image, target_image.tobytes())
 return dn.detect_image(net,
meta,
darknet_image,
thresh=prob_threshold,
hier_thresh=.5,
nms=.45)
 def predict_test(batch_size=1, prob_threhold=0.5):
image_names = os.listdir(test_image_folder)
 image_pred_results = collections.defaultdict(list)
 for model_index, cfg in enumerate(model_cfg_files):
print(f"Generating inference for model cfg file {cfg}...... ")
 weights = model_weights_files[model_index]
net = dn.load_net_custom(cfg, weights, 0, batch_size)
 meta = dn.load_meta(meta_file)
 resize = model_image_sizes[model_index]
darknet_image = dn.make_image(resize, resize, 3)
 for name in image_names:
image_id = name.split('.')[0]
image = cv2.imread(f'{test_image_folder}/{image_id}.jpg')
 image = cv2.resize(image,(resize, resize))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 infer_start = time.time()
for tta in tta_transforms:
tta_image = tta.augment(image)
 preds = detect(net, meta, darknet_image, tta_image)
 scores, preds = process_detection_result(
 preds, resize, prob_threhold)
 preds = tta.deaugment_boxes(preds)
 preds = preds.clip(min=0, max=image_width - 1)
 image_pred_results[name].append({
'boxes': preds,
'scores': scores,
})
 print(
 f"Time spent on augmented inference for {name}: {time.time() - infer_start:2f} seconds" 
)
 del image, preds
gc.collect()
dn.free_network_ptr(net)
 del net, meta
gc.collect()
return image_pred_results
def ensemble(image_pred_results, iou_thr=0.5, skip_box_thr=0.1):
ensemble_results = {}
image_names = os.listdir(test_image_folder)
 for name in image_names:
print(f"Running ensemble by WBF for {name}...... ")
 image_id = name.split('.')[0]
tta_preds = image_pred_results[name]
boxes, scores, labels = run_wbf(tta_preds,
iou_thr=iou_thr,
skip_box_thr=skip_box_thr)
 boxes = boxes.round().astype(np.int32 ).clip(min=0, max=image_width - 1)
 boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
ensemble_results[name] = {
'boxes': boxes,
'scores': scores,
}
return ensemble_results
def generate_submit(ensemble_results):
results = []
count = 0
image_names = os.listdir(test_image_folder)
 for name in image_names:
image_id = name.split('.')[0]
if len(image_names)< 11:
image = cv2.imread(f'{test_image_folder}/{image_id}.jpg')
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 r = ensemble_results[name]
boxes, scores = r["boxes"], r["scores"]
results.append({
'image_id':
image_id,
'PredictionString':
format_prediction_string(boxes, scores)
 })
 if len(image_names)< 11 and count < 10:
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
for box, score in zip(boxes, scores):
cv2.rectangle(image,(box[0], box[1], box[2], box[3]),
color=(99, 228, 255),
thickness=4)
 cv2.putText(
 image,
f"{score:.2f}", 
(box[0], box[1] + box[3] - 5),
cv2.FONT_HERSHEY_SIMPLEX,
0.8, 
(255, 255, 255),
2,
cv2.LINE_AA)
 ax.imshow(image)
 count += 1
return results
data_normall = pd.read_csv(".. /input/anomaly005/normall_all.csv")
 data_normall<load_from_csv>
pred_start = time.time()
test_preds = predict_test(batch_size=1, prob_threhold=prob_threhold)
 print(
 f"Time spent on generating submission predictions: {time.time() - pred_start:2f} seconds"
)
data_anomaly = pd.read_csv(".. /input/anomaly005/anomaly_all.csv")
 data_anomaly<save_to_csv>
pred_start = time.time()
ensemble_results = ensemble(test_preds,
iou_thr=iou_thr,
skip_box_thr=skip_box_thr)
 print(f"Time spent on ensemble: {time.time() - pred_start:2f} seconds" )
train,val25 = train_test_split(data_normall, test_size=0.25, random_state=0 , shuffle=True)
 train.to_csv('train.csv',index=False)
 val25.to_csv('val25.csv',index=False )<save_to_csv>
with open("./data/wheat.names", 'w')as out:
out.write("wheat
" )
a = pd.read_csv(".. /input/anomaly005/anomaly_all.csv")
 b = pd.read_csv("./val25.csv")
 merged =pd.concat([a,b])
 merged.to_csv('./val30.csv', index=False )<load_from_csv>
!cat./data/wheat.names
data_test =pd.read_csv("./train.csv")
 data_test<load_from_csv>
with open(f"./data/wheat_pseudo.data", 'w')as out:
out.write("classes= 1
")
 out.write(f"train = data/train_wheat_pseudo.txt
")
 out.write(f"valid = data/valid_wheat_pseudo.txt
")
 out.write("names = data/wheat.names
")
 out.write("backup = backup" )
data_val = pd.read_csv("./val30.csv")
 data_val<define_variables>
!cat./data/wheat_pseudo.data
BATCH_SIZE = 4
STEPS_PER_EPOCH = len(data_test)/ BATCH_SIZE
VALIDATION_STEPS = len(data_val)/ BATCH_SIZE
EPOCHS = 10
TARGET_SIZE = 512<data_type_conversions>
fold_id = 0
fold_train_df, fold_val_df = df_folds[~(df_folds["fold"] == fold_id)].copy(
), df_folds[df_folds["fold"] == fold_id].copy()
assert fold_train_df.shape[0] + fold_val_df.shape[0] == df_folds.shape[0]
fold_train_df.shape[0], fold_val_df.shape[0]
data_test.label = data_test.label.astype('str')
 train_datagen = ImageDataGenerator(
 preprocessing_function = None,
rotation_range = 45,
zoom_range = 0.2,
horizontal_flip = True,
vertical_flip = True,
fill_mode = 'nearest',
shear_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1,
featurewise_center = True,
featurewise_std_normalization = True)
 train_generator = train_datagen.flow_from_dataframe(data_test,
directory = os.path.join(WORK_DIR, "train_images"),
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,shuffle=True,
class_mode = "sparse" )<prepare_x_and_y>
with open(f"./data/valid_wheat_pseudo.txt", 'w')as out:
image_ids_with_head = fold_val_df["image_id"].unique()
for image_id in image_ids_with_head:
sh.copy(f"{train_image_folder}/{image_id}.jpg",
f"./data/wheat/{image_id}.jpg")
 out.write(f"./data/wheat/{image_id}.jpg
" )
data_val.label = data_val.label.astype('str')
 validation_datagen = ImageDataGenerator(rescale=1./255)
 validation_generator = train_datagen.flow_from_dataframe(data_val,
directory = os.path.join(WORK_DIR, "train_images"),
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,shuffle=True,
class_mode = "sparse" )<choose_model_class>
for image_id in df_folds["image_id"].unique() :
df = dataset_df[dataset_df["image_id"] == image_id].copy()
with open(f"./data/wheat/{image_id}.txt", 'w')as out:
for index, row in df.iterrows() :
if row['x_center'] > 0 and row[
'y_center'] and row['w'] > 0 and row['h'] > 0:
out.write(f"{row['classes']} " +
f"{row['x_center']/image_width} " +
f"{row['y_center']/image_height} " +
f"{row['w']/image_width} " +
f"{row['h']/image_height}
" )
def create_model() :
model = models.Sequential()
model.add(efn.EfficientNetB4(include_top = False, weights = ".. /input/efficientnet-keras-noisystudent-weights-b0b7/efficientnet-b4_noisy-student_notop.h5",
input_shape =(TARGET_SIZE, TARGET_SIZE, 3)))
 model.add(layers.GlobalAveragePooling2D())
 model.add(Dense(1024, activation="relu"))
model.add(Dense(512, activation="relu"))
model.add(Dropout(0.25))
model.add(Dense(5, activation="softmax"))
model.compile(optimizer = Adam(lr = 0.001),
loss = "sparse_categorical_crossentropy",
metrics = ["sparse_categorical_accuracy"])
 return model
model = create_model()
model.summary()<define_variables>
for name in os.listdir(test_image_folder):
image_id = name.split('.')[0]
with open(f"./data/wheat/{image_id}.txt", 'w')as out:
r = ensemble_results[name]
boxes, scores = r["boxes"], r["scores"]
boxes[:, 0] = boxes[:, 0] + boxes[:, 2] / 2
boxes[:, 1] = boxes[:, 1] + boxes[:, 3] / 2
print(boxes.shape)
 for i in range(boxes.shape[0]):
box = boxes[i, :]
if box[0] > 0 and box[1] > 0 and box[2] > 0 and box[3] > 0:
out.write(f"0 " + f"{box[0]/image_width} " +
f"{box[1]/image_height} " +
f"{box[2]/image_width} " +
f"{box[3]/image_height}
" )
print('Our EfficientNet CNN has %d layers' %len(model.layers))<choose_model_class>
model_save = ModelCheckpoint('./EffNetB_best_weights.h5',
save_best_only = True,
save_weights_only = True,
monitor = 'val_loss',
mode = 'min', verbose = 1)
 early_stop = EarlyStopping(monitor = 'val_loss', min_delta = 0.001,
patience = 5, mode = 'min', verbose = 1,
restore_best_weights = True)
 reduce_lr = ReduceLROnPlateau(monitor = 'val_loss', factor = 0.3,
patience = 2, min_delta = 0.001,
mode = 'min', verbose = 1 )<train_model>
history = model.fit(
 train_generator,
steps_per_epoch = STEPS_PER_EPOCH,
epochs = EPOCHS,
validation_data = validation_generator,
validation_steps = VALIDATION_STEPS,
)<save_to_csv>
ss = pd.read_csv(os.path.join(WORK_DIR, "sample_submission.csv"))
ss
preds = []
for image_id in ss.image_id:
image = Image.open(os.path.join(WORK_DIR, "test_images", image_id))
image = image.resize(( TARGET_SIZE, TARGET_SIZE))
image = np.expand_dims(image, axis = 0)
 preds.append(np.argmax(model.predict(image)))
 ss['label'] = preds
ss
ss.to_csv('submission.csv', index = False )<save_model>
cfg_content = f
model.save('outliersModel4_12.h5' )<load_pretrained>
model.save_weights('outliersWeight4_12.h5' )<import_modules>
def predict_new_test(batch_size=1, prob_threhold=0.5):
image_names = os.listdir(test_image_folder)
 image_pred_results = collections.defaultdict(list)
 cfg = b"./yolov4-mish-416-wheat-v3-pseudo.cfg"
weights = b"./backup/yolov4-mish-416-wheat-v3-pseudo_best.weights"
resize = 618
print(f"Generating inference for model cfg file {cfg}...... ")
 net = dn.load_net_custom(cfg, weights, 0, batch_size)
 meta = dn.load_meta(meta_file)
 darknet_image = dn.make_image(resize, resize, 3)
 for name in image_names:
image_id = name.split('.')[0]
image = cv2.imread(f'{test_image_folder}/{image_id}.jpg')
 image = cv2.resize(image,(resize, resize))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 infer_start = time.time()
for tta in tta_transforms:
tta_image = tta.augment(image)
 preds = detect(net, meta, darknet_image, tta_image)
 scores, preds = process_detection_result(
 preds, resize, prob_threhold)
 preds = tta.deaugment_boxes(preds)
 preds = preds.clip(min=0, max=image_width - 1)
 image_pred_results[name].append({
'boxes': preds,
'scores': scores,
})
 print(
 f"Time spent on augmented inference for {name}: {time.time() - infer_start:2f} seconds" 
)
 del image, preds
gc.collect()
dn.free_network_ptr(net)
 del net, meta
gc.collect()
return image_pred_results
!nvidia-smi<install_modules>
pred_start = time.time()
new_test_preds = predict_new_test(batch_size=1, prob_threhold=prob_threhold)
 print(
 f"Time spent on generating submission predictions: {time.time() - pred_start:2f} seconds"
)
!pip install.. /input/timmmodels/dist/timm-0.3.4.tar
!pip install.. /input/pretrainedmodels/pretrainedmodels-0.7.4
if not os.path.exists('/root/.cache/torch/hub/checkpoints/'):
os.makedirs('/root/.cache/torch/hub/checkpoints/')
 !cp.. /input/timm-pretrained-efficientnet/efficientnet/tf_efficientnet_b4_ns-d6313a46.pth /root/.cache/torch/hub/checkpoints/<import_modules>
pred_start = time.time()
new_ensemble_results = ensemble(new_test_preds,
iou_thr=iou_thr,
skip_box_thr=skip_box_thr)
 print(f"Time spent on ensemble: {time.time() - pred_start:2f} seconds" )
import cv2
import copy
import time
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim import lr_scheduler
import torchvision
from torchvision import models
from torch.utils.data import DataLoader, Dataset
from torch.cuda import amp
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.metrics import accuracy_score
from sklearn.utils import class_weight
from tqdm.notebook import tqdm
from collections import defaultdict
import albumentations as A
from albumentations.pytorch import ToTensorV2
import timm
import pretrainedmodels<define_variables>
pred_start = time.time()
final_results = generate_submit(new_ensemble_results)
 final_results[:2]
ROOT_DIR = ".. /input/cassava-leaf-disease-classification"
TRAIN_DIR = ".. /input/cassava-leaf-disease-classification/train_images"
TEST_DIR = ".. /input/cassava-leaf-disease-classification/test_images"<init_hyperparams>
test_df = pd.DataFrame(final_results, columns=['image_id', 'PredictionString'])
 test_df.head()
test_df.to_csv('submission.csv', index=False )
class CFG:
model_name = 'tf_efficientnet_b4_ns'
img_size = 512
scheduler = 'CosineAnnealingWarmRestarts'
T_max = 10
T_0 = 10
lr = 1e-4
min_lr = 1e-6
batch_size = 16
weight_decay = 1e-6
seed = 42
num_classes = 5
num_epochs = 14
n_fold = 5
smoothing = 0.2
t1 = 0.6
t2 = 1.6
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 savemodel = True
savemodelpath = '/kaggle/working/bitemp-32.pth'
dosubmission = True
crop_size = 512
crop_p = 1.0<set_options>
!rm -rf __pycache__ darknet* data libdarknet.so yolov4-* *.jpg