lfw.py 1.8 KB
Newer Older
X
xiteng1988 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# ================================================================
#   Copyright (c) 2020  PaddlePaddle Authors. All Rights Reserved.
#
# 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 numpy as np
16
import imageio
X
xiteng1988 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
import paddle
from paddle import fluid


class LFW(object):
    def __init__(self, imgl, imgr):

        self.imgl_list = imgl
        self.imgr_list = imgr
        self.shuffle_idx = [i for i in range(len(self.imgl_list))]

    def reader(self):
        while True:
            if len(self.shuffle_idx) == 0:
                self.shuffle_idx = [i for i in range(len(self.imgl_list))]
                return
            index = self.shuffle_idx.pop(0)

35
            imgl = imageio.imread(self.imgl_list[index])
X
xiteng1988 已提交
36 37
            if len(imgl.shape) == 2:
                imgl = np.stack([imgl] * 3, 2)
38
            imgr = imageio.imread(self.imgr_list[index])
X
xiteng1988 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
            if len(imgr.shape) == 2:
                imgr = np.stack([imgr] * 3, 2)

            imglist = [imgl, imgl[:, ::-1, :], imgr, imgr[:, ::-1, :]]
            for i in range(len(imglist)):
                imglist[i] = (imglist[i] - 127.5) / 128.0
                imglist[i] = imglist[i].transpose(2, 0, 1)

            imgs = [img.astype('float32') for img in imglist]
            yield imgs

    def __len__(self):
        return len(self.imgl_list)


if __name__ == '__main__':
    pass