示例#1
0
def swap_faces(Xs_raw, Xt_raw):
    detector = MTCNN()
    device = torch.device('cpu')
    G = AEI_Net(c_id=512)
    G.eval()
    G.load_state_dict(torch.load('./saved_models/G_latest.pth', map_location=device))
    G = G.cpu()

    f_siz = 256

    arcface = Backbone(50, 0.6, 'ir_se').to(device)
    arcface.eval()
    arcface.load_state_dict(torch.load('./saved_models/model_ir_se50.pth', map_location=device), strict=False)

    test_transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])

    Xs_img = Image.fromarray(Xs_raw)
    Xs = detector.align(Xs_img, crop_size=(f_siz, f_siz))

    Xs = test_transform(Xs)
    Xs = Xs.unsqueeze(0).cpu()
    with torch.no_grad():
        embeds, Xs_feats = arcface(F.interpolate(Xs, (112, 112), mode='bilinear', align_corners=True))
        embeds = embeds.mean(dim=0, keepdim=True)

    mask = np.zeros([f_siz, f_siz], dtype=np.float)
    for i in range(f_siz):
        for j in range(f_siz):
            dist = np.sqrt((i-(f_siz/2))**2 + (j-(f_siz/2))**2)/(f_siz/2)
            dist = np.minimum(dist, 1)
            mask[i, j] = 1-dist
    mask = cv2.dilate(mask, None, iterations=20)

    Xt_img = Image.fromarray(Xt_raw)

    Xt, trans_inv = detector.align(Xt_img, crop_size=(f_siz, f_siz), return_trans_inv=True)

    Xt = test_transform(Xt)

    Xt = Xt.unsqueeze(0).cpu()
    with torch.no_grad():
        st = time.time()
        Yt, _ = G(Xt, embeds)
        Yt = Yt.squeeze().detach().cpu().numpy()
        st = time.time() - st
        print(f'inference time: {st} sec')
        Yt = Yt.transpose([1, 2, 0])*0.5 + 0.5
        Yt_trans_inv = cv2.warpAffine(Yt, trans_inv, (np.size(Xt_raw, 1), np.size(Xt_raw, 0)), borderMode=cv2.BORDER_TRANSPARENT)
        mask_ = cv2.warpAffine(mask, trans_inv, (np.size(Xt_raw, 1), np.size(Xt_raw, 0)), borderMode=cv2.BORDER_TRANSPARENT)
        mask_ = np.expand_dims(mask_, 2)
        Yt_trans_inv = mask_*Yt_trans_inv + (1-mask_)*(Xt_raw.astype(np.float)/255.)

        merge = Yt_trans_inv

        return merge
示例#2
0
    model_save_path = './saved_models/'
    optim_level = 'O0'

    # fine_tune_with_identity = False

    device = torch.device('cuda')
    # torch.set_num_threads(12)

    G = AEI_Net(c_id=512).to(device)
    D = MultiscaleDiscriminator(input_nc=3, n_layers=6, norm_layer=torch.nn.InstanceNorm2d).to(device)
    G.train()
    D.train()

    arcface = Backbone(50, 0.6, 'ir_se').to(device)
    arcface.eval()
    arcface.load_state_dict(torch.load('./id_model/model_ir_se50.pth', map_location=device), strict=False)

    opt_G = optim.Adam(G.parameters(), lr=lr_G, betas=(0, 0.999))
    opt_D = optim.Adam(D.parameters(), lr=lr_D, betas=(0, 0.999))

    G, opt_G = amp.initialize(G, opt_G, opt_level=optim_level)
    D, opt_D = amp.initialize(D, opt_D, opt_level=optim_level)

    try:
        p_G = './saved_mask_models/G_latest.pth'
        p_D = './saved_mask_models/D_latest.pth'
        G.load_state_dict(torch.load(p_G, map_location=torch.device('cpu')), strict=False)
        D.load_state_dict(torch.load(p_D, map_location=torch.device('cpu')), strict=False)
        
        print('p_G : ',p_G)
        print('p_D : ',p_D)
