Kaggle competition: YKC-2nd
import torch_xla
import torch_xla.core.xla_model as xm<import_modules>
data_dir = "/kaggle/input/ykc-2nd/"
train = pd.read_csv(data_dir+"train.csv", index_col=0)
 test = pd.read_csv(data_dir+"test.csv", index_col=0)
 sub = pd.read_csv(data_dir+"sample_submission.csv", index_col=0)
 df = pd.concat([train, test])
 df["product_name"] = df["product_name"].map(lambda w: list(
 filter(None, list(filter(None,
re.sub("'", "",
re.sub(r"-|/|\\|\|"|\:|\;|\@|\^", " ",
re.sub(r"[0-9]+|\+|\&|\?|!|%|\.|,|\)|\(|'s|®|™|
"", w.lower().replace("\xa0", " ")))).split(" "))))))
df_train = df[~df.department_id.isna() ]
df_test = df[df.department_id.isna() ]
import numpy as np
import librosa as lb
import soundfile as sf
import pandas as pd
from pathlib import Path
import torch
from torch.utils.data import Dataset, DataLoader
from tqdm.notebook import tqdm
from resnest.torch import resnest50
import time<define_variables>
n_department = 21
n_split = 5
NUM_CLASSES = 24
SR = 32_000
DURATION = 10
STRIDE = 5
TEST_BATCH_SIZE = 30
TEST_NUM_WORKERS = 2
USE_PRE_COMPUTED_MFCC = True
DEVICE = xm.xla_device()
torch.set_default_tensor_type('torch.FloatTensor')
 TEST_AUDIO_ROOT = Path(".. /input/rfcx-species-audio-detection/test")
 TEST_MFCC_ROOT = ".. /input/kkiller-rfcx-test-mfcc-1-0400/test_mfcc_d10_s10_sr32000"<categorify>
docs = []
for i in range(n_department):
names = itertools.chain.from_iterable(df_train[df_train["department_id"] == i]["product_name"].tolist())
 docs.append(" ".join([n for n in names]))
vectorizer = TfidfVectorizer(max_df=0.9)
 X = vectorizer.fit_transform(docs)
 words = vectorizer.get_feature_names()
word_to_index = dict(zip(words, np.arange(len(words))))
X = X.toarray()
def mono_to_color(X, eps=1e-6, mean=None, std=None):
X = np.stack([X, X, X], axis=-1)
 mean = mean or X.mean()
std = std or X.std()
X =(X - mean)/(std + eps)
 _min, _max = X.min() , X.max()
if(_max - _min)> eps:
V = np.clip(X, _min, _max)
 V = 255 *(V - _min)/(_max - _min)
 V = V.astype(np.uint8)
 else:
V = np.zeros_like(X, dtype=np.uint8)
 return V
def normalize(image, mean=None, std=None):
image = image / 255.0
if mean is not None and std is not None:
image =(image - mean)/ std
return np.moveaxis(image, 2, 0 ).astype(np.float32)
 def crop_or_pad(y, length, sr, is_train=True):
if len(y)< length:
y = np.concatenate([y, np.zeros(length - len(y)) ])
 elif len(y)> length:
if not is_train:
start = 0
else:
start = np.random.randint(len(y)- length)
 y = y[start:start + length]
y = y.astype(np.float32, copy=False)
 return y<choose_model_class>
def calculate_score(word_list: list):

 result = np.zeros([n_department])
 n = 0
for w in word_list:
try:
result += X[:, word_to_index[w]]
n += 1
except KeyError as e:
pass
return result / n
class RFCXDataset(Dataset):
def __init__(self, data, sr, n_mels=128, fmin=0, fmax=None, num_classes=NUM_CLASSES, duration=DURATION, stride=STRIDE, root=None):
self.data = data
self.sr = sr
self.n_mels = n_mels
self.fmin = fmin
self.fmax = fmax or self.sr//2
self.num_classes = num_classes
self.duration = duration
self.stride = stride
self.audio_length = self.duration*self.sr
self.root = root or TEST_AUDIO_ROOT
self.mel_spec_computer = MelSpecComputer(sr=self.sr, n_mels=self.n_mels, fmin=self.fmin, fmax=self.fmax)
 self.res_type = "kaiser_best"
def __len__(self):
return len(self.data)
 def load(self, record):
y, _ = lb.load(self.root.joinpath(record ).with_suffix(".flac" ).as_posix() , sr=self.sr, res_type=self.res_type)
 return y
