def __getitem__(self, idx):

        temp = random.randint(0, 1)

        augment = Compose([
            TimeStretch(min_rate=0.8, max_rate=1.25, p=0.5),
            Shift(min_fraction=-0.5, max_fraction=0.5, p=0.5, rollover=False)
        ])

        self.wavPath = str(self.data.iloc[idx, 0])
        self.label = self.data.iloc[idx, 1]

        self.signal, self.sr = torchaudio.load(self.wavPath)

        if (temp == 1):
            self.signal = torch.from_numpy(
                augment(samples=self.signal.numpy(), sample_rate=self.sr))

        self.spectogram = torchaudio.transforms.Spectrogram(n_fft=320,
                                                            win_length=320,
                                                            hop_length=160)(
                                                                self.signal)
        self.logSpectogram = torchaudio.transforms.AmplitudeToDB()(
            self.spectogram)

        #self.tempImg=torchvision.transforms.ToPILImage()(self.logSpectogram)
        #self.tempImg=self.tempImg.convert("RGB")
        #self.spectogramImageTensor=self.vision_transform(self.tempImg)

        return self.logSpectogram, self.label
def pitch_shift(data_path,
                file_info,
                n_repeats=3,
                min_semitones=-4,
                max_semitones=4):
    # Create the augmenter
    augmenter = Compose([
        PitchShift(min_semitones=min_semitones,
                   max_semitones=max_semitones,
                   p=1.0)
    ])

    # Iterate through the Gibbon audio files only
    for j in file_info[file_info.label == 1].index:
        for i in range(n_repeats):
            # Read audio file
            rate, samples = wavfile.read(data_path + 'Clean/' +
                                         file_info.at[j, 'fname'])
            # Set the output path
            output_file_path = data_path + 'Augmented/PitchShift_{:03d}_'.format(
                i) + file_info.at[j, 'fname']
            # Perform time stretch
            augmented_samples = augmenter(samples=samples, sample_rate=rate)
            # Save the new audio
            wavfile.write(filename=output_file_path,
                          rate=rate,
                          data=augmented_samples)
Exemple #3
0
def augmented_feature_engineering(wavFile, settings):
    fs, rawWav = scipy.io.wavfile.read(wavFile)
    wavData = rawWav
    if (settings['CHANNELS'] == 2):
        wavData = rawWav[:, 0]

    augmenter = Compose([
        AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.015, p=0.5),
        TimeStretch(min_rate=0.8, max_rate=1.25, p=0.5),
        Shift(min_fraction=-0.5, max_fraction=0.5, p=0.5),
    ])
    wavData = augmenter(samples=np.array(wavData, dtype="float32"),
                        sample_rate=fs)

    data_row = []
    input_type = settings['FEATURE_ENGINEERING_TYPE']
    if (input_type == TYPE_FEATURE_ENGINEERING_NORM_MFCC):
        mfcc_result1 = mfcc(wavData,
                            samplerate=fs,
                            nfft=1103,
                            numcep=30,
                            nfilt=40,
                            preemph=0.5,
                            winstep=0.005,
                            winlen=0.015,
                            appendEnergy=False)
        data_row.extend(mfcc_result1.ravel())
    elif (input_type == TYPE_FEATURE_ENGINEERING_RAW_WAVE):
        data_row = wavData
    else:
        print("OLD MFCC TYPE IS NOT SUPPORTED FOR TRAINING PYTORCH")
    return data_row
def add_gaussian_noise(data_path,
                       file_info,
                       n_repeats=3,
                       min_amp=0.001,
                       max_amp=0.015):
    # Create the augmenter
    augmenter = Compose([
        AddGaussianNoise(min_amplitude=min_amp, max_amplitude=max_amp, p=1.0)
    ])

    # Iterate through the Gibbon audio files only
    for j in file_info[file_info.label == 1].index:
        for i in range(n_repeats):
            # Read audio file
            rate, samples = wavfile.read(data_path + 'Clean/' +
                                         file_info.at[j, 'fname'])
            # Set the output path
            output_file_path = data_path + 'Augmented/AddGaussianNoise_{:03d}_'.format(
                i) + file_info.at[j, 'fname']
            # Add gaussian noise
            augmented_samples = augmenter(samples=samples, sample_rate=rate)
            # Save the new audio
            wavfile.write(filename=output_file_path,
                          rate=rate,
                          data=augmented_samples)
 def __init__(
     self,
     directory_path: str,
     dataset_folds: List[str],
     train: bool,
     arguments: Dict[str, Any],
     augmentations: List[str],
     train_cqts_path: str = None,
     train_gfccs_path: str = None,
 ):
     self.arguments = arguments
     self.paths = []
     self.train = train
     for filename in tqdm(natsorted(os.listdir(directory_path))):
         if filename[0] not in dataset_folds:
             continue
         path = os.path.join(directory_path, filename)
         self.paths.append(path)
     self.augmentations = Compose([
         name2augmentation[name] for name in augmentations
         if name in name2augmentation.keys()
     ])
     self.train_cqts_path = train_cqts_path
     self.train_gfccs_path = train_gfccs_path
     if self.arguments["cqt"]:
         assert (self.train_cqts_path is not None
                 ), "cqt feature is True but there is no train_cqts_path"
     if self.arguments["gfcc"]:
         assert (self.train_gfccs_path is not None
                 ), "gfcc feature is True but there is no train_gfccs_path"
