Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<set_options>
sns.set()
py.init_notebook_mode(connected=True)
 os.listdir(".. /input/" )
%matplotlib inline<set_options>
basepath = ".. /input/siim-isic-melanoma-classification/"
modelspath = ".. /input/pytorch-pretrained-image-models/"
imagestatspath = ".. /input/siimisic-melanoma-classification-image-stats/"
device = torch.device('cuda:0' )<load_from_csv>
train_info = pd.read_csv(basepath + "train.csv")
 train_info.head()
class FacialKeypoiuntsTrainDataset(Dataset):

 def __init__(self, data_file, transform=None):
self.transform = transform
dataset = pd.read_csv(data_file)
 dataset.fillna(method='ffill', inplace=True)
 self.images = dataset['Image']
dataset = dataset.drop(['Image'], axis=1)
 self.positions_name = list(dataset.columns)
 self.positions = dataset.to_numpy()
def __len__(self):
return len(self.positions)
 def __getitem__(self, index):

 x = self.images[index]
x = np.array([float(x)for x in x.split(' ')])
 x = x.reshape(( 96, 96))
img = np.stack(( x, x, x), axis=-1)/ 255.
 y = self.positions[index]
sample = {'img': img, 'landmarks': y}
if self.transform:
sample = self.transform(sample)
 return sample
def show_samples(self, indices, title=None, count=10):

 plt.figure(figsize=(count*3, 3))
display_indices = indices[:count]
if title:
plt.suptitle(title)
 for i, index in enumerate(display_indices):
sample = self.__getitem__(index)
 x, y = sample['img'], sample['landmarks']
