{"id":8259,"date":"2021-12-02T09:06:13","date_gmt":"2021-12-02T14:06:13","guid":{"rendered":"https:\/\/blogs.mathworks.com\/deep-learning\/?p=8259"},"modified":"2021-12-02T09:06:13","modified_gmt":"2021-12-02T14:06:13","slug":"synthetic-image-generation-using-gans","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/deep-learning\/2021\/12\/02\/synthetic-image-generation-using-gans\/","title":{"rendered":"Synthetic Image Generation using GANs"},"content":{"rendered":"<em>This post is from\u00a0<a href=\"http:\/\/www.ogemarques.com\/\">Oge Marques, PhD<\/a>\u00a0and Professor of Engineering and Computer Science at FAU. Oge is a\u00a0<a href=\"https:\/\/protect-us.mimecast.com\/s\/sFvRCERy7AhoOq41INYhhZ?domain=sigmaxi.org\">Sigma Xi Distinguished Speaker<\/a>,\u00a0<a href=\"https:\/\/protect-us.mimecast.com\/s\/SYBFCG6A7DIZE83WI7UFu1?domain=ogemarques.com\/\">book author<\/a>, and\u00a0<a href=\"https:\/\/protect-us.mimecast.com\/s\/RPeGCJ61jJILln9pizR7yv?domain=aaas.org\">AAAS Leshner Fellow<\/a>.\u00a0He also happens to be a MATLAB aficionado and has been using MATLAB in his classroom for more than 20 years. You can follow him on Twitter (<a href=\"https:\/\/twitter.com\/ProfessorOge\">@ProfessorOge<\/a>).<\/em>\r\n<h6><\/h6>\r\nOccasionally a novel neural network architecture comes along that enables a truly unique way of solving specific deep learning problems. This has certainly been the case with Generative Adversarial Networks (GANs), originally proposed by Ian Goodfellow et al. in\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1406.2661\" rel=\"nofollow\">a 2014 paper<\/a>\u00a0that has been cited more than 32,000 times since its publication. Among other applications, GANs have become the preferred method for synthetic image generation. The results of using GANs for creating realistic images of\u00a0<a href=\"https:\/\/www.nytimes.com\/interactive\/2020\/11\/21\/science\/artificial-intelligence-fake-people-faces.html\" rel=\"nofollow\">people who do not exist<\/a>\u00a0have raised many\u00a0<a href=\"https:\/\/www.nytimes.com\/interactive\/2018\/01\/02\/technology\/ai-generated-photos.html\" rel=\"nofollow\">ethical issues<\/a>\u00a0along the way.\r\n<h6><\/h6>\r\nIn this blog post we focus on using GANs to generate synthetic images of skin lesions for medical image analysis in dermatology.\r\n\r\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-8271 size-large\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/10\/BP3_fig1-1024x390.jpg\" alt=\"\" width=\"1024\" height=\"390\" \/>\r\n\r\n<em>Figure 1: How a generative adversarial network (GAN) works.<\/em>\r\n<h6><\/h6>\r\n<h2>A Quick GAN Lesson<\/h2>\r\nEssentially, GANs consist of two neural network agents\/models (called\u00a0<em>generator<\/em>\u00a0and\u00a0<em>discriminator<\/em>) that compete with one another in a zero-sum game, where one agent's gain is another agent's loss. The\u00a0<em>generator<\/em>\u00a0is used to generate new plausible examples from the problem domain whereas the\u00a0<em>discriminator<\/em>\u00a0is used to classify examples as real (<em>from the domain<\/em>) or fake (<em>generated<\/em>). The discriminator is then updated to get better at discriminating real and fake samples in subsequent iterations, and the generator is updated based on how well the generated samples fooled the discriminator (Figure 1).\r\n<h6><\/h6>\r\nDuring its history, numerous architectural variations and improvements over the original GAN idea have been proposed in the literature. Most GANs today are at least loosely based on the DCGAN (Deep Convolutional Generative Adversarial Networks) architecture, formalized by\u00a0Alec Radford, Luke Metz and Soumith Chintala in\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1511.06434\" rel=\"nofollow\">their 2015 paper<\/a>.\r\n<h6><\/h6>\r\nYou\u2019re likely to see DCGAN,\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1506.05751\" rel=\"nofollow\">LAPGAN<\/a>, and\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1710.10196\" rel=\"nofollow\">PGAN<\/a>\u00a0used for unsupervised techniques like image synthesis, and\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1703.10593\" rel=\"nofollow\">cycleGAN<\/a>\u00a0and\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1611.07004\" rel=\"nofollow\">Pix2Pix<\/a>\u00a0used for cross-modality image-to-image translation.\r\n<h6><\/h6>\r\n<h2>GANs for Medical Images<\/h2>\r\nThe use of GANs to create synthetic medical images is motivated by the following aspects:\r\n<h6><\/h6>\r\n<ol>\r\n \t<li>Medical (imaging) datasets are heavily unbalanced, i.e., they contain many more images of healthy patients than any pathology. The ability to create synthetic images (in different modalities) of specific pathologies could help alleviate the problem and provide more and better samples for a deep learning model to learn from.<\/li>\r\n \t<li>Manual annotation of medical images is a costly process (compared to similar tasks for generic everyday images, which could be handled using crowdsourcing or\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/vision\/ug\/create-automation-algorithm-for-labeling.html\" rel=\"nofollow\">smart image labeling tools<\/a>). If a GAN-based solution were reliable enough to produce appropriate images requiring minimal labeling\/annotation\/validation by a medical expert, the time and cost savings would be appealing.<\/li>\r\n \t<li>Because the images are synthetically generated, there are no patient data or privacy concerns.<\/li>\r\n<\/ol>\r\nSome of the main\u00a0<strong>challenges<\/strong>\u00a0for using GANs to create synthetic medical images, however, are:\r\n<h6><\/h6>\r\n<ol>\r\n \t<li>Domain experts would still be needed to assess quality of synthetic images while the model is being refined, adding significant time to the process before a reliable synthetic medical image generator can be deployed.<\/li>\r\n \t<li>Since we are ultimately dealing with patient health, the stakes involved in training (or fine-tuning) predictive models using synthetic images are higher than using similar techniques for non-critical AI applications. Essentially, if models learn from data, we must trust the data that these models are trained on.<\/li>\r\n<\/ol>\r\nThe popularity of using GANs for medical applications has been growing at a fast pace in the past few years. In addition to synthetic image generation in a variety of medical domains, specialties, and image modalities, other applications of GANs such as cross-modality image-to-image translation (usually among MRI, PET, CT, and MRA) are also being researched in prominent labs, universities, and research centers worldwide.\r\n<h6><\/h6>\r\nIn the field of dermatology, unsupervised synthetic image generation methods have been used to create high resolution synthetic skin lesion samples, which have also been successfully used in the training of skin lesion classi\ufb01ers.\u00a0<a href=\"https:\/\/arxiv.org\/pdf\/1902.03253.pdf\" rel=\"nofollow\">State-of-the-art (SOTA) algorithms<\/a>\u00a0have been able to synthesize high resolution images of skin lesions which expert dermatologists could not reliably tell apart from real samples. Figure 2 shows examples of synthetic images generated by a\u00a0<a href=\"https:\/\/github.com\/alceubissoto\/gan-skin-lesion\" rel=\"nofollow\">recently published solution<\/a>\u00a0as well as real images from the training dataset.\r\n\r\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-8268 size-large\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/10\/BP3_fig2-1024x714.png\" alt=\"\" width=\"1024\" height=\"714\" \/>\r\n\r\n<em>Figure 2: (L) synthetically generated images using\u00a0<a href=\"https:\/\/github.com\/alceubissoto\/gan-skin-lesion\" rel=\"nofollow\">state-of-the-art techniques<\/a>;\r\n(R) actual skin lesion images from a typical training dataset.<\/em>\r\n<h6><\/h6>\r\n<h2>An example<\/h2>\r\nHere is an\u00a0<a href=\"https:\/\/github.com\/ogemarques\/gan-skin-matlab\" rel=\"nofollow\">example<\/a>\u00a0of how to use MATLAB to generate synthetic images of skin lesions.\r\n<h6><\/h6>\r\nThe training dataset consists of annotated images from the\u00a0<a href=\"https:\/\/challenge.isic-archive.com\/data\" rel=\"nofollow\">ISIC 2016 challenge<\/a>, Task 3 (<em>Lesion classification<\/em>) data set, containing 900 dermoscopic lesion images in JPEG format.\r\n<h6><\/h6>\r\nThe code is based on\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ug\/train-generative-adversarial-network.html\" rel=\"nofollow\">an example<\/a>\u00a0using a more generic dataset, and then customized for medical images. It highlights MATLAB\u2019s\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ug\/define-custom-training-loops-loss-functions-and-networks.html\" rel=\"nofollow\">recently added capabilities<\/a>\u00a0for handling more complex deep learning tasks, including the ability to:\r\n<h6><\/h6>\r\n<ul>\r\n \t<li>Create deep neural networks with\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ug\/define-nested-deep-learning-layer.html\" rel=\"nofollow\">custom layers<\/a>, in addition to\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ug\/list-of-deep-learning-layers.html\" rel=\"nofollow\">commonly used built-in layers<\/a>.<\/li>\r\n \t<li>Train deep neural networks with custom training loop and enabling\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ug\/deep-learning-with-automatic-differentiation-in-matlab.html\" rel=\"nofollow\">automatic differentiation<\/a>.<\/li>\r\n \t<li>Process and manage mini-batches of images and using custom mini-batch processing functions.<\/li>\r\n \t<li>Evaluate the model gradients for each mini-batch \u2013 and update the generator and discriminator parameters accordingly.<\/li>\r\n<\/ul>\r\nThe code walks through creating synthetic images using GANs from start (loading and\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/augmentedimagedatastore.html\" rel=\"nofollow\">augmenting the dataset<\/a>) to finish (training the model and generating new images).\r\n<h6><\/h6>\r\nOne of the nicest features of using MATLAB to create synthetic images is the ability to visualize the generated images and score plots as the networks are trained (and, at the end of training, rewind and watch the entire process in a \u201cmovie player\u201d type of interface embedded into the Live Script). Figure 3 shows a screenshot of the process after 600 epochs \/ 4200 iterations. The total training time for a 2021 M1 Mac mini with 16 GB of RAM and no GPU was close to 10 hours.\r\n\r\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-8265 size-large\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/10\/gan_skin4-1024x466.png\" alt=\"\" width=\"1024\" height=\"466\" \/>\r\n\r\n<em>Figure 3: Snapshot of the GAN after training for 600 epochs \/ 4200 iterations. On the left: 25 randomly selected generated images; on the right, generator (blue) and discriminator (red) curves showing\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ug\/monitor-gan-training-progress-and-identify-common-failure-modes.html\" rel=\"nofollow\">score<\/a>\u00a0(between 0 and 1, where 0.5 is best) for each iteration (right).<\/em>\r\n<h6><\/h6>\r\nFigure 4 shows additional examples of 25 randomly selected synthetically generated images after training has completed. The resulting images resemble skin lesions but are not realistic enough to fool a layperson, much less a dermatologist. They indicate that the solution works (notice how the images are very diverse in nature, capturing the diversity of the training set used by the discriminator), but they display several imperfections, among them: a noisy periodic pattern (in what appears to be an 8x8 grid of blocks across the image) and other visible artifacts. It is worth mentioning that the network has also learned a few meaningful artifacts (such as colorful stickers) that are actually present in a significant number of images from the training set.\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-8262 size-full\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/10\/gan_skin2.png\" alt=\"\" width=\"560\" height=\"420\" \/>\r\n<h6><\/h6>\r\n<em>Figure 4: Examples of synthetically generated images.<\/em>\r\n<h6><\/h6>\r\n<h2>Practical hints and tips<\/h2>\r\nIf you choose to go down the path of improving, expanding, and adapting the example to your needs, keep in mind that:\r\n<h6><\/h6>\r\n<ol>\r\n \t<li><strong>Image synthesis using GANs is a\u00a0<u>very<\/u>\u00a0time-consuming process<\/strong>\u00a0(just as most deep learning solutions). Be sure to secure as much computational resources as you can.<\/li>\r\n \t<li><strong>Some things can go wrong<\/strong>\u00a0and could be detected by inspecting the training progress,\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ug\/monitor-gan-training-progress-and-identify-common-failure-modes.html\" rel=\"nofollow\">among them<\/a>:\u00a0<em>convergence failure<\/em>\u00a0(when the generator and discriminator do not reach a balance during training, with one of them overpowering the other) and\u00a0<em>mode collapse<\/em>\u00a0(when the GAN produces a small variety of images with many duplicates and little diversity in the output). Our example doesn\u2019t suffer from either problem.<\/li>\r\n \t<li><strong>Your results may not look \u201cgreat\u201d<\/strong>\u00a0(contrast Figure 4 with Figure 2), but that is to be expected. After all, in this example we are basically using the standard\u00a0<a href=\"https:\/\/paperswithcode.com\/paper\/unsupervised-representation-learning-with-1\" rel=\"nofollow\">DCGAN (deep convolutional generative adversarial network)<\/a>\u00a0Specialized work in synthetic skin lesion image generation has moved significantly beyond DCGAN; SOTA solutions (such as the one by\u00a0<a href=\"https:\/\/arxiv.org\/pdf\/1902.03253.pdf\" rel=\"nofollow\">Bissoto et al.<\/a>\u00a0and the one by\u00a0<a href=\"https:\/\/arxiv.org\/pdf\/1902.03253.pdf\" rel=\"nofollow\">Baur et al.<\/a>) use more sophisticated architectures, normalization options, and validation strategies.<\/li>\r\n<\/ol>\r\n<h2>Key takeaways<\/h2>\r\nGANs (and their numerous variations) are here to stay. They are, according to Yann LeCun,\u00a0<a href=\"https:\/\/quorasessionwithyannlecun.quora.com\/What-are-some-recent-and-potentially-upcoming-breakthroughs-in-unsupervised-learning\" rel=\"nofollow\">\u201cthe coolest thing since sliced bread.\u201d<\/a>\u00a0Many different GAN architectures have been successfully used for generating realistic (i.e., semantically meaningful) synthetic images, which may help training deep learning models in cases where real images are rare, difficult to find, and expensive to annotate.\r\n<h6><\/h6>\r\nIn this blog post we have used MATLAB to show how to generate synthetic images of skin lesions using a simple DCGAN and training images from the ISIC archive.\r\n<h6><\/h6>\r\nMedical image synthesis is a very active research area, and new examples of successful applications of GANs in different medical domains, specialties, and image modalities are likely to emerge in the near future. \u00a0If you\u2019re interested in learning more about it, check out this\u00a0<a href=\"https:\/\/arxiv.org\/pdf\/1809.06222.pdf\" rel=\"nofollow\">review paper<\/a>\u00a0and use our example as a starting point for further experimentation.","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/10\/BP3_fig1-1024x390.jpg\" onError=\"this.style.display ='none';\" \/><\/div><p>This post is from\u00a0Oge Marques, PhD\u00a0and Professor of Engineering and Computer Science at FAU. Oge is a\u00a0Sigma Xi Distinguished Speaker,\u00a0book author, and\u00a0AAAS Leshner Fellow.\u00a0He also happens to be a... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2021\/12\/02\/synthetic-image-generation-using-gans\/\">read more >><\/a><\/p>","protected":false},"author":156,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/8259"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/users\/156"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/comments?post=8259"}],"version-history":[{"count":18,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/8259\/revisions"}],"predecessor-version":[{"id":8430,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/8259\/revisions\/8430"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/media?parent=8259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/categories?post=8259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/tags?post=8259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}