Exemple #6
0
def get_transforms(bckgrd_aug_dir=None, secondary_bckgrd_aug_dir=None):
    list_of_aug = [
        #         AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.015, p=0.3),
        AddGaussianNoise(p=0.2),
        AddGaussianSNR(p=0.2),
        Gain(min_gain_in_db=-15, max_gain_in_db=15, p=0.3)
    ]
    if bckgrd_aug_dir is not None:
        list_of_aug.append(AddBackgroundNoise(bckgrd_aug_dir, p=0.2))
    if secondary_bckgrd_aug_dir is not None:
        list_of_aug.append(
            AddShortNoises(secondary_bckgrd_aug_dir,
                           min_time_between_sounds=0.0,
                           max_time_between_sounds=15.0,
                           burst_probability=0.5,
                           p=0.6))
    list_of_aug += [
        AddGaussianNoise(p=0.2),
        AddGaussianSNR(p=0.2),
        Gain(min_gain_in_db=-15, max_gain_in_db=15, p=0.3)
    ]
    augmenter = Compose(list_of_aug)
    transforms = {
        "train": get_training_augmentation(augmenter),
        "valid": get_validation_augmentation()
    }
    return transforms
Exemple #7
0
 def transform_speakers(self, speakers):
     """Used to transform the separate speakers, without the added noise"""
     speaker_augment = Compose([
         t for t in augment.transforms
         if 'noise' not in t.__str__().lower()
     ])
     return (speaker_augment(speakers, sample_rate=self.sample_rate))
def build_transforms(train=True):
    return Compose([
        AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.015, p=0.5),
        TimeStretch(min_rate=0.8, max_rate=1.25, p=0.5),
        PitchShift(min_semitones=-4, max_semitones=4, p=0.5),
        Shift(min_fraction=-0.5, max_fraction=0.5, p=0.5),
    ])
def make_transform():
    return Compose([
        FrequencyMask(min_frequency_band=0.005,
                      max_frequency_band=0.10,
                      p=0.25),
        TimeStretch(min_rate=0.15, max_rate=.25, p=0.25),
        AddGaussianSNR(min_SNR=0.001, max_SNR=.25, p=0.25)
    ])
Exemple #10
0
    def get_stretched_audio(self, x, sr):
        composition = []
        composition.append(
            AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.015, p=0.5))
        augmenter = Compose(composition)

        aug_chord = augmenter(samples=x, sample_rate=sr)

        return aug_chord
Exemple #11
0
 def __init__(self, dataset):
     self.dataset = dataset
     self.sample_rate = TRAINING_CONFIG['audio_sample_rate']
     self.augmenter = Compose([
         AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.015, p=0.5),
         TimeStretch(min_rate=0.8, max_rate=1.25, p=0.5),
         PitchShift(min_semitones=-4, max_semitones=4, p=0.5),
         Shift(min_fraction=-0.5, max_fraction=0.5, p=0.5),
     ])
Exemple #12
0
 def __init__(self):
     super(Augment_Time, self).__init__()
     self.p = 0.5
     self.augmenter = Compose([
         AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.01, p=0.3),
         TimeStretch(min_rate=0.8, max_rate=1.25, p=0.5),
         PitchShift(min_semitones=-4, max_semitones=4, p=0.5),
         FrequencyMask(),
         TimeMask()
         #Shift(min_fraction=-0.5, max_fraction=0.5, p=0.5),
     ])
