{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"A_step2_inference_BirdClef","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom tqdm import tqdm\nimport numpy as np\nfrom sklearn.model_selection import StratifiedKFold\nimport cv2\nimport os\nimport matplotlib.pyplot as plt\nfrom math import ceil\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# repeat for shorter audio\ndef repeat_one_axis(x_, nb):\n    return np.repeat(x_[np.newaxis,:], nb, axis=0).flatten()\n\naa = np.array([1,2])\nrepeat_one_axis(aa, 4)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:19.328496Z","iopub.execute_input":"2022-03-06T07:45:19.328731Z","iopub.status.idle":"2022-03-06T07:45:19.338421Z","shell.execute_reply.started":"2022-03-06T07:45:19.328705Z","shell.execute_reply":"2022-03-06T07:45:19.337936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ONLY_SCORED = True\n\n\nTEST_AUDIOS = [_name for _name in os.listdir('../input/birdclef-2022/test_soundscapes') if 'ogg' in _name]\n\nsub = pd.DataFrame({\"filename\":TEST_AUDIOS})\nsub['path'] = '../input/birdclef-2022/test_soundscapes/' + sub['filename']\nsub['file_id'] = sub['filename'].str.replace('.ogg','')","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:19.339498Z","iopub.execute_input":"2022-03-06T07:45:19.339876Z","iopub.status.idle":"2022-03-06T07:45:19.367491Z","shell.execute_reply.started":"2022-03-06T07:45:19.339834Z","shell.execute_reply":"2022-03-06T07:45:19.36683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:19.368954Z","iopub.execute_input":"2022-03-06T07:45:19.369343Z","iopub.status.idle":"2022-03-06T07:45:19.381662Z","shell.execute_reply.started":"2022-03-06T07:45:19.369305Z","shell.execute_reply":"2022-03-06T07:45:19.380742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train.primary_label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:19.632213Z","iopub.execute_input":"2022-03-06T07:45:19.632807Z","iopub.status.idle":"2022-03-06T07:45:19.636992Z","shell.execute_reply.started":"2022-03-06T07:45:19.63277Z","shell.execute_reply":"2022-03-06T07:45:19.636004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## PARAMTERS","metadata":{}},{"cell_type":"code","source":"SR  = 32_000\nDURATION = 5\nNMELS = 128","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:19.971864Z","iopub.execute_input":"2022-03-06T07:45:19.972645Z","iopub.status.idle":"2022-03-06T07:45:19.978013Z","shell.execute_reply.started":"2022-03-06T07:45:19.972584Z","shell.execute_reply":"2022-03-06T07:45:19.976702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## MAKING TF RECORDS","metadata":{}},{"cell_type":"code","source":"import librosa as lb\nimport soundfile as sf\nimport tensorflow as tf\n","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:20.434903Z","iopub.execute_input":"2022-03-06T07:45:20.435142Z","iopub.status.idle":"2022-03-06T07:45:25.651926Z","shell.execute_reply.started":"2022-03-06T07:45:20.435119Z","shell.execute_reply":"2022-03-06T07:45:25.651239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_audio(filename):\n    audio, orig_sr = sf.read(filename, dtype=\"float32\")\n    if orig_sr !=SR:\n        audio = lb.resample(audio, orig_sr,SR , res_type=\"kaiser_fast\")\n    if len(audio.shape)>1:audio = audio[:, 0]\n    return audio\n#=======================================================\nclass MelSpecComputer:\n    def __init__(self, sr, n_mels, fmin, fmax, **kwargs):\n        self.sr = sr\n        self.n_mels = n_mels\n        self.fmin = fmin\n        self.fmax = fmax\n        kwargs[\"n_fft\"] = kwargs.get(\"n_fft\", self.sr//10)\n        kwargs[\"hop_length\"] = kwargs.get(\"hop_length\", self.sr//(10*4))\n        self.kwargs = kwargs\n\n    def __call__(self, y):\n\n        melspec = lb.feature.melspectrogram(\n            y, sr=self.sr, n_mels=self.n_mels, fmin=self.fmin, fmax=self.fmax, **self.kwargs,\n        )\n\n        melspec = lb.power_to_db(melspec).astype(np.float32)\n        return melspec\n#================================================","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:25.653985Z","iopub.execute_input":"2022-03-06T07:45:25.654311Z","iopub.status.idle":"2022-03-06T07:45:25.66316Z","shell.execute_reply.started":"2022-03-06T07:45:25.654262Z","shell.execute_reply":"2022-03-06T07:45:25.662639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    # TPU detection. No parameters necessary if TPU_NAME environment variable is\n    # set: this is always the case on Kaggle.\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Running on TPU ', tpu.master())\nexcept ValueError:\n    tpu = None\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.TPUStrategy(tpu)\nelse:\n    # Default distribution strategy in Tensorflow. Works on CPU and single GPU.\n    strategy = tf.distribute.get_strategy()\n\nAUTO = tf.data.experimental.AUTOTUNE\nprint(\"REPLICAS: \", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:25.664164Z","iopub.execute_input":"2022-03-06T07:45:25.664904Z","iopub.status.idle":"2022-03-06T07:45:25.686034Z","shell.execute_reply.started":"2022-03-06T07:45:25.664871Z","shell.execute_reply":"2022-03-06T07:45:25.685398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _bytes_feature(value):\n    \"\"\"Returns a bytes_list from a string / byte.\"\"\"\n    if isinstance(value, type(tf.constant(0))):\n        value = value.numpy() # BytesList won't unpack a string from an EagerTensor.\n    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))\n\ndef _float_feature(value):\n    \"\"\"Returns a float_list from a float / double.\"\"\"\n    return tf.train.Feature(float_list=tf.train.FloatList(value=value))\n\ndef _int64_feature(value):\n    \"\"\"Returns an int64_list from a bool / enum / int / uint.\"\"\"\n    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))\n#==============================================#\ndef train_serialize_example(feature0, feature1, feature2):\n    feature = {\n      'filename'         : _bytes_feature(feature0),\n      'time'      : _int64_feature(feature1),\n      'audio'         : _bytes_feature(feature2),    \n  }\n    example_proto = tf.train.Example(features=tf.train.Features(feature=feature))\n    return example_proto.SerializeToString()\n#==============================================","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:25.688234Z","iopub.execute_input":"2022-03-06T07:45:25.688536Z","iopub.status.idle":"2022-03-06T07:45:25.697554Z","shell.execute_reply.started":"2022-03-06T07:45:25.688511Z","shell.execute_reply":"2022-03-06T07:45:25.696814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EXTRACTOR = MelSpecComputer(sr=SR, n_mels=NMELS, fmin=0,fmax=None)\nEPS = 1e-5\nif 'data' not in os.listdir(): os.mkdir('./data')\nWAVE_LENGTH = DURATION * SR\n\ndef make_tf_record(df):\n    with tf.io.TFRecordWriter(f'./data/test.tfrec') as writer:\n        for i,row in tqdm(df.iterrows()):\n            # loading audio with label\n            filepath = row['path']\n            filename = row['filename']\n            audio = get_audio(filepath)\n            # making batches\n            nb_batches = ceil(audio.shape[0] / WAVE_LENGTH)\n            \n            for cnt in range(nb_batches):\n                audio_batch = audio[cnt*WAVE_LENGTH:(cnt+1)*WAVE_LENGTH]\n                if len(audio_batch)<WAVE_LENGTH:\n                    if cnt==0:\n                        rep = round(float(WAVE_LENGTH)/len(audio_batch))\n                        audio_batch = repeat_one_axis(audio_batch, rep)\n                    else:\n                        audio_batch = audio[-WAVE_LENGTH:]\n                #\n                mel = EXTRACTOR(audio_batch)\n                _min, _max = mel.min(), mel.max()\n                mel = 255 * (mel - _min ) / (_max - _min + EPS)\n                mel = mel.astype(np.uint8)\n                img = np.stack([mel, mel, mel], axis=-1)\n                \n                example = train_serialize_example(str.encode(filename), \n                                                  (cnt+1)*DURATION, \n                                                  cv2.imencode('.png', img)[1].tobytes())\n                writer.write(example)\n                #end for\n            #\n    return 0","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:25.699499Z","iopub.execute_input":"2022-03-06T07:45:25.699709Z","iopub.status.idle":"2022-03-06T07:45:25.712046Z","shell.execute_reply.started":"2022-03-06T07:45:25.699685Z","shell.execute_reply":"2022-03-06T07:45:25.711154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"make_tf_record(sub)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:25.713294Z","iopub.execute_input":"2022-03-06T07:45:25.713596Z","iopub.status.idle":"2022-03-06T07:45:26.177385Z","shell.execute_reply.started":"2022-03-06T07:45:25.71357Z","shell.execute_reply":"2022-03-06T07:45:26.176657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## READBACK","metadata":{}},{"cell_type":"code","source":"IMG_SIZE = 128\n\ndef read_test_tfrecord(example):\n    tfrec_format = {\n        'audio'                        : tf.io.FixedLenFeature([], tf.string),\n        'filename'                        : tf.io.FixedLenFeature([], tf.string),\n        'time'                       : tf.io.FixedLenFeature([], tf.int64)\n    }           \n    example = tf.io.parse_single_example(example, tfrec_format)\n    return example['filename'], example['time'], example[\"audio\"]\n#=====\ndef parse_test(fname, cnt, img):   \n    img = tf.image.decode_png(img, channels=3)\n    img = tf.cast(img, tf.float32) / 255.0\n    img = tf.image.resize(img, [IMG_SIZE, IMG_SIZE])\n    return fname, cnt, img\n#===========\ndef make_test_dataset(filenames, batch_size=64):\n    ds = tf.data.TFRecordDataset(filenames)\n    ds  = ds.map(read_test_tfrecord)\n    ds = ds.map(parse_test)\n    ds = ds.batch(batch_size)\n    ds = ds.prefetch(AUTO)\n    return ds\n#==================","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:26.178656Z","iopub.execute_input":"2022-03-06T07:45:26.179094Z","iopub.status.idle":"2022-03-06T07:45:26.192186Z","shell.execute_reply.started":"2022-03-06T07:45:26.179055Z","shell.execute_reply":"2022-03-06T07:45:26.191407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 516\nds_test  = make_test_dataset(['./data/test.tfrec'], batch_size=BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:26.193663Z","iopub.execute_input":"2022-03-06T07:45:26.194158Z","iopub.status.idle":"2022-03-06T07:45:26.413227Z","shell.execute_reply.started":"2022-03-06T07:45:26.194096Z","shell.execute_reply":"2022-03-06T07:45:26.412346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NETS = [tf.keras.models.load_model(f'../input/bird-cnn/w{idx}.h5') for idx in range(5)]","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:26.414691Z","iopub.execute_input":"2022-03-06T07:45:26.415012Z","iopub.status.idle":"2022-03-06T07:45:27.610569Z","shell.execute_reply.started":"2022-03-06T07:45:26.414975Z","shell.execute_reply":"2022-03-06T07:45:27.609638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(NETS[0].summary())","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:45:27.61344Z","iopub.execute_input":"2022-03-06T07:45:27.613689Z","iopub.status.idle":"2022-03-06T07:45:27.626249Z","shell.execute_reply.started":"2022-03-06T07:45:27.613652Z","shell.execute_reply":"2022-03-06T07:45:27.625651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_iter = iter(ds_test)\nidx = 0\n\nFN, CNT, PRED = [], [], []\nfor fn, cnt, img in tqdm(data_iter):\n    idx += 1\n    FN.append(fn.numpy().astype(str))\n    CNT.append(cnt.numpy())\n    # predict with img with your models\n    pred = 0\n    for net in NETS:\n        pred += net.predict(img, verbose=0) / 5\n    #PRED.append(pred.argmax(axis=1))\n    PRED.append(pred)\n#===\nFN = np.concatenate(FN)\nCNT = np.concatenate(CNT)\nPRED = np.concatenate(PRED)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:46:01.319897Z","iopub.execute_input":"2022-03-06T07:46:01.320603Z","iopub.status.idle":"2022-03-06T07:46:09.299756Z","shell.execute_reply.started":"2022-03-06T07:46:01.320556Z","shell.execute_reply":"2022-03-06T07:46:09.298986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NB_LABELS = 21\ndf = pd.DataFrame({\"file_id\":FN, \"end_time\":CNT})\ndg = pd.DataFrame(PRED, columns=[f\"prob-{idx}\" for idx in range(NB_LABELS)])\ndf = df.join(dg)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:53:16.387338Z","iopub.execute_input":"2022-03-06T07:53:16.387718Z","iopub.status.idle":"2022-03-06T07:53:16.395388Z","shell.execute_reply.started":"2022-03-06T07:53:16.387663Z","shell.execute_reply":"2022-03-06T07:53:16.394467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"1 / NB_LABELS","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:53:16.667696Z","iopub.execute_input":"2022-03-06T07:53:16.668357Z","iopub.status.idle":"2022-03-06T07:53:16.674264Z","shell.execute_reply.started":"2022-03-06T07:53:16.668319Z","shell.execute_reply":"2022-03-06T07:53:16.673455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.wide_to_long(df, ['prob'], i=['file_id','end_time'], j='label', sep='-').reset_index()\n\ndf_lab = pd.read_csv('../input/birdclef-trials/labels.csv')\ndf_lab.columns = ['bird','NB_BIRDS','label']\ndf = df.merge(df_lab[['label','bird']], on='label')\ndf.drop('label', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:53:17.007115Z","iopub.execute_input":"2022-03-06T07:53:17.007384Z","iopub.status.idle":"2022-03-06T07:53:17.040502Z","shell.execute_reply.started":"2022-03-06T07:53:17.007354Z","shell.execute_reply":"2022-03-06T07:53:17.039911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:53:17.939558Z","iopub.execute_input":"2022-03-06T07:53:17.939963Z","iopub.status.idle":"2022-03-06T07:53:17.948779Z","shell.execute_reply.started":"2022-03-06T07:53:17.939918Z","shell.execute_reply":"2022-03-06T07:53:17.948296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#\ndf['file_id'] = df['file_id'].str.replace('.ogg', '')\ndf['target'] = (df['prob']>1./NB_LABELS)\n#df['target'] = (df['prob']>0.26)\ndf['row_id'] = df['file_id'] + '_' + df['bird'] + '_' + df['end_time'].astype(str)\n\ndf = df.sort_values(by=['file_id','end_time','bird']).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:54:45.445478Z","iopub.execute_input":"2022-03-06T07:54:45.44599Z","iopub.status.idle":"2022-03-06T07:54:45.458188Z","shell.execute_reply.started":"2022-03-06T07:54:45.445958Z","shell.execute_reply":"2022-03-06T07:54:45.457399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(21)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:58:17.772457Z","iopub.execute_input":"2022-03-06T07:58:17.772791Z","iopub.status.idle":"2022-03-06T07:58:17.788363Z","shell.execute_reply.started":"2022-03-06T07:58:17.772759Z","shell.execute_reply":"2022-03-06T07:58:17.787705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.target.sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:55:11.896316Z","iopub.execute_input":"2022-03-06T07:55:11.896792Z","iopub.status.idle":"2022-03-06T07:55:11.903443Z","shell.execute_reply.started":"2022-03-06T07:55:11.896743Z","shell.execute_reply":"2022-03-06T07:55:11.902739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub = df[['row_id','target']].copy()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:55:50.149336Z","iopub.execute_input":"2022-03-06T07:55:50.150044Z","iopub.status.idle":"2022-03-06T07:55:50.155132Z","shell.execute_reply.started":"2022-03-06T07:55:50.149981Z","shell.execute_reply":"2022-03-06T07:55:50.154444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.head(21)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:55:50.464538Z","iopub.execute_input":"2022-03-06T07:55:50.465268Z","iopub.status.idle":"2022-03-06T07:55:50.475482Z","shell.execute_reply.started":"2022-03-06T07:55:50.465232Z","shell.execute_reply":"2022-03-06T07:55:50.474763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:57:25.53059Z","iopub.execute_input":"2022-03-06T07:57:25.531303Z","iopub.status.idle":"2022-03-06T07:57:25.535901Z","shell.execute_reply.started":"2022-03-06T07:57:25.531273Z","shell.execute_reply":"2022-03-06T07:57:25.535272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T07:57:26.335182Z","iopub.execute_input":"2022-03-06T07:57:26.335441Z","iopub.status.idle":"2022-03-06T07:57:26.342746Z","shell.execute_reply.started":"2022-03-06T07:57:26.335414Z","shell.execute_reply":"2022-03-06T07:57:26.342072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}