Kaggle competition: Rainforest Connection Species Audio Detection
<SOS> metric: WeightedLabelRankingAveragePrecision Kaggle data source: rainforest-connection-species-audio-detection<choose_model_class>
!pip install iterative-stratification
!pip install tf-nightly
!pip install --upgrade pip
def CapsNet(input_shape, n_class, num_routing):
x = layers.Input(shape=input_shape)
 conv1 = layers.Conv2D(filters=256, kernel_size=9, strides=1, padding='valid', activation='relu', name='conv1' )(x)
 primarycaps = PrimaryCap(conv1, dim_vector=8, n_channels=32, kernel_size=9, strides=2, padding='valid')
 digitcaps = CapsuleLayer(num_capsule=n_class, dim_vector=16, num_routing=num_routing, name='digitcaps' )(primarycaps)
 out_caps = Length(name='out_caps' )(digitcaps)
 y = layers.Input(shape=(n_class,))
masked = Mask()([digitcaps, y])
 x_recon = layers.Dense(512, activation='relu' )(masked)
 x_recon = layers.Dense(1024, activation='relu' )(x_recon)
 x_recon = layers.Dense(width*breadth*3, activation='sigmoid' )(x_recon)
 x_recon = layers.Reshape(target_shape=[width, breadth, 3], name='out_recon' )(x_recon)
 return models.Model([x, y], [out_caps, x_recon] )<compute_test_metric>
tf.__version__
def margin_loss(y_true, y_pred):
L = y_true * K.square(K.maximum(0., 0.9 - y_pred)) + \
0.5 *(1 - y_true)* K.square(K.maximum(0., y_pred - 0.1))
return K.mean(K.sum(L, 1))<set_options>
cfg = {
'parse_params': {
'cut_time': 10,
},
'data_params': {
'sample_time': 6,
'spec_fmax': 24000.0,
'spec_fmin': 40.0,
'spec_mel': 224,
'mel_power': 2,
'img_shape':(224, 512)
 },
'model_params': {
'batchsize_per_tpu': 16,
'iteration_per_epoch': 64,
'epoch': 15,
'arch': tf.keras.applications.ResNet50,
'arch_preprocess': tf.keras.applications.resnet50.preprocess_input,
'freeze_to': 0,
'loss': {
'fn': tfa.losses.SigmoidFocalCrossEntropy,
'params': {},
},
'optim': {
'fn': tfa.optimizers.RectifiedAdam,
'params': {'lr': 1e-3, 'total_steps': 15*64, 'warmup_proportion': 0.3, 'min_lr': 1e-6},
},
'mixup': False
}
}
width, breadth = 32, 32<define_variables>
strategy = tf.distribute.experimental.TPUStrategy(tpu)
 AUTOTUNE = tf.data.experimental.AUTOTUNE
GCS_DS_PATH = KaggleDatasets().get_gcs_path()
TRAIN_TFREC = GCS_DS_PATH + "/tfrecords/train"
TEST_TFREC = GCS_DS_PATH + "/tfrecords/test"
<feature_engineering>
CUT = cfg['parse_params']['cut_time']
SR = 48000
TIME = cfg['data_params']['sample_time']
FMAX = cfg['data_params']['spec_fmax']
FMIN = cfg['data_params']['spec_fmin']
N_MEL = cfg['data_params']['spec_mel']
HEIGHT, WIDTH = cfg['data_params']['img_shape']
CLASS_N = 24
trainCSV = pd.read_csv('.. /input/diabetic-retinopathy-detection/trainLabels.csv')
 trainCSV['PatientId'] = trainCSV['image'].map(lambda x: x.split('_')[0])
 trainCSV['imagePath'] = trainCSV['image'].map(lambda x: os.path.join('.. /input/diabetic-retinopathy-detection/','{}.jpeg'.format(x)))
 trainCSV['exists'] = trainCSV['imagePath'].map(os.path.exists)
 trainCSV['leftorright'] = trainCSV['image'].map(lambda x: 'left' if x.split('_')[-1]=='left' else 'right')
 trainCSV['label'] = trainCSV['level'].map(lambda x: to_categorical(x, 5))
