{ "info": { "author": "Ben Striner", "author_email": "bstriner@gmail.com", "bugtrack_url": null, "classifiers": [ "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3" ], "description": "Keras Adversarial Models\r\n========================\r\n\r\n**Combine multiple models into a single Keras model. GANs made easy!**\r\n\r\n``AdversarialModel`` simulates multi-player games. A single call to\r\n``model.fit`` takes targets for each player and updates all of the\r\nplayers. Use ``AdversarialOptimizer`` for complete control of whether\r\nupdates are simultaneous, alternating, or something else entirely. No\r\nmore fooling with ``Trainable`` either!\r\n\r\nInstallation\r\n------------\r\n\r\n.. code:: shell\r\n\r\n git clone https://github.com/bstriner/keras_adversarial.git\r\n cd keras_adversarial\r\n python setup.py install\r\n\r\nUsage\r\n-----\r\n\r\nPlease check the examples folder for exemplary usage.\r\n\r\nInstantiating an adversarial model\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\n- Build separate models for each component / player such as generator\r\n and discriminator.\r\n- Build a combined model. For a GAN, this might have an input for\r\n images and an input for noise and an output for D(fake) and an output\r\n for D(real)\r\n- Pass the combined model and the separate models to the\r\n ``AdversarialModel`` constructor\r\n\r\n.. code:: python\r\n\r\n adversarial_model = AdversarialModel(base_model=gan,\r\n player_params=[generator.trainable_weights, discriminator.trainable_weights],\r\n player_names=[\"generator\", \"discriminator\"])\r\n\r\nThe resulting model will have the same inputs as ``gan`` but separate\r\ntargets and metrics for each player. This is accomplished by copying the\r\nmodel for each player. If each player has a different model, use\r\n``player_models`` (see below regarding dropout).\r\n\r\n.. code:: python\r\n\r\n adversarial_model = AdversarialModel(player_models=[gan_g, gan_d],\r\n player_params=[generator.trainable_weights, discriminator.trainable_weights],\r\n player_names=[\"generator\", \"discriminator\"])\r\n\r\nCompiling an adversarial model\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\nUse ``adversarial_compile`` to compile the model. The parameters are an\r\n``AdversarialOptimizer`` and a list of ``Optimizer`` objects for each\r\nplayer. The loss is passed to ``model.compile`` for each model, so may\r\nbe a dictionary or other object. Use the same order for\r\n``player_optimizers`` as you did for ``player_params`` and\r\n``player_names``.\r\n\r\n.. code:: python\r\n\r\n model.adversarial_compile(adversarial_optimizer=adversarial_optimizer,\r\n player_optimizers=[Adam(1e-4, decay=1e-4), Adam(1e-3, decay=1e-4)],\r\n loss='binary_crossentropy')\r\n\r\nTraining a simple adversarial model\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\nAdversarial models can be trained using ``fit`` and callbacks just like\r\nany other Keras model. Just make sure to provide the correct targets in\r\nthe correct order.\r\n\r\nFor example, given simple GAN named ``gan``: \\* Inputs: ``[x]`` \\*\r\nTargets: ``[y_fake, y_real]`` \\* Metrics:\r\n``[loss, loss_y_fake, loss_y_real]``\r\n\r\n``AdversarialModel(base_model=gan, player_names=['g', 'd']...)`` will\r\nhave: \\* Inputs: ``[x]`` \\* Targets:\r\n``[g_y_fake, g_y_real, d_y_fake, d_y_real]`` \\* Metrics:\r\n``[loss, g_loss, g_loss_y_fake, g_loss_y_real, d_loss, d_loss_y_fake, d_loss_y_real]``\r\n\r\nAdversarial Optimizers\r\n----------------------\r\n\r\nThere are many possible strategies for optimizing multiplayer games.\r\n``AdversarialOptimizer`` is a base class that abstracts those strategies\r\nand is responsible for creating the training function. \\*\r\n``AdversarialOptimizerSimultaneous`` updates each player simultaneously\r\n\\* ``AdversarialOptimizerAlternating`` updates each player in a\r\nround-robin \\* ``UnrolledAdversarialOptimizer`` unrolls updates to\r\nstabilize training (only tested in Theano; slow to build graph but runs\r\nreasonably fast)\r\n\r\nExamples\r\n--------\r\n\r\nMNIST Generative Adversarial Network (GAN)\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\n`example\\_gan.py `__\r\nshows how to create a GAN in Keras for the MNIST dataset.\r\n\r\n.. figure:: https://github.com/bstriner/keras_adversarial/raw/master/doc/images/gan-epoch-099.png\r\n :alt: Example GAN\r\n\r\n Example GAN\r\n\r\nCIFAR10 Generative Adversarial Network (GAN)\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\n`example\\_gan\\_cifar10.py `__\r\nshows how to create a GAN in Keras for the CIFAR10 dataset.\r\n\r\n.. figure:: https://github.com/bstriner/keras_adversarial/raw/master/doc/images/gan-cifar10-epoch-099.png\r\n :alt: Example GAN\r\n\r\n Example GAN\r\n\r\nMNIST Bi-Directional Generative Adversarial Network (BiGAN)\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\n`example\\_bigan.py `__\r\nshows how to create a BiGAN in Keras.\r\n\r\n.. figure:: https://github.com/bstriner/keras_adversarial/raw/master/doc/images/bigan-epoch-099.png\r\n :alt: Example BiGAN\r\n\r\n Example BiGAN\r\n\r\nMNIST Adversarial Autoencoder (AAE)\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\nAn AAE is like a cross between a GAN and a Variational Autoencoder\r\n(VAE).\r\n`example\\_aae.py `__\r\nshows how to create an AAE in Keras.\r\n\r\n.. figure:: https://github.com/bstriner/keras_adversarial/raw/master/doc/images/aae-epoch-099.png\r\n :alt: Example AAE\r\n\r\n Example AAE\r\n\r\nUnrolled Generative Adversarial Network\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\n`example\\_gan\\_unrolled.py `__\r\nshows how to use the unrolled optimizer.\r\n\r\nWARNING: Unrolling the discriminator 8 times takes about 6 hours to\r\nbuild the function on my computer, but only a few minutes for epoch of\r\ntraining. Be prepared to let it run a long time or turn the depth down\r\nto around 4.\r\n\r\nNotes\r\n-----\r\n\r\nDropout\r\n~~~~~~~\r\n\r\nWhen training adversarial models using dropout, you may want to create\r\nseparate models for each player.\r\n\r\nIf you want to train a discriminator with dropout, but train the\r\ngenerator against the discriminator without dropout, create two models.\r\n\\* GAN to train generator: ``D(G(z, dropout=0.5), dropout=0)`` \\* GAN to\r\ntrain discriminator: ``D(G(z, dropout=0), dropout=0.5)``\r\n\r\nIf you create separate models, use ``player_models`` parameter of\r\n``AdversarialModel`` constructor.\r\n\r\nIf you aren't using dropout, one model is sufficient, and use\r\n``base_model`` parameter of ``AdversarialModel`` constructor, which will\r\nduplicate the ``base_model`` for each player.\r\n\r\nTheano and Tensorflow\r\n~~~~~~~~~~~~~~~~~~~~~\r\n\r\nI do most of my development in theano but try to test tensorflow when I\r\nhave extra time. The goal is to support both. Please let me know any\r\nissues you have with either backend.\r\n\r\nQuestions?\r\n~~~~~~~~~~\r\n\r\nFeel free to start an issue or a PR here or in Keras if you are having\r\nany issues or think of something that might be useful.\r\n\r\n\r\n", "description_content_type": null, "docs_url": "https://pythonhosted.org/keras-adversarial/", "download_url": "https://github.com/bstriner/keras-adversarial/tarball/v0.0.3", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/bstriner/keras-adversarial", "keywords": "keras,gan,adversarial,multiplayer", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "keras-adversarial", "package_url": "https://pypi.org/project/keras-adversarial/", "platform": "", "project_url": "https://pypi.org/project/keras-adversarial/", "project_urls": { "Download": "https://github.com/bstriner/keras-adversarial/tarball/v0.0.3", "Homepage": "https://github.com/bstriner/keras-adversarial" }, "release_url": "https://pypi.org/project/keras-adversarial/0.0.3/", "requires_dist": [ "Keras" ], "requires_python": "", "summary": "Adversarial models and optimizers for Keras", "version": "0.0.3" }, "last_serial": 2588819, "releases": { "0.0.3": [ { "comment_text": "", "digests": { "md5": "e86b392d25564d6fd9aa7e90d344c884", "sha256": "dfa01dd4a0034f326c5adf1e8f57bf10ce4424bc5fe298104dc3409543034027" }, "downloads": -1, "filename": "keras_adversarial-0.0.3-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "e86b392d25564d6fd9aa7e90d344c884", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 15209, "upload_time": "2017-01-21T00:53:29", "url": "https://files.pythonhosted.org/packages/9e/f4/8674d740ee3fc155888c5164ca85a1d8e0c04621d642bc5055a729947202/keras_adversarial-0.0.3-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ebfd1fcc47c8cdbb85f17696581764f2", "sha256": "dbdb5a530eb787ab93977fadf48e3c4d84f0a9af192e28885bc3e5bef6b7206a" }, "downloads": -1, "filename": "keras-adversarial-0.0.3.tar.gz", "has_sig": false, "md5_digest": "ebfd1fcc47c8cdbb85f17696581764f2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9501, "upload_time": "2017-01-21T00:53:30", "url": "https://files.pythonhosted.org/packages/90/e8/fd2f7501e3ca7306f56a002e4aae3eca3c36ceed685c01e62d91ca688835/keras-adversarial-0.0.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "e86b392d25564d6fd9aa7e90d344c884", "sha256": "dfa01dd4a0034f326c5adf1e8f57bf10ce4424bc5fe298104dc3409543034027" }, "downloads": -1, "filename": "keras_adversarial-0.0.3-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "e86b392d25564d6fd9aa7e90d344c884", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 15209, "upload_time": "2017-01-21T00:53:29", "url": "https://files.pythonhosted.org/packages/9e/f4/8674d740ee3fc155888c5164ca85a1d8e0c04621d642bc5055a729947202/keras_adversarial-0.0.3-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ebfd1fcc47c8cdbb85f17696581764f2", "sha256": "dbdb5a530eb787ab93977fadf48e3c4d84f0a9af192e28885bc3e5bef6b7206a" }, "downloads": -1, "filename": "keras-adversarial-0.0.3.tar.gz", "has_sig": false, "md5_digest": "ebfd1fcc47c8cdbb85f17696581764f2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9501, "upload_time": "2017-01-21T00:53:30", "url": "https://files.pythonhosted.org/packages/90/e8/fd2f7501e3ca7306f56a002e4aae3eca3c36ceed685c01e62d91ca688835/keras-adversarial-0.0.3.tar.gz" } ] }