From 4fe1ab02a42764f83ff0ee687dad35e698dd8e81 Mon Sep 17 00:00:00 2001 From: Mbaey <1092460929@qq.com> Date: Thu, 13 Oct 2022 15:48:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=90=88=E5=B9=B6commit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FFHQ_ID2978_for_Pytorch/.keep | 0 .../FFHQ_ID2978_for_Pytorch/LICENSE | 201 ++++++++++++++ .../FFHQ_ID2978_for_Pytorch/data_loader.py | 48 ++++ .../FFHQ_ID2978_for_Pytorch/deeplab.py | 261 ++++++++++++++++++ .../FFHQ_ID2978_for_Pytorch/readme.md | 43 +++ .../FFHQ_ID2978_for_Pytorch/requirements.txt | 5 + .../FFHQ_ID2978_for_Pytorch/run_deeplab.py | 85 ++++++ .../FFHQ_ID2978_for_Pytorch/utils.py | 100 +++++++ 8 files changed, 743 insertions(+) create mode 100644 PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/.keep create mode 100644 PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/LICENSE create mode 100644 PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/data_loader.py create mode 100644 PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/deeplab.py create mode 100644 PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/readme.md create mode 100644 PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/requirements.txt create mode 100644 PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/run_deeplab.py create mode 100644 PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/utils.py diff --git a/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/.keep b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/LICENSE b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/LICENSE new file mode 100644 index 0000000000..261eeb9e9f --- /dev/null +++ b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/data_loader.py b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/data_loader.py new file mode 100644 index 0000000000..d8420602ec --- /dev/null +++ b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/data_loader.py @@ -0,0 +1,48 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import torch.utils.data as data +import os +from PIL import Image +from utils import preprocess_image + + +class CelebASegmentation(data.Dataset): + CLASSES = ['background' ,'skin','nose','eye_g','l_eye','r_eye','l_brow','r_brow','l_ear','r_ear','mouth','u_lip','l_lip','hair','hat','ear_r','neck_l','neck','cloth'] + + def __init__(self, root, transform=None, crop_size=None): + self.root = root + self.transform = transform + self.crop_size = crop_size + + self.images = [] + subdirs = next(os.walk(self.root))[1] #quick trick to get all subdirectories + for subdir in subdirs: + curr_images = [os.path.join(self.root,subdir,file) for file in os.listdir(os.path.join(self.root,subdir)) if file.endswith('.png')] + self.images += curr_images + + + def __getitem__(self, index): + _img = Image.open(self.images[index]).convert('RGB') + _img=_img.resize((513,513),Image.BILINEAR) + _img = preprocess_image(_img,flip=False,scale=None,crop=(self.crop_size, self.crop_size)) + + if self.transform is not None: + _img = self.transform(_img) + + return _img + + def __len__(self): + return len(self.images) diff --git a/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/deeplab.py b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/deeplab.py new file mode 100644 index 0000000000..99afacaf17 --- /dev/null +++ b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/deeplab.py @@ -0,0 +1,261 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import torch +import torch.nn as nn +import math +import torch.utils.model_zoo as model_zoo +from torch.nn import functional as F + + +__all__ = ['ResNet', 'resnet50', 'resnet101', 'resnet152'] + + +model_urls = { + 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth', + 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth', + 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth', +} + + +class Conv2d(nn.Conv2d): + + def __init__(self, in_channels, out_channels, kernel_size, stride=1, + padding=0, dilation=1, groups=1, bias=True): + super(Conv2d, self).__init__(in_channels, out_channels, kernel_size, stride, + padding, dilation, groups, bias) + + def forward(self, x): + # return super(Conv2d, self).forward(x) + weight = self.weight + weight_mean = weight.mean(dim=1, keepdim=True).mean(dim=2, + keepdim=True).mean(dim=3, keepdim=True) + weight = weight - weight_mean + std = weight.view(weight.size(0), -1).std(dim=1).view(-1, 1, 1, 1) + 1e-5 + weight = weight / std.expand_as(weight) + return F.conv2d(x, weight, self.bias, self.stride, + self.padding, self.dilation, self.groups) + + +class ASPP(nn.Module): + + def __init__(self, C, depth, num_classes, conv=nn.Conv2d, norm=nn.BatchNorm2d, momentum=0.0003, mult=1): + super(ASPP, self).__init__() + self._C = C + self._depth = depth + self._num_classes = num_classes + + self.global_pooling = nn.AdaptiveAvgPool2d(1) + self.relu = nn.ReLU(inplace=True) + self.aspp1 = conv(C, depth, kernel_size=1, stride=1, bias=False) + self.aspp2 = conv(C, depth, kernel_size=3, stride=1, + dilation=int(6*mult), padding=int(6*mult), + bias=False) + self.aspp3 = conv(C, depth, kernel_size=3, stride=1, + dilation=int(12*mult), padding=int(12*mult), + bias=False) + self.aspp4 = conv(C, depth, kernel_size=3, stride=1, + dilation=int(18*mult), padding=int(18*mult), + bias=False) + self.aspp5 = conv(C, depth, kernel_size=1, stride=1, bias=False) + self.aspp1_bn = norm(depth, momentum) + self.aspp2_bn = norm(depth, momentum) + self.aspp3_bn = norm(depth, momentum) + self.aspp4_bn = norm(depth, momentum) + self.aspp5_bn = norm(depth, momentum) + self.conv2 = conv(depth * 5, depth, kernel_size=1, stride=1, + bias=False) + self.bn2 = norm(depth, momentum) + self.conv3 = nn.Conv2d(depth, num_classes, kernel_size=1, stride=1) + + def forward(self, x): + x1 = self.aspp1(x) + x1 = self.aspp1_bn(x1) + x1 = self.relu(x1) + x2 = self.aspp2(x) + x2 = self.aspp2_bn(x2) + x2 = self.relu(x2) + x3 = self.aspp3(x) + x3 = self.aspp3_bn(x3) + x3 = self.relu(x3) + x4 = self.aspp4(x) + x4 = self.aspp4_bn(x4) + x4 = self.relu(x4) + x5 = self.global_pooling(x) + x5 = self.aspp5(x5) + x5 = self.aspp5_bn(x5) + x5 = self.relu(x5) + x5 = nn.Upsample((x.shape[2], x.shape[3]), mode='bilinear', + align_corners=True)(x5) + x = torch.cat((x1, x2, x3, x4, x5), 1) + x = self.conv2(x) + x = self.bn2(x) + x = self.relu(x) + x = self.conv3(x) + + return x + + +class Bottleneck(nn.Module): + expansion = 4 + + def __init__(self, inplanes, planes, stride=1, downsample=None, dilation=1, conv=None, norm=None): + super(Bottleneck, self).__init__() + self.conv1 = conv(inplanes, planes, kernel_size=1, bias=False) + self.bn1 = norm(planes) + self.conv2 = conv(planes, planes, kernel_size=3, stride=stride, + dilation=dilation, padding=dilation, bias=False) + self.bn2 = norm(planes) + self.conv3 = conv(planes, planes * self.expansion, kernel_size=1, bias=False) + self.bn3 = norm(planes * self.expansion) + self.relu = nn.ReLU(inplace=True) + self.downsample = downsample + self.stride = stride + + def forward(self, x): + residual = x + + out = self.conv1(x) + out = self.bn1(out) + out = self.relu(out) + + out = self.conv2(out) + out = self.bn2(out) + out = self.relu(out) + + out = self.conv3(out) + out = self.bn3(out) + + if self.downsample is not None: + residual = self.downsample(x) + + out += residual + out = self.relu(out) + + return out + + +class ResNet(nn.Module): + + def __init__(self, block, layers, num_classes, num_groups=None, weight_std=False, beta=False): + self.inplanes = 64 + self.norm = lambda planes, momentum=0.05: nn.BatchNorm2d(planes, momentum=momentum) if num_groups is None else nn.GroupNorm(num_groups, planes) + self.conv = Conv2d if weight_std else nn.Conv2d + + super(ResNet, self).__init__() + if not beta: + self.conv1 = self.conv(3, 64, kernel_size=7, stride=2, padding=3, + bias=False) + else: + self.conv1 = nn.Sequential( + self.conv(3, 64, 3, stride=2, padding=1, bias=False), + self.conv(64, 64, 3, stride=1, padding=1, bias=False), + self.conv(64, 64, 3, stride=1, padding=1, bias=False)) + self.bn1 = self.norm(64) + self.relu = nn.ReLU(inplace=True) + self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) + self.layer1 = self._make_layer(block, 64, layers[0]) + self.layer2 = self._make_layer(block, 128, layers[1], stride=2) + self.layer3 = self._make_layer(block, 256, layers[2], stride=2) + self.layer4 = self._make_layer(block, 512, layers[3], stride=1, + dilation=2) + self.aspp = ASPP(512 * block.expansion, 256, num_classes, conv=self.conv, norm=self.norm) + + for m in self.modules(): + if isinstance(m, self.conv): + n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels + m.weight.data.normal_(0, math.sqrt(2. / n)) + elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.GroupNorm): + m.weight.data.fill_(1) + m.bias.data.zero_() + + def _make_layer(self, block, planes, blocks, stride=1, dilation=1): + downsample = None + if stride != 1 or dilation != 1 or self.inplanes != planes * block.expansion: + downsample = nn.Sequential( + self.conv(self.inplanes, planes * block.expansion, + kernel_size=1, stride=stride, dilation=max(1, dilation/2), bias=False), + self.norm(planes * block.expansion), + ) + + layers = [] + layers.append(block(self.inplanes, planes, stride, downsample, dilation=max(1, dilation/2), conv=self.conv, norm=self.norm)) + self.inplanes = planes * block.expansion + for i in range(1, blocks): + layers.append(block(self.inplanes, planes, dilation=dilation, conv=self.conv, norm=self.norm)) + + return nn.Sequential(*layers) + + def forward(self, x): + size = (x.shape[2], x.shape[3]) + x = self.conv1(x) + x = self.bn1(x) + x = self.relu(x) + x = self.maxpool(x) + + x = self.layer1(x) + x = self.layer2(x) + x = self.layer3(x) + x = self.layer4(x) + + x = self.aspp(x) + x = nn.Upsample(size, mode='bilinear', align_corners=True)(x) + return x + + +def resnet50(pretrained=False, **kwargs): + """Constructs a ResNet-50 model. + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs) + if pretrained: + model.load_state_dict(model_zoo.load_url(model_urls['resnet50'])) + return model + + +def resnet101(pretrained=False, num_groups=None, weight_std=False, **kwargs): + """Constructs a ResNet-101 model. + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + model = ResNet(Bottleneck, [3, 4, 23, 3], num_groups=num_groups, weight_std=weight_std, **kwargs) + if pretrained: + model_dict = model.state_dict() + if num_groups and weight_std: + pretrained_dict = torch.load('deeplab_model/R-101-GN-WS.pth.tar' , map_location='cpu') + overlap_dict = {k[7:]: v for k, v in pretrained_dict.items() if k[7:] in model_dict} + assert len(overlap_dict) == 312 + elif not num_groups and not weight_std: + pretrained_dict = model_zoo.load_url(model_urls['resnet101']) + overlap_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict} + else: + raise ValueError('Currently only support BN or GN+WS') + model_dict.update(overlap_dict) + model.load_state_dict(model_dict) + return model + + +def resnet152(pretrained=False, **kwargs): + """Constructs a ResNet-152 model. + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + model = ResNet(Bottleneck, [3, 8, 36, 3], **kwargs) + if pretrained: + model.load_state_dict(model_zoo.load_url(model_urls['resnet152'])) + return model diff --git a/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/readme.md b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/readme.md new file mode 100644 index 0000000000..1b5653c876 --- /dev/null +++ b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/readme.md @@ -0,0 +1,43 @@ +# FFHQ-Aging-Dataset +- Paper:[[2003.09764] Lifespan Age Transformation Synthesis](https://arxiv.org/abs/2003.09764) +- Github Code:[royorel/FFHQ-Aging-Dataset: FFHQ-Aging Dataset](https://github.com/royorel/FFHQ-Aging-Dataset) + +Face Semantic maps were acquired by training a pytorch implementation of [DeepLabV3](https://github.com/chenxi116/DeepLabv3.pytorch) network on the [CelebAMASK-HQ](https://github.com/switchablenorms/CelebAMask-HQ) dataset. + +## Directory structure +``` +. +├── data_loader.py # 数据集加载 +├── deeplab_model #存放模型参数,下载的模型请放这里。 +│   ├── deeplab_model.pth +│   └── R-101-GN-WS.pth.tar +├── ffhq_aging128×128 #存放数据集,下载的数据集请解压到这里 +├── deeplab.py #deeplap v3模型脚本 +├── readme.md +├── requirements.txt +├── run_deeplab.py +└── utils.py +``` +## Environment preparation +- Install Packages + - pip install -r requirements.txt +- Download **FFHQ-Aging-Dataset** & **Deeplab Model** from [original repo](https://github.com/royorel/FFHQ-Aging-Dataset) & [deeplab_model/R-101-GN-WS.pth.tar](https://drive.google.com/uc?id=1oRGgrI4KNdefbWVpw0rRkEP1gbJIRokM) & [deeplab_model/deeplab_model.pth](https://drive.google.com/uc?id=1w2XjDywFr2NjuUWaLQDRktH7VwIfuNlYhttps://drive.google.com/uc?id=1w2XjDywFr2NjuUWaLQDRktH7VwIfuNlY) + - The original **FFHQ-dataset** is stored on the [google drive](https://drive.google.com/drive/folders/1u2xu7bSrWxrbUxk-dT-UvEJq8IjdmNTP), By running the [original repo's get_ffhq_aging.sh](https://github.com/royorel/FFHQ-Aging-Dataset/blob/master/get_ffhq_aging.sh) , you can easily get **FFHQ-Aging-Dataset**. + +## Run + +python3.7 run_deeplab.py + + +> 分割后的图像,放在原图片存放路径里的parsings文件夹下。 + +> 例如:ffhq_aging128×128\0-2\parsings +> ffhq_aging128×128\3-6\parsings等 + +### Runing result + + + +- 原始图像、GPU分割、NPU分割效果对比如下: +
调用deeplab模型后,原始图片被分割为:背景、皮肤、鼻子、眼睛等部分。 diff --git a/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/requirements.txt b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/requirements.txt new file mode 100644 index 0000000000..f116ff3221 --- /dev/null +++ b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/requirements.txt @@ -0,0 +1,5 @@ +requests +numpy +scipy +pillow +pytorch diff --git a/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/run_deeplab.py b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/run_deeplab.py new file mode 100644 index 0000000000..41de24d34f --- /dev/null +++ b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/run_deeplab.py @@ -0,0 +1,85 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse +import os +import datetime +import numpy as np +import torch.npu +import torch +import torch.nn as nn +from pdb import set_trace as st +from PIL import Image +from torchvision import transforms +import deeplab +from data_loader import CelebASegmentation + +# resnet_file_spec = dict(file_url='https://drive.google.com/uc?id=1oRGgrI4KNdefbWVpw0rRkEP1gbJIRokM', file_path='deeplab_model/R-101-GN-WS.pth.tar', file_size=178260167, file_md5='aa48cc3d3ba3b7ac357c1489b169eb32') +# deeplab_file_spec = dict(file_url='https://drive.google.com/uc?id=1w2XjDywFr2NjuUWaLQDRktH7VwIfuNlY', file_path='deeplab_model/deeplab_model.pth', file_size=464446305, file_md5='8e8345b1b9d95e02780f9bed76cc0293') + +resolution = 128 +model_fname = 'deeplab_model/deeplab_model.pth' +dataset_root = "ffhq_aging128x128" + +assert torch.npu.is_available() +assert os.path.isdir(dataset_root) + +dataset = CelebASegmentation(dataset_root, crop_size=513) +print("len(dataset)", len(dataset)) +print("dataset.CLASSES", dataset.CLASSES) +print("dataset.images[0]", dataset.images[0]) +print("Start time:", datetime.datetime.now()) + +model = getattr(deeplab, 'resnet101')( + pretrained=True, + num_classes=len(dataset.CLASSES), + num_groups=32, + weight_std=True, + beta=False) + +checkpoint = torch.load(model_fname, map_location='cpu') +state_dict = {k[7:]: v for k, + v in checkpoint['state_dict'].items() if 'tracked' not in k} +model.load_state_dict(state_dict) + +device = "npu" +# model = model.npu() +model = model.to(device) +model.eval() + +for i in range(len(dataset)): + inputs = dataset[i] + # inputs = inputs.npu() + inputs = inputs.unsqueeze(0).to(device) + # print("inputs-----",inputs.shape) + outputs = model(inputs) + # print("outputs-----",outputs.shape) + + _, pred = torch.max(outputs, 1) + pred = pred.data.cpu().numpy().squeeze().astype(np.uint8) + imname = os.path.basename(dataset.images[i]) + mask_pred = Image.fromarray(pred) + mask_pred = mask_pred.resize((resolution, resolution), Image.NEAREST) + try: + mask_pred.save(dataset.images[i].replace( + imname, 'parsings/'+imname[:-4]+'.png')) + except FileNotFoundError: + os.makedirs(os.path.join(os.path.dirname( + dataset.images[i]), 'parsings')) + mask_pred.save(dataset.images[i].replace( + imname, 'parsings/'+imname[:-4]+'.png')) + + print('processed {0}/{1} images, Time:{2}'.format(i + + 1, len(dataset), datetime.datetime.now())) + # break diff --git a/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/utils.py b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/utils.py new file mode 100644 index 0000000000..e05d7b8e72 --- /dev/null +++ b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/utils.py @@ -0,0 +1,100 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import os +import math +import html +import glob +import uuid +import random +import hashlib +import requests +import numpy as np +import torch +import torchvision.transforms as transforms +from PIL import Image + + +def preprocess_image(image, flip=False, scale=None, crop=None): + if flip: + if random.random() < 0.5: + image = image.transpose(Image.FLIP_LEFT_RIGHT) + if scale: + w, h = image.size + rand_log_scale = math.log(scale[0], 2) + random.random() * (math.log(scale[1], 2) - math.log(scale[0], 2)) + random_scale = math.pow(2, rand_log_scale) + new_size = (int(round(w * random_scale)), int(round(h * random_scale))) + image = image.resize(new_size, Image.ANTIALIAS) + + data_transforms = transforms.Compose([ + transforms.ToTensor(), + transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) + ]) + image = data_transforms(image) + + return image + + +def download_file(session, file_spec, chunk_size=128, num_attempts=10): + file_path = file_spec['file_path'] + file_url = file_spec['file_url'] + file_dir = os.path.dirname(file_path) + tmp_path = file_path + '.tmp.' + uuid.uuid4().hex + if file_dir: + os.makedirs(file_dir, exist_ok=True) + + for attempts_left in reversed(range(num_attempts)): + data_size = 0 + try: + # Download. + data_md5 = hashlib.md5() + with session.get(file_url, stream=True) as res: + res.raise_for_status() + with open(tmp_path, 'wb') as f: + for chunk in res.iter_content(chunk_size=chunk_size<<10): + f.write(chunk) + data_size += len(chunk) + data_md5.update(chunk) + + # Validate. + if 'file_size' in file_spec and data_size != file_spec['file_size']: + raise IOError('Incorrect file size', file_path) + if 'file_md5' in file_spec and data_md5.hexdigest() != file_spec['file_md5']: + raise IOError('Incorrect file MD5', file_path) + break + + except: + # Last attempt => raise error. + if not attempts_left: + raise + + # Handle Google Drive virus checker nag. + if data_size > 0 and data_size < 8192: + with open(tmp_path, 'rb') as f: + data = f.read() + links = [html.unescape(link) for link in data.decode('utf-8').split('"') if 'export=download' in link] + if len(links) == 1: + file_url = requests.compat.urljoin(file_url, links[0]) + continue + + # Rename temp file to the correct name. + os.replace(tmp_path, file_path) # atomic + + # Attempt to clean up any leftover temps. + for filename in glob.glob(file_path + '.tmp.*'): + try: + os.remove(filename) + except: + pass -- Gitee From f9d6ba9357c9990c83ea90b803dc7bcd1aa2493a Mon Sep 17 00:00:00 2001 From: Mbaey <1092460929@qq.com> Date: Wed, 26 Oct 2022 15:09:20 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=80=A7=E8=83=BD=E8=BE=BE=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FFHQ_ID2978_for_Pytorch/readme.md | 7 +++++++ .../FFHQ_ID2978_for_Pytorch/run_deeplab.py | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/readme.md b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/readme.md index 1b5653c876..0475466d67 100644 --- a/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/readme.md +++ b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/readme.md @@ -41,3 +41,10 @@ python3.7 run_deeplab.py - 原始图像、GPU分割、NPU分割效果对比如下:
调用deeplab模型后,原始图片被分割为:背景、皮肤、鼻子、眼睛等部分。 + +### 性能达标 + +| Name | FPS | +| ------ | ---- | +| GPU-1p | 20.9 | +| NPU-1p | 21.5 | diff --git a/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/run_deeplab.py b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/run_deeplab.py index 41de24d34f..a40089791e 100644 --- a/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/run_deeplab.py +++ b/PyTorch/contrib/cv/semantic_segmentation/FFHQ_ID2978_for_Pytorch/run_deeplab.py @@ -21,6 +21,7 @@ import torch import torch.nn as nn from pdb import set_trace as st from PIL import Image +from tqdm import tqdm from torchvision import transforms import deeplab from data_loader import CelebASegmentation @@ -35,6 +36,12 @@ dataset_root = "ffhq_aging128x128" assert torch.npu.is_available() assert os.path.isdir(dataset_root) +option = {} +option["ACL_OP_COMPILER_CACHE_MODE"] = "enable" # cache功能启用 +option["ACL_OP_COMPILER_CACHE_DIR"] = "./my_kernel_meta" # cache所在文件夹 +print("option:",option) +torch.npu.set_option(option) + dataset = CelebASegmentation(dataset_root, crop_size=513) print("len(dataset)", len(dataset)) print("dataset.CLASSES", dataset.CLASSES) @@ -58,7 +65,7 @@ device = "npu" model = model.to(device) model.eval() -for i in range(len(dataset)): +for i in tqdm(range(len(dataset))): inputs = dataset[i] # inputs = inputs.npu() inputs = inputs.unsqueeze(0).to(device) -- Gitee