Ejemplo n.º 1
0
    class RichardNixonIdentifier(ff.BaseModel):
        docs = ff.PickleFeature(ff.IteratorNode, store=False)

        shuffled = ff.PickleFeature(zounds.ShuffledSamples,
                                    nsamples=int(1e5),
                                    multiplexed=True,
                                    dtype=np.float32,
                                    needs=docs,
                                    store=False)

        mu_law_source = ff.PickleFeature(zounds.MuLawCompressed,
                                         needs=shuffled.aspect('data'),
                                         store=False)

        scaled_source = ff.PickleFeature(zounds.InstanceScaling,
                                         needs=mu_law_source,
                                         store=False)

        network = ff.PickleFeature(
            zounds.PyTorchNetwork,
            trainer=zounds.SupervisedTrainer(
                model=Discriminator(),
                loss=nn.BCELoss(),
                optimizer=lambda model: optim.Adam(model.parameters(),
                                                   lr=0.00005),
                epochs=epochs,
                batch_size=64,
                holdout_percent=0.5),
            needs=dict(data=scaled_source, labels=shuffled.aspect('labels')),
            store=False)

        pipeline = ff.PickleFeature(zounds.PreprocessingPipeline,
                                    needs=(mu_law_source, scaled_source,
                                           network),
                                    store=True)
Ejemplo n.º 2
0
class FreqAdaptiveAutoEncoder(ff.BaseModel):
    """
    Define a processing pipeline to learn a compressed representation of the
    Sound.freq_adaptive feature.  Once this is trained and the pipeline is
    stored, we can apply all the pre-processing steps and the autoencoder
    forward and in reverse.
    """
    docs = ff.Feature(ff.IteratorNode)

    shuffle = ff.PickleFeature(zounds.ShuffledSamples,
                               nsamples=500000,
                               dtype=np.float32,
                               needs=docs)

    mu_law = ff.PickleFeature(zounds.MuLawCompressed, needs=shuffle)

    scaled = ff.PickleFeature(zounds.InstanceScaling, needs=mu_law)

    autoencoder = ff.PickleFeature(
        zounds.PyTorchAutoEncoder,
        trainer=zounds.SupervisedTrainer(
            AutoEncoder(),
            loss=nn.MSELoss(),
            optimizer=lambda model: optim.Adam(model.parameters(), lr=0.00005),
            epochs=100,
            batch_size=64,
            holdout_percent=0.5),
        needs=scaled)

    # assemble the previous steps into a re-usable pipeline, which can perform
    # forward and backward transformations
    pipeline = ff.PickleFeature(zounds.PreprocessingPipeline,
                                needs=(mu_law, scaled, autoencoder),
                                store=True)
    else:
        network = RawSamplesAutoEncoder()
        loss = FrequencyBandLoss()
        synthesize = raw_samples_synthesize
        pipeline_cls = AutoEncoderPipeline
        data_preprocessor = label_preprocessor = lambda x: x
        batch_size = 64
        gen = (snd.windowed for snd in Sound
               if args.internet_archive_id in snd._id)

    if args.force_train or not AutoEncoderPipeline.exists():
        trainer = zounds.SupervisedTrainer(
            network,
            loss,
            lambda model: Adam(model.parameters(), lr=0.0001),
            epochs=args.epochs,
            batch_size=batch_size,
            holdout_percent=0.25,
            data_preprocessor=data_preprocessor,
            label_preprocessor=label_preprocessor)

        gen = (snd.windowed for snd in Sound
               if args.internet_archive_id in snd._id)
        pipeline_cls.process(samples=gen, trainer=trainer)

    # instantiate the trained pipeline
    pipeline = pipeline_cls()

    snds = [snd for snd in Sound if args.internet_archive_id in snd._id]
    snd = choice(snds)
    time_slice = zounds.TimeSlice(duration=zounds.Seconds(10))