trainCSV.dropna(inplace = True)
 trainCSV = trainCSV[trainCSV['exists']]<import_modules>
raw_dataset = tf.data.TFRecordDataset([TRAIN_TFREC + '/00-148.tfrec'])
 raw_dataset
from PIL import Image
import time
import sys<normalization>
feature_description = {
'recording_id': tf.io.FixedLenFeature([], tf.string, default_value=''),
'audio_wav': tf.io.FixedLenFeature([], tf.string, default_value=''),
'label_info': tf.io.FixedLenFeature([], tf.string, default_value=''),
}
parse_dtype = {
'audio_wav': tf.float32,
'recording_id': tf.string,
'species_id': tf.int32,
'songtype_id': tf.int32,
't_min': tf.float32,
'f_min': tf.float32,
't_max': tf.float32,
'f_max':tf.float32,
'is_tp': tf.int32
}
@tf.function
def _parse_function(example_proto):
sample = tf.io.parse_single_example(example_proto, feature_description)
 wav, _ = tf.audio.decode_wav(sample['audio_wav'], desired_channels=1)
 label_info = tf.strings.split(sample['label_info'], sep='"')[1]
labels = tf.strings.split(label_info, sep=';')
 @tf.function
def _cut_audio(label):
items = tf.strings.split(label, sep=',')
 spid = tf.squeeze(tf.strings.to_number(items[0], tf.int32))
soid = tf.squeeze(tf.strings.to_number(items[1], tf.int32))
tmin = tf.squeeze(tf.strings.to_number(items[2]))
fmin = tf.squeeze(tf.strings.to_number(items[3]))
tmax = tf.squeeze(tf.strings.to_number(items[4]))
fmax = tf.squeeze(tf.strings.to_number(items[5]))
tp = tf.squeeze(tf.strings.to_number(items[6], tf.int32))
tmax_s = tmax * tf.cast(SR, tf.float32)
 tmin_s = tmin * tf.cast(SR, tf.float32)
 cut_s = tf.cast(CUT * SR, tf.float32)
 all_s = tf.cast(60 * SR, tf.float32)
 tsize_s = tmax_s - tmin_s
cut_min = tf.cast(
 tf.maximum(0.0,
tf.minimum(tmin_s -(cut_s - tsize_s)/ 2,
tf.minimum(tmax_s +(cut_s - tsize_s)/ 2, all_s)- cut_s)
), tf.int32 
)
 cut_max = cut_min + CUT * SR
_sample = {
'audio_wav': tf.reshape(wav[cut_min:cut_max], [CUT*SR]),
'recording_id': sample['recording_id'],
'species_id': spid,
'songtype_id': soid,
't_min': tmin - tf.cast(cut_min, tf.float32)/tf.cast(SR, tf.float32),
'f_min': fmin,
't_max': tmax - tf.cast(cut_min, tf.float32)/tf.cast(SR, tf.float32),
'f_max': fmax,
'is_tp': tp
}
return _sample
samples = tf.map_fn(_cut_audio, labels, dtype=parse_dtype)
 return samples
parsed_dataset = raw_dataset.map(_parse_function ).unbatch()
def transformImagetoArray(imagePathsList, width=480, breadth=480):
startTime = time.time()
imagesArrayList = []
for imagePath in imagePathsList:
image = np.array(Image.open(imagePath ).resize(( width, breadth)) , np.float ).reshape(width, breadth, 3)
 imagesArrayList.append(image)
 print("needed_time_for_makingArrays: {}".format(time.time() -startTime)+ "[sec]")
 imagesArray = np.asarray(imagesArrayList)
 print('imagesArray: {} MB'.format(str(sys.getsizeof(imagesArray)/(10**6))))
