├── LICENSE ├── README.md ├── __pycache__ └── data_loader.cpython-38.pyc ├── clipping_camera.jpg ├── data_loader.py ├── figures ├── 3d-photo-re.jpg ├── LensOCR.jpg ├── U2NETPR.png ├── U2Net_Logo.png ├── art_transfer.JPG ├── bg-removal.gif ├── bg-rm-aug.gif ├── clipping_camera.jpg ├── close-seg.jpg ├── gradio_web_demo.jpg ├── hotpot.png ├── human_seg.png ├── human_seg_results.gif ├── human_seg_video.gif ├── im_composite.jpg ├── lensto.png ├── motor-demo.gif ├── pixelmator.jpg ├── portrait-ios-app.jpg ├── portrait_kids.png ├── portrait_ladies.png ├── portrait_men.png ├── profuai.png ├── qual.png ├── quan_1.png ├── quan_2.png ├── rembg.png ├── rm_bg.JPG ├── ship-demo.gif ├── silueta.png ├── sky-seg.png ├── style-trans.JPG ├── swift-u2net.jpeg ├── u2net-best-paper.jpg ├── u2netqual.png ├── view-move.gif └── xuebin-demo.png ├── gradio └── demo.py ├── model ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-36.pyc │ ├── __init__.cpython-37.pyc │ ├── __init__.cpython-38.pyc │ ├── u2net.cpython-36.pyc │ ├── u2net.cpython-37.pyc │ └── u2net.cpython-38.pyc ├── u2net.py └── u2net_refactor.py ├── requirements.txt ├── saved_models └── face_detection_cv2 │ └── haarcascade_frontalface_default.xml ├── setup_model_weights.py ├── test_data ├── test_human_images │ ├── 19035828_web1__12294096_web1_180615-PNR-newmayorchallenge.jpg │ ├── 2019-LADIES-NIGHT-2ND-GOMES.jpg │ ├── 480112-637286191695715805-16x9.jpg │ ├── 5-mental-skills-of-successful-athletes-image.jpg │ ├── Athlete-Intake.jpg │ ├── Two_dancers.jpg │ ├── coach-yelling-at-athlete-716268.jpg │ ├── download.jpeg │ ├── f2dc6965e31f7ff0bb54618d53437006.jpg │ ├── image1440x560cropped.jpg │ ├── images (1).jpeg │ ├── images (2).jpeg │ ├── images.jpeg │ ├── julia+trotti_17.jpg │ ├── language_1280p.jpg │ ├── lionel-messi-athletes-fashion.jpg │ ├── olympic-athletes-need-to-know-2018-winter-1.jpg │ ├── photo-1552374196-c4e7ffc6e126.jpeg │ └── track-runners-hurdles-1280.jpg ├── test_images │ ├── 0002-01.jpg │ ├── 0003.jpg │ ├── bike.jpg │ ├── boat.jpg │ ├── girl.png │ ├── hockey.png │ ├── horse.jpg │ ├── im_01.png │ ├── im_14.png │ ├── im_21.png │ ├── im_27.png │ ├── lamp2_meitu_1.jpg │ ├── long.jpg │ ├── rifle1.jpg │ ├── rifle2.jpeg │ ├── sailboat3.jpg │ ├── vangogh.jpeg │ └── whisk.png ├── test_portrait_images │ ├── portrait_im │ │ ├── img_1585.png │ │ ├── img_1588.png │ │ ├── img_1594.png │ │ ├── img_1616.png │ │ ├── img_1695.png │ │ ├── img_1696.png │ │ ├── img_1771.png │ │ └── img_1859.png │ ├── portrait_results │ │ ├── img_1585.png │ │ ├── img_1588.png │ │ ├── img_1594.png │ │ ├── img_1616.png │ │ ├── img_1695.png │ │ ├── img_1696.png │ │ ├── img_1771.png │ │ └── img_1859.png │ ├── your_portrait_im │ │ ├── GalGadot.jpg │ │ ├── guliNazha3.jpg │ │ ├── kid1.jpg │ │ ├── kid2.jpg │ │ ├── kid3.jpg │ │ ├── man.jpg │ │ ├── man2.jpg │ │ ├── man4.jpg │ │ ├── man5.jpg │ │ └── smile.jpg │ └── your_portrait_results │ │ ├── GalGadot.png │ │ ├── GalGadot_sigma_20.0_alpha_0.5_composite.png │ │ ├── guliNazha3.png │ │ ├── guliNazha3_sigma_20.0_alpha_0.5_composite.png │ │ ├── kid1.png │ │ ├── kid1_sigma_20.0_alpha_0.5_composite.png │ │ ├── kid2.png │ │ ├── kid2_sigma_20.0_alpha_0.5_composite.png │ │ ├── kid3.png │ │ ├── kid3_sigma_20.0_alpha_0.5_composite.png │ │ ├── man.png │ │ ├── man2.png │ │ ├── man2_sigma_20.0_alpha_0.5_composite.png │ │ ├── man4.png │ │ ├── man4_sigma_20.0_alpha_0.5_composite.png │ │ ├── man5.png │ │ ├── man5_sigma_20.0_alpha_0.5_composite.png │ │ ├── man_sigma_20.0_alpha_0.5_composite.png │ │ ├── smile.png │ │ └── smile_sigma_20.0_alpha_0.5_composite.png ├── u2net_results │ ├── 0002-01.png │ ├── 0003.png │ ├── bike.png │ ├── boat.png │ ├── girl.png │ ├── hockey.png │ ├── horse.png │ ├── im_01.png │ ├── im_14.png │ ├── im_21.png │ ├── im_27.png │ ├── lamp2_meitu_1.png │ ├── long.png │ ├── rifle1.png │ ├── rifle2.png │ ├── sailboat3.png │ ├── vangogh.png │ └── whisk.png ├── u2net_test_human_images_results │ ├── 19035828_web1__12294096_web1_180615-PNR-newmayorchallenge.png │ ├── 2019-LADIES-NIGHT-2ND-GOMES.png │ ├── 480112-637286191695715805-16x9.png │ ├── 5-mental-skills-of-successful-athletes-image.png │ ├── Athlete-Intake.png │ ├── Two_dancers.png │ ├── coach-yelling-at-athlete-716268.png │ ├── download.png │ ├── f2dc6965e31f7ff0bb54618d53437006.png │ ├── image1440x560cropped.png │ ├── images (1).png │ ├── images (2).png │ ├── images.png │ ├── julia+trotti_17.png │ ├── language_1280p.png │ ├── lionel-messi-athletes-fashion.png │ ├── olympic-athletes-need-to-know-2018-winter-1.png │ ├── photo-1552374196-c4e7ffc6e126.png │ └── track-runners-hurdles-1280.png └── u2netp_results │ ├── 0002-01.png │ ├── 0003.png │ ├── bike.png │ ├── boat.png │ ├── girl.png │ ├── hockey.png │ ├── horse.png │ ├── im_01.png │ ├── im_14.png │ ├── im_21.png │ ├── im_27.png │ ├── lamp2_meitu_1.png │ ├── long.png │ ├── rifle1.png │ ├── rifle2.png │ ├── sailboat3.png │ ├── vangogh.png │ └── whisk.png ├── u2net_human_seg_test.py ├── u2net_portrait_composite.py ├── u2net_portrait_demo.py ├── u2net_portrait_test.py ├── u2net_test.py └── u2net_train.py /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 |

U2-Net: U Square Net

5 | 6 |