Exemple #13
0
 def __init__(self, desired_sr, mode, time_aug=False):
     super(ReadAudio, self).__init__()
     self.desired_sr = desired_sr
     self.augmenter = Compose([
         AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.01, p=0.3),
         TimeStretch(min_rate=0.8, max_rate=1.25, p=0.3),
         PitchShift(min_semitones=-4, max_semitones=4, p=0.5),
         #Shift(min_fraction=-0.5, max_fraction=0.5, p=0.5),
     ])
     self.mode = mode
     self.time_aug = time_aug
  def __getitem__(self,idx):

    augment = Compose([
        TimeStretch(min_rate=0.8, max_rate=1.25, p=0.5),
        Shift(min_fraction=-0.5, max_fraction=0.5, p=0.5,rollover=False)
    ])

    temp1=random.randint(0,1)
    temp2=random.randint(0,1)
    temp3=random.randint(0,1)

    self.anchor=str(self.data.iloc[idx,0])
    self.positive=self.data.iloc[idx,1]
    self.negative=self.data.iloc[idx,2]

    self.signalAnchor,self.srAnchor=torchaudio.load(self.anchor)
    self.signalPositive,self.srPositive=torchaudio.load(self.positive)
    self.signalNegative,self.srNegative=torchaudio.load(self.negative)

    if (temp1==1):
      self.signalAnchor=torch.from_numpy(augment(samples=self.signalAnchor.numpy(),sample_rate=self.srAnchor))

    if (temp2==1):
      self.signalPositive=torch.from_numpy(augment(samples=self.signalPositive.numpy(),sample_rate=self.srPositive))

    if (temp3==1):
      self.signalNegative=torch.from_numpy(augment(samples=self.signalNegative.numpy(),sample_rate=self.srNegative))


    self.spectogramAnchor=torchaudio.transforms.Spectrogram(n_fft=320,hop_length=160,win_length=320)(self.signalAnchor)
    self.logSpectogramAnchor=torchaudio.transforms.AmplitudeToDB()(self.spectogramAnchor)

    self.spectogramPositive=torchaudio.transforms.Spectrogram(n_fft=320,hop_length=160,win_length=320)(self.signalPositive)
    self.logSpectogramPositive=torchaudio.transforms.AmplitudeToDB()(self.spectogramPositive)

    self.spectogramNegative=torchaudio.transforms.Spectrogram(n_fft=320,hop_length=160,win_length=320)(self.signalNegative)
    self.logSpectogramNegative=torchaudio.transforms.AmplitudeToDB()(self.spectogramNegative)


    #self.tempImgAnchor=torchvision.transforms.ToPILImage()(self.logSpectogramAnchor)
    #self.tempImgAnchor=self.tempImgAnchor.convert("RGB")
    #self.spectogramAnchorImageTensor=self.vision_transform(self.tempImgAnchor)

    #self.tempImgPositive=torchvision.transforms.ToPILImage()(self.logSpectogramPositive)
    #self.tempImgPositive=self.tempImgPositive.convert("RGB")
    #self.spectogramPositiveImageTensor=self.vision_transform(self.tempImgPositive)

    #self.tempImgNegative=torchvision.transforms.ToPILImage()(self.logSpectogramNegative)
    #self.tempImgNegative=self.tempImgNegative.convert("RGB")
    #self.spectogramNegativeImageTensor=self.vision_transform(self.tempImgNegative)

    return self.logSpectogramAnchor,self.logSpectogramPositive,self.logSpectogramNegative
def get_transforms(bckgrd_aug_dir=None):
    list_of_aug = [
        AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.015, p=0.3),
        AddGaussianSNR(p=0.3)
    ]
    if bckgrd_aug_dir is not None:
        list_of_aug.append(AddBackgroundNoise(bckgrd_aug_dir,p=0.5))
    augmenter = Compose(list_of_aug)
    transforms = {
        "train": get_training_augmentation(augmenter),
        "valid": get_validation_augmentation()
    }
    return transforms
Exemple #16
0
def compose_augmentations(rir_path):
    impulse_path = os.path.join(rir_path, 'simulated_rirs')
    noise_path = os.path.join(rir_path, 'pointsource_noises')
    if not (os.path.exists(impulse_path) and os.path.exists(noise_path)):
        raise ValueError(
            'Unable to augment signal, rir_path "{}" does not exist.'.format(
                rir_path))

    return Compose([
        AddGaussianSNR(min_SNR=0.2, max_SNR=0.5, p=0.5),
        AddImpulseResponse(impulse_path, leave_length_unchanged=True, p=0.3),
        AddBackgroundNoise(noise_path, p=0.3),
        AddShortNoises(noise_path, max_snr_in_db=80, p=0.3)
    ])
Exemple #17
0
    def augment_stretched_noise(data, sr, label, noise=True, stretch=True):
        composition = []
        if noise:
            composition.append(
                AddGaussianNoise(min_amplitude=0.001,
                                 max_amplitude=0.015,
                                 p=0.5))
        if stretch:
            composition.append(TimeStretch(min_rate=0.8, max_rate=1.25, p=0.5))
        augmenter = Compose(composition)

        aug_chord = augmenter(samples=data, sample_rate=sr)
        mfccs = librosa.feature.mfcc(y=aug_chord, sr=sr, n_mfcc=40)
        mfccs_processed = np.mean(mfccs.T, axis=0)

        return mfccs_processed
Exemple #18
0
    def __getitem__(self, idx: int):
        wav_path, ebird_code = self.file_list[idx]

        y, sr = sf.read(wav_path, dtype='float32')

        signal_aug = Compose([
            AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.015, p=0.5),
            TimeStretch(min_rate=0.9, max_rate=1.15, p=0.5)
        ])

        len_y = len(y)
        effective_length = sr * PERIOD
        if len_y < effective_length:
            new_y = np.zeros(effective_length, dtype=np.float32)
            start = np.random.randint(effective_length - len_y)
            new_y[start:start + len_y] = y
            y = new_y.astype(np.float32)
        elif len_y > effective_length:
            start = np.random.randint(len_y - effective_length)
            y = y[start:start + effective_length].astype(np.float32)
        else:
            y = y.astype(np.float32)
        if self.waveform_transforms:
            y = signal_aug(samples=y, sample_rate=sr)

        melspec = librosa.feature.melspectrogram(
            y, sr=sr, **self.melspectrogram_parameters)
        melspec = librosa.power_to_db(melspec).astype(np.float32)

        if self.spectrogram_transforms:
            melspec = self.spectrogram_transforms(melspec)
        else:
            pass

        #img_aug = transforms.Compose([transforms.RandomHorizontalFlip(p=0.5)])
        image = mono_to_color(melspec)
        height, width, _ = image.shape
        image = cv2.resize(
            image, (int(width * self.img_size / height), self.img_size))
        #image = img_aug(image)
        image = np.moveaxis(image, 2, 0)
        image = (image / 255.0).astype(np.float32)

        labels = np.zeros(len(BIRD_CODE), dtype="f")
        labels[BIRD_CODE[ebird_code]] = 1

        return image, labels