def serve():
    data = {"success": False}
    detector = MTCNN()
    device = torch.device('cuda')
    G = AEI_Net(c_id=512)
    G.eval()
    G.load_state_dict(
        torch.load('./saved_models/G_latest.pth',
                   map_location=torch.device('cpu')))
    G = G.cuda()
    arcface = Backbone(50, 0.6, 'ir_se').to(device)
    arcface.eval()
    arcface.load_state_dict(torch.load('./face_modules/model_ir_se50.pth',
                                       map_location=device),
                            strict=False)

    test_transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])
    if flask.request.method == 'POST':
        st = time.time()
        metadata = flask.request.form
        source_image = metadata['source_image']
        target_image = metadata['target_image']
        source_image = base64.b64decode(source_image.encode('utf-8'))
        Xs_raw = cv2.imdecode(np.frombuffer(source_image, np.uint8),
                              cv2.IMREAD_COLOR)
        target_image = base64.b64decode(target_image.encode('utf-8'))
        Xt_raw = cv2.imdecode(np.frombuffer(target_image, np.uint8),
                              cv2.IMREAD_COLOR)
        Xs = detector.align(Image.fromarray(Xs_raw[:, :, ::-1]),
                            crop_size=(256, 256))
        Xs_raw = np.array(Xs)[:, :, ::-1]
        Xs = test_transform(Xs)
        Xs = Xs.unsqueeze(0).cuda()
        with torch.no_grad():
            embeds = arcface(
                F.interpolate(Xs[:, :, 19:237, 19:237], (112, 112),
                              mode='bilinear',
                              align_corners=True))
        Xt, trans_inv = detector.align(Image.fromarray(Xt_raw[:, :, ::-1]),
                                       crop_size=(256, 256),
                                       return_trans_inv=True)
        Xt_raw = Xt_raw.astype(np.float) / 255.0
        Xt = test_transform(Xt)
        Xt = Xt.unsqueeze(0).cuda()
        mask = np.zeros([256, 256], dtype=np.float)
        for i in range(256):
            for j in range(256):
                dist = np.sqrt((i - 128)**2 + (j - 128)**2) / 128
                dist = np.minimum(dist, 1)
                mask[i, j] = 1 - dist
        mask = cv2.dilate(mask, None, iterations=20)

        with torch.no_grad():
            Yt, _ = G(Xt, embeds)
            Yt = Yt.squeeze().detach().cpu().numpy().transpose([1, 2, 0
                                                                ]) * 0.5 + 0.5
            Yt = Yt[:, :, ::-1]
            Yt_trans_inv = cv2.warpAffine(
                Yt,
                trans_inv, (np.size(Xt_raw, 1), np.size(Xt_raw, 0)),
                borderValue=(0, 0, 0))
            mask_ = cv2.warpAffine(mask,
                                   trans_inv,
                                   (np.size(Xt_raw, 1), np.size(Xt_raw, 0)),
                                   borderValue=(0, 0, 0))
            mask_ = np.expand_dims(mask_, 2)
            Yt_trans_inv = mask_ * Yt_trans_inv + (1 - mask_) * Xt_raw
        img_data = Yt_trans_inv * 255
        retval, buffer = cv2.imencode('.jpg', img_data)
        pic_str = base64.b64encode(buffer)
        pic_str = pic_str.decode()
        data['success'] = True
        data['image'] = pic_str
        st = time.time() - st
        print(f'process time: {st} sec')
        return flask.jsonify(data)
    os.makedirs(inner_dir)

gpu_str = parse_gpu_id(opts.gpu_ids)
os.environ['CUDA_VISIBLE_DEVICES'] = gpu_str

G = AETNet().cuda()
D = MultiscaleDiscriminator(input_nc=3,
                            n_layers=6,
                            norm_layer=torch.nn.InstanceNorm2d).cuda()

G.train()
D.train()

arcface = Backbone(50, 0.6, 'ir_se').cuda()
arcface.eval()
arcface.load_state_dict(torch.load('./saved_models/model_ir_se50.pth'),
                        strict=False)

opt_G = optim.Adam(G.parameters(), lr=opts.lr_G, betas=(0, 0.999))
opt_D = optim.Adam(D.parameters(), lr=opts.lr_D, betas=(0, 0.999))

resume_epoch = 0
resume_iter = 0
l_adv = 1
l_att = 10
l_id = 5
l_rec = 10
inner_count = 0  # save mid results idx. -> idx.jpg
if opts.resume:
    G_checkpoint = torch.load(os.path.join(checkponits_dir, "G_latest.pth"))
    D_checkpoint = torch.load(os.path.join(checkponits_dir, "D_latest.pth"))
    param_checkpoint = torch.load(os.path.join(checkponits_dir, "params.pth"))