def load2(self, record):
y, orig_sr = sf.read(self.root.joinpath(record ).with_suffix(".flac" ).as_posix())
 y = lb.resample(y, orig_sr=orig_sr, target_sr=self.sr, res_type=self.res_type)
 return y
def read_index(self, idx):
d = self.data.iloc[idx]
record = d["recording_id"]
y = self.load2(record)
 window = self.duration*self.sr
stride = self.stride*self.sr
y = np.stack([y[i:i+window] for i in range(0, 60*self.sr+stride-window, stride)])
 return y
def process(self, y):
melspec = self.mel_spec_computer(y)
 image = mono_to_color(melspec)
 image = normalize(image, mean=None, std=None)
 return image
def __getitem__(self, idx):
y = self.read_index(idx)
 image = np.stack([self.process(_y)for _y in y])
 return image<load_pretrained>
tf_idf = df["product_name"].map(calculate_score)
 tf_idf = np.nan_to_num(np.array(tf_idf.tolist()))
max_value, min_value = tf_idf.max() , tf_idf.min()
tf_idf =(tf_idf - min_value)/(max_value - min_value)
 df = df.merge(pd.DataFrame(tf_idf.tolist() , columns=["dept_"+str(i)for i in range(n_department)]), left_index=True, right_index=True)
 df = df.reset_index()
class SimpleRFCXDataset(Dataset):
def __init__(self, data):
self.data = data
def __getitem__(self, idx):
row = self.data.iloc[idx]
record_id_path = Path(row.mfcc_root ).joinpath(row.recording_id ).with_suffix(".npy")
 image = np.load(record_id_path)
 return image
def __len__(self):
return len(self.data )<prepare_output>
model_ft = gensim.models.KeyedVectors.load_word2vec_format('.. /input/ykc-2nd/wiki-news-300d-1M.vec/wiki-news-300d-1M.vec' )
%%time
data = pd.DataFrame({
"recording_id": [path.stem for path in Path(TEST_AUDIO_ROOT ).glob("*.flac")],
})
 data["mfcc_root"] = TEST_MFCC_ROOT
print(data.shape)
 data.head()<define_variables>
!pip install hyphenate
TEST_MFCC_ROOTs = [
".. /input/kkiller-rfcx-test-mfcc-0000-0400/test_mfcc_d10_s2_sr32000_0000_0400",
".. /input/kkiller-rfcx-test-mfcc-0400-0800/test_mfcc_d10_s2_sr32000_0400_0800",
".. /input/kkiller-rfcx-test-mfcc-0800-1200/test_mfcc_d10_s2_sr32000_0800_1200",
".. /input/kkiller-rfcx-test-mfcc-1200-1600/test_mfcc_d10_s2_sr32000_1200_1600",
".. /input/kkiller-rfcx-test-mfcc-1600-2000/test_mfcc_d10_s2_sr32000_1600_2000",
]<merge>
from hyphenate import hyphenate_word
mfccs = []
for mfcc_root in TEST_MFCC_ROOTs:
mfccs += [(mfcc.stem, mfcc.parent.as_posix())for mfcc in Path(mfcc_root ).glob("*.npy")]
mfccs = pd.DataFrame(mfccs, columns = ["recording_id", 'mfcc_root'])
 data = data[["recording_id"]].merge(mfccs, on="recording_id")
 print(data.shape)
 data.head()<create_dataframe>
unused_words = defaultdict(int)
 def to_vec(x, model_ft):
v = np.zeros(model_ft.vector_size)
 for w in x:
try:
v += model_ft[w]
except:
hw = hyphenate_word(w)
 for w2 in hw:
try:
v += model_ft[w2]
except:
unused_words[w2] += 1
v = v /(np.sqrt(np.sum(v ** 2)) + 1e-16)
 return v
def to_vec_max(x, model_ft):
v = [np.zeros(model_ft.vector_size), np.zeros(model_ft.vector_size)]
for w in x:
try:
v.append(model_ft[w])
 except:
hw = hyphenate_word(w)
 for w2 in hw:
try:
v.append(model_ft[w2])
 except:
pass
v = np.max(v, axis=0)
 v = v /(np.sqrt(np.sum(v ** 2)) + 1e-16)
 return v
vecs = df["product_name"].apply(lambda x : to_vec(x, model_ft))
vecs = np.vstack(vecs)
 vecs = np.nan_to_num(vecs)
 vecs_max = df["product_name"].apply(lambda x : to_vec_max(x, model_ft))
vecs_max = np.vstack(vecs_max)
 vecs_max = np.nan_to_num(vecs_max)
 fasttext_pretrain_cols = [f"fasttext_pretrain_vec{k}" for k in range(vecs.shape[1])]
