Skip to content
Snippets Groups Projects
print_annot.py 1.2 KiB
Newer Older
Paul Best's avatar
Paul Best committed
import os, argparse
Paul Best's avatar
Paul Best committed
from tqdm import tqdm
import matplotlib.pyplot as plt
import pandas as pd
Paul Best's avatar
Paul Best committed
import models, utils as u
Paul Best's avatar
Paul Best committed
import torch

Paul Best's avatar
Paul Best committed
parser = argparse.ArgumentParser()
parser.add_argument("specie", type=str)
Paul Best's avatar
Paul Best committed
parser.add_argument("-frontend", type=str, default='logMel')
parser.add_argument("-nMel", type=int, default=64)
Paul Best's avatar
Paul Best committed
args = parser.parse_args()

meta = models.meta[args.specie]
df = pd.read_csv(f'{args.specie}/{args.specie}.csv')
Paul Best's avatar
Paul Best committed
frontend = models.frontend[args.frontend](meta['sr'], meta['nfft'], meta['sampleDur'], args.nMel)
Paul Best's avatar
Paul Best committed
for label, grp in df.groupby('label'):
    # if os.path.isdir(f'{args.specie}/annot_pngs/{label}'):
        # continue
Paul Best's avatar
Paul Best committed
    os.system(f'mkdir -p "{args.specie}/annot_pngs/{label}"')
Paul Best's avatar
Paul Best committed
    loader = torch.utils.data.DataLoader(u.Dataset(grp.sample(min(len(grp), 100)), args.specie+'/audio/', meta['sr'], meta['sampleDur']),\
                                         batch_size=1, num_workers=4, pin_memory=True)
    for x, idx in tqdm(loader, desc=args.specie + ' ' + label, leave=False):
        x = frontend(x).squeeze().detach()
        plt.figure()
        plt.imshow(x, origin='lower', aspect='auto')
        plt.savefig(f'{args.specie}/annot_pngs/{label}/{idx.item()}')
        plt.close()
Paul Best's avatar
Paul Best committed