utility.py 11.8 KB
Newer Older
L
lvmengsi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
#
#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.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import paddle.fluid as fluid
import os
import sys
import math
import distutils.util
import numpy as np
import inspect
import matplotlib
import six
matplotlib.use('agg')
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
L
lvmengsi 已提交
30 31
import imageio
import copy
L
lvmengsi 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

img_dim = 28


def plot(gen_data):
    pad_dim = 1
    paded = pad_dim + img_dim
    gen_data = gen_data.reshape(gen_data.shape[0], img_dim, img_dim)
    n = int(math.ceil(math.sqrt(gen_data.shape[0])))
    gen_data = (np.pad(
        gen_data, [[0, n * n - gen_data.shape[0]], [pad_dim, 0], [pad_dim, 0]],
        'constant').reshape((n, n, paded, paded)).transpose((0, 2, 1, 3))
                .reshape((n * paded, n * paded)))
    fig = plt.figure(figsize=(8, 8))
    plt.axis('off')
    plt.imshow(gen_data, cmap='Greys_r', vmin=-1, vmax=1)
    return fig


def checkpoints(epoch, cfg, exe, trainer, name):
L
lvmengsi 已提交
52
    output_path = cfg.output + '/checkpoints/' + str(epoch)
L
lvmengsi 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    if not os.path.exists(output_path):
        os.makedirs(output_path)
    fluid.io.save_persistables(
        exe, os.path.join(output_path, name), main_program=trainer.program)
    print('save checkpoints {} to {}'.format(name, output_path))
    sys.stdout.flush()


def init_checkpoints(cfg, exe, trainer, name):
    assert os.path.exists(cfg.init_model), "{} cannot be found.".format(
        cfg.init_model)
    fluid.io.load_persistables(
        exe, os.path.join(cfg.init_model, name), main_program=trainer.program)
    print('load checkpoints {} {} DONE'.format(cfg.init_model, name))
    sys.stdout.flush()


Z
zhumanyu 已提交
70 71 72 73 74 75 76 77
def save_test_image(epoch,
                    cfg,
                    exe,
                    place,
                    test_program,
                    g_trainer,
                    A_test_reader,
                    B_test_reader=None):
L
lvmengsi 已提交
78 79 80
    out_path = cfg.output + '/test'
    if not os.path.exists(out_path):
        os.makedirs(out_path)
Z
zhumanyu 已提交
81
    if cfg.model_net == "Pix2pix":
Z
zhumanyu 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        for data in zip(A_test_reader()):
            data_A, data_B, name = data[0]
            name = name[0]
            tensor_A = fluid.LoDTensor()
            tensor_B = fluid.LoDTensor()
            tensor_A.set(data_A, place)
            tensor_B.set(data_B, place)
            fake_B_temp = exe.run(
                test_program,
                fetch_list=[g_trainer.fake_B],
                feed={"input_A": tensor_A,
                      "input_B": tensor_B})
            fake_B_temp = np.squeeze(fake_B_temp[0]).transpose([1, 2, 0])
            input_A_temp = np.squeeze(data_A[0]).transpose([1, 2, 0])
            input_B_temp = np.squeeze(data_A[0]).transpose([1, 2, 0])
L
lvmengsi 已提交
97

L
lvmengsi 已提交
98
            imageio.imwrite(out_path + "/fakeB_" + str(epoch) + "_" + name, (
Z
zhumanyu 已提交
99
                (fake_B_temp + 1) * 127.5).astype(np.uint8))
L
lvmengsi 已提交
100
            imageio.imwrite(out_path + "/inputA_" + str(epoch) + "_" + name, (
Z
zhumanyu 已提交
101
                (input_A_temp + 1) * 127.5).astype(np.uint8))
L
lvmengsi 已提交
102
            imageio.imwrite(out_path + "/inputB_" + str(epoch) + "_" + name, (
Z
zhumanyu 已提交
103
                (input_B_temp + 1) * 127.5).astype(np.uint8))
Z
zhumanyu 已提交
104 105 106
    elif cfg.model_net == "StarGAN":
        for data in zip(A_test_reader()):
            real_img, label_org, name = data[0]
L
lvmengsi 已提交
107
            attr_names = cfg.selected_attrs.split(',')
Z
zhumanyu 已提交
108 109 110 111 112 113 114
            tensor_img = fluid.LoDTensor()
            tensor_label_org = fluid.LoDTensor()
            tensor_img.set(real_img, place)
            tensor_label_org.set(label_org, place)
            real_img_temp = np.squeeze(real_img).transpose([1, 2, 0])
            images = [real_img_temp]
            for i in range(cfg.c_dim):
