{"id":20207534,"url":"https://github.com/alexiajm/maximummargingans","last_synced_at":"2025-04-10T12:34:22.761Z","repository":{"id":177429343,"uuid":"212397158","full_name":"AlexiaJM/MaximumMarginGANs","owner":"AlexiaJM","description":"Code for paper: \"Support Vector Machines, Wasserstein's distance and gradient-penalty GANs maximize a margin\"","archived":false,"fork":false,"pushed_at":"2020-03-12T14:52:28.000Z","size":70,"stargazers_count":178,"open_issues_count":0,"forks_count":24,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-03-24T11:21:20.316Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlexiaJM.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-02T17:09:43.000Z","updated_at":"2025-01-17T16:06:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"a1e0bbc7-d396-4d53-aa8a-9acdc616d995","html_url":"https://github.com/AlexiaJM/MaximumMarginGANs","commit_stats":null,"previous_names":["alexiajm/maximummargingans"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexiaJM%2FMaximumMarginGANs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexiaJM%2FMaximumMarginGANs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexiaJM%2FMaximumMarginGANs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexiaJM%2FMaximumMarginGANs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexiaJM","download_url":"https://codeload.github.com/AlexiaJM/MaximumMarginGANs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248217146,"owners_count":21066633,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T05:29:38.851Z","updated_at":"2025-04-10T12:34:22.750Z","avatar_url":"https://github.com/AlexiaJM.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MaximumMarginGANs\nCode for paper: [Support Vector Machines, Wasserstein's distance and gradient-penalty GANs maximize a margin](https://arxiv.org/abs/1910.06922)\n\n**Discussion at https://ajolicoeur.wordpress.com/MaximumMarginGANs.**\n\nThis basically the same code as https://github.com/AlexiaJM/relativistic-f-divergences, but with more options.\n\n## Citation\n\nIf you use our novel gradient penalties or would like to mention that gradient penalties correspond to having a maximum-margin discriminator, please cite us in your work:\n```\n@article{jolicoeur2019connections}\n  title={Connections between Support Vector Machines, Wasserstein distance and gradient-penalty GANs},\n  author={Jolicoeur-Martineau, Alexia},\n  journal={arXiv preprint arXiv:1910.06922},\n  year={2019}\n}\n```\n\n\n**Sample PyTorch code to use L1, L2, Linfinity gradient penalties with hinge or LS:**\n\n```python\n\n# Best setting (novel Hinge Linfinity gradient penalty)\ngrad_penalty_Lp_norm = 'Linf'\npenalty_type = 'hinge'\n\n# Default setting from WGAN-GP and most cases (L2 gradient penalty)\ngrad_penalty_Lp_norm = 'L2'\npenalty_type = 'LS'\n\n# Calculate gradient\npenalty = 20 # 10 is the more usual choice\nu.resize_(batch_size, 1, 1, 1)\nu.uniform_(0, 1)\nx_both = x.data*u + x_fake.data*(1-u) # interpolation between real and fake samples\nx_both = x_both.cuda()\nx_both = Variable(x_both, requires_grad=True)\ny0 = D(x_both)\ngrad = torch.autograd.grad(outputs=y0, inputs=x_both, grad_outputs=grad_outputs, retain_graph=True, \ncreate_graph=True, only_inputs=True)[0]\nx_both.requires_grad_(False)\ngrad = grad.view(current_batch_size,-1)\n\t\t\t\nif grad_penalty_Lp_norm = 'Linf': # Linfinity gradient norm penalty (Corresponds to L1 margin, BEST results)\n  grad_abs = torch.abs(grad) # Absolute value of gradient\n  grad_norm , _ = torch.max(grad_abs,1)\nelif grad_penalty_Lp_norm = 'L1': # L1 gradient norm penalty (Corresponds to Linfinity margin, WORST results)\n  grad_norm = grad.norm(1,1) \nelse: # L2 gradient norm penalty (Corresponds to L2 margin, this is what people generally use)\n  grad_norm = grad.norm(2,1)\n\nif penalty_type == 'LS': # The usual choice, penalize values below 1 and above 1 (too constraining to properly estimate the Wasserstein distance)\n  constraint = (grad_norm-1).pow(2)\nelif penalty_type == 'hinge': # Penalize values above 1 only (best choice)\n  constraint = torch.nn.ReLU()(grad_norm - 1)\n\nconstraint = constraint.mean()\ngrad_penalty = penalty*constraint\ngrad_penalty.backward(retain_graph=True)\n```\n\n**Needed**\n\n* Python 3.6\n* Pytorch (Latest from source)\n* Tensorflow (Latest from source, needed to get FID)\n* Cat Dataset (http://academictorrents.com/details/c501571c29d16d7f41d159d699d0e7fb37092cbd)\n\n**To do beforehand**\n\n* Change all folders locations in GAN.py (and startup_tmp.sh, fid_script.sh, experiments.sh if you want FID and replication of the paper)\n* Make sure that there are existing folders at the locations you used\n* To get the CAT dataset: open and run each necessary lines of setting_up_script.sh in same folder as preprocess_cat_dataset.py (It will automatically download the cat datasets, if this doesn't work well download it from http://academictorrents.com/details/c501571c29d16d7f41d159d699d0e7fb37092cbd)\n\n**To run models**\n* HingeGAN Linfinity grad norm penalty with max(0, ||grad||-1):\n   * python GAN.py --loss_D 3 --image_size 32 --CIFAR10 True --grad_penalty True --l1_margin --penalty-type 'hinge'\n* WGAN Linfinity grad norm penalty with max(0, ||grad||-1):\n   * python GAN.py --loss_D 4 --image_size 32 --CIFAR10 True --grad_penalty True --l1_margin --penalty-type 'hinge'\n* WGAN L2 grad norm penalty with (||grad||-1)^2 (i.e., WGAN-GP):\n   * python GAN.py --loss_D 4 --image_size 32 --CIFAR10 True --grad_penalty True\n  \n**To replicate the paper**\n  * Open experiments.sh and run the lines you want\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexiajm%2Fmaximummargingans","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexiajm%2Fmaximummargingans","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexiajm%2Fmaximummargingans/lists"}