fasttext_pretrain_cols_max = [f"fasttext_pretrain_vec_max{k}" for k in range(vecs.shape[1])]
vec_df = pd.DataFrame(vecs, columns=fasttext_pretrain_cols)
 vec_max_df = pd.DataFrame(vecs_max, columns=fasttext_pretrain_cols_max)
 df = pd.concat([df, vec_df], axis = 1)
 df = pd.concat([df, vec_max_df], axis = 1)
 df.head()
ds = RFCXDataset(data=data, sr=SR )<create_dataframe>
df_train = df[~df.department_id.isna() ]
df_test = df[df.department_id.isna() ]
test_data = SimpleRFCXDataset(data)if(USE_PRE_COMPUTED_MFCC and TEST_MFCC_ROOT)else RFCXDataset(data=data, sr=SR)
 test_loader = DataLoader(test_data, batch_size=TEST_BATCH_SIZE, num_workers=TEST_NUM_WORKERS )<load_pretrained>
np.random.seed(42)
 trainCols = fasttext_pretrain_cols + fasttext_pretrain_cols_max + \
["order_rate", "order_dow_mode", "order_hour_of_day_mode"]
trainCols2 = ["dept_"+str(i)for i in range(n_department)] + ["order_rate", "order_dow_mode", "order_hour_of_day_mode"]
nn_cols = ["dept_"+str(i)for i in range(n_department)]
nn_cols2 = fasttext_pretrain_cols + fasttext_pretrain_cols_max + ["dept_"+str(i)for i in range(n_department)]
X_train = df_train[trainCols]
X2_train = df_train[trainCols2]
Xnn_train = df_train[nn_cols]
Xnn2_train = df_train[nn_cols2]
Y_train = df_train["department_id"]
Ynn_train = pd.get_dummies(df_train, columns=["department_id"])[[f"department_id_{i}.0" for i in range(21)]]
validInds = np.random.choice(X_train.index.values, 4000, replace = False)
 trainInds = np.setdiff1d(X_train.index.values, validInds )
def load_net(checkpoint_path):
net = resnest50(pretrained=True)
 n_features = net.fc.in_features
net.fc = torch.nn.Linear(n_features, NUM_CLASSES)
 dummy_device = torch.device("cpu")
 net.load_state_dict(torch.load(checkpoint_path, map_location=dummy_device))
net = net.to(DEVICE)
 net = net.eval()
return net<define_variables>
params = {
'lambda_l1': 0.0,
'lambda_l2': 0.0,
'num_leaves': 200,
'feature_fraction': 0.9840000000000001,
'bagging_fraction': 1.0,
'bagging_freq': 0,
'min_child_samples': 20,
'task': 'train',
'boosting_type': 'dart',
'objective': 'multiclass',
'num_class': 21,
'metric': 'multi_error',
'verbose': 1,
'learning_rate': 0.1,
'num_iterations': 2000,
'max_depth': 7
}
checkpoint_paths = [
".. /input/kkiller-rfcx-species-detection-public-checkpoints/rfcx_resnest50/rfcx_resnest_50_fold0.pth",
".. /input/kkiller-rfcx-species-detection-public-checkpoints/rfcx_resnest50/rfcx_resnest_50_fold1.pth",
".. /input/kkiller-rfcx-species-detection-public-checkpoints/rfcx_resnest50/rfcx_resnest_50_fold2.pth",
".. /input/kkiller-rfcx-species-detection-public-checkpoints/rfcx_resnest50/rfcx_resnest_50_fold3.pth",
".. /input/kkiller-rfcx-species-detection-public-checkpoints/rfcx_resnest50/rfcx_resnest_50_fold4.pth",
]
nets = [
load_net(checkpoint_path)for checkpoint_path in checkpoint_paths
]<predict_on_test>
lgb_train = lgb.Dataset(X_train.iloc[trainInds], Y_train.iloc[trainInds])
 lgb_val = lgb.Dataset(X_train.iloc[validInds], Y_train.iloc[validInds])
 best_params, history = {}, []
lgb_model = lgb.train(params, lgb_train,
valid_sets=lgb_val,
verbose_eval=10 )
preds = []
with torch.no_grad() :
for xb in tqdm(test_loader):
bsize, nframes = xb.shape[:2]
xb = xb.to(DEVICE ).view(bsize*nframes, *xb.shape[2:])
 pred = 0.
 for net in nets:
o = net(xb)
 o = torch.sigmoid(o)
 o = o.view(bsize, nframes, *o.shape[1:] ).max(1 ).values