L
lvmengsi 已提交
115 116 117 118
                label_trg_tmp = copy.deepcopy(label_org)
                label_trg_tmp[0][i] = 1.0 - label_trg_tmp[j][i]
                label_trg = check_attribute_conflict(
                    label_trg_tmp, attr_names[i], attr_names)
L
lvmengsi 已提交
119
                tensor_label_trg = fluid.LoDTensor()
Z
zhumanyu 已提交
120 121
                tensor_label_trg.set(label_trg, place)
                fake_temp, rec_temp = exe.run(
L
lvmengsi 已提交
122
                    test_program,
Z
zhumanyu 已提交
123 124 125 126 127 128 129 130 131 132 133
                    feed={
                        "image_real": tensor_img,
                        "label_org": tensor_label_org,
                        "label_trg": tensor_label_trg
                    },
                    fetch_list=[g_trainer.fake_img, g_trainer.rec_img])
                fake_temp = np.squeeze(fake_temp[0]).transpose([1, 2, 0])
                rec_temp = np.squeeze(rec_temp[0]).transpose([1, 2, 0])
                images.append(fake_temp)
                images.append(rec_temp)
            images_concat = np.concatenate(images, 1)
L
lvmengsi 已提交
134 135
            imageio.imwrite(out_path + "/fake_img" + str(epoch) + "_" + name[0],
                            ((images_concat + 1) * 127.5).astype(np.uint8))
Z
zhumanyu 已提交
136 137 138
    elif cfg.model_net == 'AttGAN' or cfg.model_net == 'STGAN':
        for data in zip(A_test_reader()):
            real_img, label_org, name = data[0]
L
lvmengsi 已提交
139
            attr_names = cfg.selected_attrs.split(',')
Z
zhumanyu 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
            label_trg = copy.deepcopy(label_org)
            tensor_img = fluid.LoDTensor()
            tensor_label_org = fluid.LoDTensor()
            tensor_label_trg = fluid.LoDTensor()
            tensor_label_org_ = fluid.LoDTensor()
            tensor_label_trg_ = fluid.LoDTensor()
            tensor_img.set(real_img, place)
            tensor_label_org.set(label_org, place)
            real_img_temp = np.squeeze(real_img).transpose([0, 2, 3, 1])
            images = [real_img_temp]
            for i in range(cfg.c_dim):
                label_trg_tmp = copy.deepcopy(label_trg)

                for j in range(len(label_org)):
                    label_trg_tmp[j][i] = 1.0 - label_trg_tmp[j][i]
L
lvmengsi 已提交
155 156
                    label_trg_tmp = check_attribute_conflict(
                        label_trg_tmp, attr_names[i], attr_names)
Z
zhumanyu 已提交
157

158 159
                label_trg_ = list(
                    map(lambda x: ((x * 2) - 1) * 0.5, label_trg_tmp))
Z
zhumanyu 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

                for j in range(len(label_org)):
                    label_trg_[j][i] = label_trg_[j][i] * 2.0
                tensor_label_org_.set(label_org, place)
                tensor_label_trg.set(label_trg, place)
                tensor_label_trg_.set(label_trg_, place)
                out = exe.run(test_program,
                              feed={
                                  "image_real": tensor_img,
                                  "label_org": tensor_label_org,
                                  "label_org_": tensor_label_org_,
                                  "label_trg": tensor_label_trg,
                                  "label_trg_": tensor_label_trg_
                              },
                              fetch_list=[g_trainer.fake_img])
                fake_temp = np.squeeze(out[0]).transpose([0, 2, 3, 1])
                images.append(fake_temp)
            images_concat = np.concatenate(images, 1)
            images_concat = np.concatenate(images_concat, 1)
L
lvmengsi 已提交
179 180
            imageio.imwrite(out_path + "/fake_img" + str(epoch) + '_' + name[0],
                            ((images_concat + 1) * 127.5).astype(np.uint8))
Z
zhumanyu 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

    else:
        for data_A, data_B in zip(A_test_reader(), B_test_reader()):
            A_name = data_A[0][1]
            B_name = data_B[0][1]
            tensor_A = fluid.LoDTensor()
            tensor_B = fluid.LoDTensor()
            tensor_A.set(data_A[0][0], place)
            tensor_B.set(data_B[0][0], place)
            fake_A_temp, fake_B_temp, cyc_A_temp, cyc_B_temp = exe.run(
                test_program,
                fetch_list=[
                    g_trainer.fake_A, g_trainer.fake_B, g_trainer.cyc_A,
                    g_trainer.cyc_B
                ],
                feed={"input_A": tensor_A,
                      "input_B": tensor_B})
            fake_A_temp = np.squeeze(fake_A_temp[0]).transpose([1, 2, 0])
            fake_B_temp = np.squeeze(fake_B_temp[0]).transpose([1, 2, 0])
            cyc_A_temp = np.squeeze(cyc_A_temp[0]).transpose([1, 2, 0])
            cyc_B_temp = np.squeeze(cyc_B_temp[0]).transpose([1, 2, 0])
            input_A_temp = np.squeeze(data_A[0][0]).transpose([1, 2, 0])
            input_B_temp = np.squeeze(data_B[0][0]).transpose([1, 2, 0])

