Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<choose_model_class>
import pandas as pd
import numpy as np
from sklearn.model_selection import cross_val_score
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten
from keras.layers.normalization import BatchNormalization
from keras.wrappers.scikit_learn import KerasClassifier
from keras.preprocessing.image import ImageDataGenerator
def get_model() :
with strategy.scope() :
model = RFCX_MODEL()
model.summary()
model.compile(
 optimizer="adam",
loss = masked_bce_focal,
metrics = [LWLRAP(num_classes = params.num_classes),
])
 return model<create_dataframe>
train_data = pd.read_csv('.. /input/digit-recognizer/train.csv')
 x_train = train_data.drop(columns = 'label')
 y_train = train_data['label']
x_test = pd.read_csv('.. /input/digit-recognizer/test.csv' )
def get_test_dataset(filenames, training = False):
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads = AUTO)
 dataset = dataset.map(read_unlabeled_tfrecord , num_parallel_calls = AUTO ).unbatch()
dataset = dataset.map(lambda spec : waveform_to_log_mel_spectrogram(spec['audio_wav'], spec['recording_id']), num_parallel_calls = AUTO)
 dataset = dataset.map(preprocess, num_parallel_calls = AUTO)
 return dataset.batch(GLOBAL_BATCH_SIZE*4 ).cache()<load_pretrained>
print('Train data missing values: %2d' %x_train.isnull().any().sum())
 print('Test data missing values: %2d' %x_test.isnull().any().sum() )
test_predict = []
test_data = get_test_dataset(TEST_FILES, training = False)
 test_audio = test_data.map(lambda frames, recording_id: frames)
 model = get_model()
for fold in range(N_FOLDS):
model.load_weights(f'{MODEL_DIR}/RFCX_model_fold {fold}.h5')
 test_predict.append(model.predict(test_audio, verbose = 1))<load_from_csv>
x_train = x_train.values.astype('float32')/ 255
x_test = x_test.values.astype('float32')/ 255
x_train = x_train.reshape(-1,28,28,1)
 x_test = x_test.reshape(-1,28,28,1)
 y_train = np_utils.to_categorical(y_train.values )
SUB = pd.read_csv('.. /input/rfcx-species-audio-detection/sample_submission.csv')
 NREC =(60 - CUT)//STRIDE + 1
predict = np.array(test_predict ).reshape(N_FOLDS, len(SUB), NREC, params.num_classes)
 predict = np.mean(np.max(predict ,axis = 2), axis = 0)
 recording_id = test_data.map(lambda frames, recording_id: recording_id ).unbatch()
test_ids = next(iter(recording_id.batch(len(SUB)* NREC)) ).numpy().astype('U' ).reshape(len(SUB), NREC)
 pred_df = pd.DataFrame({ 'recording_id' : test_ids[:, 0],
**{f's{i}' : predict[:, i] for i in range(params.num_classes)} } )<save_to_csv>
def cnn_digit(n_pre, n_filters, input_dim, n_dense, n_nodes, drop, n_out, act):
model = Sequential()
for i in range(n_pre):
if i == 0:
model.add(Conv2D(filters = n_filters,
kernel_size =(3,3),
strides =(1,1),
input_shape =(input_dim[0],input_dim[1],input_dim[2]),
activation = 'relu'))
else:
model.add(Conv2D(filters = n_filters,
kernel_size =(3,3),
strides =(1,1),
activation = 'relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size =(2,2)))
 model.add(Flatten())
 for i in range(n_dense - 1):
model.add(Dense(units = n_nodes, activation = 'relu'))
model.add(Dropout(drop))
model.add(Dense(units = n_out, activation = act))
model.compile(loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'])
 return model
pred_df.sort_values('recording_id', inplace = True)
 pred_df.to_csv('submission.csv', index = False )<normalization>
first_model = cnn_digit(n_pre = 1,
n_filters = 16,
input_dim = x_train.shape[1:],
n_dense = 1,
n_nodes = 32,
drop = 0.2,
n_out = 10,
act = 'softmax')
 first_model.fit(x = x_train, y = y_train,
epochs = 50, batch_size = 128 )
MODE = 1
FUDGE = 2.0
for k in range(24):
pred_df.iloc[:,1+k] -= pred_df.iloc[:,1+k].min()
pred_df.iloc[:,1+k] /= pred_df.iloc[:,1+k].max()
def scale(probs, factor):
probs = probs.copy()
idx = np.where(probs!=1)[0]
odds = factor * probs[idx] /(1-probs[idx])
 probs[idx] = odds/(1+odds)
 return probs
d1 = pred_df.iloc[:,1:].mean().values
d2 = np.array([113, 204, 44, 923, 53, 41, 3, 213, 44, 23, 26, 149, 255,
14, 123, 222, 46, 6, 474, 4, 17, 18, 23, 72])/1000.
 for k in range(24):
if MODE==1: d = FUDGE
if MODE==2: d = d1[k]/(1-d1[k])
 if MODE==3: s = d2[k] / d1[k]
else: s =(d2[k]/(1-d2[k])) /d
pred_df.iloc[:,k+1] = scale(pred_df.iloc[:,k+1].values,s)
 pred_df.to_csv('submission_with_pp.csv',index=False )<install_modules>
aug_train = ImageDataGenerator(rotation_range = 7,
horizontal_flip = True,
shear_range = 0.2,
height_shift_range = 0.07,
zoom_range = 0.2)
 aug_train = aug_train.flow(x_train, y_train, batch_size = 128 )
! pip install -q efficientnet<import_modules>
aug_model = cnn_digit(n_pre = 2,
n_filters = 64,
input_dim = x_train.shape[1:],
n_dense = 2,
n_nodes = 128,
drop = 0.2,
n_out = 10,
act = 'softmax')
 aug_model.fit_generator(aug_train,
steps_per_epoch = x_train.shape[0]/128,
epochs = 150 )
import math, os, re, warnings, random
import tensorflow as tf
import numpy as np
import pandas as pd
import librosa
from kaggle_datasets import KaggleDatasets
import matplotlib.pyplot as plt
from IPython.display import Audio
from tensorflow.keras import Model, layers
from sklearn.model_selection import KFold
import tensorflow.keras.backend as K
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, LearningRateScheduler
from tensorflow.keras.layers import GlobalAveragePooling2D, Input, Dense, Dropout, GaussianNoise
from tensorflow.keras.applications import ResNet50
import efficientnet.keras as efn
import seaborn as sns<set_options>
pred = [np.argmax(x)for x in aug_model.predict(x_test)]
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print(f'Running on TPU {tpu.master() }')
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
print(f'REPLICAS: {REPLICAS}' )<define_variables>
sub_file = pd.DataFrame(columns = ['ImageId', 'Label'])
 sub_file['ImageId'] = list(range(1,len(x_test)+1))
sub_file['Label'] = pred
sub_file.to_csv('submission.csv', index = False )