Exemple #19
0
def compose(sounds_path):
  _p = 0.2

  transforms = [
    MyGain(p=_p),
    AddGaussianNoise(p=_p),
    Shift(p=_p, min_fraction=-0.25, max_fraction=0.25),
    FrequencyMask(p=_p),
    TimeMask(p=_p, max_band_part=0.25),
    AddGaussianSNR(p=_p),
    ClippingDistortion(p=_p, max_percentile_threshold=20),
    AddBackgroundNoise(sounds_path=sounds_path, p=_p),
    TimeStretch(p=_p/10),
    PitchShift(p=_p/30),
  ]
  
  return Compose(transforms, p=0.4, shuffle=True)
 def __init__(
     self,
     dataset_path: str,
     dataset_folds: List[int],
     train: bool,
     augmentations: List[str],
 ):
     self.train = train
     self.paths = []
     for filename in tqdm(natsorted(os.listdir(dataset_path))):
         if filename[0] not in dataset_folds:
             continue
         path = os.path.join(dataset_path, filename)
         self.paths.append(path)
     self.sampling_rate = int(dataset_path.split("_")[-1])
     self.augmentations = Compose([
         name2augmentation[name] for name in augmentations
         if name in name2augmentation.keys()
     ])
Exemple #21
0
def raw_audio_process(transform_fn):
    augment_fn = Compose([
        AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.015, p=0.5),
        TimeStretch(min_rate=0.8, max_rate=1.25, p=0.5),
        PitchShift(min_semitones=-4, max_semitones=4, p=0.5),
        Shift(min_fraction=-0.5, max_fraction=0.5, p=0.5)
    ])

    @wraps(transform_fn)
    def augment_audio(audio, **kwargs):
        sr = kwargs.setdefault('sr', 22050)
        n_win = kwargs.setdefault('n_win', 20)
        win_length = int(n_win * sr / 1000)

        audio = augment_fn(audio)
        return transform_fn(audio,
                            win_length=win_length,
                            hop_length=win_length // 4)

    return augment_audio
Exemple #22
0
def process_fn(output='stft', spec_aug=False, p=0.5, sr=22050):
    augment_fn = Compose([
        AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.015, p=p),
        TimeStretch(min_rate=0.8, max_rate=1.25, p=p),
        PitchShift(min_semitones=-4, max_semitones=4, p=p),
        Shift(min_fraction=-0.5, max_fraction=0.5, p=p)
    ])

    win_length = int(20 * sr / 1000)
    if output == 'stft':

        def stft_transform(feats):
            if feats.ndim == 1:
                feats = augment_fn(samples=feats, sample_rate=sr)
                feats = np.log(
                    np.abs(librosa.stft(feats, 1023, win_length=win_length)).T
                    + 1e-12)
            if spec_aug:
                feats = spec_augment(feats)
            return feats

        return stft_transform
    if output == 'lms':

        def lms_transform(feats):
            if feats.ndim == 1:
                feats = augment_fn(samples=feats, sample_rate=sr)
                hop_length = win_length // 4
                feats = np.log(
                    np.abs(
                        librosa.feature.melspectrogram(
                            feats,
                            n_fft=win_length,
                            hop_length=hop_length,
                            win_length=win_length)).T + 1e-12)
            if spec_aug:
                feats = spec_augment(feats)
            return feats

        return lms_transform
 def __init__(self, path_audio, y, resample_freq = 32000, max_length=3, augmentation=[], validation=False, num_class=264, pseudo_labels=None):
     self.labels2idx = {'Pump': 0, 'Spinach': 1,  'abalimi': 2,  'afukirira': 3,  'agriculture': 4, 'akammwanyi': 5,  'akamonde': 6, 'akasaanyi': 7, 'akatunda': 8, 'akatungulu': 9,
   'akawuka': 10, 'amakoola': 11, 'amakungula': 12, 'amalagala': 13, 'amappapaali': 14, 'amatooke': 15, 'banana': 16, 'beans': 17, 'bibala': 18, 'bulimi': 19, 'butterfly': 20, 'cabbages': 21,
   'cassava': 22, 'caterpillar': 23, 'caterpillars': 24, 'coffee': 25, 'crop': 26, 'ddagala': 27, 'dig': 28, 'disease': 29, 'doodo': 30, 'drought': 31, 'ebbugga': 32, 'ebibala': 33, 'ebigimusa': 34,
   'ebijanjaalo': 35, 'ebijjanjalo': 36, 'ebikajjo': 37, 'ebikolo': 38, 'ebikongoliro': 39, 'ebikoola': 40, 'ebimera': 41, 'ebinyebwa': 42, 'ebirime': 43, 'ebisaanyi': 44, 'ebisooli': 45,
   'ebisoolisooli': 46, 'ebitooke': 47, 'ebiwojjolo': 48, 'ebiwuka': 49, 'ebyobulimi': 50, 'eddagala': 51, 'eggobe': 52, 'ejjobyo': 53, 'ekibala': 54, 'ekigimusa': 55, 'ekijanjaalo': 56,
   'ekikajjo': 57, 'ekikolo': 58, 'ekikoola': 59, 'ekimera': 60, 'ekirime': 61, 'ekirwadde': 62, 'ekisaanyi': 63, 'ekitooke': 64, 'ekiwojjolo': 65, 'ekyeya': 66, 'emboga': 67, 'emicungwa': 68,
   'emisiri': 69, 'emiyembe': 70, 'emmwanyi': 71, 'endagala': 72, 'endokwa': 73, 'endwadde': 74, 'enkota': 75, 'ennima': 76, 'ennimiro': 77, 'ennyaanya': 78, 'ensigo': 79, 'ensiringanyi': 80, 'ensujju': 81,
   'ensuku': 82, 'ensukusa': 83, 'enva endiirwa': 84, 'eppapaali': 85, 'faamu': 86, 'farm': 87, 'farmer': 88, 'farming instructor': 89, 'fertilizer': 90, 'fruit': 91, 'fruit picking': 92,
   'garden': 93, 'greens': 94, 'ground nuts': 95, 'harvest': 96, 'harvesting': 97, 'insect': 98, 'insects': 99, 'irish potatoes': 100, 'irrigate': 101, 'kaamulali': 102, 'kasaanyi': 103, 'kassooli': 104,
   'kikajjo': 105, 'kikolo': 106, 'kisaanyi': 107, 'kukungula': 108, 'leaf': 109, 'leaves': 110, 'lumonde': 111, 'lusuku': 112, 'maize': 113, 'maize stalk borer': 114, 'maize streak virus': 115, 'mango': 116, 'mangoes': 117, 'matooke': 118,
   'matooke seedlings': 119, 'medicine': 120, 'miceere': 121, 'micungwa': 122, 'mpeke': 123, 'muceere': 124, 'mucungwa': 125, 'mulimi': 126, 'munyeera': 127, 'muwogo': 128,
   'nakavundira': 129, 'nambaale': 130, 'namuginga': 131, 'ndwadde': 132, 'nfukirira': 133, 'nnakati': 134, 'nnasale beedi': 135, 'nnimiro': 136, 'nnyaanya': 137, 'npk': 138, 'nursery bed': 139,
   'obulimi': 140, 'obulwadde': 141, 'obumonde': 142, 'obusaanyi': 143, 'obutunda': 144, 'obutungulu': 145, 'obuwuka': 146, 'okufukirira': 147, 'okufuuyira': 148, 'okugimusa': 149, 'okukkoola': 150,
   'okukungula': 151, 'okulima': 152, 'okulimibwa': 153, 'okunnoga': 154, 'okusaasaana': 155, 'okusaasaanya': 156, 'okusiga': 157,
   'okusimba': 158, 'okuzifuuyira': 159, 'olusuku': 160, 'omuceere': 161, 'omucungwa': 162, 'omulimi': 163, 'omulimisa': 164, 'omusiri': 165, 'omuyembe': 166,
   'onion': 167, 'orange': 168, 'pampu': 169, 'passion fruit': 170, 'pawpaw': 171, 'pepper': 172, 'plant': 173, 'plantation': 174, 'ppaapaali': 175, 'pumpkin': 176, 'rice': 177, 'seed': 178,
   'sikungula': 179, 'sow': 180, 'spray': 181, 'spread': 182, 'suckers': 183, 'sugarcane': 184, 'sukumawiki': 185, 'super grow': 186, 'sweet potatoes': 187, 'tomatoes': 188, 'vegetables': 189,
   'watermelon': 190, 'weeding': 191, 'worm': 192}
     
     self.idx2labels = {k:v for v,k in self.labels2idx.items()}
     identity = np.eye(num_class)
     self.augmentation = set(augmentation)
     self.samples = path_audio #+ path_augment
     self.max_length = max_length # 99% are shorter than 3 sec
     self.resample_freq=resample_freq
     self.validation = validation
     self.y = np.array([identity[self.labels2idx[t]] for t in y]).astype(np.float32) #+ [self.labels2idx[t] for t in y_aug]
     self.num_class = num_class
     self.noise = Compose([AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.015, p=0.6),
                             TimeStretch(min_rate=0.8, max_rate=1.25, p=0.6),
                             PitchShift(min_semitones=-4, max_semitones=4, p=0.5),
                             Shift(min_fraction=-0.5, max_fraction=0.5, p=0.5),
                             Gain(min_gain_in_db=-12, max_gain_in_db=12, p=0.6), 
                             ])
 
     if pseudo_labels is not None:
         self.add_pl(pseudo_labels[0], pseudo_labels[1])