L
lvmengsi 已提交
205
            imageio.imwrite(out_path + "/fakeB_" + str(epoch) + "_" + A_name, (
Z
zhumanyu 已提交
206
                (fake_B_temp + 1) * 127.5).astype(np.uint8))
L
lvmengsi 已提交
207
            imageio.imwrite(out_path + "/fakeA_" + str(epoch) + "_" + B_name, (
Z
zhumanyu 已提交
208
                (fake_A_temp + 1) * 127.5).astype(np.uint8))
L
lvmengsi 已提交
209
            imageio.imwrite(out_path + "/cycA_" + str(epoch) + "_" + A_name, (
Z
zhumanyu 已提交
210
                (cyc_A_temp + 1) * 127.5).astype(np.uint8))
L
lvmengsi 已提交
211
            imageio.imwrite(out_path + "/cycB_" + str(epoch) + "_" + B_name, (
Z
zhumanyu 已提交
212
                (cyc_B_temp + 1) * 127.5).astype(np.uint8))
L
lvmengsi 已提交
213
            imageio.imwrite(out_path + "/inputA_" + str(epoch) + "_" + A_name, (
Z
zhumanyu 已提交
214
                (input_A_temp + 1) * 127.5).astype(np.uint8))
L
lvmengsi 已提交
215
            imageio.imwrite(out_path + "/inputB_" + str(epoch) + "_" + B_name, (
Z
zhumanyu 已提交
216
                (input_B_temp + 1) * 127.5).astype(np.uint8))
L
lvmengsi 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238


class ImagePool(object):
    def __init__(self, pool_size=50):
        self.pool = []
        self.count = 0
        self.pool_size = pool_size

    def pool_image(self, image):
        if self.count < self.pool_size:
            self.pool.append(image)
            self.count += 1
            return image
        else:
            p = np.random.rand()
            if p > 0.5:
                random_id = np.random.randint(0, self.pool_size - 1)
                temp = self.pool[random_id]
                self.pool[random_id] = image
                return temp
            else:
                return image
L
lvmengsi 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266


def check_attribute_conflict(label_batch, attr, attrs):
    def _set(label, value, attr):
        if attr in attrs:
            label[attrs.index(attr)] = value

    attr_id = attrs.index(attr)
    for label in label_batch:
        if attr in ['Bald', 'Receding_Hairline'] and attrs[attr_id] != 0:
            _set(label, 0, 'Bangs')
        elif attr == 'Bangs' and attrs[attr_id] != 0:
            _set(label, 0, 'Bald')
            _set(label, 0, 'Receding_Hairline')
        elif attr in ['Black_Hair', 'Blond_Hair', 'Brown_Hair', 'Gray_Hair'
                      ] and attrs[attr_id] != 0:
            for a in ['Black_Hair', 'Blond_Hair', 'Brown_Hair', 'Gray_Hair']:
                if a != attr:
                    _set(label, 0, a)
        elif attr in ['Straight_Hair', 'Wavy_Hair'] and attrs[attr_id] != 0:
            for a in ['Straight_Hair', 'Wavy_Hair']:
                if a != attr:
                    _set(label, 0, a)
        elif attr in ['Mustache', 'No_Beard'] and attrs[attr_id] != 0:
            for a in ['Mustache', 'No_Beard']:
                if a != attr:
                    _set(label, 0, a)
    return label_batch
L
lvmengsi 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285


def check_gpu(use_gpu):
     """
     Log error and exit when set use_gpu=true in paddlepaddle
     cpu version.
     """
     err = "Config use_gpu cannot be set as true while you are " \
           "using paddlepaddle cpu version ! \nPlease try: \n" \
           "\t1. Install paddlepaddle-gpu to run model on GPU \n" \
           "\t2. Set use_gpu as false in config file to run " \
           "model on CPU"

     try:
         if use_gpu and not fluid.is_compiled_with_cuda():
             logger.error(err)
             sys.exit(1)
     except Exception as e:
         pass