return imagesArray<prepare_x_and_y>
@tf.function
def _cut_wav(x):
cut_min = tf.random.uniform([], maxval=tf.minimum(( CUT-TIME)*SR, tf.cast(x['t_max']*SR, tf.int32)) , dtype=tf.int32)
 cut_max = cut_min + TIME * SR
cutwave = tf.reshape(x['audio_wav'][cut_min:cut_max], [TIME*SR])
 print("The cutwave shape is")
 print(cutwave.shape)
 print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
 y = {}
y.update(x)
 y['audio_wav'] = cutwave
y['t_min'] = tf.maximum(0.0, x['t_min'] - tf.cast(cut_min, tf.float32)/ SR)
 y['t_max'] = tf.maximum(0.0, x['t_max'] - tf.cast(cut_min, tf.float32)/ SR)
 return y
@tf.function
def _cut_wav_val(x):
cut_min = tf.minimum(( CUT-TIME)*SR // 2, tf.cast(( x['t_min'] + x['t_max'])/ 2 * SR, tf.int32))
cut_max = cut_min + TIME * SR
cutwave = tf.reshape(x['audio_wav'][cut_min:cut_max], [TIME*SR])
 y = {}
y.update(x)
 y['audio_wav'] = cutwave
y['t_min'] = tf.maximum(0.0, x['t_min'] - tf.cast(cut_min, tf.float32)/ SR)
 y['t_max'] = tf.maximum(0.0, x['t_max'] - tf.cast(cut_min, tf.float32)/ SR)
 return y
x = transformImagetoArray(list(trainCSV['imagePath']), width=width, breadth=breadth)
 y = np.asarray(list(trainCSV['label']))<split>
@tf.function
def _filtTP(x):
return x['is_tp'] == 1
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2 )<data_type_conversions>
def show_wav(sample, ax):
wav = sample["audio_wav"].numpy()
rate = SR
ax.plot(np.arange(len(wav)) / rate, wav)
 ax.set_title(
 sample["recording_id"].numpy().decode()
+("/%d" % sample["species_id"])
 +("TP" if sample["is_tp"] else "FP"))
return Audio(( wav * 2**15 ).astype(np.int16), rate=rate)
 fig, ax = plt.subplots(figsize=(15, 3))
show_wav(next(iter(parsed_dataset)) , ax )
<set_options>
@tf.function
def _wav_to_spec(x):
mel_power = cfg['data_params']['mel_power']
stfts = tf.signal.stft(x["audio_wav"], frame_length=2048, frame_step=512, fft_length=2048)
 spectrograms = tf.abs(stfts)** mel_power
num_spectrogram_bins = stfts.shape[-1]
lower_edge_hertz, upper_edge_hertz, num_mel_bins = FMIN, FMAX, N_MEL
linear_to_mel_weight_matrix = tf.signal.linear_to_mel_weight_matrix(
 num_mel_bins, num_spectrogram_bins, SR, lower_edge_hertz,
upper_edge_hertz)
 mel_spectrograms = tf.tensordot(
 spectrograms, linear_to_mel_weight_matrix, 1)
 mel_spectrograms.set_shape(spectrograms.shape[:-1].concatenate(
 linear_to_mel_weight_matrix.shape[-1:]))
log_mel_spectrograms = tf.math.log(mel_spectrograms + 1e-6)
 y = {
'audio_spec': tf.transpose(log_mel_spectrograms),
}
y.update(x)
 return y
spec_dataset = parsed_dataset.filter(_filtTP ).map(_cut_wav ).map(_wav_to_spec )
del x
gc.collect()<train_model>
@tf.function
def _create_annot(x):
targ = tf.one_hot(x["species_id"], CLASS_N, on_value=x["is_tp"], off_value=0)
 return {
'input': x["audio_spec"],
'target': tf.cast(targ, tf.float32)
 }
annot_dataset = spec_dataset.map(_create_annot )
print('x train: %s' % str(x_train.shape))
print('x test: %s' % str(x_test.shape))
print('y train: %s' % str(y_train.shape))
print('y test: %s' % str(y_test.shape))<train_model>
@tf.function
def _preprocess_img(x, training=False):
image = tf.expand_dims(x, axis=-1)
 image = tf.image.resize(image, [HEIGHT, WIDTH])
 image = tf.image.per_image_standardization(image)
 @tf.function