Exemple #24
0
    def __init__(
        self,
        manifest_path,
        sample_rate,
        max_sample_size=None,
        min_sample_size=None,
        shuffle=True,
        min_length=0,
        pad=False,
        normalize=False,
    ):
        super(AugmentedFileAudioDataset, self).__init__(
            manifest_path=manifest_path,
            sample_rate=sample_rate,
            max_sample_size=max_sample_size,
            min_sample_size=min_sample_size,
            shuffle=shuffle,
            min_length=min_length,
            pad=pad,
            normalize=normalize,
        )

        self.pre_transform = Compose([
            #AddGaussianNoise(min_amplitude=1e-3, max_amplitude=5e-2, p=0.8),
            #PitchShift(min_semitones=-4, max_semitones=4, p=0.8),
            FrequencyMask(min_frequency_band=0.0,
                          max_frequency_band=0.05,
                          p=0.5),
            TimeMask(min_band_part=0.0, max_band_part=0.05, p=0.5)
            #ClippingDistortion(min_percentile_threshold=10, max_percentile_threshold=40, p=0.2),
        ])

        random_reverb = RandomReverb()
        random_clip = RandomClip()
        random_time_dropout = RandomTimeDropout()
        self.post_transform = augment.EffectChain().reverb(
            random_reverb).channels(1).clip(random_clip)  #.time_dropout(200)
