text_plain_output_1.png
from pqdm.processes import pqdm

from scipy import signal

import cochleagram as cgram

import cv2

import erbfilter as erb

import glob

import librosa

import librosa

import librosa

import numpy as np

import numpy as np

import numpy as np # linear algebra

import pandas as pd

import pandas as pd

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import torch

import torch

os.chdir('/kaggle/working')

output_dir = './data'
if not os.path.isdir(output_dir):
    os.mkdir(output_dir)
os.mkdir('./data/nocall')

path_to_json = '../input/freefield1010/freefield1010/*/*'
json_pattern = os.path.join(path_to_json, '*.json')
file_list = glob.glob(json_pattern)

import pandas as pd
dfs = []
for file in file_list:
    with open(file) as f:
        json_data = pd.json_normalize(json.loads(f.read()))
    dfs.append(json_data)
df = pd.concat(dfs, sort=False)

df['hasbird'] = df.apply(lambda x: 'bird' in x['tags'], axis=1).astype(int)
train_ff1010 = df.sort_values(by='id').reset_index(drop=True)
train_ff1010 = train_ff1010[train_ff1010.hasbird == 0].reset_index(drop=True)

def get_clip(path):
    clip, sr_native = librosa.load(path, sr=None, mono=True, dtype=np.float32)
    sr = 32000
    if sr_native != 0:
        clip = librosa.resample(clip, sr_native, sr, res_type='kaiser_best')
    return (clip, sr, sr_native)

class cochleagram:
    IMAGE_HEIGHT = 256
    IMAGE_WIDTH = 576
    SR = 32000
    hi_lim = SR // 2
    low_lim = 10
    n_filters = int(np.floor(erb.freq2erb(hi_lim) - erb.freq2erb(low_lim)) - 1)
    nonlinearity = 'db'
    ret_mode = ('envs',)
    sample_factor = 2

def cochlea(signal):
    human_co = cgram.human_cochleagram(signal, sr=cochleagram.SR, n=cochleagram.n_filters, low_lim=cochleagram.low_lim, hi_lim=cochleagram.hi_lim, sample_factor=cochleagram.sample_factor, nonlinearity=cochleagram.nonlinearity)
    return human_co

config_patch = {'use_inv_stem': True, 'use_patch': [True, 16]}
def inv_stem(x):
    x1 = x.transpose(0, 1).view(24, 24, 16, 16)
    y = torch.zeros(384, 384, dtype=x.dtype)
    for i in range(24):
        for j in range(24):
            y[i * 16:(i + 1) * 16, j * 16:(j + 1) * 16] = x1[i, j]
    return y
def image_patch(mel_spect):
    if config_patch.get('use_inv_stem'):
        spect = torch.from_numpy(mel_spect)
        spect = inv_stem(spect)
    else:
        if config_patch.get('use_patch')[0]:
            patch_size = config_patch.get('use_patch')[1]
            spect = np.zeros((384, 384), dtype=np.float32)
        for i in range(0, 192, patch_size):
            spect[2 * i:2 * i + patch_size, :] = mel_spect[i:i + patch_size, :384]
            spect[2 * i + patch_size:2 * i + 2 * patch_size, :] = mel_spect[i:i + patch_size, 384:]
            mel_spect = spect
    return spect.cpu().detach().numpy()

def work_sub(path):
    output_path = './data/nocall'
    clip, sr, sr_native = get_clip(path)
    clip = clip.astype('float32')
    length = clip.shape[0]
    head, tail = os.path.split(path[:-4])
    spect = cochlea(clip)
    res = cv2.resize(np.flipud(spect), dsize=(576, 256), interpolation=cv2.INTER_CUBIC)
    filename = 'ff1010_%d_0.jpg' % int(tail)
    img_patch = image_patch(res)
    cv2.imwrite(os.path.join(output_path, filename), img_patch * 255)
    return (sr, sr_native, length)

path_to_wav = '../input/freefield1010/freefield1010/*/*'
json_pattern = os.path.join(path_to_wav, '*.wav')
file_list_wav = glob.glob(json_pattern)

check = []
for file in file_list_wav:
    head, tail = os.path.split(file[:-4])
    check.append(int(tail))

list_remove = file_list_wav.copy()
for file in file_list_wav:
    head, tail = os.path.split(file[:-4])
    if not int(tail) in list(train_ff1010['id']):
        list_remove.remove(file)

res = pqdm(list_remove, work_sub, n_jobs=8)