{"id":17983293,"url":"https://github.com/yu4u/mixup-generator","last_synced_at":"2025-04-13T06:36:24.893Z","repository":{"id":79870972,"uuid":"109304519","full_name":"yu4u/mixup-generator","owner":"yu4u","description":"An implementation of \"mixup: Beyond Empirical Risk Minimization\"","archived":false,"fork":false,"pushed_at":"2017-11-05T17:30:21.000Z","size":530,"stargazers_count":285,"open_issues_count":7,"forks_count":47,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-26T23:05:28.229Z","etag":null,"topics":["data-augmentation","deep-neural-networks","generator","keras","mixup"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","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/yu4u.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":"2017-11-02T18:39:04.000Z","updated_at":"2025-03-21T14:39:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"7fc091df-4182-4588-abc4-7092fc319cbf","html_url":"https://github.com/yu4u/mixup-generator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yu4u%2Fmixup-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yu4u%2Fmixup-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yu4u%2Fmixup-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yu4u%2Fmixup-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yu4u","download_url":"https://codeload.github.com/yu4u/mixup-generator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248675302,"owners_count":21143763,"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":["data-augmentation","deep-neural-networks","generator","keras","mixup"],"created_at":"2024-10-29T18:16:43.407Z","updated_at":"2025-04-13T06:36:24.860Z","avatar_url":"https://github.com/yu4u.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mixup generator\nThis is an implementation of the mixup algorithm.\n\n\u003cimg src=\"example1.png\" width=\"480px\"\u003e\n\n## Mixup\nMixup [1] is a kind of image augmentation methods,\nwhich augments training data by *mixing-up* both of training images and labels\nby linear interpolation with weight `lambda`:\n\n```\nX = lambda * X1 + (1 - lambda) * X2,\ny = lambda * y1 + (1 - lambda) * y2,\n```\n\nwhere `lambda` is drawn from the Beta distribution `Be(alpha, alpha)`,\nand `alpha` is a hyperparameter.\n\nPlease check [mixup_generator.py](mixup_generator.py) for implementation details.\n\n\n## Usage\nGet `MixupGenerator`:\n\n```python\nfrom mixup_generator import MixupGenerator\ntraining_generator = MixupGenerator(x_train, y_train, batch_size=batch_size, alpha=0.2)()\n```\n\n- `x_train` : training images (#images x h x w x c)\n- `y_train` : labels as one hot vectors (#images x #classes or list of #images x #classes for multi-task training)\n- `batch_size` : batch size\n- `alpha` : hyper parameter; `lambda` is drawn from the beta distribution `Be(alpha, alpha)`.\n\nGet *mixed* training batch:\n\n```python\nx, y = next(generator)\n```\n\nPlease refer to [test_mixup.ipynb](test_mixup.ipynb) to see how it works.\n\n\n### Use MixupGenerator with fit_generator in Keras\nIt is very easy to use `MixupGenerator` in training if you are using Keras;\nGet `MixupGenerator`, and then fit model by `fit_generator`:\n\n```python\nmodel.fit_generator(generator=training_generator,\n                    steps_per_epoch=x_train.shape[0] // batch_size,\n                    validation_data=(x_test, y_test),\n                    epochs=epochs, verbose=1,\n                    callbacks=callbacks)\n```\n\nPlease refer to [cifar10_resnet.py](cifar10_resnet.py) for complete example,\nwhich is imported from [official Keras examples](https://github.com/fchollet/keras/tree/master/examples).\n\n### MixupGenerator with ImageDataGenerator\nThe `MixupGenerator` can be combined with\n`keras.preprocessing.image.ImageDataGenerator` for further image augmentation:\n\n```python\ndatagen = ImageDataGenerator(\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    horizontal_flip=True)\n    \ntraining_generator = MixupGenerator(x_train, y_train, batch_size=batch_size, alpha=0.2, datagen=datagen)()\n```\n\nIn this case, the *mixed-up* training images are further augmented by ImageDataGenerator.\n\n\u003cimg src=\"example2.png\" width=\"480px\"\u003e\n\n### Mixup with Random Erasing\nRandom Erasing [2] is a kind of image augmentation methods for convolutional neural networks (CNN).\nIt tries to regularize models using training images that are randomly masked with random values.\n\nPlease refer to [this repository](https://github.com/yu4u/cutout-random-erasing)\nfor the details of algorithm and its implementation.\n\nMixup can be combined with Random Erasing via ImageDataGenerator by:\n\n```python\nfrom random_eraser import get_random_eraser\n\ndatagen = ImageDataGenerator(\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    horizontal_flip=True,\n    preprocessing_function=get_random_eraser(v_l=0, v_h=255))\n\ngenerator = MixupGenerator(x_train, y_train, alpha=1.0, datagen=datagen)()\n```\n\nThe augmented images become like this:\n\n\u003cimg src=\"example3.png\" width=\"480px\"\u003e\n\n\n## Results\n(!Only a single trial)\n\nWithout mixup:\n\n```\nTest loss: 0.862150103855\nTest accuracy: 0.8978\n```\n\nWith mixup alpha = 0.2:\n\n```\nTest loss: 0.510702615929\nTest accuracy: 0.9117\n```\n\nWith mixup alpha = 0.5:\n\n```\nTest loss: 0.48489781661\nTest accuracy: 0.9181\n```\n\nWith mixup alpha = 1.0:\n\n```\nTest loss: 0.493033925915\nTest accuracy: 0.9167\n```\n\n## References\n[1] H. Zhang, M. Cisse, Y. N. Dauphin, and D. Lopez-Paz, \"mixup: Beyond Empirical Risk Minimization,\" in arXiv:1710.09412, 2017.\n\n[2] Z. Zhong, L. Zheng, G. Kang, S. Li, and Y. Yang, \"Random Erasing Data Augmentation,\" in arXiv:1708.04896, 2017.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyu4u%2Fmixup-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyu4u%2Fmixup-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyu4u%2Fmixup-generator/lists"}