Exemplo n.º 1
0
def prepare_image():
    dataset_image_path="/home/qianqianjun/CODE/DataSets/DaeDatasets"
    save_dir="/home/qianqianjun/CODE/实验结果/重建结果/改进编码器"
    os.makedirs(save_dir,exist_ok=True)
    length=5000
    dataset=Datas(dataset_image_path,length)
    loader=torch.utils.data.DataLoader(dataset,batch_size=25,shuffle=False,num_workers=4)
    encoders,decoders=Encoders(parm),Decoders(parm)
    device=torch.device("cpu")
    nn.Module.load_state_dict(encoders,torch.load("../CelebA_out/2020-03-28_23:13:37/checkpoints/encoders.pth",map_location=device))
    nn.Module.load_state_dict(decoders,torch.load("../CelebA_out/2020-03-28_23:13:37/checkpoints/decoders.pth",map_location=device))
    if parm.useCuda:
        encoders=encoders.cuda()
        decoders=decoders.cuda()
    for name,data in loader:
        batch_data=batchDataTransform(data,3)
        base=getBaseGrid(imgSize=parm.imgSize,Inbatch=True,batchSize=batch_data.shape[0])
        if parm.useCuda:
            batch_data = batch_data.cuda()
            base = base.cuda()
        z,zI,zW,intersI,intersW=encoders(batch_data)
        texture,warp,out,_=decoders(zI,zW,intersI,intersW,base)
        images=out.detach().cpu().numpy()
        images=np.transpose(images,[0,2,3,1])
        images=np.array(images*255,dtype=np.uint8)
        for index,img in enumerate(images):
            temp=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
            cv2.imwrite(os.path.join(save_dir,name[index]),temp)
Exemplo n.º 2
0
def interpolation(attr_path, decoders,batch_data,
                  test_texture_code, test_warp_code,
                  test_texture_inters,test_warp_inters,**lambda_params):
    # 属性向量加载
    with open(attr_path, "rb") as f:
        attribute = pk.load(f, encoding="utf-8")
        texture_code = attribute["texture_code"]
        warp_code = attribute["warp_code"]
        texture_inters = attribute["texture_inter_outs_attribute"]
        warp_inters = attribute["warp_inter_outs_attribute"]
    # 插值系数
    lambda_texture_inter_out = lambda_params["lambda_texture_inter_out"]
    lambda_warp_inter_out = lambda_params["lambda_warp_inter_out"]
    lambda_deepest_texture = lambda_params["lambda_deepest_texture"]
    lambda_deepest_warp = lambda_params["lambda_deepest_warp"]

    base = getBaseGrid(parm.imgSize, True, batch_data.size()[0])
    if parm.useCuda:
        decoders = torch.nn.Module.cuda(decoders)
        texture_code = texture_code.cuda()
        warp_code = warp_code.cuda()
        for i in range(len(texture_inters)):
            texture_inters[i] = texture_inters[i].cuda()
        for i in range(len(warp_inters)):
            warp_inters[i] = warp_inters[i].cuda()
        base = base.cuda()

    # 纹理信息插值
    texture_code_interpolation_result = test_texture_code + lambda_deepest_texture * texture_code.unsqueeze(0).repeat(
        test_texture_code.size()[0], 1)
    texture_inter_interpolation_result = []
    for i in range(len(test_texture_inters)):
        texture_inter_interpolation_result.append(
            test_texture_inters[i] + lambda_texture_inter_out[i] *
            texture_inters[i].repeat(test_texture_inters[i].size()[0], 1, 1, 1)
        )
    ### 变形信息插值
    warp_code_interpolation_result = test_warp_code + lambda_deepest_warp * warp_code.unsqueeze(0).repeat(
        test_warp_code.size()[0], 1)
    warp_inter_interpolation_result = []
    for i in range(len(test_warp_inters)):
        warp_inter_interpolation_result.append(
            test_warp_inters[i] + lambda_warp_inter_out[i] *
            warp_inters[i].repeat(test_warp_inters[i].size()[0], 1, 1, 1)
        )
    # 结果图像重建
    texture_result, warp_result, result, _ = decoders(texture_code_interpolation_result,
                                                      warp_code_interpolation_result,
                                                      texture_inter_interpolation_result,
                                                      warp_inter_interpolation_result, base)
    return result