Exemple #25
0
    def __init__(
        self,
        sound_file_paths,
        batch_size=8,
        augment=True,
        save_augmented_sounds_to_path=None,
        fixed_sound_length=FIXED_SOUND_LENGTH,
        num_mels=NUM_MELS,
        preprocessing_fn=None,
    ):
        self.sound_file_paths = sound_file_paths
        self.batch_size = batch_size
        self.augment = augment
        self.save_augmented_sounds_to_path = save_augmented_sounds_to_path
        self.fixed_sound_length = fixed_sound_length
        self.min_num_samples = (fixed_sound_length + 3) * HOP_LENGTH
        self.num_mels = num_mels
        self.preprocessing_fn = preprocessing_fn

        self.laughter_paths = self.sound_file_paths["laughter"]
        self.non_laughter_paths = []
        for category in self.sound_file_paths:
            if not is_laughter_category(category):
                self.non_laughter_paths += self.sound_file_paths[category]

        if save_augmented_sounds_to_path:
            os.makedirs(save_augmented_sounds_to_path, exist_ok=True)

        self.augmenter = Compose(
            [
                AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.002, p=0.1),
                TimeStretch(min_rate=0.8, max_rate=1.25, p=0.02),
                PitchShift(min_semitones=-3, max_semitones=3, p=0.02),
                Shift(min_fraction=-0.5, max_fraction=0.5, p=0.5),
            ]
        )
Exemple #26
0
def load_wav_file(sound_file_path):
    sample_rate, sound_np = wavfile.read(sound_file_path)
    if sample_rate != SAMPLE_RATE:
        raise Exception("Unexpected sample rate {} (expected {})".format(
            sample_rate, SAMPLE_RATE))

    if sound_np.dtype != np.float32:
        assert sound_np.dtype == np.int16
        sound_np = sound_np / 32767  # ends up roughly between -1 and 1

    return sound_np


augmenter = Compose([
    AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.015, p=0.5),
    TimeStretch(min_rate=0.8, max_rate=1.25, p=0.5),
    PitchShift(min_semitones=-4, max_semitones=4, p=0.5),
    Shift(min_fraction=-0.5, max_fraction=0.5, p=0.5),
])

current_dir = os.path.dirname(__file__)
output_dir = os.path.join(current_dir, "output")
os.makedirs(output_dir, exist_ok=True)

samples = load_wav_file(os.path.join(current_dir, "acoustic_guitar_0.wav"))
for i in tqdm(range(20)):
    output_file_path = os.path.join(output_dir, "{:03d}.wav".format(i))
    augmented_samples = augmenter(samples=samples, sample_rate=SAMPLE_RATE)
    wavfile.write(output_file_path, rate=SAMPLE_RATE, data=augmented_samples)
