-
Notifications
You must be signed in to change notification settings - Fork 68
/
compute_FID.py
107 lines (92 loc) · 3.84 KB
/
compute_FID.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import argparse
import os
import numpy as np
import math
import sys
import random
import torchvision.transforms as transforms
from torchvision.utils import save_image
from floorplan_dataset_maps import FloorplanGraphDataset, floorplan_collate_fn
from torch.utils.data import DataLoader
from torchvision import datasets
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.autograd as autograd
import torch
from PIL import Image, ImageDraw
from reconstruct import reconstructFloorplan
import svgwrite
from utils import bb_to_img, bb_to_vec, bb_to_seg, mask_to_bb, remove_junctions, ID_COLOR, bb_to_im_fid
from models import Generator
from collections import defaultdict
import matplotlib.pyplot as plt
import networkx as nx
parser = argparse.ArgumentParser()
parser.add_argument("--n_cpu", type=int, default=16, help="number of cpu threads to use during batch generation")
parser.add_argument("--latent_dim", type=int, default=128, help="dimensionality of the latent space")
parser.add_argument("--batch_size", type=int, default=1, help="size of the batches")
parser.add_argument("--channels", type=int, default=1, help="number of image channels")
parser.add_argument("--num_variations", type=int, default=10, help="number of variations")
parser.add_argument("--exp_folder", type=str, default='exp', help="destination folder")
opt = parser.parse_args()
print(opt)
numb_iters = 200000
exp_name = 'exp_with_graph_global_new'
target_set = 'E'
phase='eval'
checkpoint = './checkpoints/{}_{}_{}.pth'.format(exp_name, target_set, numb_iters)
# Create folder
path_real = './FID/{}_{}/real'.format(exp_name, target_set)
path_fake = './FID/{}_{}/fake'.format(exp_name, target_set)
os.makedirs(path_real, exist_ok=True)
os.makedirs(path_fake, exist_ok=True)
# Initialize generator and discriminator
generator = Generator()
generator.load_state_dict(torch.load(checkpoint))
# Initialize variables
cuda = True if torch.cuda.is_available() else False
if cuda:
generator.cuda()
rooms_path = '/home/nelson/Workspace/autodesk/autodesk/FloorplanDataset/'
# Initialize dataset iterator
fp_dataset_test = FloorplanGraphDataset(rooms_path, transforms.Normalize(mean=[0.5], std=[0.5]), target_set=target_set, split=phase)
fp_loader = torch.utils.data.DataLoader(fp_dataset_test,
batch_size=opt.batch_size,
shuffle=True, collate_fn=floorplan_collate_fn)
# Optimizers
Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor
# ------------
# Vectorize
# ------------
globalIndexReal = 0
globalIndexFake = 0
final_images = []
for i, batch in enumerate(fp_loader):
print(i)
# if i >= 100:
# break
# Unpack batch
mks, nds, eds, nd_to_sample, ed_to_sample = batch
# Configure input
real_mks = Variable(mks.type(Tensor))
given_nds = Variable(nds.type(Tensor))
given_eds = eds
for k in range(opt.num_variations):
# plot images
z = Variable(Tensor(np.random.normal(0, 1, (real_mks.shape[0], opt.latent_dim))))
with torch.no_grad():
gen_mks = generator(z, given_nds, given_eds.cuda())
gen_bbs = np.array([np.array(mask_to_bb(mk)) for mk in gen_mks.detach().cpu()])
real_bbs = np.array([np.array(mask_to_bb(mk)) for mk in real_mks.detach().cpu()])
real_nodes = np.where(given_nds.detach().cpu()==1)[-1]
if k == 0:
real_bbs = real_bbs[np.newaxis, :, :]/32.0
real_im = bb_to_im_fid(real_bbs, real_nodes)
real_im.save('{}/{}.jpg'.format(path_real, globalIndexReal))
globalIndexReal += 1
# draw vector
gen_bbs = gen_bbs[np.newaxis, :, :]/32.0
fake_im = bb_to_im_fid(gen_bbs, real_nodes)
fake_im.save('{}/{}.jpg'.format(path_fake, globalIndexFake))
globalIndexFake += 1