x = np.asarray(x)
 y = np.asarray(y)
 y = y.reshape(( y.size // 2, 2))
plt.subplot(1, count, i + 1)
 if self.transform:
plt.imshow(np.transpose(x,(1, 2, 0)))
 else:
plt.imshow(x)
 plt.scatter(y[:, 0], y[:, 1], s=15, marker='.', c='r')
 plt.grid(False)
 plt.axis('off' )<load_from_csv>
test_info = pd.read_csv(basepath + "test.csv")
 test_info.head()
class FacialKeypoiuntsTestDataset(Dataset):

 def __init__(self, data_file):
self.dataset = pd.read_csv(data_file)
 def __len__(self):
return len(self.dataset)
 def __getitem__(self, index):

 data_sr = self.dataset.iloc[index]
img_id = int(data_sr['ImageId'])
 x = data_sr['Image']
x = np.array([float(x)for x in x.split(' ')])
 x = x.reshape(( 96, 96))
img = np.stack(( x, x, x), axis=-1)/ 255.
 img = np.transpose(img,(2, 0, 1)).copy()
img_t = torch.from_numpy(img ).type(torch.FloatTensor)
 return img_t, img_id
def show_samples(self, indices, title=None, count=10):

 plt.figure(figsize=(count*3, 3))
display_indices = indices[:count]
if title:
plt.suptitle(title)
 for i, index in enumerate(display_indices):
x, y = self.__getitem__(index)
 x = np.asarray(x)
 plt.subplot(1, count, i + 1)
 plt.imshow(np.transpose(x,(1, 2, 0)))
 plt.title(f'Image Id {y}')
 plt.grid(False)
 plt.axis('off' )<load_from_csv>
missing_vals_train = train_info.isnull().sum() / train_info.shape[0]
missing_vals_train[missing_vals_train > 0].sort_values(ascending=False )
train_dataset = FacialKeypoiuntsTrainDataset('./training.csv')
 test_dataset = FacialKeypoiuntsTestDataset('./test.csv')
 train_plot_indices = np.random.choice(len(train_dataset), 10)
 test_plot_indices = np.random.choice(len(test_dataset), 10)
 train_dataset.show_samples(
 train_plot_indices, title='Samples from Facial Keypoints Train Dataset', count=7)
 test_dataset.show_samples(
 test_plot_indices, title='Samples from Facial Keypoints Test Dataset', count=7 )<randomize_order>
missing_vals_test = test_info.isnull().sum() / test_info.shape[0]
missing_vals_test[missing_vals_test > 0].sort_values(ascending=False )
batch_size = 32
data_size = len(train_dataset)
 validation_fraction =.2
val_split_size = int(np.floor(validation_fraction * data_size))
indeces = list(range(data_size))
np.random.shuffle(indeces)
 val_indeces, train_indeces = indeces[:val_split_size], indeces[val_split_size:]
test_indeces = list(range(len(test_dataset)))
 print(f'train size = {len(train_indeces)}, validation size = {len(val_indeces)}')
 print(f'test size = {len(test_indeces)}')
 train_sampler = SubsetRandomSampler(train_indeces)
 val_sampler = SubsetRandomSampler(val_indeces)
 test_sampler = SubsetRandomSampler(test_indeces)
 train_loader = torch.utils.data.DataLoader(train_dataset,
batch_size=batch_size,
sampler=train_sampler)
 val_loader = torch.utils.data.DataLoader(train_dataset,
batch_size=batch_size,
sampler=val_sampler)
 test_loader = torch.utils.data.DataLoader(test_dataset,
batch_size=batch_size,
sampler=test_sampler )<choose_model_class>
train_info.image_name.value_counts().max()
nn_model = models.resnet18(pretrained=True)
 nn_model.fc = nn.Linear(nn_model.fc.in_features, 30)
 nn_model = nn_model.type(torch.FloatTensor)
 nn_model = nn_model.to(device)
 *old_params, new_params = nn_model.parameters()<train_model>
test_info.image_name.value_counts().max()
def train_model(model, train_loader, val_loader, loss, optimizer, scheduler, num_epoch, plot_epoch):

 loss_history = []
train_rmse_history = []
val_rmse_history = []
best_model = None
best_val_rmse = None
indices = np.random.choice(batch_size, 10)
 for epoch in range(num_epoch):
print(f'Epoch {epoch + 1:2d} / {num_epoch:2d}', end=' ')
 model.train()
average_loss = 0
average_rmse = 0
for i_step, sample in enumerate(train_loader):
x, y = sample['img'], sample['landmarks']
x_gpu = x.to(device)
 y_gpu = y.to(device)
 prediction = model(x_gpu)
 loss_value = loss(prediction, y_gpu)
 optimizer.zero_grad()
loss_value.backward()
optimizer.step()
average_loss += loss_value.item()
rmse = mean_squared_error(
 y,
prediction.cpu().detach().numpy() ,
squared=False)
 average_rmse += rmse
average_loss = average_loss / i_step
train_rmse = average_rmse / i_step
val_rmse = compute_rmse(model, val_loader)
 loss_history.append(average_loss)
 train_rmse_history.append(train_rmse)
 val_rmse_history.append(val_rmse)
 print(f'Loss = {average_loss:.4f}, Train RMSE = {train_rmse:.4f}, Val RMSE = {val_rmse:.4f}')
 if best_val_rmse is None:
best_val_rmse = val_rmse
best_model = copy.deepcopy(model)
 if val_rmse < best_val_rmse:
best_val_rmse = val_rmse
best_model = copy.deepcopy(model)
 scheduler.step()
if(( epoch + 1)% plot_epoch)== 0:
plot_results(model, val_loader, indices, title=f'Examples for epoch {epoch + 1}')
 print(f' Best val RMSE = {best_val_rmse:.4f}')
 return loss_history, train_rmse_history, val_rmse_history, best_val_rmse, best_model
def compute_rmse(model, val_loader):

 average_rmse = 0
model.eval()
for i_step, sample in enumerate(val_loader):
x, y = sample['img'], sample['landmarks']
x_gpu = x.to(device)
 with torch.no_grad() :
prediction = model(x_gpu)
 rmse = mean_squared_error(
 y,
prediction.cpu().detach().numpy() ,
squared=False)
 average_rmse += rmse
val_rmse = float(average_rmse)/ i_step
return val_rmse
def plot_results(model, val_loader, indices, title=None, count=10):

 plt.figure(figsize=(count*3, 3))
display_indices = indices[:count]
if title:
plt.suptitle(title)
 model.eval()
for i_step, samples in enumerate(val_loader):
x_gpu, _ = samples['img'], samples['landmarks']
x_gpu = x_gpu.to(device)
 with torch.no_grad() :
prediction = model(x_gpu)
 ys = prediction.cpu().detach().numpy()
xs = x_gpu.cpu().detach().numpy()
for i, index in enumerate(display_indices):
x = xs[index]
y = ys[index]
y = y.reshape(( y.size // 2, 2))
plt.subplot(1, count, i + 1)
 if x.shape[0] == 3:
plt.imshow(np.transpose(x,(1, 2, 0)))
 else:
plt.imshow(x)
 plt.scatter(y[:, 0], y[:, 1], s=15, marker='.', c='r')
 plt.grid(False)
 plt.axis('off')
 plt.show()
break<train_model>
train_info.patient_id.value_counts().max()
loss = nn.MSELoss().to(device)
 optimizer = optim.Adam([
{'params': old_params, 'lr': 0.0001},
{'params': new_params}
], lr=0.001, weight_decay=0.01)
 scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=1.) 
(loss_history, train_rmse_history, val_rmse_history,
best_val_rmse, best_model)= train_model(nn_model, train_loader, val_loader, loss,
optimizer, scheduler, num_epoch=50, plot_epoch=3 )<load_from_csv>
test_info.patient_id.value_counts().max()
def test_dataset_prediction(test_loader, submissions_data_file,
lookup_table_data_file, positions_name, model):

 submission = pd.read_csv(submissions_data_file)
 lookup_table = pd.read_csv(lookup_table_data_file)
 model.eval()
for i_step,(x, img_id)in enumerate(test_loader):
x_gpu = x.to(device)
 with torch.no_grad() :
prediction = model(x_gpu)
 prediction = np.asarray(prediction.cpu())
 for b_id in range(prediction.shape[0]):
for p_id in range(len(positions_name)) :
p_name = positions_name[p_id]
location = prediction[b_id, p_id]
row_id = lookup_table.loc[(
(lookup_table['FeatureName'] == p_name)& 
(lookup_table['ImageId'] == img_id[b_id].item())
)]['RowId'].to_numpy()
if row_id.size == 1:
submission['Location'][int(row_id[0])- 1] = location
return submission<save_to_csv>
200/test_info.shape[0] * 100
test_submissions = test_dataset_prediction(
 test_loader, '.. /input/facial-keypoints-detection/SampleSubmission.csv',
'.. /input/facial-keypoints-detection/IdLookupTable.csv',
train_dataset.positions_name, best_model)
 test_submissions.to_csv('./submission.csv', index=False )<load_from_csv>
train_info.groupby("benign_malignant" ).target.nunique()
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 X_train =(train.iloc[:,1:].values ).astype('float32')
 y_train = train.iloc[:,0].values.astype('int32')
 X_test = test.values.astype('float32')
 X_train_scaled = X_train.reshape(X_train.shape[0], 28, 28,1)/ 255
X_test_scaled = X_test.reshape(X_test.shape[0], 28, 28,1)/ 255
y_train = to_categorical(y_train)
 X_train_mean = train.iloc[:, 1:].sum().sum() /(train.shape[0] * train.shape[1])
 X_test_mean = test.iloc[:, 1:].sum().sum() /(test.shape[0] * test.shape[1])
 X_train_normalized =(( X_train - X_train_mean)/ X_train_mean ).reshape(train.shape[0], 28, 28,1)
 X_test_normalized =(( X_test - X_test_mean)/ X_test_mean ).reshape(test.shape[0], 28, 28,1 )<predict_on_test>
patient_ages_table_train = train_info.groupby(["patient_id", "age_approx"] ).size() / train_info.groupby("patient_id" ).size()
patient_ages_table_train = patient_ages_table_train.unstack().transpose()
patient_ages_table_test = test_info.groupby(["patient_id", "age_approx"] ).size() / test_info.groupby("patient_id" ).size()
patient_ages_table_test = patient_ages_table_test.unstack().transpose()
patient_with_known_ages_train = train_info[train_info.patient_id.isin(patient_ages_table_train.columns.values)]
sorted_patients_train = patient_with_known_ages_train.patient_id.value_counts().index.values
patient_with_known_ages_test = test_info[test_info.patient_id.isin(patient_ages_table_test.columns.values)]
sorted_patients_test = patient_with_known_ages_test.patient_id.value_counts().index.values
fig, ax = plt.subplots(2,1, figsize=(20,20))
sns.heatmap(patient_ages_table_train[sorted_patients_train], cmap="Reds", ax=ax[0], cbar=False);
ax[0].set_title("Image coverage in % per patient and age in train data");
sns.heatmap(patient_ages_table_test[sorted_patients_test], cmap="Blues", ax=ax[1], cbar=False);
ax[1].set_title("Image coverage in % per patient and age in test data");
ax[0].set_xlabel("")
 ax[1].set_xlabel("");
def format_predictions(model, test_data=X_test_scaled):

 preds = model.predict(test_data)
 preds_test = []
for i in preds:
preds_test.append(np.argmax(i))
return preds_test
early_stop = EarlyStopping(monitor='val_loss',
patience=20,
mode='min',
restore_best_weights=True)
 def scheduler(epoch, lr):
if epoch < 20:
return lr
return lr *(0.9 **(( epoch - 19)// 10))
rate_scheduler = LearningRateScheduler(scheduler )<categorify>
patient_gender_train = train_info.groupby("patient_id" ).sex.unique().apply(lambda l: l[0])
 patient_gender_test = test_info.groupby("patient_id" ).sex.unique().apply(lambda l: l[0])
 train_patients = pd.DataFrame(index=patient_gender_train.index.values, data=patient_gender_train.values, columns=["sex"])
 test_patients = pd.DataFrame(index=patient_gender_test.index.values, data=patient_gender_test.values, columns=["sex"])
 train_patients.loc[:, "num_images"] = train_info.groupby("patient_id" ).size()
test_patients.loc[:, "num_images"] = test_info.groupby("patient_id" ).size()
train_patients.loc[:, "min_age"] = train_info.groupby("patient_id" ).age_approx.min()
train_patients.loc[:, "max_age"] = train_info.groupby("patient_id" ).age_approx.max()
test_patients.loc[:, "min_age"] = test_info.groupby("patient_id" ).age_approx.min()
test_patients.loc[:, "max_age"] = test_info.groupby("patient_id" ).age_approx.max()
train_patients.loc[:, "age_span"] = train_patients["max_age"] - train_patients["min_age"]
test_patients.loc[:, "age_span"] = test_patients["max_age"] - test_patients["min_age"]
train_patients.loc[:, "benign_cases"] = train_info.groupby(["patient_id", "benign_malignant"] ).size().loc[:, "benign"]
train_patients.loc[:, "malignant_cases"] = train_info.groupby(["patient_id", "benign_malignant"] ).size().loc[:, "malignant"]
train_patients["min_age_malignant"] = train_info.groupby(["patient_id", "benign_malignant"] ).age_approx.min().loc[:, "malignant"]
train_patients["max_age_malignant"] = train_info.groupby(["patient_id", "benign_malignant"] ).age_approx.max().loc[:, "malignant"]
def conv_block1(In, std):
out = SeparableConv2D(16,(3, 3), kernel_initializer=TruncatedNormal(0, std, 1))(In)
 out = BatchNormalization()(out)
 out = SeparableConv2D(16,(1, 1), kernel_initializer=TruncatedNormal(0, std-1e-5, 1))(out)
 return out
def conv_block2(In, std):
out = SeparableConv2D(16,(3, 1), kernel_initializer=TruncatedNormal(0, std, 1), padding='same' )(In)
 out = SeparableConv2D(16,(1, 3), kernel_initializer=TruncatedNormal(0, std+1e-4, 1), padding='same' )(out)
 out = MaxPooling2D(2 )(out)
 return out
def conv_block3(In, std):
out = SeparableConv2D(16,(5, 3), kernel_initializer=TruncatedNormal(0, std, 1), padding='same' )(In)
 out = BatchNormalization()(out)
 out = SeparableConv2D(16,(3, 5), kernel_initializer=TruncatedNormal(0, std+1e-4, 1))(out)
 out = MaxPooling2D(2 )(out)
 return out<categorify>
train_patients.sort_values(by="malignant_cases", ascending=False ).head()
scaled_input = tf.keras.Input(shape=(28,28,1))
norm_in = BatchNormalization(name='norm_in' )(scaled_input)
 out1 = []
for i in range(0, 8):
out1.append(conv_block1(norm_in, 1e3/10**(i)))
 out2 = []
for i in range(0, 8, 2):
out2.append(Add()([out1[i], out1[i+1]]))
for i in range(0, len(out2)) :
out2[i] = conv_block2(out2[i], 1e4/10**(i))
out2[i] = conv_block3(out2[i], 10/10**(i // 2))
flats = Concatenate()([Flatten()(out)for out in out2])
 d1 = Dense(1024,
kernel_initializer=TruncatedNormal(mean=10, stddev=2, seed=11),
bias_initializer=TruncatedNormal(mean=0, stddev=0.5, seed=11),
kernel_regularizer=l1(0.01)
 )(flats)
 d1 = BatchNormalization()(d1)
 d2 = Dense(1024,
kernel_initializer=TruncatedNormal(mean=1, stddev=0.02, seed=11),
bias_initializer=TruncatedNormal(mean=0, stddev=0.5, seed=11),
kernel_regularizer=l1(0.01)
 )(d1)
 d2 = BatchNormalization()(d2)
 final_prediction = Dense(10, activation='softmax' )(d2)
 model = tf.keras.Model(inputs=[scaled_input], outputs=[final_prediction] )<find_best_params>
example_files = os.listdir(basepath + "train/")[0:2]
example_files
model.count_params() / 1e6<choose_model_class>
train_info["dcm_path"] = basepath + "train/" + train_info.image_name + ".dcm"
test_info["dcm_path"] = basepath + "test/" + test_info.image_name + ".dcm"
model.compile(optimizer=Adam(0.0075),
loss=CategoricalCrossentropy() ,
metrics=['accuracy'] )<train_model>
example_dcm = pydicom.dcmread(train_info.dcm_path[2])
 example_dcm
def scheduler(epoch, lr):
if epoch < 20:
return lr
elif lr > 5e-5:
return lr *(0.95 **(epoch // 10 - 1))
return lr
hist = model.fit(X_train_scaled, y_train, epochs= 2000, batch_size=128,
callbacks=[
LearningRateScheduler(scheduler),
tfdocs.modeling.EpochDots() ,
ReduceLROnPlateau(
 monitor='val_accuracy', factor=0.6, patience=20,
verbose=1, mode='auto', min_delta=1e-4, cooldown=0, min_lr=1e-15 
)
 ],
verbose=True,
validation_split=0.33,
shuffle=True )<save_to_csv>
train_info["image_path"] = basepath + "jpeg/train/" + train_info.image_name + ".jpg"
test_info["image_path"] = basepath + "jpeg/test/" + test_info.image_name + ".jpg"
submission = pd.DataFrame({
"ImageId": [i+1 for i in range(0, 28000)],
"Label": format_predictions(model)
 })
 submission.to_csv('s.csv', index=False )<choose_model_class>
test_image_stats = pd.read_csv(imagestatspath + "test_image_stats.csv")
 test_image_stats.head(1 )
try :
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)
 except:
print('No TPU being used!')
<choose_model_class>
train_image_stats_1 = pd.read_csv(imagestatspath + "train_image_stats_10000.csv")
 train_image_stats_2 = pd.read_csv(imagestatspath + "train_image_stats_20000.csv")
 train_image_stats_3 = pd.read_csv(imagestatspath + "train_image_stats_toend.csv")
 train_image_stats_4 = train_image_stats_1.append(train_image_stats_2)
 train_image_stats = train_image_stats_4.append(train_image_stats_3)
 train_image_stats.shape
<create_dataframe>
plot_test = True
<choose_model_class>
test_image_stats.groupby(["rows", "columns"] ).size().sort_values(ascending=False ).iloc[0:10] / test_image_stats.shape[0]
<create_dataframe>
train_image_stats.groupby(["rows", "columns"] ).size().sort_values(ascending=False ).iloc[0:10] / train_image_stats.shape[0]
<create_dataframe>
examples1 = {"rows": 1080, "columns": 1920}
examples2 = {"rows": 4000, "columns": 6000}
<install_modules>
train = pd.read_csv('.. /input/siim-isic-melanoma-classification/train.csv')
 print('Examples WITH Melanoma')
 imgs = train.loc[train.target==1].sample(10 ).image_name.values
plt.figure(figsize=(20,8))
for i,k in enumerate(imgs):
img = cv2.imread('.. /input/jpeg-melanoma-128x128/train/%s.jpg'%k)
 img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
 plt.subplot(2,5,i+1); plt.axis('off')
 plt.imshow(img)
 plt.show()
print('Examples WITHOUT Melanoma')
 imgs = train.loc[train.target==0].sample(10 ).image_name.values
plt.figure(figsize=(20,8))
for i,k in enumerate(imgs):
img = cv2.imread('.. /input/jpeg-melanoma-128x128/train/%s.jpg'%k)
 img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
 plt.subplot(2,5,i+1); plt.axis('off')
 plt.imshow(img)
 plt.show()
!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<feature_engineering>
!pip install -q efficientnet >> /dev/null
sys.path.insert(0, ".. /input/timm-efficientdet-pytorch")
<set_options>
import pandas as pd, numpy as np
from kaggle_datasets import KaggleDatasets
import tensorflow as tf, re, math
import tensorflow.keras.backend as K
import efficientnet.tfkeras as efn
from sklearn.model_selection import KFold
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt
SEED = 42
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
seed_everything(SEED )<load_from_csv>
DEVICE = "TPU"
SEED = 42
FOLDS = 5
IMG_SIZES = [384,384,384,384,384]
INC2019 = [0,0,0,0,0]
INC2018 = [1,1,1,1,1]
BATCH_SIZES = [32]*FOLDS
EPOCHS = [12]*FOLDS
EFF_NETS = [6,6,6,6,6]
WGTS = [1/FOLDS]*FOLDS
TTA = 11
marking = pd.read_csv('.. /input/global-wheat-detection/train.csv')
 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 )<define_search_model>
if DEVICE == "TPU":
print("connecting to TPU...")
 try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
print("Could not connect to TPU")
 tpu = None
if tpu:
try:
print("initializing TPU...")
 tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print("TPU initialized")
 except _:
print("failed to initialize TPU")
 else:
DEVICE = "GPU"
if DEVICE != "TPU":
print("Using default strategy for CPU and single GPU")
 strategy = tf.distribute.get_strategy()
if DEVICE == "GPU":
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
 AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
print(f'REPLICAS: {REPLICAS}' )
def get_train_transforms() :
return A.Compose(
 [
A.RandomSizedCrop(min_max_height=(800, 800), height=1024, width=1024, p=0.5),
A.OneOf([
A.HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit= 0.1,
val_shift_limit=0.1, p=0.9),
A.RandomBrightnessContrast(brightness_limit=0.1,
contrast_limit=0.1, p=0.9),
],p=0.9),
A.ToGray(p=0.01),
A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.Resize(height=512, width=512, p=1),
A.Cutout(num_holes=10, max_h_size=64, max_w_size=64, fill_value=0, p=0.5),
ToTensorV2(p=1.0),
],
p=1.0,
bbox_params=A.BboxParams(
 format='pascal_voc',
min_area=0,
min_visibility=0,
label_fields=['labels'] 
)
)
 def get_valid_transforms() :
return A.Compose(
 [
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
],
p=1.0,
bbox_params=A.BboxParams(
 format='pascal_voc',
min_area=0,
min_visibility=0,
label_fields=['labels'] 
)
)
 def get_test_transforms() :
return A.Compose([
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )<init_hyperparams>
GCS_PATH = [None]*FOLDS; GCS_PATH2 = [None]*FOLDS
for i,k in enumerate(IMG_SIZES):
GCS_PATH[i] = KaggleDatasets().get_gcs_path('melanoma-%ix%i'%(k,k))
GCS_PATH2[i] = KaggleDatasets().get_gcs_path('isic2019-%ix%i'%(k,k))
files_train = np.sort(np.array(tf.io.gfile.glob(GCS_PATH[0] + '/train*.tfrec')))
 files_test = np.sort(np.array(tf.io.gfile.glob(GCS_PATH[0] + '/test*.tfrec')) )
warnings.filterwarnings("ignore")
 class Fitter:
def __init__(self, model, device, config):
self.config = config
self.epoch = 0
self.base_dir = f'./{config.folder}'
if not os.path.exists(self.base_dir):
os.makedirs(self.base_dir)
 self.log_path = f'{self.base_dir}/log.txt'
self.best_summary_loss = 10**5
self.model = model
self.device = device
param_optimizer = list(self.model.named_parameters())
 no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.001},
{'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
self.optimizer = torch.optim.Adam(self.model.parameters() , lr=config.lr)
 self.scheduler = config.SchedulerClass(self.optimizer, **config.scheduler_params)
 self.log(f'Fitter prepared.Device is {self.device}')
 def fit(self, train_loader, validation_loader):
for e in range(self.config.n_epochs):
if self.config.verbose:
lr = self.optimizer.param_groups[0]['lr']
timestamp = datetime.utcnow().isoformat()
self.log(f'
{timestamp}
LR: {lr}')
 t = time.time()
summary_loss = self.train_one_epoch(train_loader)
 self.log(f'[RESULT]: Train.Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, time: {(time.time() - t):.5f}')
 self.save(f'{self.base_dir}/last-checkpoint1.bin')
 t = time.time()
summary_loss = self.validation(validation_loader)
 self.log(f'[RESULT]: Val.Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, time: {(time.time() - t):.5f}')
 if summary_loss.avg < self.best_summary_loss:
self.best_summary_loss = summary_loss.avg
self.model.eval()
self.save(f'{self.base_dir}/best-checkpoint-{str(self.epoch ).zfill(3)}epoch.bin')
 for path in sorted(glob(f'{self.base_dir}/best-checkpoint-*epoch.bin')) [:-3]:
os.remove(path)
 if self.config.validation_scheduler:
self.scheduler.step(metrics=summary_loss.avg)
 self.epoch += 1
def validation(self, val_loader):
self.model.eval()
summary_loss = AverageMeter()
t = time.time()
for step,(images, targets, image_ids)in enumerate(val_loader):
if self.config.verbose:
if step % self.config.verbose_step == 0:
print(
 f'Val Step {step}/{len(val_loader)}, ' + \
f'summary_loss: {summary_loss.avg:.5f}, ' + \
f'time: {(time.time() - t):.5f}', end='\r' 
)
 with torch.no_grad() :
images = torch.stack(images)
 batch_size = images.shape[0]
images = images.to(self.device ).float()
boxes = [target['boxes'].to(self.device ).float() for target in targets]
labels = [target['labels'].to(self.device ).float() for target in targets]
loss, _, _ = self.model(images, boxes, labels)
 summary_loss.update(loss.detach().item() , batch_size)
 return summary_loss
def train_one_epoch(self, train_loader):
self.model.train()
summary_loss = AverageMeter()
t = time.time()
for step,(images, targets, image_ids)in enumerate(train_loader):
if self.config.verbose:
if step % self.config.verbose_step == 0:
print(
 f'Train Step {step}/{len(train_loader)}, ' + \
f'summary_loss: {summary_loss.avg:.5f}, ' + \
f'time: {(time.time() - t):.5f}', end='\r' 
)
 images = torch.stack(images)
 images = images.to(self.device ).float()
batch_size = images.shape[0]
boxes = [target['boxes'].to(self.device ).float() for target in targets]
labels = [target['labels'].to(self.device ).float() for target in targets]
accumulation_steps = 3
loss, _, _ = self.model(images, boxes, labels)
 loss = loss/accumulation_steps
loss.backward()
summary_loss.update(loss.detach().item() , batch_size)
 if(( step + 1)%accumulation_steps)== 0:
self.optimizer.step()
self.optimizer.zero_grad()
if self.config.step_scheduler:
self.scheduler.step()
return summary_loss
def save(self, path):
self.model.eval()
torch.save({
'model_state_dict': self.model.model.state_dict() ,
'optimizer_state_dict': self.optimizer.state_dict() ,
'scheduler_state_dict': self.scheduler.state_dict() ,
'best_summary_loss': self.best_summary_loss,
'epoch': self.epoch,
}, path)
 def load(self, path):
checkpoint = torch.load(path)
 self.model.model.load_state_dict(checkpoint['model_state_dict'])
 self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
 self.scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
 self.best_summary_loss = checkpoint['best_summary_loss']
self.epoch = checkpoint['epoch'] + 1
def log(self, message):
if self.config.verbose:
print(message)
 with open(self.log_path, 'a+')as logger:
logger.write(f'{message}
' )<init_hyperparams>
ROT_ = 180.0
SHR_ = 2.0
HZOOM_ = 8.0
WZOOM_ = 8.0
HSHIFT_ = 8.0
WSHIFT_ = 8.0
class TrainGlobalConfig:
num_workers = 2
batch_size = 5
n_epochs = 7
lr = 0.0001
folder = 'plabel_model'
verbose = True
verbose_step = 1
step_scheduler = False
validation_scheduler = True
SchedulerClass = torch.optim.lr_scheduler.ReduceLROnPlateau
scheduler_params = dict(
 mode='min',
factor=0.5,
patience=1,
verbose=False,
threshold=0.0001,
threshold_mode='abs',
cooldown=0,
min_lr=1e-8,
eps=1e-08
)<data_type_conversions>
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 def get_3x3_mat(lst):
return tf.reshape(tf.concat([lst],axis=0), [3,3])
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = get_3x3_mat([c1, s1, zero,
-s1, c1, zero,
zero, zero, one])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = get_3x3_mat([one, s2, zero,
zero, c2, zero,
zero, zero, one])
 zoom_matrix = get_3x3_mat([one/height_zoom, zero, zero,
zero, one/width_zoom, zero,
zero, zero, one])
 shift_matrix = get_3x3_mat([one, zero, height_shift,
zero, one, width_shift,
zero, zero, one])
 return K.dot(K.dot(rotation_matrix, shear_matrix),
K.dot(zoom_matrix, shift_matrix))
def transform(image, DIM=256):
XDIM = DIM%2
rot = ROT_ * tf.random.normal([1], dtype='float32')
 shr = SHR_ * tf.random.normal([1], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ HZOOM_
w_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ WZOOM_
h_shift = HSHIFT_ * tf.random.normal([1], dtype='float32')
 w_shift = WSHIFT_ * tf.random.normal([1], dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2, -DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2, DIM//2), [DIM])
 z = tf.ones([DIM*DIM], dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m, tf.cast(idx, dtype='float32'))
idx2 = K.cast(idx2, dtype='int32')
 idx2 = K.clip(idx2, -DIM//2+XDIM+1, DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image, tf.transpose(idx3))
return tf.reshape(d,[DIM, DIM,3] )
DATA_ROOT_PATH = '.. /input/global-wheat-detection/test'
class DatasetRetriever(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
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]<load_pretrained>
def read_labeled_tfrecord(example):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
'patient_id' : tf.io.FixedLenFeature([], tf.int64),
'sex' : tf.io.FixedLenFeature([], tf.int64),
'age_approx' : tf.io.FixedLenFeature([], tf.int64),
'anatom_site_general_challenge': tf.io.FixedLenFeature([], tf.int64),
'diagnosis' : tf.io.FixedLenFeature([], tf.int64),
'target' : tf.io.FixedLenFeature([], tf.int64)
 }
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['target']
def read_unlabeled_tfrecord(example, return_image_name):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['image_name'] if return_image_name else 0
def prepare_image(img, augment=True, dim=256):
img = tf.image.decode_jpeg(img, channels=3)
 img = tf.cast(img, tf.float32)/ 255.0
if augment:
img = transform(img,DIM=dim)
 img = tf.image.random_flip_left_right(img)
 img = tf.image.random_saturation(img, 0.7, 1.3)
 img = tf.image.random_contrast(img, 0.8, 1.2)
 img = tf.image.random_brightness(img, 0.1)
 img = tf.reshape(img, [dim,dim, 3])
 return img
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1))
for filename in filenames]
return np.sum(n )
dataset = DatasetRetriever(
 image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),
transforms=get_test_transforms() 
)
 def collate_fn(batch):
return tuple(zip(*batch))
data_loader = DataLoader(
 dataset,
batch_size=1,
shuffle=False,
num_workers=4,
drop_last=False,
collate_fn=collate_fn
)<choose_model_class>
def get_dataset(files, augment = False, shuffle = False, repeat = False,
labeled=True, return_image_names=True, batch_size=16, dim=256):
ds = tf.data.TFRecordDataset(files, num_parallel_reads=AUTO)
 ds = ds.cache()
if repeat:
ds = ds.repeat()
if shuffle:
ds = ds.shuffle(1024*8)
 opt = tf.data.Options()
opt.experimental_deterministic = False
ds = ds.with_options(opt)
 if labeled:
ds = ds.map(read_labeled_tfrecord, num_parallel_calls=AUTO)
 else:
ds = ds.map(lambda example: read_unlabeled_tfrecord(example, return_image_names),
num_parallel_calls=AUTO)
 ds = ds.map(lambda img, imgname_or_label:(prepare_image(img, augment=augment, dim=dim),
imgname_or_label),
num_parallel_calls=AUTO)
 ds = ds.batch(batch_size * REPLICAS)
 ds = ds.prefetch(AUTO)
 return ds
def load_net(checkpoint_path):
config = get_efficientdet_config('tf_efficientdet_d5')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size=512
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
checkpoint = torch.load(checkpoint_path)
 net.load_state_dict(checkpoint['model_state_dict'])
 del checkpoint
gc.collect()
net = DetBenchEval(net, config)
 net.eval() ;
return net.cuda()
net = load_net('.. /input/wheatgoodmodels/best-checkpoint-096epoch.bin' )<categorify>
EFNS = [efn.EfficientNetB0, efn.EfficientNetB1, efn.EfficientNetB2, efn.EfficientNetB3,
efn.EfficientNetB4, efn.EfficientNetB5, efn.EfficientNetB6]
def build_model(dim=128, ef=0):
inp = tf.keras.layers.Input(shape=(dim,dim,3))
base = EFNS[ef](input_shape=(dim,dim,3),weights='imagenet',include_top=False)
 x = base(inp)
 x = tf.keras.layers.GlobalAveragePooling2D()(x)
 x = tf.keras.layers.Dense(1,activation='sigmoid' )(x)
 model = tf.keras.Model(inputs=inp,outputs=x)
 opt = tf.keras.optimizers.Adam(learning_rate=0.001)
 loss = tf.keras.losses.BinaryCrossentropy(label_smoothing=0.05)
 model.compile(optimizer=opt,loss=loss,metrics=['AUC'])
 return model
class BaseWheatTTA:

 image_size = 512
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 )<concatenate>
def get_lr_callback(batch_size=8):
lr_start = 0.000005
lr_max = 0.00000125 * REPLICAS * batch_size
lr_min = 0.000001
lr_ramp_ep = 5
lr_sus_ep = 0
lr_decay = 0.8
def lrfn(epoch):
if epoch < lr_ramp_ep:
lr =(lr_max - lr_start)/ lr_ramp_ep * epoch + lr_start
elif epoch < lr_ramp_ep + lr_sus_ep:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_decay**(epoch - lr_ramp_ep - lr_sus_ep)+ lr_min
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=False)
 return lr_callback
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]))<predict_on_test>
VERBOSE = 0
DISPLAY_PLOT = True
skf = KFold(n_splits=FOLDS,shuffle=True,random_state=SEED)
 oof_pred = []; oof_tar = []; oof_val = []; oof_names = []; oof_folds = []
preds = np.zeros(( count_data_items(files_test),1))
for fold,(idxT,idxV)in enumerate(skf.split(np.arange(15))):
if DEVICE=='TPU':
if tpu: tf.tpu.experimental.initialize_tpu_system(tpu)
 print('
print(' 
(IMG_SIZES[fold],EFF_NETS[fold],BATCH_SIZES[fold]*REPLICAS))
files_train = tf.io.gfile.glob([GCS_PATH[fold] + '/train%.2i*.tfrec'%x for x in idxT])
 if INC2019[fold]:
files_train += tf.io.gfile.glob([GCS_PATH2[fold] + '/train%.2i*.tfrec'%x for x in idxT*2+1])
 print('
if INC2018[fold]:
files_train += tf.io.gfile.glob([GCS_PATH2[fold] + '/train%.2i*.tfrec'%x for x in idxT*2])
 print('
np.random.shuffle(files_train); print('
files_valid = tf.io.gfile.glob([GCS_PATH[fold] + '/train%.2i*.tfrec'%x for x in idxV])
 files_test = np.sort(np.array(tf.io.gfile.glob(GCS_PATH[fold] + '/test*.tfrec')))
 K.clear_session()
with strategy.scope() :
model = build_model(dim=IMG_SIZES[fold],ef=EFF_NETS[fold])
 sv = tf.keras.callbacks.ModelCheckpoint(
 'fold-%i.h5'%fold, monitor='val_loss', verbose=0, save_best_only=True,
save_weights_only=True, mode='min', save_freq='epoch')
 print('Training...')
 history = model.fit(
 get_dataset(files_train, augment=True, shuffle=True, repeat=True,
dim=IMG_SIZES[fold], batch_size = BATCH_SIZES[fold]),
epochs=EPOCHS[fold], callbacks = [sv,get_lr_callback(BATCH_SIZES[fold])],
steps_per_epoch=count_data_items(files_train)/BATCH_SIZES[fold]//REPLICAS,
validation_data=get_dataset(files_valid,augment=False,shuffle=False,
repeat=False,dim=IMG_SIZES[fold]),
verbose=VERBOSE 
)
 print('Loading best model...')
 model.load_weights('fold-%i.h5'%fold)
 print('Predicting OOF with TTA...')
 ds_valid = get_dataset(files_valid,labeled=False,return_image_names=False,augment=True,
repeat=True,shuffle=False,dim=IMG_SIZES[fold],batch_size=BATCH_SIZES[fold]*4)
 ct_valid = count_data_items(files_valid); STEPS = TTA * ct_valid/BATCH_SIZES[fold]/4/REPLICAS
pred = model.predict(ds_valid,steps=STEPS,verbose=VERBOSE)[:TTA*ct_valid,]
oof_pred.append(np.mean(pred.reshape(( ct_valid,TTA),order='F'),axis=1))
ds_valid = get_dataset(files_valid, augment=False, repeat=False, dim=IMG_SIZES[fold],
labeled=True, return_image_names=True)
 oof_tar.append(np.array([target.numpy() for img, target in iter(ds_valid.unbatch())]))
oof_folds.append(np.ones_like(oof_tar[-1],dtype='int8')*fold)
 ds = get_dataset(files_valid, augment=False, repeat=False, dim=IMG_SIZES[fold],
labeled=False, return_image_names=True)
 oof_names.append(np.array([img_name.numpy().decode("utf-8")for img, img_name in iter(ds.unbatch())]))
print('Predicting Test with TTA...')
 ds_test = get_dataset(files_test,labeled=False,return_image_names=False,augment=True,
repeat=True,shuffle=False,dim=IMG_SIZES[fold],batch_size=BATCH_SIZES[fold]*4)
 ct_test = count_data_items(files_test); STEPS = TTA * ct_test/BATCH_SIZES[fold]/4/REPLICAS
pred = model.predict(ds_test,steps=STEPS,verbose=VERBOSE)[:TTA*ct_test,]
preds[:,0] += np.mean(pred.reshape(( ct_test,TTA),order='F'),axis=1)* WGTS[fold]
auc = roc_auc_score(oof_tar[-1],oof_pred[-1])
 oof_val.append(np.max(history.history['val_auc']))
print('
if DISPLAY_PLOT:
plt.figure(figsize=(15,5))
plt.plot(np.arange(EPOCHS[fold]),history.history['auc'],'-o',label='Train AUC',color='
plt.plot(np.arange(EPOCHS[fold]),history.history['val_auc'],'-o',label='Val AUC',color='
x = np.argmax(history.history['val_auc']); y = np.max(history.history['val_auc'])
 xdist = plt.xlim() [1] - plt.xlim() [0]; ydist = plt.ylim() [1] - plt.ylim() [0]
plt.scatter(x,y,s=200,color='
%.2f'%y,size=14)
 plt.ylabel('AUC',size=14); plt.xlabel('Epoch',size=14)
 plt.legend(loc=2)
 plt2 = plt.gca().twinx()
plt2.plot(np.arange(EPOCHS[fold]),history.history['loss'],'-o',label='Train Loss',color='
plt2.plot(np.arange(EPOCHS[fold]),history.history['val_loss'],'-o',label='Val Loss',color='
x = np.argmin(history.history['val_loss']); y = np.min(history.history['val_loss'])
 ydist = plt.ylim() [1] - plt.ylim() [0]
plt.scatter(x,y,s=200,color='
plt.ylabel('Loss',size=14)
 plt.title('FOLD %i - Image Size %i, EfficientNet B%i, inc2019=%i, inc2018=%i'% 
(fold+1,IMG_SIZES[fold],EFF_NETS[fold],INC2019[fold],INC2018[fold]),size=18)
 plt.legend(loc=3)
 plt.show()
def make_tta_predictions(images, score_threshold=0.25):
with torch.no_grad() :
images = torch.stack(images ).float().cuda()
predictions = []
for tta_transform in tta_transforms:
result = []
det = net(tta_transform.batch_augment(images.clone()), torch.tensor([1]*images.shape[0] ).float().cuda())
 for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes = tta_transform.deaugment_boxes(boxes.copy())
 result.append({
'boxes': boxes,
'scores': scores[indexes],
})
 predictions.append(result)
 return predictions
def run_wbf(predictions, image_index, image_size=512, iou_thr=0.44, skip_box_thr=0.43, weights=None):
boxes = [(prediction[image_index]['boxes']/(image_size-1)).tolist() for prediction in predictions]
scores = [prediction[image_index]['scores'].tolist() for prediction in predictions]
labels = [np.ones(prediction[image_index]['scores'].shape[0] ).astype(int ).tolist() for prediction in predictions]
boxes, scores, labels = ensemble_boxes.ensemble_boxes_wbf.weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = boxes*(image_size-1)
 return boxes, scores, labels<define_variables>
oof = np.concatenate(oof_pred); true = np.concatenate(oof_tar);
names = np.concatenate(oof_names); folds = np.concatenate(oof_folds)
 auc = roc_auc_score(true,oof)
 print('Overall OOF AUC with TTA = %.3f'%auc)
 df_oof = pd.DataFrame(dict(
 image_name = names, target=true, pred = oof, fold=folds))
df_oof.to_csv('oof.csv',index=False)
 df_oof.head()
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>
ds = get_dataset(files_test, augment=False, repeat=False, dim=IMG_SIZES[fold],
labeled=False, return_image_names=True)
 image_names = np.array([img_name.numpy().decode("utf-8")
 for img, img_name in iter(ds.unbatch())] )
<prepare_output><EOS> 
submission = pd.DataFrame(dict(image_name=image_names, target=preds[:,0]))
submission = submission.sort_values('image_name')
 submission.to_csv('submission.csv', index=False)
 submission.head()