Exemple #27
0
    def generate(self, wave_file, output_dir):
        """
        For each transformation, apply it to an example sound and write the transformed sounds to
        an output folder.
        """
        samples = load_wav_file(wave_file)
        _filename = os.path.basename(wave_file).split('.')[0]
        # AddImpulseResponse
        if self.AddImpulseResponse[0]:
            augmenter = Compose([
                AddImpulseResponse(p=1.0, ir_path=os.path.join(DEMO_DIR, "ir"))
            ])
            output_file_path = os.path.join(
                output_dir,
                _filename + "_AddImpulseResponse{:03d}.wav".format(0))
            augmented_samples = augmenter(samples=samples,
                                          sample_rate=SAMPLE_RATE)
            wavfile.write(output_file_path,
                          rate=SAMPLE_RATE,
                          data=augmented_samples)
        # FrequencyMask
        if self.FrequencyMask[0]:
            augmenter = Compose([FrequencyMask(p=1.0)])
            for i in range(5):
                output_file_path = os.path.join(
                    output_dir,
                    _filename + "_FrequencyMask{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)

        # TimeMask
        if self.TimeMask[0]:
            augmenter = Compose([TimeMask(p=1.0)])
            for i in range(5):
                output_file_path = os.path.join(
                    output_dir, _filename + "_TimeMask{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)

        # AddGaussianSNR
        if self.AddGaussianSNR[0]:
            augmenter = Compose([AddGaussianSNR(p=1.0)])
            for i in range(5):
                output_file_path = os.path.join(
                    output_dir,
                    _filename + "_AddGaussianSNR{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)

        # AddGaussianNoise
        if self.AddGaussianNoise[0]:
            augmenter = Compose([
                AddGaussianNoise(min_amplitude=0.001,
                                 max_amplitude=0.015,
                                 p=1.0)
            ])
            for i in range(5):
                output_file_path = os.path.join(
                    output_dir,
                    _filename + "_AddGaussianNoise{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)

        # TimeStretch
        if self.TimeStretch[0]:
            augmenter = Compose(
                [TimeStretch(min_rate=0.5, max_rate=1.5, p=1.0)])
            for i in range(5):
                output_file_path = os.path.join(
                    output_dir, _filename + "_TimeStretch{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)

        # PitchShift
        if self.PitchShift[0]:
            augmenter = Compose(
                [PitchShift(min_semitones=-6, max_semitones=12, p=1.0)])
            for i in range(5):
                output_file_path = os.path.join(
                    output_dir, _filename + "_PitchShift{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)

        # Shift
        if self.Shift[0]:
            augmenter = Compose(
                [Shift(min_fraction=-0.5, max_fraction=0.5, p=1.0)])
            for i in range(5):
                output_file_path = os.path.join(
                    output_dir, _filename + "_Shift{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)

        # Shift without rollover
        if self.ShiftWithoutRoll[0]:
            augmenter = Compose([
                Shift(min_fraction=-0.2,
                      max_fraction=0.2,
                      rollover=False,
                      p=1.0)
            ])
            for i in range(5):
                output_file_path = os.path.join(
                    output_dir,
                    _filename + "_ShiftWithoutRollover{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)

        # Normalize
        if self.Normalize[0]:
            augmenter = Compose([Normalize(p=1.0)])
            output_file_path = os.path.join(
                output_dir, _filename + "_Normalize{:03d}.wav".format(0))
            augmented_samples = augmenter(samples=samples,
                                          sample_rate=SAMPLE_RATE)
            wavfile.write(output_file_path,
                          rate=SAMPLE_RATE,
                          data=augmented_samples)

        # Resample
        if self.Resample[0]:
            augmenter = Compose([
                Resample(min_sample_rate=12000, max_sample_rate=44100, p=1.0)
            ])
            for i in range(5):
                output_file_path = os.path.join(
                    output_dir, _filename + "_Resample{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)

        # ClippingDistortion
        if self.ClippingDistortion[0]:
            augmenter = Compose(
                [ClippingDistortion(max_percentile_threshold=10, p=1.0)])
            for i in range(5):
                output_file_path = os.path.join(
                    output_dir,
                    _filename + "_ClippingDistortion{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)

        # AddBackgroundNoise
        if self.AddBackgroundNoise[0]:
            augmenter = Compose([
                AddBackgroundNoise(sounds_path=os.path.join(
                    DEMO_DIR, "background_noises"),
                                   p=1.0)
            ])
            for i in range(5):
                output_file_path = os.path.join(
                    output_dir,
                    _filename + "_AddBackgroundNoise{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)
        # AddWhiteNoise
        if self.AddWhiteNoise[0]:
            augmenter = Compose([
                AddBackgroundNoise(sounds_path=os.path.join(
                    DEMO_DIR, "white_noises"),
                                   p=1.0)
            ])
            for i in range(self.AddWhiteNoise[1]):
                output_file_path = os.path.join(
                    output_dir,
                    _filename + "_AddWhiteNoise{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)
        # AddPinkNoise
        if self.AddPinkNoise[0]:
            augmenter = Compose([
                AddBackgroundNoise(sounds_path=os.path.join(
                    DEMO_DIR, "pink_noises"),
                                   p=1.0)
            ])
            for i in range(self.AddPinkNoise[1]):
                output_file_path = os.path.join(
                    output_dir,
                    _filename + "_AddPinkNoise{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)
        # AddShortNoises
        if self.AddShortNoises[0]:
            augmenter = Compose([
                AddShortNoises(
                    sounds_path=os.path.join(DEMO_DIR, "short_noises"),
                    min_snr_in_db=0,
                    max_snr_in_db=8,
                    min_time_between_sounds=2.0,
                    max_time_between_sounds=4.0,
                    burst_probability=0.4,
                    min_pause_factor_during_burst=0.01,
                    max_pause_factor_during_burst=0.95,
                    min_fade_in_time=0.005,
                    max_fade_in_time=0.08,
                    min_fade_out_time=0.01,
                    max_fade_out_time=0.1,
                    p=1.0,
                )
            ])
            for i in range(5):
                output_file_path = os.path.join(
                    output_dir,
                    _filename + "_AddShortNoises{:03d}.wav".format(i))
                augmented_samples = augmenter(samples=samples,
                                              sample_rate=SAMPLE_RATE)
                wavfile.write(output_file_path,
                              rate=SAMPLE_RATE,
                              data=augmented_samples)
Exemple #28
0
DEMO_DIR = os.path.dirname(__file__)

if __name__ == "__main__":
    """
    For each transformation, apply it to an example sound and write the transformed sounds to
    an output folder.
    """
    output_dir = os.path.join(DEMO_DIR, "output")
    os.makedirs(output_dir, exist_ok=True)

    samples = load_wav_file(os.path.join(DEMO_DIR, "acoustic_guitar_0.wav"))

    # AddImpulseResponse
    augmenter = Compose(
        [AddImpulseResponse(p=1.0, ir_path=os.path.join(DEMO_DIR, "ir"))]
    )
    output_file_path = os.path.join(
        output_dir, "AddImpulseResponse_{:03d}.wav".format(0)
    )
    augmented_samples = augmenter(samples=samples, sample_rate=SAMPLE_RATE)
    wavfile.write(output_file_path, rate=SAMPLE_RATE, data=augmented_samples)

    # FrequencyMask
    augmenter = Compose([FrequencyMask(p=1.0)])
    for i in range(5):
        output_file_path = os.path.join(
            output_dir, "FrequencyMask_{:03d}.wav".format(i)
        )
        augmented_samples = augmenter(samples=samples, sample_rate=SAMPLE_RATE)
        wavfile.write(output_file_path, rate=SAMPLE_RATE, data=augmented_samples)
Exemple #29
0
    def __init__(self,
                 root_dir,
                 csv_dir,
                 conf,
                 bird_code,
                 inv_ebird_label,
                 num_test_samples=10,
                 bckgrd_aug_dir=None,
                 background_audio_dir=None,
                 file_type="mp3",
                 isTraining=True,
                 transform=None,
                 apply_mixer=False):
        self.root_dir = root_dir
        self.conf = conf
        self.isTraining = isTraining
        self.bird_code = bird_code
        self.inv_ebird_label = inv_ebird_label
        self.transform = transform
        self.file_type = file_type
        self.apply_mixer = apply_mixer
        self.additional_loader_params = {
            "worker_init_fn": self.init_workers_fn,
            "collate_fn": self.collate_fn
        }
        self.sampler = ImbalancedDatasetSampler

        df = pd.read_csv(csv_dir)
        df.secondary_labels = df.secondary_labels.apply(eval)
        self.data = list(df[["filename", "ebird_code",
                             "secondary_labels"]].to_dict('index').values())

        self.background_audio_dir = background_audio_dir
        if self.background_audio_dir is not None:
            for bk in background_audio_dir.glob('**/*.wav'):
                self.data.append({"filename": bk})

        self.num_test_samples = num_test_samples
        self.length = len(self.data)

        if self.apply_mixer:
            self.dict_grp = {}
            for grp, d in df.groupby("ebird_code"):
                self.dict_grp[grp] = d.index.values
            self.possible_mixer_keys = list(self.dict_grp.keys())

            if bckgrd_aug_dir is not None:
                self.augmenter = Compose([
                    AddGaussianNoise(min_amplitude=0.001,
                                     max_amplitude=0.015,
                                     p=0.3),
                    AddGaussianSNR(p=0.3),
                    PitchShift(min_semitones=-4, max_semitones=4, p=0.3),
                    AddBackgroundNoise(bckgrd_aug_dir, p=0.5),
                ])
            else:
                self.augmenter = Compose([
                    AddGaussianNoise(min_amplitude=0.001,
                                     max_amplitude=0.015,
                                     p=0.3),
                    AddGaussianSNR(p=0.3),
                    PitchShift(min_semitones=-4, max_semitones=4, p=0.3)
                ])
        del df
        sound_np = np.divide(
            sound_np, 32768, dtype=np.float32
        )
    number = os.path.split(audio_file)[-1][:-4]

    transforms = [
        {"instance": AddGaussianSNR(p=1.0), "num_runs": 3},
        {"instance": TimeStretch(min_rate=0.4, max_rate=1.25, p=1.0), "num_runs": 5},
        {
            "instance": PitchShift(min_semitones=-5, max_semitones=5, p=1.0),
            "num_runs": 6,
        },
        {"instance": Shift(min_fraction=-0.85, max_fraction=0.85, p=1.0), "num_runs": 4},
        {"instance": Resample(p=1.0), "num_runs": 5},
        {"instance": ClippingDistortion(p=1.0), "num_runs": 3},
    ]

    for transform in transforms:
        augmenter = Compose([transform["instance"]])
        run_name = (
            transform.get("name")
            if transform.get("name")
            else transform["instance"].__class__.__name__
        )
        for i in range(transform["num_runs"]):
            output_file_path = os.path.join(
                'augmented', "{}_{}_{:03d}.wav".format(number, run_name, i)
            )
            augmented_samples = augmenter(samples=sound_np, sample_rate=sample_rate)
            wavfile.write(output_file_path, rate=sample_rate, data=augmented_samples)