o = o.detach().cpu().numpy()
pred += o
pred /= len(nets)
 preds.append(pred)
 preds = np.vstack(preds)
 preds.shape<prepare_output>
pred_val_lgb = lgb_model.predict(X_train.iloc[validInds])
 score = {
"logloss" : log_loss(Y_train.iloc[validInds], pred_val_lgb),
"f1_micro" : f1_score(Y_train.iloc[validInds], np.argmax(pred_val_lgb, axis = 1), average = "micro")}
pred_test_lgb = lgb_model.predict(df_test[trainCols] )
sub = pd.DataFrame(preds, columns=[f"s{i}" for i in range(24)])
 sub["recording_id"] = data["recording_id"].values[:len(sub)]
sub = sub[["recording_id"] + [f"s{i}" for i in range(24)]]
print(sub.shape)
 sub.head()<save_to_csv>
params = {
'lambda_l1': 0.0,
'lambda_l2': 0.0,
'num_leaves': 111,
'feature_fraction': 0.9840000000000001,
'bagging_fraction': 1.0,
'bagging_freq': 0,
'min_child_samples': 20,
'task': 'train',
'boosting_type': 'dart',
'objective': 'multiclass',
'num_class': 21,
'metric': 'multi_error',
'verbose': 1,
'learning_rate': 0.05,
'num_iterations': 1000
}
sub.to_csv("submission.csv", index=False )<install_modules>
lgb_train2 = lgb.Dataset(X2_train.iloc[trainInds], Y_train.iloc[trainInds])
 lgb_val2 = lgb.Dataset(X2_train.iloc[validInds], Y_train.iloc[validInds])
 best_params, history = {}, []
lgb_model2 = lgb.train(params, lgb_train2,
valid_sets=lgb_val2,
verbose_eval=10 )
!pip install iterative-stratification
!pip install tf-nightly
!pip install --upgrade pip<import_modules>
pred_val_lgb2 = lgb_model2.predict(X2_train.iloc[validInds])
 score = {
"logloss" : log_loss(Y_train.iloc[validInds], pred_val_lgb2),
"f1_micro" : f1_score(Y_train.iloc[validInds], np.argmax(pred_val_lgb2, axis = 1), average = "micro")}
pred_test_lgb2 = lgb_model2.predict(df_test[trainCols2] )
tf.__version__<init_hyperparams>
nn_model1 = tf.keras.Sequential([
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(21, activation='softmax')
 ] )
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
}
}<define_variables>
nn_model1.compile(optimizer=tf.optimizers.Adam(lr=0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
 es_cb = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, verbose=0, mode='auto' )
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"<define_variables>
history = nn_model1.fit(Xnn2_train.iloc[trainInds], Ynn_train.iloc[trainInds], validation_data=(Xnn2_train.iloc[validInds], Ynn_train.iloc[validInds]), epochs=100, batch_size=16, verbose=2, callbacks=[es_cb] )
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<create_dataframe>
pred_val_nn1 = nn_model1.predict(Xnn2_train.iloc[validInds])
 pred_test_nn1 = nn_model1.predict(df_test[nn_cols2] )
raw_dataset = tf.data.TFRecordDataset([TRAIN_TFREC + '/00-148.tfrec'])
 raw_dataset<categorify>
log_loss(Y_train.iloc[validInds], pred_val_nn1), f1_score(Y_train.iloc[validInds], np.argmax(pred_val_nn1, axis = 1), average = "micro" )
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()<categorify>
nn_model = tf.keras.Sequential([
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(21, activation='softmax')
 ] )
@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<feature_engineering>
nn_model.compile(optimizer=tf.optimizers.Adam(lr=0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
 es_cb = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, verbose=0, mode='auto' )
@tf.function
def _filtTP(x):
return x['is_tp'] == 1<data_type_conversions>
nn_cols = ["dept_"+str(i)for i in range(n_department)]
Xnn_train = df_train[nn_cols]
Y_train = df_train["department_id"]
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 )<normalization>
history = nn_model.fit(Xnn_train.iloc[trainInds], Ynn_train.iloc[trainInds], validation_data=(Xnn_train.iloc[validInds], Ynn_train.iloc[validInds]), epochs=100, batch_size=16, verbose=2, callbacks=[es_cb] )
@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 )<categorify>
pred_val_nn = nn_model.predict(Xnn_train.iloc[validInds])
 Xnn_test = df_test[nn_cols]