7 | 8 | This is the official repo for our paper **U2-Net(U square net)** published in Pattern Recognition 2020: 9 | 10 | ## [U2-Net: Going Deeper with Nested U-Structure for Salient Object Detection](https://arxiv.org/pdf/2005.09007.pdf) 11 | [Xuebin Qin](https://xuebinqin.github.io/), [Zichen Zhang](https://webdocs.cs.ualberta.ca/~zichen2/), [Chenyang Huang](https://chenyangh.com/), [Masood Dehghan](https://sites.google.com/view/masooddehghan), [Osmar R. Zaiane](http://webdocs.cs.ualberta.ca/~zaiane/) and [Martin Jagersand](https://webdocs.cs.ualberta.ca/~jag/) 12 | 13 | 14 | __Contact__: xuebin[at]ualberta[dot]ca 15 | 16 | ## Updates !!! 17 | 18 | ** (2022-Aug.-24) ** We are glad to announce that our U2-Net published in Pattern Recognition has been awarded the 2020 Pattern Recognition BEST PAPER AWARD !!! 19 | ![u2net-best-paper](figures/u2net-best-paper.jpg) 20 | 21 | ** (2022-Aug.-17) ** 22 | Our U2-Net models are now available on [PlayTorch](https://playtorch.dev/), where you can build your own demo and run it on your Android/iOS phone. Try out this demo on [![PlayTorch Demo](https://github.com/facebookresearch/playtorch/blob/main/website/static/assets/playtorch_badge.svg)](https://playtorch.dev/snack/@playtorch/u2net/) and bring your ideas about U2-Net to truth in minutes! 23 | 24 | ** (2022-Jul.-5)** Our new work **Highly Accurate Dichotomous Image Segmentation (DIS) [**Project Page**](https://xuebinqin.github.io/dis/index.html), [**Github**](https://github.com/xuebinqin/DIS) is accepted by ECCV 2022. Our code and dataset will be released before July 17th, 2022. Please be aware of our updates. 25 | ![ship-demo](figures/ship-demo.gif) 26 | ![bg-removal](figures/bg-removal.gif) 27 | ![view-move](figures/view-move.gif) 28 | ![motor-demo](figures/motor-demo.gif) 29 | 30 | ** (2022-Jun.-3)** Thank [**Adir Kol**](https://github.com/adirkol) for sharing the iOS App [**3D Photo Creator**](https://apps.apple.com/us/app/3d-photo-creator/id1619676262) based on our U2-Net. 31 | ![portrait-ios-app](figures/3d-photo-re.jpg) 32 | 33 | ** (2022-Mar.-31)** Thank [**Hikaru Tsuyumine**] for implementing the iOS App [**Portrait Drawing**](https://apps.apple.com/us/app/portrait-drawing/id1623269600) based on our U2-Net portrait generation model. 34 | ![portrait-ios-app](figures/portrait-ios-app.jpg) 35 | 36 | ** (2022-Apr.-12)** Thank [**Kevin Shah**](https://github.com/ioskevinshah) for providing us a great iOS App [**Lensto**](https://apps.apple.com/in/app/lensto-background-changer/id1574844033), ([**Demo Video**](https://www.youtube.com/shorts/jWwUiKZjfok)), based on U2-Net. 37 | ![lensto](figures/lensto.png) 38 | 39 | ** (2022-Mar.-31)** Our U2-Net model is also integrated by [**Hotpot.ai**](https://hotpot.ai/) for art design. 40 | ![hotpot](figures/hotpot.png) 41 | 42 | ** (2022-Mar-19)** Thank [**Kikedao**](https://github.com/Kikedao) for providing a fantastic webapp [**Silueta**](https://silueta.me/) based on U2-Net. More details can be found at [**https://github.com/xuebinqin/U-2-Net/issues/295**](https://github.com/xuebinqin/U-2-Net/issues/295). 43 | ![silueta](figures/silueta.png) 44 | 45 | ** (2022-Mar-17)** Thank [**Ezaldeen Sahb**](https://github.com/Ezaldeen99/BackgroundRemoval) for implementing the iOS library for image background removal based on U2-Net, which will greatly facilitate the developing of mobile apps. 46 | ![close-seg](figures/swift-u2net.jpeg) 47 | 48 | 50 | 51 | ** (2022-Mar-8)** Thank [**Levin Dabhi**](https://github.com/levindabhi/cloth-segmentation) for training the amazing clothes segmentation U2-Net. 52 | ![close-seg](figures/close-seg.jpg) 53 | 54 | ** (2022-Mar-3)** Thank [**Renato Violin**](https://github.com/renatoviolin/bg-remove-augment) for providing an awesome webapp for image background removal and replacement based on our U2-Net. 55 | ![bg-rm-aug](figures/bg-rm-aug.gif) 56 | 57 | **(2021-Dec-21)** This [**blog**](https://rockyshikoku.medium.com/u2net-to-coreml-machine-learning-segmentation-on-iphone-eac0c721d67b) clearly describes the way of converting U2-Net to [**CoreML**](https://github.com/john-rocky/CoreML-Models) and running it on iphone. 58 | 59 | **(2021-Nov-28)** Interesting Sky Segmentation models developed by [**xiongzhu**](https://github.com/xiongzhu666/Sky-Segmentation-and-Post-processing) using U2-Net. 60 | 61 | ![im_sky_segmentation](figures/sky-seg.png) 62 | 63 | **(2021-Nov-28)** Awesome image editing app [**Pixelmator pro**](https://www.pixelmator.com/pro/) uses U2-Net as one of its background removal models. 64 | 65 | ![im_sky_segmentation](figures/pixelmator.jpg) 66 | 67 | **(2021-Aug-24)** We played a bit more about fusing the orignal image and the generated portraits to composite different styles. You can
68 | (1) Download this repo by 69 | ``` 70 | git clone https://github.com/NathanUA/U-2-Net.git 71 | ``` 72 | (2) Download the u2net_portrait.pth from [**GoogleDrive**](https://drive.google.com/file/d/1IG3HdpcRiDoWNookbncQjeaPN28t90yW/view?usp=sharing) or [**Baidu Pan(提取码:chgd)**](https://pan.baidu.com/s/1BYT5Ts6BxwpB8_l2sAyCkw)model and put it into the directory: ```./saved_models/u2net_portrait/```,
73 | (3) run the code by command 74 | ``` 75 | python u2net_portrait_composite.py -s 20 -a 0.5 76 | ``` 77 | ,where ``-s`` indicates the sigma of gaussian function for blurring the orignal image and ``-a`` denotes the alpha weights of the orignal image when fusing them.
78 | 79 | ![im_portrait_composite](figures/im_composite.jpg) 80 | 81 | **(2021-July-16)** A new [background removal webapp](https://remove-background.net/) developed by Изатоп Василий. 82 | 83 | ![rm_bg](figures/rm_bg.JPG) 84 | 85 | **(2021-May-26)** Thank [**Dang Quoc Quy**](https://github.com/quyvsquy) for his [**Art Transfer APP**](https://play.google.com/store/apps/details?id=com.quyvsquy.arttransfer) built upon U2-Net. 86 | 87 | 88 | 89 | **(2021-May-5)** Thank [**AK391**](https://github.com/AK391) for sharing his [**Gradio Web Demo of U2-Net**](https://gradio.app/hub/AK391/U-2-Net). 90 | 91 | ![gradio_web_demo](figures/gradio_web_demo.jpg) 92 | 93 | 94 | **(2021-Apr-29)** Thanks [**Jonathan Benavides Vallejo**](https://www.linkedin.com/in/jonathanbv/) for releasing his App [**LensOCR: Extract Text & Image**](https://apps.apple.com/ch/app/lensocr-extract-text-image/id1549961729?l=en&mt=12), which uses U2-Net for extracting the image foreground. 95 | 96 | ![LensOCR APP](figures/LensOCR.jpg) 97 | 98 | **(2021-Apr-18)** Thanks [**Andrea Scuderi**](https://www.linkedin.com/in/andreascuderi/) for releasing his App [**Clipping Camera**](https://apps.apple.com/us/app/clipping-camera/id1548192169?ign-mpt=uo%3D2), which is an U2-Net driven realtime camera app and "is able to detect relevant object from the scene and clip them to apply fancy filters". 99 | 100 | ![Clipping Camera APP](figures/clipping_camera.jpg) 101 | 102 | **(2021-Mar-17)** [**Dennis Bappert**](https://github.com/dennisbappert) re-trained the U2-Net model for [**human portrait matting**](https://github.com/dennisbappert/u-2-net-portrait). The results look very promising and he also provided the details of the training process and data generation(and augmentation) strategy, which are inspiring. 103 | 104 | **(2021-Mar-11)** Dr. Tim developed a [**video version rembg**](https://github.com/ecsplendid/rembg-greenscreen) for removing video backgrounds using U2-Net. The awesome demo results can be found on [**YouTube**](https://www.youtube.com/watch?v=4NjqR2vCV_k). 105 | 106 | **(2021-Mar-02)** We found some other interesting applications of our U2-Net including [**MOJO CUT**](https://play.google.com/store/apps/details?id=com.innoria.magicut&hl=en_CA&gl=US), [**Real-Time Background Removal on Iphone**](https://www.linkedin.com/feed/update/urn:li:activity:6752303661705170944/?updateEntityUrn=urn%3Ali%3Afs_feedUpdate%3A%28V2%2Curn%3Ali%3Aactivity%3A6752303661705170944%29), [**Video Background Removal**](https://nisargkapkar.hashnode.dev/image-and-video-background-removal-using-deep-learning), [**Another Online Portrait Generation Demo on AWS**](http://s3-website-hosting-u2net.s3-website-eu-west-1.amazonaws.com/), [**AI Scissor**](https://qooba.net/2020/09/11/ai-scissors-sharp-cut-with-neural-networks/). 107 | 108 | **(2021-Feb-15)** We just released an online demo [**http://profu.ai**](http://profu.ai) for the portrait generation. Please feel free to give it a try and provide any suggestions or comments.
109 | ![Profuai](figures/profuai.png)
110 | 111 | **(2021-Feb-06)** Recently, some people asked the problem of using U2-Net for human segmentation, so we trained another example model for human segemntation based on [**Supervisely Person Dataset**](https://supervise.ly/explore/projects/supervisely-person-dataset-23304/datasets).
112 | 113 | (1) To run the human segmentation model, please first downlowd the [**u2net_human_seg.pth**](https://drive.google.com/file/d/1m_Kgs91b21gayc2XLW0ou8yugAIadWVP/view?usp=sharing) model weights into ``` ./saved_models/u2net_human_seg/```.
114 | (2) Prepare the to-be-segmented images into the corresponding directory, e.g. ```./test_data/test_human_images/```.
115 | (3) Run the inference by command: ```python u2net_human_seg_test.py``` and the results will be output into the corresponding dirctory, e.g. ```./test_data/u2net_test_human_images_results/```
116 | [**Notes: Due to the labeling accuracy of the Supervisely Person Dataset, the human segmentation model (u2net_human_seg.pth) here won't give you hair-level accuracy. But it should be more robust than u2net trained with DUTS-TR dataset on general human segmentation task. It can be used for human portrait segmentation, human body segmentation, etc.**](https://github.com/NathanUA/U-2-Net)
117 | 118 | ![Human Image Segmentation](figures/human_seg.png)
119 | ![Human Video](figures/human_seg_video.gif) 120 | ![Human Video Results](figures/human_seg_results.gif) 121 | 122 | **(2020-Dec-28)** Some interesting applications and useful tools based on U2-Net:
123 | (1) [**Xiaolong Liu**](https://github.com/LiuXiaolong19920720) developed several very interesting applications based on U2-Net including [**Human Portrait Drawing**](https://www.cvpy.net/studio/cv/func/DeepLearning/sketch/sketch/page/)(As far as I know, Xiaolong is the first one who uses U2-Net for portrait generation), [**image matting**](https://www.cvpy.net/studio/cv/func/DeepLearning/matting/matting/page/) and [**so on**](https://www.cvpy.net/).
124 | (2) [**Vladimir Seregin**](https://github.com/peko/nn-lineart) developed an interesting tool, [**NN based lineart**](https://peko.github.io/nn-lineart/), for comparing the portrait results of U2-Net and that of another popular model, [**ArtLine**](https://github.com/vijishmadhavan/ArtLine), developed by [**Vijish Madhavan**](https://github.com/vijishmadhavan).
125 | (3) [**Daniel Gatis**](https://github.com/danielgatis/rembg) built a python tool, [**Rembg**](https://pypi.org/project/rembg/), for image backgrounds removal based on U2-Net. I think this tool will greatly facilitate the application of U2-Net in different fields.
126 | ![REMBG](figures/rembg.png) 127 | 128 | **(2020-Nov-21)** Recently, we found an interesting application of U2-Net for [**human portrait drawing**](https://www.pythonf.cn/read/141098). Therefore, we trained another model for this task based on the [**APDrawingGAN dataset**](https://github.com/yiranran/APDrawingGAN). 129 | 130 | ![Sample Results: Kids](figures/portrait_kids.png) 131 | 132 | ![Sample Results: Ladies](figures/portrait_ladies.png) 133 | 134 | ![Sample Results: Men](figures/portrait_men.png) 135 | 136 | ### Usage for portrait generation 137 | 1. Clone this repo to local 138 | ``` 139 | git clone https://github.com/NathanUA/U-2-Net.git 140 | ``` 141 | 142 | 2. Download the u2net_portrait.pth from [**GoogleDrive**](https://drive.google.com/file/d/1IG3HdpcRiDoWNookbncQjeaPN28t90yW/view?usp=sharing) or [**Baidu Pan(提取码:chgd)**](https://pan.baidu.com/s/1BYT5Ts6BxwpB8_l2sAyCkw)model and put it into the directory: ```./saved_models/u2net_portrait/```. 143 | 144 | 3. Run on the testing set.
145 | (1) Download the train and test set from [**APDrawingGAN**](https://github.com/yiranran/APDrawingGAN). These images and their ground truth are stitched side-by-side (512x1024). You need to split each of these images into two 512x512 images and put them into ```./test_data/test_portrait_images/portrait_im/```. You can also download the split testing set on [GoogleDrive](https://drive.google.com/file/d/1NkTsDDN8VO-JVik6VxXyV-3l2eo29KCk/view?usp=sharing).
146 | (2) Running the inference with command ```python u2net_portrait_test.py``` will ouptut the results into ```./test_data/test_portrait_images/portrait_results```.
147 | 148 | 4. Run on your own dataset.
149 | (1) Prepare your images and put them into ```./test_data/test_portrait_images/your_portrait_im/```. [**To obtain enough details of the protrait, human head region in the input image should be close to or larger than 512x512. The head background should be relatively clear.**](https://github.com/NathanUA/U-2-Net)
150 | (2) Run the prediction by command ```python u2net_portrait_demo.py``` will outputs the results to ```./test_data/test_portrait_images/your_portrait_results/```.
151 | (3) The difference between ```python u2net_portrait_demo.py``` and ```python u2net_portrait_test.py``` is that we added a simple [**face detection**](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_objdetect/py_face_detection/py_face_detection.html) step before the portrait generation in ```u2net_portrait_demo.py```. Because the testing set of APDrawingGAN are normalized and cropped to 512x512 for including only heads of humans, while our own dataset may varies with different resolutions and contents. Therefore, the code ```python u2net_portrait_demo.py``` will detect the biggest face from the given image and then crop, pad and resize the ROI to 512x512 for feeding to the network. The following figure shows how to take your own photos for generating high quality portraits. 152 | 153 | **(2020-Sep-13)** Our U2-Net based model is the **6th** in [**MICCAI 2020 Thyroid Nodule Segmentation Challenge**](https://tn-scui2020.grand-challenge.org/Resultannouncement/). 154 | 155 | **(2020-May-18)** The official paper of our **U2-Net (U square net)** ([**PDF in elsevier**(free until July 5 2020)](https://www.sciencedirect.com/science/article/pii/S0031320320302077?dgcid=author), [**PDF in arxiv**](http://arxiv.org/abs/2005.09007)) is now available. If you are not able to access that, please feel free to drop me an email. 156 | 157 | **(2020-May-16)** We fixed the upsampling issue of the network. Now, the model should be able to handle **arbitrary input size**. (Tips: This modification is to facilitate the retraining of U2-Net on your own datasets. When using our pre-trained model on SOD datasets, please keep the input size as 320x320 to guarantee the performance.) 158 | 159 | **(2020-May-16)** We highly appreciate **Cyril Diagne** for building this fantastic AR project: [**AR Copy and Paste**](https://github.com/cyrildiagne/ar-cutpaste) using our **U2-Net** (Qin *et al*, PR 2020) and [**BASNet**](https://github.com/NathanUA/BASNet)(Qin *et al*, CVPR 2019). The [**demo video**](https://twitter.com/cyrildiagne/status/1256916982764646402) in twitter has achieved over **5M** views, which is phenomenal and shows us more application possibilities of SOD. 160 | 161 | ## U2-Net Results (176.3 MB) 162 | 163 | ![U2-Net Results](figures/u2netqual.png) 164 | 165 | 166 | ## Our previous work: [BASNet (CVPR 2019)](https://github.com/NathanUA/BASNet) 167 | 168 | ## Required libraries 169 | 170 | Python 3.6 171 | numpy 1.15.2 172 | scikit-image 0.14.0 173 | python-opencv 174 | PIL 5.2.0 175 | PyTorch 0.4.0 176 | torchvision 0.2.1 177 | glob 178 | 179 | ## Usage for salient object detection 180 | 1. Clone this repo 181 | ``` 182 | git clone https://github.com/NathanUA/U-2-Net.git 183 | ``` 184 | 2. Download the pre-trained model u2net.pth (176.3 MB) from [**GoogleDrive**](https://drive.google.com/file/d/1ao1ovG1Qtx4b7EoskHXmi2E9rp5CHLcZ/view?usp=sharing) or [**Baidu Pan 提取码: pf9k**](https://pan.baidu.com/s/1WjwyEwDiaUjBbx_QxcXBwQ) or u2netp.pth (4.7 MB) from [**GoogleDrive**](https://drive.google.com/file/d/1rbSTGKAE-MTxBYHd-51l2hMOQPT_7EPy/view?usp=sharing) or [**Baidu Pan 提取码: 8xsi**](https://pan.baidu.com/s/10tW12OlecRpE696z8FxdNQ) and put it into the dirctory './saved_models/u2net/' and './saved_models/u2netp/' 185 | 186 | 3. Cd to the directory 'U-2-Net', run the train or inference process by command: ```python u2net_train.py``` 187 | or ```python u2net_test.py``` respectively. The 'model_name' in both files can be changed to 'u2net' or 'u2netp' for using different models. 188 | 189 | We also provide the predicted saliency maps ([u2net results](https://drive.google.com/file/d/1mZFWlS4WygWh1eVI8vK2Ad9LrPq4Hp5v/view?usp=sharing),[u2netp results](https://drive.google.com/file/d/1j2pU7vyhOO30C2S_FJuRdmAmMt3-xmjD/view?usp=sharing)) for datasets SOD, ECSSD, DUT-OMRON, PASCAL-S, HKU-IS and DUTS-TE. 190 | 191 | 192 | ## U2-Net Architecture 193 | 194 | ![U2-Net architecture](figures/U2NETPR.png) 195 | 196 | 197 | ## Quantitative Comparison 198 | 199 | ![Quantitative Comparison](figures/quan_1.png) 200 | 201 | ![Quantitative Comparison](figures/quan_2.png) 202 | 203 | 204 | ## Qualitative Comparison 205 | 206 | ![Qualitative Comparison](figures/qual.png?raw=true) 207 | 208 | ## Citation 209 | ``` 210 | @InProceedings{Qin_2020_PR, 211 | title = {U2-Net: Going Deeper with Nested U-Structure for Salient Object Detection}, 212 | author = {Qin, Xuebin and Zhang, Zichen and Huang, Chenyang and Dehghan, Masood and Zaiane, Osmar and Jagersand, Martin}, 213 | journal = {Pattern Recognition}, 214 | volume = {106}, 215 | pages = {107404}, 216 | year = {2020} 217 | } 218 | ``` 219 | -------------------------------------------------------------------------------- /__pycache__/data_loader.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/__pycache__/data_loader.cpython-38.pyc -------------------------------------------------------------------------------- /clipping_camera.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/clipping_camera.jpg -------------------------------------------------------------------------------- /data_loader.py: -------------------------------------------------------------------------------- 1 | # data loader 2 | from __future__ import print_function, division 3 | import glob 4 | import torch 5 | from skimage import io, transform, color 6 | import numpy as np 7 | import random 8 | import math 9 | import matplotlib.pyplot as plt 10 | from torch.utils.data import Dataset, DataLoader 11 | from torchvision import transforms, utils 12 | from PIL import Image 13 | 14 | #==========================dataset load========================== 15 | class RescaleT(object): 16 | 17 | def __init__(self,output_size): 18 | assert isinstance(output_size,(int,tuple)) 19 | self.output_size = output_size 20 | 21 | def __call__(self,sample): 22 | imidx, image, label = sample['imidx'], sample['image'],sample['label'] 23 | 24 | h, w = image.shape[:2] 25 | 26 | if isinstance(self.output_size,int): 27 | if h > w: 28 | new_h, new_w = self.output_size*h/w,self.output_size 29 | else: 30 | new_h, new_w = self.output_size,self.output_size*w/h 31 | else: 32 | new_h, new_w = self.output_size 33 | 34 | new_h, new_w = int(new_h), int(new_w) 35 | 36 | # #resize the image to new_h x new_w and convert image from range [0,255] to [0,1] 37 | # img = transform.resize(image,(new_h,new_w),mode='constant') 38 | # lbl = transform.resize(label,(new_h,new_w),mode='constant', order=0, preserve_range=True) 39 | 40 | img = transform.resize(image,(self.output_size,self.output_size),mode='constant') 41 | lbl = transform.resize(label,(self.output_size,self.output_size),mode='constant', order=0, preserve_range=True) 42 | 43 | return {'imidx':imidx, 'image':img,'label':lbl} 44 | 45 | class Rescale(object): 46 | 47 | def __init__(self,output_size): 48 | assert isinstance(output_size,(int,tuple)) 49 | self.output_size = output_size 50 | 51 | def __call__(self,sample): 52 | imidx, image, label = sample['imidx'], sample['image'],sample['label'] 53 | 54 | if random.random() >= 0.5: 55 | image = image[::-1] 56 | label = label[::-1] 57 | 58 | h, w = image.shape[:2] 59 | 60 | if isinstance(self.output_size,int): 61 | if h > w: 62 | new_h, new_w = self.output_size*h/w,self.output_size 63 | else: 64 | new_h, new_w = self.output_size,self.output_size*w/h 65 | else: 66 | new_h, new_w = self.output_size 67 | 68 | new_h, new_w = int(new_h), int(new_w) 69 | 70 | # #resize the image to new_h x new_w and convert image from range [0,255] to [0,1] 71 | img = transform.resize(image,(new_h,new_w),mode='constant') 72 | lbl = transform.resize(label,(new_h,new_w),mode='constant', order=0, preserve_range=True) 73 | 74 | return {'imidx':imidx, 'image':img,'label':lbl} 75 | 76 | class RandomCrop(object): 77 | 78 | def __init__(self,output_size): 79 | assert isinstance(output_size, (int, tuple)) 80 | if isinstance(output_size, int): 81 | self.output_size = (output_size, output_size) 82 | else: 83 | assert len(output_size) == 2 84 | self.output_size = output_size 85 | def __call__(self,sample): 86 | imidx, image, label = sample['imidx'], sample['image'], sample['label'] 87 | 88 | if random.random() >= 0.5: 89 | image = image[::-1] 90 | label = label[::-1] 91 | 92 | h, w = image.shape[:2] 93 | new_h, new_w = self.output_size 94 | 95 | top = np.random.randint(0, h - new_h) 96 | left = np.random.randint(0, w - new_w) 97 | 98 | image = image[top: top + new_h, left: left + new_w] 99 | label = label[top: top + new_h, left: left + new_w] 100 | 101 | return {'imidx':imidx,'image':image, 'label':label} 102 | 103 | class ToTensor(object): 104 | """Convert ndarrays in sample to Tensors.""" 105 | 106 | def __call__(self, sample): 107 | 108 | imidx, image, label = sample['imidx'], sample['image'], sample['label'] 109 | 110 | tmpImg = np.zeros((image.shape[0],image.shape[1],3)) 111 | tmpLbl = np.zeros(label.shape) 112 | 113 | image = image/np.max(image) 114 | if(np.max(label)<1e-6): 115 | label = label 116 | else: 117 | label = label/np.max(label) 118 | 119 | if image.shape[2]==1: 120 | tmpImg[:,:,0] = (image[:,:,0]-0.485)/0.229 121 | tmpImg[:,:,1] = (image[:,:,0]-0.485)/0.229 122 | tmpImg[:,:,2] = (image[:,:,0]-0.485)/0.229 123 | else: 124 | tmpImg[:,:,0] = (image[:,:,0]-0.485)/0.229 125 | tmpImg[:,:,1] = (image[:,:,1]-0.456)/0.224 126 | tmpImg[:,:,2] = (image[:,:,2]-0.406)/0.225 127 | 128 | tmpLbl[:,:,0] = label[:,:,0] 129 | 130 | 131 | tmpImg = tmpImg.transpose((2, 0, 1)) 132 | tmpLbl = label.transpose((2, 0, 1)) 133 | 134 | return {'imidx':torch.from_numpy(imidx), 'image': torch.from_numpy(tmpImg), 'label': torch.from_numpy(tmpLbl)} 135 | 136 | class ToTensorLab(object): 137 | """Convert ndarrays in sample to Tensors.""" 138 | def __init__(self,flag=0): 139 | self.flag = flag 140 | 141 | def __call__(self, sample): 142 | 143 | imidx, image, label =sample['imidx'], sample['image'], sample['label'] 144 | 145 | tmpLbl = np.zeros(label.shape) 146 | 147 | if(np.max(label)<1e-6): 148 | label = label 149 | else: 150 | label = label/np.max(label) 151 | 152 | # change the color space 153 | if self.flag == 2: # with rgb and Lab colors 154 | tmpImg = np.zeros((image.shape[0],image.shape[1],6)) 155 | tmpImgt = np.zeros((image.shape[0],image.shape[1],3)) 156 | if image.shape[2]==1: 157 | tmpImgt[:,:,0] = image[:,:,0] 158 | tmpImgt[:,:,1] = image[:,:,0] 159 | tmpImgt[:,:,2] = image[:,:,0] 160 | else: 161 | tmpImgt = image 162 | tmpImgtl = color.rgb2lab(tmpImgt) 163 | 164 | # nomalize image to range [0,1] 165 | tmpImg[:,:,0] = (tmpImgt[:,:,0]-np.min(tmpImgt[:,:,0]))/(np.max(tmpImgt[:,:,0])-np.min(tmpImgt[:,:,0])) 166 | tmpImg[:,:,1] = (tmpImgt[:,:,1]-np.min(tmpImgt[:,:,1]))/(np.max(tmpImgt[:,:,1])-np.min(tmpImgt[:,:,1])) 167 | tmpImg[:,:,2] = (tmpImgt[:,:,2]-np.min(tmpImgt[:,:,2]))/(np.max(tmpImgt[:,:,2])-np.min(tmpImgt[:,:,2])) 168 | tmpImg[:,:,3] = (tmpImgtl[:,:,0]-np.min(tmpImgtl[:,:,0]))/(np.max(tmpImgtl[:,:,0])-np.min(tmpImgtl[:,:,0])) 169 | tmpImg[:,:,4] = (tmpImgtl[:,:,1]-np.min(tmpImgtl[:,:,1]))/(np.max(tmpImgtl[:,:,1])-np.min(tmpImgtl[:,:,1])) 170 | tmpImg[:,:,5] = (tmpImgtl[:,:,2]-np.min(tmpImgtl[:,:,2]))/(np.max(tmpImgtl[:,:,2])-np.min(tmpImgtl[:,:,2])) 171 | 172 | # tmpImg = tmpImg/(np.max(tmpImg)-np.min(tmpImg)) 173 | 174 | tmpImg[:,:,0] = (tmpImg[:,:,0]-np.mean(tmpImg[:,:,0]))/np.std(tmpImg[:,:,0]) 175 | tmpImg[:,:,1] = (tmpImg[:,:,1]-np.mean(tmpImg[:,:,1]))/np.std(tmpImg[:,:,1]) 176 | tmpImg[:,:,2] = (tmpImg[:,:,2]-np.mean(tmpImg[:,:,2]))/np.std(tmpImg[:,:,2]) 177 | tmpImg[:,:,3] = (tmpImg[:,:,3]-np.mean(tmpImg[:,:,3]))/np.std(tmpImg[:,:,3]) 178 | tmpImg[:,:,4] = (tmpImg[:,:,4]-np.mean(tmpImg[:,:,4]))/np.std(tmpImg[:,:,4]) 179 | tmpImg[:,:,5] = (tmpImg[:,:,5]-np.mean(tmpImg[:,:,5]))/np.std(tmpImg[:,:,5]) 180 | 181 | elif self.flag == 1: #with Lab color 182 | tmpImg = np.zeros((image.shape[0],image.shape[1],3)) 183 | 184 | if image.shape[2]==1: 185 | tmpImg[:,:,0] = image[:,:,0] 186 | tmpImg[:,:,1] = image[:,:,0] 187 | tmpImg[:,:,2] = image[:,:,0] 188 | else: 189 | tmpImg = image 190 | 191 | tmpImg = color.rgb2lab(tmpImg) 192 | 193 | # tmpImg = tmpImg/(np.max(tmpImg)-np.min(tmpImg)) 194 | 195 | tmpImg[:,:,0] = (tmpImg[:,:,0]-np.min(tmpImg[:,:,0]))/(np.max(tmpImg[:,:,0])-np.min(tmpImg[:,:,0])) 196 | tmpImg[:,:,1] = (tmpImg[:,:,1]-np.min(tmpImg[:,:,1]))/(np.max(tmpImg[:,:,1])-np.min(tmpImg[:,:,1])) 197 | tmpImg[:,:,2] = (tmpImg[:,:,2]-np.min(tmpImg[:,:,2]))/(np.max(tmpImg[:,:,2])-np.min(tmpImg[:,:,2])) 198 | 199 | tmpImg[:,:,0] = (tmpImg[:,:,0]-np.mean(tmpImg[:,:,0]))/np.std(tmpImg[:,:,0]) 200 | tmpImg[:,:,1] = (tmpImg[:,:,1]-np.mean(tmpImg[:,:,1]))/np.std(tmpImg[:,:,1]) 201 | tmpImg[:,:,2] = (tmpImg[:,:,2]-np.mean(tmpImg[:,:,2]))/np.std(tmpImg[:,:,2]) 202 | 203 | else: # with rgb color 204 | tmpImg = np.zeros((image.shape[0],image.shape[1],3)) 205 | image = image/np.max(image) 206 | if image.shape[2]==1: 207 | tmpImg[:,:,0] = (image[:,:,0]-0.485)/0.229 208 | tmpImg[:,:,1] = (image[:,:,0]-0.485)/0.229 209 | tmpImg[:,:,2] = (image[:,:,0]-0.485)/0.229 210 | else: 211 | tmpImg[:,:,0] = (image[:,:,0]-0.485)/0.229 212 | tmpImg[:,:,1] = (image[:,:,1]-0.456)/0.224 213 | tmpImg[:,:,2] = (image[:,:,2]-0.406)/0.225 214 | 215 | tmpLbl[:,:,0] = label[:,:,0] 216 | 217 | 218 | tmpImg = tmpImg.transpose((2, 0, 1)) 219 | tmpLbl = label.transpose((2, 0, 1)) 220 | 221 | return {'imidx':torch.from_numpy(imidx), 'image': torch.from_numpy(tmpImg), 'label': torch.from_numpy(tmpLbl)} 222 | 223 | class SalObjDataset(Dataset): 224 | def __init__(self,img_name_list,lbl_name_list,transform=None): 225 | # self.root_dir = root_dir 226 | # self.image_name_list = glob.glob(image_dir+'*.png') 227 | # self.label_name_list = glob.glob(label_dir+'*.png') 228 | self.image_name_list = img_name_list 229 | self.label_name_list = lbl_name_list 230 | self.transform = transform 231 | 232 | def __len__(self): 233 | return len(self.image_name_list) 234 | 235 | def __getitem__(self,idx): 236 | 237 | # image = Image.open(self.image_name_list[idx])#io.imread(self.image_name_list[idx]) 238 | # label = Image.open(self.label_name_list[idx])#io.imread(self.label_name_list[idx]) 239 | 240 | image = io.imread(self.image_name_list[idx]) 241 | imname = self.image_name_list[idx] 242 | imidx = np.array([idx]) 243 | 244 | if(0==len(self.label_name_list)): 245 | label_3 = np.zeros(image.shape) 246 | else: 247 | label_3 = io.imread(self.label_name_list[idx]) 248 | 249 | label = np.zeros(label_3.shape[0:2]) 250 | if(3==len(label_3.shape)): 251 | label = label_3[:,:,0] 252 | elif(2==len(label_3.shape)): 253 | label = label_3 254 | 255 | if(3==len(image.shape) and 2==len(label.shape)): 256 | label = label[:,:,np.newaxis] 257 | elif(2==len(image.shape) and 2==len(label.shape)): 258 | image = image[:,:,np.newaxis] 259 | label = label[:,:,np.newaxis] 260 | 261 | sample = {'imidx':imidx, 'image':image, 'label':label} 262 | 263 | if self.transform: 264 | sample = self.transform(sample) 265 | 266 | return sample 267 | -------------------------------------------------------------------------------- /figures/3d-photo-re.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/3d-photo-re.jpg -------------------------------------------------------------------------------- /figures/LensOCR.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/LensOCR.jpg -------------------------------------------------------------------------------- /figures/U2NETPR.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/U2NETPR.png -------------------------------------------------------------------------------- /figures/U2Net_Logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/U2Net_Logo.png -------------------------------------------------------------------------------- /figures/art_transfer.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/art_transfer.JPG -------------------------------------------------------------------------------- /figures/bg-removal.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/bg-removal.gif -------------------------------------------------------------------------------- /figures/bg-rm-aug.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/bg-rm-aug.gif -------------------------------------------------------------------------------- /figures/clipping_camera.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/clipping_camera.jpg -------------------------------------------------------------------------------- /figures/close-seg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/close-seg.jpg -------------------------------------------------------------------------------- /figures/gradio_web_demo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/gradio_web_demo.jpg -------------------------------------------------------------------------------- /figures/hotpot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/hotpot.png -------------------------------------------------------------------------------- /figures/human_seg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/human_seg.png -------------------------------------------------------------------------------- /figures/human_seg_results.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/human_seg_results.gif -------------------------------------------------------------------------------- /figures/human_seg_video.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/human_seg_video.gif -------------------------------------------------------------------------------- /figures/im_composite.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/im_composite.jpg -------------------------------------------------------------------------------- /figures/lensto.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/lensto.png -------------------------------------------------------------------------------- /figures/motor-demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/motor-demo.gif -------------------------------------------------------------------------------- /figures/pixelmator.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/pixelmator.jpg -------------------------------------------------------------------------------- /figures/portrait-ios-app.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/portrait-ios-app.jpg -------------------------------------------------------------------------------- /figures/portrait_kids.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/portrait_kids.png -------------------------------------------------------------------------------- /figures/portrait_ladies.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/portrait_ladies.png -------------------------------------------------------------------------------- /figures/portrait_men.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/portrait_men.png -------------------------------------------------------------------------------- /figures/profuai.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/profuai.png -------------------------------------------------------------------------------- /figures/qual.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/qual.png -------------------------------------------------------------------------------- /figures/quan_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/quan_1.png -------------------------------------------------------------------------------- /figures/quan_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/quan_2.png -------------------------------------------------------------------------------- /figures/rembg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/rembg.png -------------------------------------------------------------------------------- /figures/rm_bg.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/rm_bg.JPG -------------------------------------------------------------------------------- /figures/ship-demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/ship-demo.gif -------------------------------------------------------------------------------- /figures/silueta.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/silueta.png -------------------------------------------------------------------------------- /figures/sky-seg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/sky-seg.png -------------------------------------------------------------------------------- /figures/style-trans.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/style-trans.JPG -------------------------------------------------------------------------------- /figures/swift-u2net.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/swift-u2net.jpeg -------------------------------------------------------------------------------- /figures/u2net-best-paper.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/u2net-best-paper.jpg -------------------------------------------------------------------------------- /figures/u2netqual.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/u2netqual.png -------------------------------------------------------------------------------- /figures/view-move.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/view-move.gif -------------------------------------------------------------------------------- /figures/xuebin-demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/figures/xuebin-demo.png -------------------------------------------------------------------------------- /gradio/demo.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import paddlehub as hub 3 | import gradio as gr 4 | import torch 5 | 6 | # Images 7 | torch.hub.download_url_to_file('https://cdn.pixabay.com/photo/2018/08/12/16/59/ara-3601194_1280.jpg', 'parrot.jpg') 8 | torch.hub.download_url_to_file('https://cdn.pixabay.com/photo/2016/10/21/14/46/fox-1758183_1280.jpg', 'fox.jpg') 9 | 10 | model = hub.Module(name='U2Net') 11 | 12 | def infer(img): 13 | result = model.Segmentation( 14 | images=[cv2.imread(img.name)], 15 | paths=None, 16 | batch_size=1, 17 | input_size=320, 18 | output_dir='output', 19 | visualization=True) 20 | return result[0]['front'][:,:,::-1], result[0]['mask'] 21 | 22 | inputs = gr.inputs.Image(type='file', label="Original Image") 23 | outputs = [ 24 | gr.outputs.Image(type="numpy",label="Front"), 25 | gr.outputs.Image(type="numpy",label="Mask") 26 | ] 27 | 28 | title = "U^2-Net" 29 | description = "demo for U^2-Net. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below." 30 | article = "

U^2-Net: Going Deeper with Nested U-Structure for Salient Object Detection | Github Repo

" 31 | 32 | examples = [ 33 | ['fox.jpg'], 34 | ['parrot.jpg'] 35 | ] 36 | 37 | gr.Interface(infer, inputs, outputs, title=title, description=description, article=article, examples=examples).launch() -------------------------------------------------------------------------------- /model/__init__.py: -------------------------------------------------------------------------------- 1 | from .u2net import U2NET 2 | from .u2net import U2NETP 3 | -------------------------------------------------------------------------------- /model/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/model/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /model/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/model/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /model/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/model/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /model/__pycache__/u2net.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/model/__pycache__/u2net.cpython-36.pyc -------------------------------------------------------------------------------- /model/__pycache__/u2net.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/model/__pycache__/u2net.cpython-37.pyc -------------------------------------------------------------------------------- /model/__pycache__/u2net.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/model/__pycache__/u2net.cpython-38.pyc -------------------------------------------------------------------------------- /model/u2net.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | 5 | class REBNCONV(nn.Module): 6 | def __init__(self,in_ch=3,out_ch=3,dirate=1): 7 | super(REBNCONV,self).__init__() 8 | 9 | self.conv_s1 = nn.Conv2d(in_ch,out_ch,3,padding=1*dirate,dilation=1*dirate) 10 | self.bn_s1 = nn.BatchNorm2d(out_ch) 11 | self.relu_s1 = nn.ReLU(inplace=True) 12 | 13 | def forward(self,x): 14 | 15 | hx = x 16 | xout = self.relu_s1(self.bn_s1(self.conv_s1(hx))) 17 | 18 | return xout 19 | 20 | ## upsample tensor 'src' to have the same spatial size with tensor 'tar' 21 | def _upsample_like(src,tar): 22 | 23 | src = F.upsample(src,size=tar.shape[2:],mode='bilinear') 24 | 25 | return src 26 | 27 | 28 | ### RSU-7 ### 29 | class RSU7(nn.Module):#UNet07DRES(nn.Module): 30 | 31 | def __init__(self, in_ch=3, mid_ch=12, out_ch=3): 32 | super(RSU7,self).__init__() 33 | 34 | self.rebnconvin = REBNCONV(in_ch,out_ch,dirate=1) 35 | 36 | self.rebnconv1 = REBNCONV(out_ch,mid_ch,dirate=1) 37 | self.pool1 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 38 | 39 | self.rebnconv2 = REBNCONV(mid_ch,mid_ch,dirate=1) 40 | self.pool2 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 41 | 42 | self.rebnconv3 = REBNCONV(mid_ch,mid_ch,dirate=1) 43 | self.pool3 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 44 | 45 | self.rebnconv4 = REBNCONV(mid_ch,mid_ch,dirate=1) 46 | self.pool4 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 47 | 48 | self.rebnconv5 = REBNCONV(mid_ch,mid_ch,dirate=1) 49 | self.pool5 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 50 | 51 | self.rebnconv6 = REBNCONV(mid_ch,mid_ch,dirate=1) 52 | 53 | self.rebnconv7 = REBNCONV(mid_ch,mid_ch,dirate=2) 54 | 55 | self.rebnconv6d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 56 | self.rebnconv5d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 57 | self.rebnconv4d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 58 | self.rebnconv3d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 59 | self.rebnconv2d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 60 | self.rebnconv1d = REBNCONV(mid_ch*2,out_ch,dirate=1) 61 | 62 | def forward(self,x): 63 | 64 | hx = x 65 | hxin = self.rebnconvin(hx) 66 | 67 | hx1 = self.rebnconv1(hxin) 68 | hx = self.pool1(hx1) 69 | 70 | hx2 = self.rebnconv2(hx) 71 | hx = self.pool2(hx2) 72 | 73 | hx3 = self.rebnconv3(hx) 74 | hx = self.pool3(hx3) 75 | 76 | hx4 = self.rebnconv4(hx) 77 | hx = self.pool4(hx4) 78 | 79 | hx5 = self.rebnconv5(hx) 80 | hx = self.pool5(hx5) 81 | 82 | hx6 = self.rebnconv6(hx) 83 | 84 | hx7 = self.rebnconv7(hx6) 85 | 86 | hx6d = self.rebnconv6d(torch.cat((hx7,hx6),1)) 87 | hx6dup = _upsample_like(hx6d,hx5) 88 | 89 | hx5d = self.rebnconv5d(torch.cat((hx6dup,hx5),1)) 90 | hx5dup = _upsample_like(hx5d,hx4) 91 | 92 | hx4d = self.rebnconv4d(torch.cat((hx5dup,hx4),1)) 93 | hx4dup = _upsample_like(hx4d,hx3) 94 | 95 | hx3d = self.rebnconv3d(torch.cat((hx4dup,hx3),1)) 96 | hx3dup = _upsample_like(hx3d,hx2) 97 | 98 | hx2d = self.rebnconv2d(torch.cat((hx3dup,hx2),1)) 99 | hx2dup = _upsample_like(hx2d,hx1) 100 | 101 | hx1d = self.rebnconv1d(torch.cat((hx2dup,hx1),1)) 102 | 103 | return hx1d + hxin 104 | 105 | ### RSU-6 ### 106 | class RSU6(nn.Module):#UNet06DRES(nn.Module): 107 | 108 | def __init__(self, in_ch=3, mid_ch=12, out_ch=3): 109 | super(RSU6,self).__init__() 110 | 111 | self.rebnconvin = REBNCONV(in_ch,out_ch,dirate=1) 112 | 113 | self.rebnconv1 = REBNCONV(out_ch,mid_ch,dirate=1) 114 | self.pool1 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 115 | 116 | self.rebnconv2 = REBNCONV(mid_ch,mid_ch,dirate=1) 117 | self.pool2 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 118 | 119 | self.rebnconv3 = REBNCONV(mid_ch,mid_ch,dirate=1) 120 | self.pool3 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 121 | 122 | self.rebnconv4 = REBNCONV(mid_ch,mid_ch,dirate=1) 123 | self.pool4 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 124 | 125 | self.rebnconv5 = REBNCONV(mid_ch,mid_ch,dirate=1) 126 | 127 | self.rebnconv6 = REBNCONV(mid_ch,mid_ch,dirate=2) 128 | 129 | self.rebnconv5d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 130 | self.rebnconv4d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 131 | self.rebnconv3d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 132 | self.rebnconv2d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 133 | self.rebnconv1d = REBNCONV(mid_ch*2,out_ch,dirate=1) 134 | 135 | def forward(self,x): 136 | 137 | hx = x 138 | 139 | hxin = self.rebnconvin(hx) 140 | 141 | hx1 = self.rebnconv1(hxin) 142 | hx = self.pool1(hx1) 143 | 144 | hx2 = self.rebnconv2(hx) 145 | hx = self.pool2(hx2) 146 | 147 | hx3 = self.rebnconv3(hx) 148 | hx = self.pool3(hx3) 149 | 150 | hx4 = self.rebnconv4(hx) 151 | hx = self.pool4(hx4) 152 | 153 | hx5 = self.rebnconv5(hx) 154 | 155 | hx6 = self.rebnconv6(hx5) 156 | 157 | 158 | hx5d = self.rebnconv5d(torch.cat((hx6,hx5),1)) 159 | hx5dup = _upsample_like(hx5d,hx4) 160 | 161 | hx4d = self.rebnconv4d(torch.cat((hx5dup,hx4),1)) 162 | hx4dup = _upsample_like(hx4d,hx3) 163 | 164 | hx3d = self.rebnconv3d(torch.cat((hx4dup,hx3),1)) 165 | hx3dup = _upsample_like(hx3d,hx2) 166 | 167 | hx2d = self.rebnconv2d(torch.cat((hx3dup,hx2),1)) 168 | hx2dup = _upsample_like(hx2d,hx1) 169 | 170 | hx1d = self.rebnconv1d(torch.cat((hx2dup,hx1),1)) 171 | 172 | return hx1d + hxin 173 | 174 | ### RSU-5 ### 175 | class RSU5(nn.Module):#UNet05DRES(nn.Module): 176 | 177 | def __init__(self, in_ch=3, mid_ch=12, out_ch=3): 178 | super(RSU5,self).__init__() 179 | 180 | self.rebnconvin = REBNCONV(in_ch,out_ch,dirate=1) 181 | 182 | self.rebnconv1 = REBNCONV(out_ch,mid_ch,dirate=1) 183 | self.pool1 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 184 | 185 | self.rebnconv2 = REBNCONV(mid_ch,mid_ch,dirate=1) 186 | self.pool2 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 187 | 188 | self.rebnconv3 = REBNCONV(mid_ch,mid_ch,dirate=1) 189 | self.pool3 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 190 | 191 | self.rebnconv4 = REBNCONV(mid_ch,mid_ch,dirate=1) 192 | 193 | self.rebnconv5 = REBNCONV(mid_ch,mid_ch,dirate=2) 194 | 195 | self.rebnconv4d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 196 | self.rebnconv3d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 197 | self.rebnconv2d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 198 | self.rebnconv1d = REBNCONV(mid_ch*2,out_ch,dirate=1) 199 | 200 | def forward(self,x): 201 | 202 | hx = x 203 | 204 | hxin = self.rebnconvin(hx) 205 | 206 | hx1 = self.rebnconv1(hxin) 207 | hx = self.pool1(hx1) 208 | 209 | hx2 = self.rebnconv2(hx) 210 | hx = self.pool2(hx2) 211 | 212 | hx3 = self.rebnconv3(hx) 213 | hx = self.pool3(hx3) 214 | 215 | hx4 = self.rebnconv4(hx) 216 | 217 | hx5 = self.rebnconv5(hx4) 218 | 219 | hx4d = self.rebnconv4d(torch.cat((hx5,hx4),1)) 220 | hx4dup = _upsample_like(hx4d,hx3) 221 | 222 | hx3d = self.rebnconv3d(torch.cat((hx4dup,hx3),1)) 223 | hx3dup = _upsample_like(hx3d,hx2) 224 | 225 | hx2d = self.rebnconv2d(torch.cat((hx3dup,hx2),1)) 226 | hx2dup = _upsample_like(hx2d,hx1) 227 | 228 | hx1d = self.rebnconv1d(torch.cat((hx2dup,hx1),1)) 229 | 230 | return hx1d + hxin 231 | 232 | ### RSU-4 ### 233 | class RSU4(nn.Module):#UNet04DRES(nn.Module): 234 | 235 | def __init__(self, in_ch=3, mid_ch=12, out_ch=3): 236 | super(RSU4,self).__init__() 237 | 238 | self.rebnconvin = REBNCONV(in_ch,out_ch,dirate=1) 239 | 240 | self.rebnconv1 = REBNCONV(out_ch,mid_ch,dirate=1) 241 | self.pool1 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 242 | 243 | self.rebnconv2 = REBNCONV(mid_ch,mid_ch,dirate=1) 244 | self.pool2 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 245 | 246 | self.rebnconv3 = REBNCONV(mid_ch,mid_ch,dirate=1) 247 | 248 | self.rebnconv4 = REBNCONV(mid_ch,mid_ch,dirate=2) 249 | 250 | self.rebnconv3d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 251 | self.rebnconv2d = REBNCONV(mid_ch*2,mid_ch,dirate=1) 252 | self.rebnconv1d = REBNCONV(mid_ch*2,out_ch,dirate=1) 253 | 254 | def forward(self,x): 255 | 256 | hx = x 257 | 258 | hxin = self.rebnconvin(hx) 259 | 260 | hx1 = self.rebnconv1(hxin) 261 | hx = self.pool1(hx1) 262 | 263 | hx2 = self.rebnconv2(hx) 264 | hx = self.pool2(hx2) 265 | 266 | hx3 = self.rebnconv3(hx) 267 | 268 | hx4 = self.rebnconv4(hx3) 269 | 270 | hx3d = self.rebnconv3d(torch.cat((hx4,hx3),1)) 271 | hx3dup = _upsample_like(hx3d,hx2) 272 | 273 | hx2d = self.rebnconv2d(torch.cat((hx3dup,hx2),1)) 274 | hx2dup = _upsample_like(hx2d,hx1) 275 | 276 | hx1d = self.rebnconv1d(torch.cat((hx2dup,hx1),1)) 277 | 278 | return hx1d + hxin 279 | 280 | ### RSU-4F ### 281 | class RSU4F(nn.Module):#UNet04FRES(nn.Module): 282 | 283 | def __init__(self, in_ch=3, mid_ch=12, out_ch=3): 284 | super(RSU4F,self).__init__() 285 | 286 | self.rebnconvin = REBNCONV(in_ch,out_ch,dirate=1) 287 | 288 | self.rebnconv1 = REBNCONV(out_ch,mid_ch,dirate=1) 289 | self.rebnconv2 = REBNCONV(mid_ch,mid_ch,dirate=2) 290 | self.rebnconv3 = REBNCONV(mid_ch,mid_ch,dirate=4) 291 | 292 | self.rebnconv4 = REBNCONV(mid_ch,mid_ch,dirate=8) 293 | 294 | self.rebnconv3d = REBNCONV(mid_ch*2,mid_ch,dirate=4) 295 | self.rebnconv2d = REBNCONV(mid_ch*2,mid_ch,dirate=2) 296 | self.rebnconv1d = REBNCONV(mid_ch*2,out_ch,dirate=1) 297 | 298 | def forward(self,x): 299 | 300 | hx = x 301 | 302 | hxin = self.rebnconvin(hx) 303 | 304 | hx1 = self.rebnconv1(hxin) 305 | hx2 = self.rebnconv2(hx1) 306 | hx3 = self.rebnconv3(hx2) 307 | 308 | hx4 = self.rebnconv4(hx3) 309 | 310 | hx3d = self.rebnconv3d(torch.cat((hx4,hx3),1)) 311 | hx2d = self.rebnconv2d(torch.cat((hx3d,hx2),1)) 312 | hx1d = self.rebnconv1d(torch.cat((hx2d,hx1),1)) 313 | 314 | return hx1d + hxin 315 | 316 | 317 | ##### U^2-Net #### 318 | class U2NET(nn.Module): 319 | 320 | def __init__(self,in_ch=3,out_ch=1): 321 | super(U2NET,self).__init__() 322 | 323 | self.stage1 = RSU7(in_ch,32,64) 324 | self.pool12 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 325 | 326 | self.stage2 = RSU6(64,32,128) 327 | self.pool23 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 328 | 329 | self.stage3 = RSU5(128,64,256) 330 | self.pool34 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 331 | 332 | self.stage4 = RSU4(256,128,512) 333 | self.pool45 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 334 | 335 | self.stage5 = RSU4F(512,256,512) 336 | self.pool56 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 337 | 338 | self.stage6 = RSU4F(512,256,512) 339 | 340 | # decoder 341 | self.stage5d = RSU4F(1024,256,512) 342 | self.stage4d = RSU4(1024,128,256) 343 | self.stage3d = RSU5(512,64,128) 344 | self.stage2d = RSU6(256,32,64) 345 | self.stage1d = RSU7(128,16,64) 346 | 347 | self.side1 = nn.Conv2d(64,out_ch,3,padding=1) 348 | self.side2 = nn.Conv2d(64,out_ch,3,padding=1) 349 | self.side3 = nn.Conv2d(128,out_ch,3,padding=1) 350 | self.side4 = nn.Conv2d(256,out_ch,3,padding=1) 351 | self.side5 = nn.Conv2d(512,out_ch,3,padding=1) 352 | self.side6 = nn.Conv2d(512,out_ch,3,padding=1) 353 | 354 | self.outconv = nn.Conv2d(6*out_ch,out_ch,1) 355 | 356 | def forward(self,x): 357 | 358 | hx = x 359 | 360 | #stage 1 361 | hx1 = self.stage1(hx) 362 | hx = self.pool12(hx1) 363 | 364 | #stage 2 365 | hx2 = self.stage2(hx) 366 | hx = self.pool23(hx2) 367 | 368 | #stage 3 369 | hx3 = self.stage3(hx) 370 | hx = self.pool34(hx3) 371 | 372 | #stage 4 373 | hx4 = self.stage4(hx) 374 | hx = self.pool45(hx4) 375 | 376 | #stage 5 377 | hx5 = self.stage5(hx) 378 | hx = self.pool56(hx5) 379 | 380 | #stage 6 381 | hx6 = self.stage6(hx) 382 | hx6up = _upsample_like(hx6,hx5) 383 | 384 | #-------------------- decoder -------------------- 385 | hx5d = self.stage5d(torch.cat((hx6up,hx5),1)) 386 | hx5dup = _upsample_like(hx5d,hx4) 387 | 388 | hx4d = self.stage4d(torch.cat((hx5dup,hx4),1)) 389 | hx4dup = _upsample_like(hx4d,hx3) 390 | 391 | hx3d = self.stage3d(torch.cat((hx4dup,hx3),1)) 392 | hx3dup = _upsample_like(hx3d,hx2) 393 | 394 | hx2d = self.stage2d(torch.cat((hx3dup,hx2),1)) 395 | hx2dup = _upsample_like(hx2d,hx1) 396 | 397 | hx1d = self.stage1d(torch.cat((hx2dup,hx1),1)) 398 | 399 | 400 | #side output 401 | d1 = self.side1(hx1d) 402 | 403 | d2 = self.side2(hx2d) 404 | d2 = _upsample_like(d2,d1) 405 | 406 | d3 = self.side3(hx3d) 407 | d3 = _upsample_like(d3,d1) 408 | 409 | d4 = self.side4(hx4d) 410 | d4 = _upsample_like(d4,d1) 411 | 412 | d5 = self.side5(hx5d) 413 | d5 = _upsample_like(d5,d1) 414 | 415 | d6 = self.side6(hx6) 416 | d6 = _upsample_like(d6,d1) 417 | 418 | d0 = self.outconv(torch.cat((d1,d2,d3,d4,d5,d6),1)) 419 | 420 | return F.sigmoid(d0), F.sigmoid(d1), F.sigmoid(d2), F.sigmoid(d3), F.sigmoid(d4), F.sigmoid(d5), F.sigmoid(d6) 421 | 422 | ### U^2-Net small ### 423 | class U2NETP(nn.Module): 424 | 425 | def __init__(self,in_ch=3,out_ch=1): 426 | super(U2NETP,self).__init__() 427 | 428 | self.stage1 = RSU7(in_ch,16,64) 429 | self.pool12 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 430 | 431 | self.stage2 = RSU6(64,16,64) 432 | self.pool23 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 433 | 434 | self.stage3 = RSU5(64,16,64) 435 | self.pool34 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 436 | 437 | self.stage4 = RSU4(64,16,64) 438 | self.pool45 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 439 | 440 | self.stage5 = RSU4F(64,16,64) 441 | self.pool56 = nn.MaxPool2d(2,stride=2,ceil_mode=True) 442 | 443 | self.stage6 = RSU4F(64,16,64) 444 | 445 | # decoder 446 | self.stage5d = RSU4F(128,16,64) 447 | self.stage4d = RSU4(128,16,64) 448 | self.stage3d = RSU5(128,16,64) 449 | self.stage2d = RSU6(128,16,64) 450 | self.stage1d = RSU7(128,16,64) 451 | 452 | self.side1 = nn.Conv2d(64,out_ch,3,padding=1) 453 | self.side2 = nn.Conv2d(64,out_ch,3,padding=1) 454 | self.side3 = nn.Conv2d(64,out_ch,3,padding=1) 455 | self.side4 = nn.Conv2d(64,out_ch,3,padding=1) 456 | self.side5 = nn.Conv2d(64,out_ch,3,padding=1) 457 | self.side6 = nn.Conv2d(64,out_ch,3,padding=1) 458 | 459 | self.outconv = nn.Conv2d(6*out_ch,out_ch,1) 460 | 461 | def forward(self,x): 462 | 463 | hx = x 464 | 465 | #stage 1 466 | hx1 = self.stage1(hx) 467 | hx = self.pool12(hx1) 468 | 469 | #stage 2 470 | hx2 = self.stage2(hx) 471 | hx = self.pool23(hx2) 472 | 473 | #stage 3 474 | hx3 = self.stage3(hx) 475 | hx = self.pool34(hx3) 476 | 477 | #stage 4 478 | hx4 = self.stage4(hx) 479 | hx = self.pool45(hx4) 480 | 481 | #stage 5 482 | hx5 = self.stage5(hx) 483 | hx = self.pool56(hx5) 484 | 485 | #stage 6 486 | hx6 = self.stage6(hx) 487 | hx6up = _upsample_like(hx6,hx5) 488 | 489 | #decoder 490 | hx5d = self.stage5d(torch.cat((hx6up,hx5),1)) 491 | hx5dup = _upsample_like(hx5d,hx4) 492 | 493 | hx4d = self.stage4d(torch.cat((hx5dup,hx4),1)) 494 | hx4dup = _upsample_like(hx4d,hx3) 495 | 496 | hx3d = self.stage3d(torch.cat((hx4dup,hx3),1)) 497 | hx3dup = _upsample_like(hx3d,hx2) 498 | 499 | hx2d = self.stage2d(torch.cat((hx3dup,hx2),1)) 500 | hx2dup = _upsample_like(hx2d,hx1) 501 | 502 | hx1d = self.stage1d(torch.cat((hx2dup,hx1),1)) 503 | 504 | 505 | #side output 506 | d1 = self.side1(hx1d) 507 | 508 | d2 = self.side2(hx2d) 509 | d2 = _upsample_like(d2,d1) 510 | 511 | d3 = self.side3(hx3d) 512 | d3 = _upsample_like(d3,d1) 513 | 514 | d4 = self.side4(hx4d) 515 | d4 = _upsample_like(d4,d1) 516 | 517 | d5 = self.side5(hx5d) 518 | d5 = _upsample_like(d5,d1) 519 | 520 | d6 = self.side6(hx6) 521 | d6 = _upsample_like(d6,d1) 522 | 523 | d0 = self.outconv(torch.cat((d1,d2,d3,d4,d5,d6),1)) 524 | 525 | return F.sigmoid(d0), F.sigmoid(d1), F.sigmoid(d2), F.sigmoid(d3), F.sigmoid(d4), F.sigmoid(d5), F.sigmoid(d6) 526 | -------------------------------------------------------------------------------- /model/u2net_refactor.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | 4 | import math 5 | 6 | __all__ = ['U2NET_full', 'U2NET_lite'] 7 | 8 | 9 | def _upsample_like(x, size): 10 | return nn.Upsample(size=size, mode='bilinear', align_corners=False)(x) 11 | 12 | 13 | def _size_map(x, height): 14 | # {height: size} for Upsample 15 | size = list(x.shape[-2:]) 16 | sizes = {} 17 | for h in range(1, height): 18 | sizes[h] = size 19 | size = [math.ceil(w / 2) for w in size] 20 | return sizes 21 | 22 | 23 | class REBNCONV(nn.Module): 24 | def __init__(self, in_ch=3, out_ch=3, dilate=1): 25 | super(REBNCONV, self).__init__() 26 | 27 | self.conv_s1 = nn.Conv2d(in_ch, out_ch, 3, padding=1 * dilate, dilation=1 * dilate) 28 | self.bn_s1 = nn.BatchNorm2d(out_ch) 29 | self.relu_s1 = nn.ReLU(inplace=True) 30 | 31 | def forward(self, x): 32 | return self.relu_s1(self.bn_s1(self.conv_s1(x))) 33 | 34 | 35 | class RSU(nn.Module): 36 | def __init__(self, name, height, in_ch, mid_ch, out_ch, dilated=False): 37 | super(RSU, self).__init__() 38 | self.name = name 39 | self.height = height 40 | self.dilated = dilated 41 | self._make_layers(height, in_ch, mid_ch, out_ch, dilated) 42 | 43 | def forward(self, x): 44 | sizes = _size_map(x, self.height) 45 | x = self.rebnconvin(x) 46 | 47 | # U-Net like symmetric encoder-decoder structure 48 | def unet(x, height=1): 49 | if height < self.height: 50 | x1 = getattr(self, f'rebnconv{height}')(x) 51 | if not self.dilated and height < self.height - 1: 52 | x2 = unet(getattr(self, 'downsample')(x1), height + 1) 53 | else: 54 | x2 = unet(x1, height + 1) 55 | 56 | x = getattr(self, f'rebnconv{height}d')(torch.cat((x2, x1), 1)) 57 | return _upsample_like(x, sizes[height - 1]) if not self.dilated and height > 1 else x 58 | else: 59 | return getattr(self, f'rebnconv{height}')(x) 60 | 61 | return x + unet(x) 62 | 63 | def _make_layers(self, height, in_ch, mid_ch, out_ch, dilated=False): 64 | self.add_module('rebnconvin', REBNCONV(in_ch, out_ch)) 65 | self.add_module('downsample', nn.MaxPool2d(2, stride=2, ceil_mode=True)) 66 | 67 | self.add_module(f'rebnconv1', REBNCONV(out_ch, mid_ch)) 68 | self.add_module(f'rebnconv1d', REBNCONV(mid_ch * 2, out_ch)) 69 | 70 | for i in range(2, height): 71 | dilate = 1 if not dilated else 2 ** (i - 1) 72 | self.add_module(f'rebnconv{i}', REBNCONV(mid_ch, mid_ch, dilate=dilate)) 73 | self.add_module(f'rebnconv{i}d', REBNCONV(mid_ch * 2, mid_ch, dilate=dilate)) 74 | 75 | dilate = 2 if not dilated else 2 ** (height - 1) 76 | self.add_module(f'rebnconv{height}', REBNCONV(mid_ch, mid_ch, dilate=dilate)) 77 | 78 | 79 | class U2NET(nn.Module): 80 | def __init__(self, cfgs, out_ch): 81 | super(U2NET, self).__init__() 82 | self.out_ch = out_ch 83 | self._make_layers(cfgs) 84 | 85 | def forward(self, x): 86 | sizes = _size_map(x, self.height) 87 | maps = [] # storage for maps 88 | 89 | # side saliency map 90 | def unet(x, height=1): 91 | if height < 6: 92 | x1 = getattr(self, f'stage{height}')(x) 93 | x2 = unet(getattr(self, 'downsample')(x1), height + 1) 94 | x = getattr(self, f'stage{height}d')(torch.cat((x2, x1), 1)) 95 | side(x, height) 96 | return _upsample_like(x, sizes[height - 1]) if height > 1 else x 97 | else: 98 | x = getattr(self, f'stage{height}')(x) 99 | side(x, height) 100 | return _upsample_like(x, sizes[height - 1]) 101 | 102 | def side(x, h): 103 | # side output saliency map (before sigmoid) 104 | x = getattr(self, f'side{h}')(x) 105 | x = _upsample_like(x, sizes[1]) 106 | maps.append(x) 107 | 108 | def fuse(): 109 | # fuse saliency probability maps 110 | maps.reverse() 111 | x = torch.cat(maps, 1) 112 | x = getattr(self, 'outconv')(x) 113 | maps.insert(0, x) 114 | return [torch.sigmoid(x) for x in maps] 115 | 116 | unet(x) 117 | maps = fuse() 118 | return maps 119 | 120 | def _make_layers(self, cfgs): 121 | self.height = int((len(cfgs) + 1) / 2) 122 | self.add_module('downsample', nn.MaxPool2d(2, stride=2, ceil_mode=True)) 123 | for k, v in cfgs.items(): 124 | # build rsu block 125 | self.add_module(k, RSU(v[0], *v[1])) 126 | if v[2] > 0: 127 | # build side layer 128 | self.add_module(f'side{v[0][-1]}', nn.Conv2d(v[2], self.out_ch, 3, padding=1)) 129 | # build fuse layer 130 | self.add_module('outconv', nn.Conv2d(int(self.height * self.out_ch), self.out_ch, 1)) 131 | 132 | 133 | def U2NET_full(): 134 | full = { 135 | # cfgs for building RSUs and sides 136 | # {stage : [name, (height(L), in_ch, mid_ch, out_ch, dilated), side]} 137 | 'stage1': ['En_1', (7, 3, 32, 64), -1], 138 | 'stage2': ['En_2', (6, 64, 32, 128), -1], 139 | 'stage3': ['En_3', (5, 128, 64, 256), -1], 140 | 'stage4': ['En_4', (4, 256, 128, 512), -1], 141 | 'stage5': ['En_5', (4, 512, 256, 512, True), -1], 142 | 'stage6': ['En_6', (4, 512, 256, 512, True), 512], 143 | 'stage5d': ['De_5', (4, 1024, 256, 512, True), 512], 144 | 'stage4d': ['De_4', (4, 1024, 128, 256), 256], 145 | 'stage3d': ['De_3', (5, 512, 64, 128), 128], 146 | 'stage2d': ['De_2', (6, 256, 32, 64), 64], 147 | 'stage1d': ['De_1', (7, 128, 16, 64), 64], 148 | } 149 | return U2NET(cfgs=full, out_ch=1) 150 | 151 | 152 | def U2NET_lite(): 153 | lite = { 154 | # cfgs for building RSUs and sides 155 | # {stage : [name, (height(L), in_ch, mid_ch, out_ch, dilated), side]} 156 | 'stage1': ['En_1', (7, 3, 16, 64), -1], 157 | 'stage2': ['En_2', (6, 64, 16, 64), -1], 158 | 'stage3': ['En_3', (5, 64, 16, 64), -1], 159 | 'stage4': ['En_4', (4, 64, 16, 64), -1], 160 | 'stage5': ['En_5', (4, 64, 16, 64, True), -1], 161 | 'stage6': ['En_6', (4, 64, 16, 64, True), 64], 162 | 'stage5d': ['De_5', (4, 128, 16, 64, True), 64], 163 | 'stage4d': ['De_4', (4, 128, 16, 64), 64], 164 | 'stage3d': ['De_3', (5, 128, 16, 64), 64], 165 | 'stage2d': ['De_2', (6, 128, 16, 64), 64], 166 | 'stage1d': ['De_1', (7, 128, 16, 64), 64], 167 | } 168 | return U2NET(cfgs=lite, out_ch=1) 169 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy==1.15.2 2 | scikit-image==0.14.0 3 | torch 4 | torchvision 5 | pillow==8.1.1 6 | opencv-python 7 | paddlepaddle 8 | paddlehub 9 | gradio 10 | -------------------------------------------------------------------------------- /setup_model_weights.py: -------------------------------------------------------------------------------- 1 | import os 2 | import gdown 3 | 4 | os.makedirs('./saved_models/u2net', exist_ok=True) 5 | os.makedirs('./saved_models/u2net_portrait', exist_ok=True) 6 | 7 | gdown.download('https://drive.google.com/uc?id=1ao1ovG1Qtx4b7EoskHXmi2E9rp5CHLcZ', 8 | './saved_models/u2net/u2net.pth', 9 | quiet=False) 10 | 11 | gdown.download('https://drive.google.com/uc?id=1IG3HdpcRiDoWNookbncQjeaPN28t90yW', 12 | './saved_models/u2net_portrait/u2net_portrait.pth', 13 | quiet=False) 14 | -------------------------------------------------------------------------------- /test_data/test_human_images/19035828_web1__12294096_web1_180615-PNR-newmayorchallenge.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/19035828_web1__12294096_web1_180615-PNR-newmayorchallenge.jpg -------------------------------------------------------------------------------- /test_data/test_human_images/2019-LADIES-NIGHT-2ND-GOMES.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/2019-LADIES-NIGHT-2ND-GOMES.jpg -------------------------------------------------------------------------------- /test_data/test_human_images/480112-637286191695715805-16x9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/480112-637286191695715805-16x9.jpg -------------------------------------------------------------------------------- /test_data/test_human_images/5-mental-skills-of-successful-athletes-image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/5-mental-skills-of-successful-athletes-image.jpg -------------------------------------------------------------------------------- /test_data/test_human_images/Athlete-Intake.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/Athlete-Intake.jpg -------------------------------------------------------------------------------- /test_data/test_human_images/Two_dancers.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/Two_dancers.jpg -------------------------------------------------------------------------------- /test_data/test_human_images/coach-yelling-at-athlete-716268.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/coach-yelling-at-athlete-716268.jpg -------------------------------------------------------------------------------- /test_data/test_human_images/download.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/download.jpeg -------------------------------------------------------------------------------- /test_data/test_human_images/f2dc6965e31f7ff0bb54618d53437006.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/f2dc6965e31f7ff0bb54618d53437006.jpg -------------------------------------------------------------------------------- /test_data/test_human_images/image1440x560cropped.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/image1440x560cropped.jpg -------------------------------------------------------------------------------- /test_data/test_human_images/images (1).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/images (1).jpeg -------------------------------------------------------------------------------- /test_data/test_human_images/images (2).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/images (2).jpeg -------------------------------------------------------------------------------- /test_data/test_human_images/images.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/images.jpeg -------------------------------------------------------------------------------- /test_data/test_human_images/julia+trotti_17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/julia+trotti_17.jpg -------------------------------------------------------------------------------- /test_data/test_human_images/language_1280p.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/language_1280p.jpg -------------------------------------------------------------------------------- /test_data/test_human_images/lionel-messi-athletes-fashion.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/lionel-messi-athletes-fashion.jpg -------------------------------------------------------------------------------- /test_data/test_human_images/olympic-athletes-need-to-know-2018-winter-1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/olympic-athletes-need-to-know-2018-winter-1.jpg -------------------------------------------------------------------------------- /test_data/test_human_images/photo-1552374196-c4e7ffc6e126.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/photo-1552374196-c4e7ffc6e126.jpeg -------------------------------------------------------------------------------- /test_data/test_human_images/track-runners-hurdles-1280.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_human_images/track-runners-hurdles-1280.jpg -------------------------------------------------------------------------------- /test_data/test_images/0002-01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/0002-01.jpg -------------------------------------------------------------------------------- /test_data/test_images/0003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/0003.jpg -------------------------------------------------------------------------------- /test_data/test_images/bike.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/bike.jpg -------------------------------------------------------------------------------- /test_data/test_images/boat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/boat.jpg -------------------------------------------------------------------------------- /test_data/test_images/girl.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/girl.png -------------------------------------------------------------------------------- /test_data/test_images/hockey.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/hockey.png -------------------------------------------------------------------------------- /test_data/test_images/horse.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/horse.jpg -------------------------------------------------------------------------------- /test_data/test_images/im_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/im_01.png -------------------------------------------------------------------------------- /test_data/test_images/im_14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/im_14.png -------------------------------------------------------------------------------- /test_data/test_images/im_21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/im_21.png -------------------------------------------------------------------------------- /test_data/test_images/im_27.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/im_27.png -------------------------------------------------------------------------------- /test_data/test_images/lamp2_meitu_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/lamp2_meitu_1.jpg -------------------------------------------------------------------------------- /test_data/test_images/long.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/long.jpg -------------------------------------------------------------------------------- /test_data/test_images/rifle1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/rifle1.jpg -------------------------------------------------------------------------------- /test_data/test_images/rifle2.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/rifle2.jpeg -------------------------------------------------------------------------------- /test_data/test_images/sailboat3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/sailboat3.jpg -------------------------------------------------------------------------------- /test_data/test_images/vangogh.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/vangogh.jpeg -------------------------------------------------------------------------------- /test_data/test_images/whisk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_images/whisk.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_im/img_1585.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_im/img_1585.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_im/img_1588.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_im/img_1588.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_im/img_1594.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_im/img_1594.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_im/img_1616.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_im/img_1616.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_im/img_1695.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_im/img_1695.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_im/img_1696.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_im/img_1696.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_im/img_1771.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_im/img_1771.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_im/img_1859.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_im/img_1859.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_results/img_1585.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_results/img_1585.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_results/img_1588.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_results/img_1588.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_results/img_1594.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_results/img_1594.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_results/img_1616.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_results/img_1616.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_results/img_1695.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_results/img_1695.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_results/img_1696.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_results/img_1696.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_results/img_1771.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_results/img_1771.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/portrait_results/img_1859.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/portrait_results/img_1859.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_im/GalGadot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_im/GalGadot.jpg -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_im/guliNazha3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_im/guliNazha3.jpg -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_im/kid1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_im/kid1.jpg -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_im/kid2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_im/kid2.jpg -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_im/kid3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_im/kid3.jpg -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_im/man.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_im/man.jpg -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_im/man2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_im/man2.jpg -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_im/man4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_im/man4.jpg -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_im/man5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_im/man5.jpg -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_im/smile.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_im/smile.jpg -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/GalGadot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/GalGadot.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/GalGadot_sigma_20.0_alpha_0.5_composite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/GalGadot_sigma_20.0_alpha_0.5_composite.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/guliNazha3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/guliNazha3.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/guliNazha3_sigma_20.0_alpha_0.5_composite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/guliNazha3_sigma_20.0_alpha_0.5_composite.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/kid1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/kid1.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/kid1_sigma_20.0_alpha_0.5_composite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/kid1_sigma_20.0_alpha_0.5_composite.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/kid2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/kid2.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/kid2_sigma_20.0_alpha_0.5_composite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/kid2_sigma_20.0_alpha_0.5_composite.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/kid3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/kid3.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/kid3_sigma_20.0_alpha_0.5_composite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/kid3_sigma_20.0_alpha_0.5_composite.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/man.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/man.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/man2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/man2.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/man2_sigma_20.0_alpha_0.5_composite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/man2_sigma_20.0_alpha_0.5_composite.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/man4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/man4.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/man4_sigma_20.0_alpha_0.5_composite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/man4_sigma_20.0_alpha_0.5_composite.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/man5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/man5.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/man5_sigma_20.0_alpha_0.5_composite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/man5_sigma_20.0_alpha_0.5_composite.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/man_sigma_20.0_alpha_0.5_composite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/man_sigma_20.0_alpha_0.5_composite.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/smile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/smile.png -------------------------------------------------------------------------------- /test_data/test_portrait_images/your_portrait_results/smile_sigma_20.0_alpha_0.5_composite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/test_portrait_images/your_portrait_results/smile_sigma_20.0_alpha_0.5_composite.png -------------------------------------------------------------------------------- /test_data/u2net_results/0002-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/0002-01.png -------------------------------------------------------------------------------- /test_data/u2net_results/0003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/0003.png -------------------------------------------------------------------------------- /test_data/u2net_results/bike.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/bike.png -------------------------------------------------------------------------------- /test_data/u2net_results/boat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/boat.png -------------------------------------------------------------------------------- /test_data/u2net_results/girl.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/girl.png -------------------------------------------------------------------------------- /test_data/u2net_results/hockey.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/hockey.png -------------------------------------------------------------------------------- /test_data/u2net_results/horse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/horse.png -------------------------------------------------------------------------------- /test_data/u2net_results/im_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/im_01.png -------------------------------------------------------------------------------- /test_data/u2net_results/im_14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/im_14.png -------------------------------------------------------------------------------- /test_data/u2net_results/im_21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/im_21.png -------------------------------------------------------------------------------- /test_data/u2net_results/im_27.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/im_27.png -------------------------------------------------------------------------------- /test_data/u2net_results/lamp2_meitu_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/lamp2_meitu_1.png -------------------------------------------------------------------------------- /test_data/u2net_results/long.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/long.png -------------------------------------------------------------------------------- /test_data/u2net_results/rifle1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/rifle1.png -------------------------------------------------------------------------------- /test_data/u2net_results/rifle2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/rifle2.png -------------------------------------------------------------------------------- /test_data/u2net_results/sailboat3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/sailboat3.png -------------------------------------------------------------------------------- /test_data/u2net_results/vangogh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/vangogh.png -------------------------------------------------------------------------------- /test_data/u2net_results/whisk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_results/whisk.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/19035828_web1__12294096_web1_180615-PNR-newmayorchallenge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/19035828_web1__12294096_web1_180615-PNR-newmayorchallenge.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/2019-LADIES-NIGHT-2ND-GOMES.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/2019-LADIES-NIGHT-2ND-GOMES.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/480112-637286191695715805-16x9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/480112-637286191695715805-16x9.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/5-mental-skills-of-successful-athletes-image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/5-mental-skills-of-successful-athletes-image.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/Athlete-Intake.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/Athlete-Intake.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/Two_dancers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/Two_dancers.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/coach-yelling-at-athlete-716268.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/coach-yelling-at-athlete-716268.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/download.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/download.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/f2dc6965e31f7ff0bb54618d53437006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/f2dc6965e31f7ff0bb54618d53437006.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/image1440x560cropped.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/image1440x560cropped.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/images (1).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/images (1).png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/images (2).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/images (2).png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/images.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/julia+trotti_17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/julia+trotti_17.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/language_1280p.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/language_1280p.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/lionel-messi-athletes-fashion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/lionel-messi-athletes-fashion.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/olympic-athletes-need-to-know-2018-winter-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/olympic-athletes-need-to-know-2018-winter-1.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/photo-1552374196-c4e7ffc6e126.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/photo-1552374196-c4e7ffc6e126.png -------------------------------------------------------------------------------- /test_data/u2net_test_human_images_results/track-runners-hurdles-1280.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2net_test_human_images_results/track-runners-hurdles-1280.png -------------------------------------------------------------------------------- /test_data/u2netp_results/0002-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/0002-01.png -------------------------------------------------------------------------------- /test_data/u2netp_results/0003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/0003.png -------------------------------------------------------------------------------- /test_data/u2netp_results/bike.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/bike.png -------------------------------------------------------------------------------- /test_data/u2netp_results/boat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/boat.png -------------------------------------------------------------------------------- /test_data/u2netp_results/girl.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/girl.png -------------------------------------------------------------------------------- /test_data/u2netp_results/hockey.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/hockey.png -------------------------------------------------------------------------------- /test_data/u2netp_results/horse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/horse.png -------------------------------------------------------------------------------- /test_data/u2netp_results/im_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/im_01.png -------------------------------------------------------------------------------- /test_data/u2netp_results/im_14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/im_14.png -------------------------------------------------------------------------------- /test_data/u2netp_results/im_21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/im_21.png -------------------------------------------------------------------------------- /test_data/u2netp_results/im_27.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/im_27.png -------------------------------------------------------------------------------- /test_data/u2netp_results/lamp2_meitu_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/lamp2_meitu_1.png -------------------------------------------------------------------------------- /test_data/u2netp_results/long.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/long.png -------------------------------------------------------------------------------- /test_data/u2netp_results/rifle1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/rifle1.png -------------------------------------------------------------------------------- /test_data/u2netp_results/rifle2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/rifle2.png -------------------------------------------------------------------------------- /test_data/u2netp_results/sailboat3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/sailboat3.png -------------------------------------------------------------------------------- /test_data/u2netp_results/vangogh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/vangogh.png -------------------------------------------------------------------------------- /test_data/u2netp_results/whisk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuebinqin/U-2-Net/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/test_data/u2netp_results/whisk.png -------------------------------------------------------------------------------- /u2net_human_seg_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | from skimage import io, transform 3 | import torch 4 | import torchvision 5 | from torch.autograd import Variable 6 | import torch.nn as nn 7 | import torch.nn.functional as F 8 | from torch.utils.data import Dataset, DataLoader 9 | from torchvision import transforms#, utils 10 | # import torch.optim as optim 11 | 12 | import numpy as np 13 | from PIL import Image 14 | import glob 15 | 16 | from data_loader import RescaleT 17 | from data_loader import ToTensor 18 | from data_loader import ToTensorLab 19 | from data_loader import SalObjDataset 20 | 21 | from model import U2NET # full size version 173.6 MB 22 | 23 | # normalize the predicted SOD probability map 24 | def normPRED(d): 25 | ma = torch.max(d) 26 | mi = torch.min(d) 27 | 28 | dn = (d-mi)/(ma-mi) 29 | 30 | return dn 31 | 32 | def save_output(image_name,pred,d_dir): 33 | 34 | predict = pred 35 | predict = predict.squeeze() 36 | predict_np = predict.cpu().data.numpy() 37 | 38 | im = Image.fromarray(predict_np*255).convert('RGB') 39 | img_name = image_name.split(os.sep)[-1] 40 | image = io.imread(image_name) 41 | imo = im.resize((image.shape[1],image.shape[0]),resample=Image.BILINEAR) 42 | 43 | pb_np = np.array(imo) 44 | 45 | aaa = img_name.split(".") 46 | bbb = aaa[0:-1] 47 | imidx = bbb[0] 48 | for i in range(1,len(bbb)): 49 | imidx = imidx + "." + bbb[i] 50 | 51 | imo.save(d_dir+imidx+'.png') 52 | 53 | def main(): 54 | 55 | # --------- 1. get image path and name --------- 56 | model_name='u2net' 57 | 58 | 59 | image_dir = os.path.join(os.getcwd(), 'test_data', 'test_human_images') 60 | prediction_dir = os.path.join(os.getcwd(), 'test_data', 'test_human_images' + '_results' + os.sep) 61 | model_dir = os.path.join(os.getcwd(), 'saved_models', model_name+'_human_seg', model_name + '_human_seg.pth') 62 | 63 | img_name_list = glob.glob(image_dir + os.sep + '*') 64 | print(img_name_list) 65 | 66 | # --------- 2. dataloader --------- 67 | #1. dataloader 68 | test_salobj_dataset = SalObjDataset(img_name_list = img_name_list, 69 | lbl_name_list = [], 70 | transform=transforms.Compose([RescaleT(320), 71 | ToTensorLab(flag=0)]) 72 | ) 73 | test_salobj_dataloader = DataLoader(test_salobj_dataset, 74 | batch_size=1, 75 | shuffle=False, 76 | num_workers=1) 77 | 78 | # --------- 3. model define --------- 79 | if(model_name=='u2net'): 80 | print("...load U2NET---173.6 MB") 81 | net = U2NET(3,1) 82 | 83 | if torch.cuda.is_available(): 84 | net.load_state_dict(torch.load(model_dir)) 85 | net.cuda() 86 | else: 87 | net.load_state_dict(torch.load(model_dir, map_location='cpu')) 88 | net.eval() 89 | 90 | # --------- 4. inference for each image --------- 91 | for i_test, data_test in enumerate(test_salobj_dataloader): 92 | 93 | print("inferencing:",img_name_list[i_test].split(os.sep)[-1]) 94 | 95 | inputs_test = data_test['image'] 96 | inputs_test = inputs_test.type(torch.FloatTensor) 97 | 98 | if torch.cuda.is_available(): 99 | inputs_test = Variable(inputs_test.cuda()) 100 | else: 101 | inputs_test = Variable(inputs_test) 102 | 103 | d1,d2,d3,d4,d5,d6,d7= net(inputs_test) 104 | 105 | # normalization 106 | pred = d1[:,0,:,:] 107 | pred = normPRED(pred) 108 | 109 | # save results to test_results folder 110 | if not os.path.exists(prediction_dir): 111 | os.makedirs(prediction_dir, exist_ok=True) 112 | save_output(img_name_list[i_test],pred,prediction_dir) 113 | 114 | del d1,d2,d3,d4,d5,d6,d7 115 | 116 | if __name__ == "__main__": 117 | main() 118 | -------------------------------------------------------------------------------- /u2net_portrait_composite.py: -------------------------------------------------------------------------------- 1 | import os 2 | from skimage import io, transform 3 | from skimage.filters import gaussian 4 | import torch 5 | import torchvision 6 | from torch.autograd import Variable 7 | import torch.nn as nn 8 | import torch.nn.functional as F 9 | from torch.utils.data import Dataset, DataLoader 10 | from torchvision import transforms#, utils 11 | # import torch.optim as optim 12 | 13 | import numpy as np 14 | from PIL import Image 15 | import glob 16 | 17 | from data_loader import RescaleT 18 | from data_loader import ToTensor 19 | from data_loader import ToTensorLab 20 | from data_loader import SalObjDataset 21 | 22 | from model import U2NET # full size version 173.6 MB 23 | from model import U2NETP # small version u2net 4.7 MB 24 | 25 | import argparse 26 | 27 | # normalize the predicted SOD probability map 28 | def normPRED(d): 29 | ma = torch.max(d) 30 | mi = torch.min(d) 31 | 32 | dn = (d-mi)/(ma-mi) 33 | 34 | return dn 35 | 36 | def save_output(image_name,pred,d_dir,sigma=2,alpha=0.5): 37 | 38 | predict = pred 39 | predict = predict.squeeze() 40 | predict_np = predict.cpu().data.numpy() 41 | 42 | image = io.imread(image_name) 43 | pd = transform.resize(predict_np,image.shape[0:2],order=2) 44 | pd = pd/(np.amax(pd)+1e-8)*255 45 | pd = pd[:,:,np.newaxis] 46 | 47 | print(image.shape) 48 | print(pd.shape) 49 | 50 | ## fuse the orignal portrait image and the portraits into one composite image 51 | ## 1. use gaussian filter to blur the orginal image 52 | sigma=sigma 53 | image = gaussian(image, sigma=sigma, preserve_range=True) 54 | 55 | ## 2. fuse these orignal image and the portrait with certain weight: alpha 56 | alpha = alpha 57 | im_comp = image*alpha+pd*(1-alpha) 58 | 59 | print(im_comp.shape) 60 | 61 | 62 | img_name = image_name.split(os.sep)[-1] 63 | aaa = img_name.split(".") 64 | bbb = aaa[0:-1] 65 | imidx = bbb[0] 66 | for i in range(1,len(bbb)): 67 | imidx = imidx + "." + bbb[i] 68 | io.imsave(d_dir+'/'+imidx+'_sigma_' + str(sigma) + '_alpha_' + str(alpha) + '_composite.png',im_comp) 69 | 70 | def main(): 71 | 72 | parser = argparse.ArgumentParser(description="image and portrait composite") 73 | parser.add_argument('-s',action='store',dest='sigma') 74 | parser.add_argument('-a',action='store',dest='alpha') 75 | args = parser.parse_args() 76 | print(args.sigma) 77 | print(args.alpha) 78 | print("--------------------") 79 | 80 | # --------- 1. get image path and name --------- 81 | model_name='u2net_portrait'#u2netp 82 | 83 | 84 | image_dir = './test_data/test_portrait_images/your_portrait_im' 85 | prediction_dir = './test_data/test_portrait_images/your_portrait_results' 86 | if(not os.path.exists(prediction_dir)): 87 | os.mkdir(prediction_dir) 88 | 89 | model_dir = './saved_models/u2net_portrait/u2net_portrait.pth' 90 | 91 | img_name_list = glob.glob(image_dir+'/*') 92 | print("Number of images: ", len(img_name_list)) 93 | 94 | # --------- 2. dataloader --------- 95 | #1. dataloader 96 | test_salobj_dataset = SalObjDataset(img_name_list = img_name_list, 97 | lbl_name_list = [], 98 | transform=transforms.Compose([RescaleT(512), 99 | ToTensorLab(flag=0)]) 100 | ) 101 | test_salobj_dataloader = DataLoader(test_salobj_dataset, 102 | batch_size=1, 103 | shuffle=False, 104 | num_workers=1) 105 | 106 | # --------- 3. model define --------- 107 | 108 | print("...load U2NET---173.6 MB") 109 | net = U2NET(3,1) 110 | 111 | net.load_state_dict(torch.load(model_dir)) 112 | if torch.cuda.is_available(): 113 | net.cuda() 114 | net.eval() 115 | 116 | # --------- 4. inference for each image --------- 117 | for i_test, data_test in enumerate(test_salobj_dataloader): 118 | 119 | print("inferencing:",img_name_list[i_test].split(os.sep)[-1]) 120 | 121 | inputs_test = data_test['image'] 122 | inputs_test = inputs_test.type(torch.FloatTensor) 123 | 124 | if torch.cuda.is_available(): 125 | inputs_test = Variable(inputs_test.cuda()) 126 | else: 127 | inputs_test = Variable(inputs_test) 128 | 129 | d1,d2,d3,d4,d5,d6,d7= net(inputs_test) 130 | 131 | # normalization 132 | pred = 1.0 - d1[:,0,:,:] 133 | pred = normPRED(pred) 134 | 135 | # save results to test_results folder 136 | save_output(img_name_list[i_test],pred,prediction_dir,sigma=float(args.sigma),alpha=float(args.alpha)) 137 | 138 | del d1,d2,d3,d4,d5,d6,d7 139 | 140 | if __name__ == "__main__": 141 | main() 142 | -------------------------------------------------------------------------------- /u2net_portrait_demo.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import torch 3 | from model import U2NET 4 | from torch.autograd import Variable 5 | import numpy as np 6 | from glob import glob 7 | import os 8 | 9 | def detect_single_face(face_cascade,img): 10 | # Convert into grayscale 11 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | 13 | # Detect faces 14 | faces = face_cascade.detectMultiScale(gray, 1.1, 4) 15 | if(len(faces)==0): 16 | print("Warming: no face detection, the portrait u2net will run on the whole image!") 17 | return None 18 | 19 | # filter to keep the largest face 20 | wh = 0 21 | idx = 0 22 | for i in range(0,len(faces)): 23 | (x,y,w,h) = faces[i] 24 | if(whwidth): 55 | r = right-width 56 | right = width 57 | 58 | tpad = int(float(h)*0.6) 59 | top = y - tpad 60 | if(top<0): 61 | t = tpad-y 62 | top = 0 63 | 64 | bpad = int(float(h)*0.2) 65 | bottom = y+h+bpad 66 | if(bottom>height): 67 | b = bottom-height 68 | bottom = height 69 | 70 | 71 | im_face = img[top:bottom,left:right] 72 | if(len(im_face.shape)==2): 73 | im_face = np.repeat(im_face[:,:,np.newaxis],(1,1,3)) 74 | 75 | im_face = np.pad(im_face,((t,b),(l,r),(0,0)),mode='constant',constant_values=((255,255),(255,255),(255,255))) 76 | 77 | # pad to achieve image with square shape for avoding face deformation after resizing 78 | hf,wf = im_face.shape[0:2] 79 | if(hf-2>wf): 80 | wfp = int((hf-wf)/2) 81 | im_face = np.pad(im_face,((0,0),(wfp,wfp),(0,0)),mode='constant',constant_values=((255,255),(255,255),(255,255))) 82 | elif(wf-2>hf): 83 | hfp = int((wf-hf)/2) 84 | im_face = np.pad(im_face,((hfp,hfp),(0,0),(0,0)),mode='constant',constant_values=((255,255),(255,255),(255,255))) 85 | 86 | # resize to have 512x512 resolution 87 | im_face = cv2.resize(im_face, (512,512), interpolation = cv2.INTER_AREA) 88 | 89 | return im_face 90 | 91 | def normPRED(d): 92 | ma = torch.max(d) 93 | mi = torch.min(d) 94 | 95 | dn = (d-mi)/(ma-mi) 96 | 97 | return dn 98 | 99 | def inference(net,input): 100 | 101 | # normalize the input 102 | tmpImg = np.zeros((input.shape[0],input.shape[1],3)) 103 | input = input/np.max(input) 104 | 105 | tmpImg[:,:,0] = (input[:,:,2]-0.406)/0.225 106 | tmpImg[:,:,1] = (input[:,:,1]-0.456)/0.224 107 | tmpImg[:,:,2] = (input[:,:,0]-0.485)/0.229 108 | 109 | # convert BGR to RGB 110 | tmpImg = tmpImg.transpose((2, 0, 1)) 111 | tmpImg = tmpImg[np.newaxis,:,:,:] 112 | tmpImg = torch.from_numpy(tmpImg) 113 | 114 | # convert numpy array to torch tensor 115 | tmpImg = tmpImg.type(torch.FloatTensor) 116 | 117 | if torch.cuda.is_available(): 118 | tmpImg = Variable(tmpImg.cuda()) 119 | else: 120 | tmpImg = Variable(tmpImg) 121 | 122 | # inference 123 | d1,d2,d3,d4,d5,d6,d7= net(tmpImg) 124 | 125 | # normalization 126 | pred = 1.0 - d1[:,0,:,:] 127 | pred = normPRED(pred) 128 | 129 | # convert torch tensor to numpy array 130 | pred = pred.squeeze() 131 | pred = pred.cpu().data.numpy() 132 | 133 | del d1,d2,d3,d4,d5,d6,d7 134 | 135 | return pred 136 | 137 | def main(): 138 | 139 | # get the image path list for inference 140 | im_list = glob('./test_data/test_portrait_images/your_portrait_im/*') 141 | print("Number of images: ",len(im_list)) 142 | # indicate the output directory 143 | out_dir = './test_data/test_portrait_images/your_portrait_results' 144 | if(not os.path.exists(out_dir)): 145 | os.mkdir(out_dir) 146 | 147 | # Load the cascade face detection model 148 | face_cascade = cv2.CascadeClassifier('./saved_models/face_detection_cv2/haarcascade_frontalface_default.xml') 149 | # u2net_portrait path 150 | model_dir = './saved_models/u2net_portrait/u2net_portrait.pth' 151 | 152 | # load u2net_portrait model 153 | net = U2NET(3,1) 154 | net.load_state_dict(torch.load(model_dir)) 155 | if torch.cuda.is_available(): 156 | net.cuda() 157 | net.eval() 158 | 159 | # do the inference one-by-one 160 | for i in range(0,len(im_list)): 161 | print("--------------------------") 162 | print("inferencing ", i, "/", len(im_list), im_list[i]) 163 | 164 | # load each image 165 | img = cv2.imread(im_list[i]) 166 | height,width = img.shape[0:2] 167 | face = detect_single_face(face_cascade,img) 168 | im_face = crop_face(img, face) 169 | im_portrait = inference(net,im_face) 170 | 171 | # save the output 172 | cv2.imwrite(out_dir+"/"+im_list[i].split('/')[-1][0:-4]+'.png',(im_portrait*255).astype(np.uint8)) 173 | 174 | if __name__ == '__main__': 175 | main() 176 | -------------------------------------------------------------------------------- /u2net_portrait_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | from skimage import io, transform 3 | import torch 4 | import torchvision 5 | from torch.autograd import Variable 6 | import torch.nn as nn 7 | import torch.nn.functional as F 8 | from torch.utils.data import Dataset, DataLoader 9 | from torchvision import transforms#, utils 10 | # import torch.optim as optim 11 | 12 | import numpy as np 13 | from PIL import Image 14 | import glob 15 | 16 | from data_loader import RescaleT 17 | from data_loader import ToTensor 18 | from data_loader import ToTensorLab 19 | from data_loader import SalObjDataset 20 | 21 | from model import U2NET # full size version 173.6 MB 22 | from model import U2NETP # small version u2net 4.7 MB 23 | 24 | # normalize the predicted SOD probability map 25 | def normPRED(d): 26 | ma = torch.max(d) 27 | mi = torch.min(d) 28 | 29 | dn = (d-mi)/(ma-mi) 30 | 31 | return dn 32 | 33 | def save_output(image_name,pred,d_dir): 34 | 35 | predict = pred 36 | predict = predict.squeeze() 37 | predict_np = predict.cpu().data.numpy() 38 | 39 | im = Image.fromarray(predict_np*255).convert('RGB') 40 | img_name = image_name.split(os.sep)[-1] 41 | image = io.imread(image_name) 42 | imo = im.resize((image.shape[1],image.shape[0]),resample=Image.BILINEAR) 43 | 44 | pb_np = np.array(imo) 45 | 46 | aaa = img_name.split(".") 47 | bbb = aaa[0:-1] 48 | imidx = bbb[0] 49 | for i in range(1,len(bbb)): 50 | imidx = imidx + "." + bbb[i] 51 | 52 | imo.save(d_dir+'/'+imidx+'.png') 53 | 54 | def main(): 55 | 56 | # --------- 1. get image path and name --------- 57 | model_name='u2net_portrait'#u2netp 58 | 59 | 60 | image_dir = './test_data/test_portrait_images/portrait_im' 61 | prediction_dir = './test_data/test_portrait_images/portrait_results' 62 | if(not os.path.exists(prediction_dir)): 63 | os.mkdir(prediction_dir) 64 | 65 | model_dir = './saved_models/u2net_portrait/u2net_portrait.pth' 66 | 67 | img_name_list = glob.glob(image_dir+'/*') 68 | print("Number of images: ", len(img_name_list)) 69 | 70 | # --------- 2. dataloader --------- 71 | #1. dataloader 72 | test_salobj_dataset = SalObjDataset(img_name_list = img_name_list, 73 | lbl_name_list = [], 74 | transform=transforms.Compose([RescaleT(512), 75 | ToTensorLab(flag=0)]) 76 | ) 77 | test_salobj_dataloader = DataLoader(test_salobj_dataset, 78 | batch_size=1, 79 | shuffle=False, 80 | num_workers=1) 81 | 82 | # --------- 3. model define --------- 83 | 84 | print("...load U2NET---173.6 MB") 85 | net = U2NET(3,1) 86 | 87 | net.load_state_dict(torch.load(model_dir)) 88 | if torch.cuda.is_available(): 89 | net.cuda() 90 | net.eval() 91 | 92 | # --------- 4. inference for each image --------- 93 | for i_test, data_test in enumerate(test_salobj_dataloader): 94 | 95 | print("inferencing:",img_name_list[i_test].split(os.sep)[-1]) 96 | 97 | inputs_test = data_test['image'] 98 | inputs_test = inputs_test.type(torch.FloatTensor) 99 | 100 | if torch.cuda.is_available(): 101 | inputs_test = Variable(inputs_test.cuda()) 102 | else: 103 | inputs_test = Variable(inputs_test) 104 | 105 | d1,d2,d3,d4,d5,d6,d7= net(inputs_test) 106 | 107 | # normalization 108 | pred = 1.0 - d1[:,0,:,:] 109 | pred = normPRED(pred) 110 | 111 | # save results to test_results folder 112 | save_output(img_name_list[i_test],pred,prediction_dir) 113 | 114 | del d1,d2,d3,d4,d5,d6,d7 115 | 116 | if __name__ == "__main__": 117 | main() 118 | -------------------------------------------------------------------------------- /u2net_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | from skimage import io, transform 3 | import torch 4 | import torchvision 5 | from torch.autograd import Variable 6 | import torch.nn as nn 7 | import torch.nn.functional as F 8 | from torch.utils.data import Dataset, DataLoader 9 | from torchvision import transforms#, utils 10 | # import torch.optim as optim 11 | 12 | import numpy as np 13 | from PIL import Image 14 | import glob 15 | 16 | from data_loader import RescaleT 17 | from data_loader import ToTensor 18 | from data_loader import ToTensorLab 19 | from data_loader import SalObjDataset 20 | 21 | from model import U2NET # full size version 173.6 MB 22 | from model import U2NETP # small version u2net 4.7 MB 23 | 24 | # normalize the predicted SOD probability map 25 | def normPRED(d): 26 | ma = torch.max(d) 27 | mi = torch.min(d) 28 | 29 | dn = (d-mi)/(ma-mi) 30 | 31 | return dn 32 | 33 | def save_output(image_name,pred,d_dir): 34 | 35 | predict = pred 36 | predict = predict.squeeze() 37 | predict_np = predict.cpu().data.numpy() 38 | 39 | im = Image.fromarray(predict_np*255).convert('RGB') 40 | img_name = image_name.split(os.sep)[-1] 41 | image = io.imread(image_name) 42 | imo = im.resize((image.shape[1],image.shape[0]),resample=Image.BILINEAR) 43 | 44 | pb_np = np.array(imo) 45 | 46 | aaa = img_name.split(".") 47 | bbb = aaa[0:-1] 48 | imidx = bbb[0] 49 | for i in range(1,len(bbb)): 50 | imidx = imidx + "." + bbb[i] 51 | 52 | imo.save(d_dir+imidx+'.png') 53 | 54 | def main(): 55 | 56 | # --------- 1. get image path and name --------- 57 | model_name='u2net'#u2netp 58 | 59 | 60 | 61 | image_dir = os.path.join(os.getcwd(), 'test_data', 'test_images') 62 | prediction_dir = os.path.join(os.getcwd(), 'test_data', model_name + '_results' + os.sep) 63 | model_dir = os.path.join(os.getcwd(), 'saved_models', model_name, model_name + '.pth') 64 | 65 | img_name_list = glob.glob(image_dir + os.sep + '*') 66 | print(img_name_list) 67 | 68 | # --------- 2. dataloader --------- 69 | #1. dataloader 70 | test_salobj_dataset = SalObjDataset(img_name_list = img_name_list, 71 | lbl_name_list = [], 72 | transform=transforms.Compose([RescaleT(320), 73 | ToTensorLab(flag=0)]) 74 | ) 75 | test_salobj_dataloader = DataLoader(test_salobj_dataset, 76 | batch_size=1, 77 | shuffle=False, 78 | num_workers=1) 79 | 80 | # --------- 3. model define --------- 81 | if(model_name=='u2net'): 82 | print("...load U2NET---173.6 MB") 83 | net = U2NET(3,1) 84 | elif(model_name=='u2netp'): 85 | print("...load U2NEP---4.7 MB") 86 | net = U2NETP(3,1) 87 | 88 | if torch.cuda.is_available(): 89 | net.load_state_dict(torch.load(model_dir)) 90 | net.cuda() 91 | else: 92 | net.load_state_dict(torch.load(model_dir, map_location='cpu')) 93 | net.eval() 94 | 95 | # --------- 4. inference for each image --------- 96 | for i_test, data_test in enumerate(test_salobj_dataloader): 97 | 98 | print("inferencing:",img_name_list[i_test].split(os.sep)[-1]) 99 | 100 | inputs_test = data_test['image'] 101 | inputs_test = inputs_test.type(torch.FloatTensor) 102 | 103 | if torch.cuda.is_available(): 104 | inputs_test = Variable(inputs_test.cuda()) 105 | else: 106 | inputs_test = Variable(inputs_test) 107 | 108 | d1,d2,d3,d4,d5,d6,d7= net(inputs_test) 109 | 110 | # normalization 111 | pred = d1[:,0,:,:] 112 | pred = normPRED(pred) 113 | 114 | # save results to test_results folder 115 | if not os.path.exists(prediction_dir): 116 | os.makedirs(prediction_dir, exist_ok=True) 117 | save_output(img_name_list[i_test],pred,prediction_dir) 118 | 119 | del d1,d2,d3,d4,d5,d6,d7 120 | 121 | if __name__ == "__main__": 122 | main() 123 | -------------------------------------------------------------------------------- /u2net_train.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | import torchvision 4 | from torch.autograd import Variable 5 | import torch.nn as nn 6 | import torch.nn.functional as F 7 | 8 | from torch.utils.data import Dataset, DataLoader 9 | from torchvision import transforms, utils 10 | import torch.optim as optim 11 | import torchvision.transforms as standard_transforms 12 | 13 | import numpy as np 14 | import glob 15 | import os 16 | 17 | from data_loader import Rescale 18 | from data_loader import RescaleT 19 | from data_loader import RandomCrop 20 | from data_loader import ToTensor 21 | from data_loader import ToTensorLab 22 | from data_loader import SalObjDataset 23 | 24 | from model import U2NET 25 | from model import U2NETP 26 | 27 | # ------- 1. define loss function -------- 28 | 29 | bce_loss = nn.BCELoss(size_average=True) 30 | 31 | def muti_bce_loss_fusion(d0, d1, d2, d3, d4, d5, d6, labels_v): 32 | 33 | loss0 = bce_loss(d0,labels_v) 34 | loss1 = bce_loss(d1,labels_v) 35 | loss2 = bce_loss(d2,labels_v) 36 | loss3 = bce_loss(d3,labels_v) 37 | loss4 = bce_loss(d4,labels_v) 38 | loss5 = bce_loss(d5,labels_v) 39 | loss6 = bce_loss(d6,labels_v) 40 | 41 | loss = loss0 + loss1 + loss2 + loss3 + loss4 + loss5 + loss6 42 | print("l0: %3f, l1: %3f, l2: %3f, l3: %3f, l4: %3f, l5: %3f, l6: %3f\n"%(loss0.data.item(),loss1.data.item(),loss2.data.item(),loss3.data.item(),loss4.data.item(),loss5.data.item(),loss6.data.item())) 43 | 44 | return loss0, loss 45 | 46 | 47 | # ------- 2. set the directory of training dataset -------- 48 | 49 | model_name = 'u2net' #'u2netp' 50 | 51 | data_dir = os.path.join(os.getcwd(), 'train_data' + os.sep) 52 | tra_image_dir = os.path.join('DUTS', 'DUTS-TR', 'DUTS-TR', 'im_aug' + os.sep) 53 | tra_label_dir = os.path.join('DUTS', 'DUTS-TR', 'DUTS-TR', 'gt_aug' + os.sep) 54 | 55 | image_ext = '.jpg' 56 | label_ext = '.png' 57 | 58 | model_dir = os.path.join(os.getcwd(), 'saved_models', model_name + os.sep) 59 | 60 | epoch_num = 100000 61 | batch_size_train = 12 62 | batch_size_val = 1 63 | train_num = 0 64 | val_num = 0 65 | 66 | tra_img_name_list = glob.glob(data_dir + tra_image_dir + '*' + image_ext) 67 | 68 | tra_lbl_name_list = [] 69 | for img_path in tra_img_name_list: 70 | img_name = img_path.split(os.sep)[-1] 71 | 72 | aaa = img_name.split(".") 73 | bbb = aaa[0:-1] 74 | imidx = bbb[0] 75 | for i in range(1,len(bbb)): 76 | imidx = imidx + "." + bbb[i] 77 | 78 | tra_lbl_name_list.append(data_dir + tra_label_dir + imidx + label_ext) 79 | 80 | print("---") 81 | print("train images: ", len(tra_img_name_list)) 82 | print("train labels: ", len(tra_lbl_name_list)) 83 | print("---") 84 | 85 | train_num = len(tra_img_name_list) 86 | 87 | salobj_dataset = SalObjDataset( 88 | img_name_list=tra_img_name_list, 89 | lbl_name_list=tra_lbl_name_list, 90 | transform=transforms.Compose([ 91 | RescaleT(320), 92 | RandomCrop(288), 93 | ToTensorLab(flag=0)])) 94 | salobj_dataloader = DataLoader(salobj_dataset, batch_size=batch_size_train, shuffle=True, num_workers=1) 95 | 96 | # ------- 3. define model -------- 97 | # define the net 98 | if(model_name=='u2net'): 99 | net = U2NET(3, 1) 100 | elif(model_name=='u2netp'): 101 | net = U2NETP(3,1) 102 | 103 | if torch.cuda.is_available(): 104 | net.cuda() 105 | 106 | # ------- 4. define optimizer -------- 107 | print("---define optimizer...") 108 | optimizer = optim.Adam(net.parameters(), lr=0.001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0) 109 | 110 | # ------- 5. training process -------- 111 | print("---start training...") 112 | ite_num = 0 113 | running_loss = 0.0 114 | running_tar_loss = 0.0 115 | ite_num4val = 0 116 | save_frq = 2000 # save the model every 2000 iterations 117 | 118 | for epoch in range(0, epoch_num): 119 | net.train() 120 | 121 | for i, data in enumerate(salobj_dataloader): 122 | ite_num = ite_num + 1 123 | ite_num4val = ite_num4val + 1 124 | 125 | inputs, labels = data['image'], data['label'] 126 | 127 | inputs = inputs.type(torch.FloatTensor) 128 | labels = labels.type(torch.FloatTensor) 129 | 130 | # wrap them in Variable 131 | if torch.cuda.is_available(): 132 | inputs_v, labels_v = Variable(inputs.cuda(), requires_grad=False), Variable(labels.cuda(), 133 | requires_grad=False) 134 | else: 135 | inputs_v, labels_v = Variable(inputs, requires_grad=False), Variable(labels, requires_grad=False) 136 | 137 | # y zero the parameter gradients 138 | optimizer.zero_grad() 139 | 140 | # forward + backward + optimize 141 | d0, d1, d2, d3, d4, d5, d6 = net(inputs_v) 142 | loss2, loss = muti_bce_loss_fusion(d0, d1, d2, d3, d4, d5, d6, labels_v) 143 | 144 | loss.backward() 145 | optimizer.step() 146 | 147 | # # print statistics 148 | running_loss += loss.data.item() 149 | running_tar_loss += loss2.data.item() 150 | 151 | # del temporary outputs and loss 152 | del d0, d1, d2, d3, d4, d5, d6, loss2, loss 153 | 154 | print("[epoch: %3d/%3d, batch: %5d/%5d, ite: %d] train loss: %3f, tar: %3f " % ( 155 | epoch + 1, epoch_num, (i + 1) * batch_size_train, train_num, ite_num, running_loss / ite_num4val, running_tar_loss / ite_num4val)) 156 | 157 | if ite_num % save_frq == 0: 158 | 159 | torch.save(net.state_dict(), model_dir + model_name+"_bce_itr_%d_train_%3f_tar_%3f.pth" % (ite_num, running_loss / ite_num4val, running_tar_loss / ite_num4val)) 160 | running_loss = 0.0 161 | running_tar_loss = 0.0 162 | net.train() # resume train 163 | ite_num4val = 0 164 | 165 | --------------------------------------------------------------------------------