Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<set_options>
GlobalAveragePooling2D, Conv2D, BatchNormalization, Dropout
INPUT_DIR = '.. /input'
EMB_SIZE = 8
BATCH_SIZE = 1024
N_FOLDS = 2
N_ITER = 50
SEED = 32
sr = 22050
T = 5.0
t = np.linspace(0, T, int(T*sr), endpoint=False)
 x = 0.5*np.sin(2*np.pi*220*t)
 ipd.Audio(x, rate=sr)
 librosa.output.write_wav('tone_220.wav', x, sr )<normalization>

 def _all_diffs(a, b):

 return tf.expand_dims(a, axis=1)- tf.expand_dims(b, axis=0)
 def _cdist(a, b, metric='euclidean'):

 with tf.name_scope("_cdist"):
diffs = _all_diffs(a, b)
 if metric == 'sqeuclidean':
return tf.reduce_sum(tf.square(diffs), axis=-1)
 elif metric == 'euclidean':
return tf.sqrt(tf.reduce_sum(tf.square(diffs), axis=-1)+ 1e-12)
 elif metric == 'cityblock':
return tf.reduce_sum(tf.abs(diffs), axis=-1)
 else:
raise NotImplementedError(
 'The following metric is not implemented by `_cdist` yet: {}'.format(metric))
_cdist.supported_metrics = [
'euclidean',
'sqeuclidean',
'cityblock',
]
def _get_at_indices(tensor, indices):

 counter = tf.range(tf.shape(indices, out_type=indices.dtype)[0])
 return tf.gather_nd(tensor, tf.stack(( counter, indices), -1))
def batch_hard_loss(features, pids, metric='euclidean', margin=0.1):

 with tf.name_scope("batch_hard_loss"):
dists = _cdist(features, features, metric=metric)
 pids = tf.argmax(pids, axis=1)
 exp_dims0 = tf.expand_dims(pids, axis=0)
 exp_dims1 = tf.expand_dims(pids, axis=1)
 same_identity_mask = tf.equal(exp_dims1, exp_dims0)
 negative_mask = tf.logical_not(same_identity_mask)
 positive_mask = tf.logical_xor(same_identity_mask,
tf.eye(tf.shape(pids)[0], dtype=tf.bool))
furthest_positive = tf.reduce_max(dists*tf.cast(positive_mask, tf.float32), axis=1)
 closest_negative = tf.reduce_min(dists + 1e5*tf.cast(same_identity_mask, tf.float32), axis=1)
 diff = furthest_positive - closest_negative
if isinstance(margin, numbers.Real):
diff = tf.maximum(diff + margin, 0.0)
 elif margin == 'soft':
diff = tf.nn.softplus(diff)
 elif margin is None:
pass
else:
raise NotImplementedError('The margin {} is not implemented in batch_hard_loss'.format(margin))
return diff
def triplet_loss(labels, features):
return tf.reduce_mean(batch_hard_loss(features, labels, margin=0.2))
zero_crossings = librosa.zero_crossings(x[n0:n1], pad=False)
 print(sum(zero_crossings))<import_modules>
