text
stringlengths
1
93.6k
self.fid_flag = False
self.log_path = log_path
self.device = device
self.G = G
self.z_dim = z_dim
self.dim = dim
self.batchsize = batchsize
self.model_dir = model_dir
self.init_writer()
self.net = load_inception_net(parallel=False)
def init_writer(self):
if not os.path.exists(self.log_path):
os.makedirs(self.log_path)
self.f = open(self.log_path + 'metrics.csv', 'w')
fieldnames = ['iter',
'is_mean', 'is_std',
'FID score']
self.writer = csv.DictWriter(self.f, fieldnames=fieldnames)
self.writer.writeheader()
def load_model(self, model_path):
chkpoint = torch.load(model_path)
self.G.load_state_dict(chkpoint['G'])
print('loading model from %s' % model_path)
def get_metrics(self, count):
print('===Iter %d===' % count)
content = {'iter': count}
if self.is_flag:
is_score = self.get_inception_score(batch_num=500)
np.set_printoptions(precision=5)
print('Inception score mean: {}, std: {}'.format(is_score[0], is_score[1]))
content.update({'is_mean': is_score[0],
'is_std': is_score[1]})
if self.fid_flag:
fid_score = self.get_fid_score()
np.set_printoptions(precision=5)
print('FID score: {}'.format(fid_score))
content.update({'FID score': fid_score})
self.writer.writerow(content)
self.f.flush()
def eval_metrics(self, begin, end, step,
is_flag=True, fid_flag=True,
dataname='cifar10'):
print('%d ==> %d, step: %d' %(begin, end, step))
self.is_flag = is_flag
self.fid_flag = fid_flag
if fid_flag:
self.load_fid(dataname=dataname)
with torch.no_grad():
for i in range(begin, end + step, step):
self.load_model(model_path=self.model_dir + '%d.pth' % i)
self.get_metrics(i)
self.f.close()
def generate_data(self):
if self.dim == 3:
z = torch.randn((self.batchsize, self.z_dim, 1, 1), device=self.device)
else:
z = torch.randn((self.batchsize, self.z_dim), device=self.device)
data = self.G(z)
return data
def get_inception_score(self, batch_num, splits_num=10):
# net = inception_v3(pretrained=True, transform_input=False).eval().to(self.device)
resize_module = nn.Upsample(size=(299, 299), mode='bilinear', align_corners=True).to(
self.device)
preds = np.zeros((self.batchsize * batch_num, 1000))
for e in range(batch_num):
imgs = resize_module(self.generate_data())
pred = F.softmax(self.net(imgs), dim=1).data.cpu().numpy()
preds[e * self.batchsize: e * self.batchsize + self.batchsize] = pred
split_score = []
chunk_size = preds.shape[0] // splits_num
for k in range(splits_num):
pred_chunk = preds[k * chunk_size: k * chunk_size + chunk_size, :]
kl_score = pred_chunk * (
np.log(pred_chunk) - np.log(np.expand_dims(np.mean(pred_chunk, 0), 0)))
kl_score = np.mean(np.sum(kl_score, 1))
split_score.append(np.exp(kl_score))
return np.mean(split_score), np.std(split_score)
def load_fid(self, dataname):
# if dataname == 'cifar10':
# stats_path = 'metrics/stats/CIFAR10_inception_moments.npz'
# elif dataname == 'lsun-bedroom':
# stats_path = 'metrics/stats/LSUN-bedroom_inception_moments.npz'
# elif dataname == 'MNIST':
stats_path = 'metrics/stats/%s_inception_moments.npz' % dataname
print('Load stats of %s' % dataname)
f = np.load(stats_path)
self.mu_real, self.sigma_real = f['mu'][:], f['sigma'][:]
self.mu_real = torch.tensor(self.mu_real).float().cuda()
self.sigma_real = torch.tensor(self.sigma_real).float().cuda()
f.close()
def accumulate_activations(self, img_num=50000):
pool, logits = [], []