pred_test_nn = nn_model.predict(Xnn_test )
@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 )<normalization>
log_loss(Y_train.iloc[validInds], pred_val_nn), f1_score(Y_train.iloc[validInds], np.argmax(pred_val_nn, axis = 1), average = "micro" )
@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"] )<choose_model_class>
rfc = RandomForestClassifier(random_state=0, n_estimators=1000, n_jobs=-1, verbose=1)
 rfc.fit(Xnn_train.iloc[trainInds], Y_train.iloc[trainInds] )
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()<prepare_x_and_y>
pred_val_rfc = rfc.predict_proba(Xnn_train.iloc[validInds])
 pred_test_rfc = rfc.predict_proba(Xnn_test )
@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<create_dataframe>
log_loss(Y_train.iloc[validInds], pred_val_rfc), f1_score(Y_train.iloc[validInds], np.argmax(pred_val_rfc, axis = 1), average = "micro" )
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))<load_from_csv>
rfc2 = RandomForestClassifier(random_state=0, n_estimators=1000, n_jobs=-1, verbose=1)
 rfc2.fit(X_train.iloc[trainInds], Y_train.iloc[trainInds] )
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))<categorify>
pred_val_rfc2 = rfc2.predict_proba(X_train.iloc[validInds])
 pred_test_rfc2 = rfc2.predict_proba(df_test[trainCols])
 log_loss(Y_train.iloc[validInds], pred_val_rfc2), f1_score(Y_train.iloc[validInds], np.argmax(pred_val_rfc2, axis = 1), average = "micro" )
def create_recid_filter(recids):
@tf.function
def _filt(x):
return tf.reduce_any(recids == x['recording_id'])
 return _filt<categorify>
svc_clf = make_pipeline(StandardScaler() , SVC(gamma='auto', probability=True))
svc_clf.fit(Xnn_train.iloc[trainInds], Y_train.iloc[trainInds] )
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<categorify>
pred_val_svc = svc_clf.predict_proba(Xnn_train.iloc[validInds])
 pred_test_svc = svc_clf.predict_proba(Xnn_test )
@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 )<categorify>
log_loss(Y_train.iloc[validInds], pred_val_svc), f1_score(Y_train.iloc[validInds], np.argmax(pred_val_svc, axis = 1), average = "micro" )
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<train_model>
svc_clf2 = make_pipeline(StandardScaler() , SVC(gamma='auto', probability=True))
svc_clf2.fit(X_train.iloc[trainInds], Y_train.iloc[trainInds] )
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<train_model>
pred_val_svc2 = svc_clf2.predict_proba(X_train.iloc[validInds] )
sub = sum(
 map(
 lambda i: train_and_inference(splits, i)[0].set_index('recording_id'),
range(len(splits)) 
)
 ).reset_index()<save_to_csv>
pred_test_svc2 = svc_clf2.predict_proba(df_test[trainCols])
 log_loss(Y_train.iloc[validInds], pred_val_svc2), f1_score(Y_train.iloc[validInds], np.argmax(pred_val_svc2, axis = 1), average = "micro" )
sub.to_csv("submission.csv", index=False )<choose_model_class>
val_preds_all = np.hstack([pred_val_lgb2, pred_val_lgb, pred_val_nn, pred_val_nn1, pred_val_svc, pred_val_svc2, pred_val_rfc, pred_val_rfc2])
 test_preds_all = np.hstack([pred_test_lgb2, pred_test_lgb, pred_test_nn, pred_test_nn1, pred_test_svc, pred_test_svc2, pred_test_rfc, pred_test_rfc2] )
model = create_model()<import_modules>
clf = LogisticRegression(multi_class="ovr" ).fit(val_preds_all, Y_train.iloc[validInds] )
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<categorify>
prediction = clf.predict(test_preds_all)
 prediction
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))<import_modules>
sub["department_id"] = prediction.astype(int)
 sub.to_csv("submission.csv", index = False)
 sub.head()
tf.__version__<init_hyperparams>
sub.to_csv('submission.csv' )
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': 64,
'iteration_per_epoch': 64,
'epoch': 18,
'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': 18*64, 'warmup_proportion': 0.3, 'min_lr': 1e-6},
},
'mixup': False
}
}<define_variables>
df.to_csv("dataframe_all.csv")
 np.savetxt("val_preds_all.csv", val_preds_all, delimiter=",")
 np.savetxt("test_preds_all.csv", test_preds_all, delimiter=",")
 np.save("trainInds", trainInds)
 np.save("validInds", validInds )
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"<define_variables>
df_test.to_csv("dataframe_test.csv" )