def _specaugment(image):
ERASE_TIME = 40
ERASE_MEL = 20
ERASE_TIME_N = 1
ERASE_MEL_N = 1
image = tf.expand_dims(image, axis=0)
 xoff = tf.random.uniform([ERASE_TIME_N], minval=ERASE_TIME//2, maxval=WIDTH-ERASE_TIME//2, dtype=tf.int32)
 xsize = tf.random.uniform([ERASE_TIME_N], minval=ERASE_TIME//2, maxval=ERASE_TIME, dtype=tf.int32)
 yoff = tf.random.uniform([ERASE_MEL_N], minval=ERASE_MEL//2, maxval=HEIGHT-ERASE_MEL//2, dtype=tf.int32)
 ysize = tf.random.uniform([ERASE_MEL_N], minval=ERASE_MEL//2, maxval=ERASE_MEL, dtype=tf.int32)
 for i in range(ERASE_TIME_N):
image = tfa.image.cutout(image, [HEIGHT, xsize[i]], offset=[HEIGHT//2, xoff[i]])
 for i in range(ERASE_MEL_N):
image = tfa.image.cutout(image, [ysize[i], WIDTH], offset=[yoff[i], WIDTH//2])
 return tf.squeeze(image, axis=0)
 if training:
gau = tf.keras.layers.GaussianNoise(0.3)
 image = tf.cond(tf.random.uniform([])< 0.5, lambda: gau(image, training=True), lambda: image)
 image = tf.image.random_brightness(image, 0.2)
 image = tf.cond(tf.random.uniform([])< 0.5, lambda: _specaugment(image), lambda: image)
 image =(image - tf.reduce_min(image)) /(tf.reduce_max(image)- tf.reduce_min(image)) * 255.0
image = tf.image.grayscale_to_rgb(image)
 image = cfg['model_params']['arch_preprocess'](image)
 return image
@tf.function
def _preprocess(x):
image = _preprocess_img(x['input'], True)
 return(image, x["target"])
 @tf.function
def _preprocess_val(x):
image = _preprocess_img(x['input'], False)
 return(image, x["target"])
 @tf.function
def _preprocess_test(x):
image = _preprocess_img(x['audio_spec'], False)
 return(image, x["recording_id"] )
def train(model, data, epoch_size_frac=1.0, epochs=100, batch_size=64): 
(x_train, y_train),(x_test, y_test)= data
log = callbacks.CSVLogger('log.csv')
 checkpoint = callbacks.ModelCheckpoint('weights-{epoch:02d}val_loss-{val_loss}.h5',
save_best_only=True, save_weights_only=False, verbose=1)
 lr_decay = callbacks.LearningRateScheduler(schedule=lambda epoch: 0.001 * np.exp(-epoch / 10.))
 early_stopping = callbacks.EarlyStopping(monitor = 'val_loss', min_delta=0, patience = 5, verbose = 1)
 model.compile(optimizer='adam',
loss=[margin_loss, 'mse'],
loss_weights=[1., 0.0005],
metrics={'out_caps': 'accuracy'})
 def train_generator(x, y, batch_size, shift_fraction=0.) :
train_datagen = ImageDataGenerator(width_shift_range=shift_fraction,
height_shift_range=shift_fraction)
 generator = train_datagen.flow(x, y, batch_size=batch_size)
 while 1:
x_batch, y_batch = generator.next()
yield([x_batch, y_batch], [y_batch, x_batch])
 model.fit_generator(generator=train_generator(x_train, y_train, batch_size, 0.1),
max_queue_size=2,
steps_per_epoch=int(epoch_size_frac*y_train.shape[0] / batch_size),
epochs=epochs,
validation_data=[[x_test, y_test], [y_test, x_test]],
callbacks=[log, checkpoint, lr_decay, early_stopping])
 model.save('trained_model.h5')
 print('Trained model saved to 'trained_model.h5'')
 return model<train_model>
def create_model() :
with strategy.scope() :
backbone = cfg['model_params']['arch'](include_top=False, weights='imagenet')
 if cfg['model_params']['freeze_to'] is None:
for layer in backbone.layers:
layer.trainable = False
else:
for layer in backbone.layers[:cfg['model_params']['freeze_to']]:
layer.trainable = False
prior = 1/CLASS_N
pi = -np.log(( 1-prior)/ prior)
 head = tf.keras.Sequential([
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Dense(1024, activation='relu', kernel_initializer=tf.keras.initializers.he_normal()),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Dense(CLASS_N, bias_initializer=tf.keras.initializers.Constant(pi))
])
 model = tf.keras.Sequential([backbone, head])
 return model
model = create_model()
model.summary()
train(model=model, data=(( x_train, y_train),(x_test[:60], y_test[:60])) ,
epoch_size_frac = 1 )<categorify>
@tf.function
def _mixup(inp, targ):
indice = tf.range(len(inp))
indice = tf.random.shuffle(indice)
 sinp = tf.gather(inp, indice, axis=0)
 starg = tf.gather(targ, indice, axis=0)
 alpha = 0.2
t = tf.compat.v1.distributions.Beta(alpha, alpha ).sample([len(inp)])
 tx = tf.reshape(t, [-1, 1, 1, 1])
 ty = tf.reshape(t, [-1, 1])
 x = inp * tx + sinp *(1-tx)
 y = targ * ty + starg *(1-ty)
 return x, y
def combine_images(generated_images):
num = generated_images.shape[0]
width = int(np.sqrt(num))
height = int(np.ceil(float(num)/width))
shape = generated_images.shape[1:3]
image = np.zeros(( height*shape[0], width*shape[1]),
dtype=generated_images.dtype)
 for index, img in enumerate(generated_images):
i = int(index/width)
 j = index % width
image[i*shape[0]:(i+1)*shape[0], j*shape[1]:(j+1)*shape[1]] = \
img[:, :, 0]
return image
def test(model, data):
x_test, y_test = data
y_pred, x_recon = model.predict([x_test, y_test], batch_size=100)
 print('-'*50)
 print('Test acc:', np.sum(np.argmax(y_pred, 1)== np.argmax(y_test, 1)) /y_test.shape[0])
 img = combine_images(np.concatenate([x_test[:50],x_recon[:50]]))
image = img * 255
Image.fromarray(image.astype(np.uint8)).save("real_and_recon.png")
 print()
print('Reconstructed images are saved to./real_and_recon.png')
 print('-'*50)
 plt.imshow(plt.imread("real_and_recon.png",))
plt.show()<train_model>
tfrecs = sorted(tf.io.gfile.glob(TRAIN_TFREC + '/*.tfrec'))
parsed_trainval =(tf.data.TFRecordDataset(tfrecs, num_parallel_reads=AUTOTUNE)
.map(_parse_function, num_parallel_calls=AUTOTUNE ).unbatch() 
.filter(_filtTP))
test(model=model, data=(x_test[:100], y_test[:100]))<drop_column>
df = pd.read_csv("/kaggle/input/rfcx-species-audio-detection/train_tp.csv")
 table = df.groupby('recording_id')['species_id'].apply(
 lambda x: tf.scatter_nd(tf.expand_dims(x, 1), np.ones_like(x), shape=[CLASS_N] ).numpy()
).reset_index()
skf = MultilabelStratifiedKFold(n_splits=5, shuffle=True, random_state=0)
 idx_splits = list(skf.split(table.recording_id, np.stack(table.species_id.to_numpy())))
 splits = list(map(lambda xs:(table.recording_id[xs[0]].to_numpy() , table.recording_id[xs[1]].to_numpy()), idx_splits))
del x_train, x_test, y_train, y_test
gc.collect()<predict_on_test>
def create_recid_filter(recids):
@tf.function
def _filt(x):
return tf.reduce_any(recids == x['recording_id'])
 return _filt
def predictandSaveSubmission() :
imagePaths = os.listdir('.. /input/resized-2015-2019-blindness-detection-images/resized test 15')
 predictionList = []
for imagePath in imagePaths:
tmp = [int(imagePath.split('.')[0].split('_')[0]), imagePath.split('.')[0].split('_')[1], imagePath.split('.')[0]]
imagePath = '.. /input/resized-2015-2019-blindness-detection-images/resized test 15/' + imagePath
imageArray = np.array([np.array(Image.open(imagePath ).resize(( width, breadth)) , np.float ).reshape(width, breadth, 3)])
 y_pred, _ = model.predict_on_batch([imageArray, np.zeros(( 1, 5)) ])
 tmp += [1]
predictionList.append(tmp)
 predictionList.sort(key=lambda x:(x[0], x[1]))
predictionDict = {}
predictionDict['image'] = [i[2] for i in predictionList]
predictionDict['level'] = [i[3] for i in predictionList]
df_submission = pd.DataFrame(predictionDict)
 df_submission.to_csv("submission.csv",index=False )<prepare_output>
def create_train_dataset(batchsize, train_recids):
global parsed_trainval
print("train split: %d" % len(train_recids))
parsed_train =(parsed_trainval 
.filter(create_recid_filter(train_recids)))
 dataset =(parsed_train.cache() 
.shuffle(len(train_recids)) 
.repeat() 
.map(_cut_wav, num_parallel_calls=AUTOTUNE)
.map(_wav_to_spec, num_parallel_calls=AUTOTUNE)
.map(_create_annot, num_parallel_calls=AUTOTUNE)
.map(_preprocess, num_parallel_calls=AUTOTUNE)
.batch(batchsize))
print(dataset.element_spec)
 print("dataset shape train %%%%%%%%%%%%%%%%%%%%%%%%%%%%")
 if cfg['model_params']['mixup']:
dataset =(dataset.map(_mixup, num_parallel_calls=AUTOTUNE)
.prefetch(AUTOTUNE))
else:
dataset = dataset.prefetch(AUTOTUNE)
 return dataset
def create_val_dataset(batchsize, val_recids):
global parsed_trainval
print("val split: %d" % len(val_recids))
parsed_val =(parsed_trainval 
.filter(create_recid_filter(val_recids)))
 vdataset =(parsed_val 
.map(_cut_wav_val, num_parallel_calls=AUTOTUNE)
.map(_wav_to_spec, num_parallel_calls=AUTOTUNE)
.map(_create_annot, num_parallel_calls=AUTOTUNE)
.map(_preprocess_val, num_parallel_calls=AUTOTUNE)
.batch(8*strategy.num_replicas_in_sync)
.cache())
 return vdataset
predictandSaveSubmission()<import_modules>
@tf.function
def _one_sample_positive_class_precisions(example):
y_true, y_pred = example
retrieved_classes = tf.argsort(y_pred, direction='DESCENDING')
 class_rankings = tf.argsort(retrieved_classes)
 retrieved_class_true = tf.gather(y_true, retrieved_classes)
 retrieved_cumulative_hits = tf.math.cumsum(tf.cast(retrieved_class_true, tf.float32))
idx = tf.where(y_true)[:, 0]
i = tf.boolean_mask(class_rankings, y_true)
 r = tf.gather(retrieved_cumulative_hits, i)
 c = 1 + tf.cast(i, tf.float32)
 precisions = r / c
dense = tf.scatter_nd(idx[:, None], precisions, [y_pred.shape[0]])
 return dense
class LWLRAP(tf.keras.metrics.Metric):
def __init__(self, num_classes, name='lwlrap'):
super().__init__(name=name)
 self._precisions = self.add_weight(
 name='per_class_cumulative_precision',
shape=[num_classes],
initializer='zeros', 
)
 self._counts = self.add_weight(
 name='per_class_cumulative_count',
shape=[num_classes],
initializer='zeros', 
)
 def update_state(self, y_true, y_pred, sample_weight=None):
precisions = tf.map_fn(
 fn=_one_sample_positive_class_precisions,
elems=(y_true, y_pred),
dtype=(tf.float32), 
)
 increments = tf.cast(precisions > 0, tf.float32)
 total_increments = tf.reduce_sum(increments, axis=0)
 total_precisions = tf.reduce_sum(precisions, axis=0)
 self._precisions.assign_add(total_precisions)
 self._counts.assign_add(total_increments)
 def result(self):
per_class_lwlrap = self._precisions / tf.maximum(self._counts, 1.0)
 per_class_weight = self._counts / tf.reduce_sum(self._counts)
 overall_lwlrap = tf.reduce_sum(per_class_lwlrap * per_class_weight)
 return overall_lwlrap
def reset_states(self):
self._precisions.assign(self._precisions * 0)
 self._counts.assign(self._counts * 0 )
from sklearn.metrics import roc_curve, auc
import xgboost as xgb<load_from_csv>
def _parse_function_test(example_proto):
sample = tf.io.parse_single_example(example_proto, feature_description)
 wav, _ = tf.audio.decode_wav(sample['audio_wav'], desired_channels=1)
 @tf.function
def _cut_audio(i):
_sample = {
'audio_wav': tf.reshape(wav[i*SR*TIME:(i+1)*SR*TIME], [SR*TIME]),
'recording_id': sample['recording_id']
}
return _sample
return tf.map_fn(_cut_audio, tf.range(60//TIME), dtype={
'audio_wav': tf.float32,
'recording_id': tf.string
})
 def inference(model):
tdataset =(tf.data.TFRecordDataset(tf.io.gfile.glob(TEST_TFREC + '/*.tfrec'), num_parallel_reads=AUTOTUNE)
.map(_parse_function_test, num_parallel_calls=AUTOTUNE ).unbatch() 
.map(_wav_to_spec, num_parallel_calls=AUTOTUNE)
.map(_preprocess_test, num_parallel_calls=AUTOTUNE)
.batch(128*(60//TIME)).prefetch(AUTOTUNE))
rec_ids = []
probs = []
for inp, rec_id in tqdm(tdataset):
with strategy.scope() :
pred = model.predict_on_batch(tf.reshape(inp, [-1, HEIGHT, WIDTH, 3]))
prob = tf.sigmoid(pred)
 prob = tf.reduce_max(tf.reshape(prob, [-1, 60//TIME, CLASS_N]), axis=1)
 rec_id_stack = tf.reshape(rec_id, [-1, 60//TIME])
 for rec in rec_id.numpy() :
assert len(np.unique(rec)) == 1
rec_ids.append(rec_id_stack.numpy() [:,0])
 probs.append(prob.numpy())
 crec_ids = np.concatenate(rec_ids)
 cprobs = np.concatenate(probs)
 sub = pd.DataFrame({
'recording_id': list(map(lambda x: x.decode() , crec_ids.tolist())) ,
**{f's{i}': cprobs[:,i] for i in range(CLASS_N)}
})
 sub = sub.sort_values('recording_id')
 return sub
print('Loading the training/test data using pandas...')
 train = pd.read_csv('.. /input/training.csv')
 test = pd.read_csv('.. /input/test.csv')
 check_agreement = pd.read_csv('.. /input/check_agreement.csv')
 check_correlation = pd.read_csv('.. /input/check_correlation.csv' )<feature_engineering>
def train_and_inference(splits, split_id):
batchsize = cfg['model_params']['batchsize_per_tpu'] * strategy.num_replicas_in_sync
print("batchsize", batchsize)
 loss_fn = cfg['model_params']['loss']['fn'](from_logits=True, **cfg['model_params']['loss']['params'])
 idx_train_tf = tf.constant(splits[split_id][0])
 idx_val_tf = tf.constant(splits[split_id][1])
 dataset = create_train_dataset(batchsize, idx_train_tf)
 vdataset = create_val_dataset(batchsize, idx_val_tf)
 optimizer = cfg['model_params']['optim']['fn'](**cfg['model_params']['optim']['params'])
 model = create_model()
with strategy.scope() :
model.compile(optimizer=optimizer, loss=loss_fn, metrics=[LWLRAP(CLASS_N)])
 history = model.fit(dataset,
steps_per_epoch=cfg['model_params']['iteration_per_epoch'],
epochs=cfg['model_params']['epoch'],
validation_data=vdataset,
callbacks=[
tf.keras.callbacks.ModelCheckpoint(
 filepath='model_best_%d.h5' % split_id,
save_weights_only=True,
monitor='val_lwlrap',
mode='max',
save_best_only=True),
])
 plot_history(history, 'history_%d.png' % split_id)
 model.load_weights('model_best_%d.h5' % split_id)
 return inference(model), history
def add_features(df):
df['flight_dist_sig'] = df['FlightDistance']/df['FlightDistanceError']
df['flight_dist_sig2'] =(df['FlightDistance']/df['FlightDistanceError'])**2
df['NEW_IP_dira'] = df['IP']*df['dira']
df['p0p2_ip_ratio']=df['IP']/df['IP_p0p2']
df['p1p2_ip_ratio']=df['IP']/df['IP_p1p2']
df['DCA_MAX'] = df.loc[:, ['DOCAone', 'DOCAtwo', 'DOCAthree']].max(axis=1)
 df['iso_bdt_min'] = df.loc[:, ['p0_IsoBDT', 'p1_IsoBDT', 'p2_IsoBDT']].min(axis=1)
 df['iso_min'] = df.loc[:, ['isolationa', 'isolationb', 'isolationc','isolationd', 'isolatione', 'isolationf']].min(axis=1)
 df['NEW_FD_LT']=df['FlightDistance']/df['LifeTime']
return df<feature_engineering>
sub = sum(
 map(
 lambda i: train_and_inference(splits, i)[0].set_index('recording_id'),
range(len(splits)) 
)
 ).reset_index()
train = add_features(train)
 test = add_features(test)
 check_agreement = add_features(check_agreement)
 check_correlation = add_features(check_correlation )<define_variables>
sub.to_csv("submission.csv", index=False )
filter_out = ['id','min_ANNmuon', 'production', 'mass', 'signal', 'SPDhits', 'isolationb', 'isolationc', 'DOCAone', 'DOCAtwo', 'DOCAthree','CDF1', 'CDF2', 'CDF3']
features = list(f for f in train.columns if f not in filter_out )<train_model>
model = create_model()
print('Training XGBoost model...')
 model_xgb = xgb.XGBClassifier()
params ={'nthread': 4,
'objective': 'binary:logistic',
'max_depth' : 8,
'min_child_weight': 3,
'learning_rate' : 0.1,
'n_estimators' : 300,
'subsample' : 0.9,
'colsample_bytree' : 0.5,
'silent': 1}
model_xgb.fit(train[features],train.signal )<predict_on_test>

 def grad_cam(model, inputs, class_index):
x = tf.keras.Input(( None, None, 3))
conv_y = model.get_layer(index=0 )(x, training=False)
 y = model.get_layer(index=1 )(conv_y, training=False)
 grad_model = tf.keras.Model(x, [conv_y, y])
 with tf.GradientTape() as tape:
conv_outputs, predictions = grad_model(inputs, training=False)
 loss = predictions[:, class_index]
grads = tape.gradient(loss, conv_outputs)
 cams = []
for grad, output in zip(grads, conv_outputs):
weights = tf.reduce_mean(grad, axis=(0, 1))
cam = tf.reduce_sum(tf.multiply(weights, output), axis=-1 ).numpy()
cams.append(cam)
 return cams
<save_to_csv><EOS> 
val_recids = splits[0][1]
parsed_val =(parsed_trainval 
.filter(create_recid_filter(val_recids)))
 vdataset =(parsed_val 
.map(_cut_wav_val)
.map(_wav_to_spec))