Exemplo n.º 3
0
    decoders,
    torch.load(os.path.join(model_path, "decoders.pth"), map_location=device))

img_dir = "/home/qianqianjun/桌面/杨洋男"
names = os.listdir(img_dir)
imgs_path = [os.path.join(img_dir, name) for name in names]
batch_data = batchDataTransform(torch.tensor([
    cv2.cvtColor(
        cv2.resize(cv2.imread(img_path),
                   (parm.imgSize, parm.imgSize)), cv2.COLOR_BGR2RGB)
    for img_path in imgs_path[:9]
],
                                             dtype=torch.float32),
                                channel=3)
base = getBaseGrid(imgSize=parm.imgSize,
                   Inbatch=True,
                   batchSize=batch_data.shape[0])
if parm.useCuda:
    batch_data = batch_data.cuda()
    encoders = encoders.cuda()
    decoders = decoders.cuda()
    base = base.cuda()

print("开始")
import time

start = time.time()
z, zI, zW, inters_t, inters_w = encoders(batch_data)
texture, warp, out, _ = decoders(zI, zW, inters_t, inters_w, base)
end = time.time()
print(end - start)
Exemplo n.º 4
0
import torch.nn as nn
model_path = "../CelebA_out/2020-03-28_23:13:37/checkpoints"
attr_file_path = "/home/qianqianjun/CODE/DataSets/CelebA/Anno/list_attr_celeba.txt"
image_data_path = "/home/qianqianjun/CODE/DataSets/DaeDatasets"
encoders = Encoders(parm)
decoders = Decoders(parm)
device = torch.device("cpu")
nn.Module.load_state_dict(
    encoders,
    torch.load(os.path.join(model_path, "encoders.pth"), map_location=device))
nn.Module.load_state_dict(
    decoders,
    torch.load(os.path.join(model_path, "decoders.pth"), map_location=device))

selecter = ImgSeleter(attr_file_path, image_data_path)
imgs = selecter.getImagesWithoutAttribute("Mustache", ["Male"],
                                          amount=207,
                                          shuffle=False)[200:]

tensor = batchDataTransform(torch.tensor(imgs, dtype=torch.float32), channel=3)

z, zI, zW, inters_I, inters_W = encoders(tensor)
base = getBaseGrid(parm.imgSize, True, tensor.shape[0])
texture, res_warp, out, warp = decoders(zI, zW, inters_I, inters_W, base)

result = torch.cat([tensor, texture, out], dim=0)
saveIntermediateImage(result.data.clone(),
                      n_sample=21,
                      nrow=7,
                      filename="重建结果",
                      output_dir="out")
Exemplo n.º 5
0
texture_inter_interpolation_result=[]
for i in range(len(test_texture_inters)):
    texture_inter_interpolation_result.append(
        test_texture_inters[i] + lambda_texture_inter_out[i] *
        texture_inters[i].repeat(test_texture_inters[i].size()[0], 1, 1, 1)
    )
### 变形信息插值
warp_code_interpolation_result= test_warp_code + lambda_deepest_warp * warp_code.unsqueeze(0).repeat(test_warp_code.size()[0], 1)
warp_inter_interpolation_result=[]
for i in range(len(test_warp_inters)):
    warp_inter_interpolation_result.append(
        test_warp_inters[i] + lambda_warp_inter_out[i] *
        warp_inters[i].repeat(test_warp_inters[i].size()[0], 1, 1, 1)
    )
# 结果图像重建
base=getBaseGrid(parm.imgSize,True,batch_data.size()[0])
if parm.useCuda:
    base=base.cuda()

texture_result,warp_result,result,_=decoders(texture_code_interpolation_result,
                                             warp_code_interpolation_result,
                                             texture_inter_interpolation_result,
                                             warp_inter_interpolation_result,base)


result1=torch.cat([batch_data[:7],result[:7]],dim=0)
result2=torch.cat([batch_data[7:],result[7:]],dim=0)
res=torch.cat([result1,result2],dim=0)
saveIntermediateImage(
    img_list=res.data.clone(),
    output_dir="out",