def f1(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 precision = true_positives /(predicted_positives + K.epsilon())
 return 2 *(( precision * recall)/(precision + recall + K.epsilon()))
import cv2
import audioread
import logging
import os
import random
import time
import warnings
import librosa
import numpy as np
import pandas as pd
import soundfile as sf
import torch
import torch.nn as nn
import torch.cuda
import torch.nn.functional as F
import torch.utils.data as data
from contextlib import contextmanager
from pathlib import Path
from typing import Optional
from fastprogress import progress_bar
from sklearn.metrics import f1_score
from torchvision import models<set_options>

 df_train = pd.read_csv(os.path.join(INPUT_DIR, 'train.csv'))
df_test = pd.read_csv(os.path.join(INPUT_DIR, 'test.csv'))
print(df_train.head() )
def set_seed(seed: int = 42):
random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
def get_logger(out_file=None):
logger = logging.getLogger()
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
 logger.handlers = []
logger.setLevel(logging.INFO)
 handler = logging.StreamHandler()
handler.setFormatter(formatter)
 handler.setLevel(logging.INFO)
 logger.addHandler(handler)
 if out_file is not None:
fh = logging.FileHandler(out_file)
 fh.setFormatter(formatter)
 fh.setLevel(logging.INFO)
 logger.addHandler(fh)
 logger.info("logger set up")
 return logger
@contextmanager
def timer(name: str, logger: Optional[logging.Logger] = None):
t0 = time.time()
msg = f"[{name}] start"
if logger is None:
print(msg)
 else:
logger.info(msg)
 yield
msg = f"[{name}] done in {time.time() - t0:.2f} s"
if logger is None:
print(msg)
 else:
logger.info(msg )<set_options>

 x_train = df_train.iloc[:,1:].values.astype('float32')/ 255.
 x_test = df_test.values.astype('float32')/ 255.
 xc_train = np.reshape(x_train,(len(x_train), 28, 28, 1))
xc_test = np.reshape(x_test,(len(x_test), 28, 28, 1))
y_train = df_train.label.values
yc_train = to_categorical(y_train)
 input_size = output_size = x_train.shape[1]
input_csize = output_csize = xc_train.shape[1]
print(xc_train[:5], xc_test[:5])
logger = get_logger("main.log")
 set_seed(1213 )<define_variables>

 def base_network(model_type='triplet', input_shape=input_csize):

 if model_type == 'autoencoder':
pass
elif model_type == 'triplet':
model = Sequential([
Conv2D(filters=64, kernel_size=(3, 3), padding='same', input_shape=(input_csize, input_csize, 1,), activation='relu'),
Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu'),
BatchNormalization() ,
MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'),
Dropout(0.25),
Conv2D(filters=128, kernel_size=(3, 3), padding='same', activation='relu'),
Conv2D(filters=128, kernel_size=(3, 3), padding='same', activation='relu'),
BatchNormalization() ,
MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'),
Dropout(0.25),
Conv2D(filters=256, kernel_size=(3, 3), padding='same', activation='relu'),
Conv2D(filters=256, kernel_size=(3, 3), padding='same', activation='relu'),
BatchNormalization() ,
MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'),
Dropout(0.25),
Flatten() ,
Dense(512, activation='relu'),
Dense(256, activation='relu'),
BatchNormalization() ,
Dropout(0.25),
Dense(10, name='embeddings', activation='softmax'),
])
 return model
TARGET_SR = 32000
TEST = Path(".. /input/birdsong-recognition/test_audio" ).exists()<load_from_csv>

 datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(xc_train )
if TEST:
DATA_DIR = Path(".. /input/birdsong-recognition/")
 else:
DATA_DIR = Path(".. /input/birdcall-check/")
 test = pd.read_csv(DATA_DIR / "test.csv")
 test_audio = DATA_DIR / "test_audio"
test.head()<save_to_csv>

 yfull_test = []
skf = StratifiedKFold(n_splits=N_FOLDS, random_state=SEED, shuffle=True)
 print(len(xc_train), len(y_train))
for i,(train_index, val_index)in enumerate(skf.split(xc_train, y_train)) :
triplet_model = base_network()
triplet_model.compile(optimizer=RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0),
loss=['categorical_crossentropy'], metrics=[f1])
 weights_path = os.path.join('.', f'w{i}.h5')
 callbacks=[
ReduceLROnPlateau(monitor='val_f1', patience=3, verbose=1, factor=0.5, min_lr=0.00001),
ModelCheckpoint(weights_path, monitor='val_f1', mode='max', save_best_only=True, verbose=0)
 ]
xb_train = xc_train[train_index]
yb_train = yc_train[train_index]
xb_val = xc_train[val_index]
yb_val = yc_train[val_index]
history = triplet_model.fit_generator(
 datagen.flow(xb_train, yb_train, batch_size=BATCH_SIZE),
validation_data=datagen.flow(xb_val, yb_val, batch_size=BATCH_SIZE),
validation_steps=xb_val.shape[0] // BATCH_SIZE,
verbose=2,
epochs=N_ITER,
steps_per_epoch=xb_train.shape[0] // BATCH_SIZE,
shuffle=True,
callbacks=callbacks)
 if os.path.isfile(weights_path):
triplet_model.load_weights(weights_path)
 print(fbeta_score(yb_val, np.array(triplet_model.predict(xb_val, batch_size=128, verbose=2)) > 0.2, beta=2, average='samples'))
yfull_test.append(triplet_model.predict(xc_test, batch_size=128, verbose=2))
sub = pd.read_csv(".. /input/birdsong-recognition/sample_submission.csv")
 sub.to_csv("submission.csv", index=False )<choose_model_class>
pred = np.array(yfull_test)
 pred = np.argmax(pred, axis=2)
 values, counts = np.unique(pred, axis=0, return_counts=True)
 pred = values[np.argmax(counts)]
print(pred.shape )
class ResNet(nn.Module):
def __init__(self, base_model_name: str, pretrained=False,
num_classes=264):
super().__init__()
base_model = models.__getattribute__(base_model_name )(
 pretrained=pretrained)
 layers = list(base_model.children())[:-2]
layers.append(nn.AdaptiveMaxPool2d(1))
self.encoder = nn.Sequential(*layers)
 in_features = base_model.fc.in_features
self.classifier = nn.Sequential(
 nn.Linear(in_features, 1024), nn.ReLU() , nn.Dropout(p=0.2),
nn.Linear(1024, 1024), nn.ReLU() , nn.Dropout(p=0.2),
nn.Linear(1024, num_classes))
def forward(self, x):
batch_size = x.size(0)
 x = self.encoder(x ).view(batch_size, -1)
 x = self.classifier(x)
 multiclass_proba = F.softmax(x, dim=1)
 multilabel_proba = F.sigmoid(x)
 return {
"logits": x,
"multiclass_proba": multiclass_proba,
"multilabel_proba": multilabel_proba
}<define_variables>
model_config = {
"base_model_name": "resnet50",
"pretrained": False,
"num_classes": 264
}
melspectrogram_parameters = {
"n_mels": 128,
"fmin": 20,
"fmax": 16000
}
weights_path = ".. /input/birdcall-resnet50-init-weights/best.pth"<define_variables>
submission = pd.DataFrame({'ImageId': range(1, pred.shape[0]+1), 'Label': pred})
 submission.to_csv('submission.csv', index=False )