in evaluate.py [0:0]
def get_acts_and_preds(files, model, batch_size=50, dims=2048,
cuda=False, verbose=False, name='birds'):
"""Calculates the activations of the pool_3 layer for all images.
Params:
-- files : List of image files paths
-- model : Instance of inception model
-- batch_size : Batch size of images for the model to process at once.
Make sure that the number of samples is a multiple of
the batch size, otherwise some samples are ignored. This
behavior is retained to match the original FID score
implementation.
-- dims : Dimensionality of features returned by Inception
-- cuda : If set to True, use GPU
-- verbose : If set to True and parameter out_step is given, the number
of calculated batches is reported.
-- name : The name of the dataset: for birds we calculate CS only and for creatures we also calculate SDS.
"""
if name == 'birds':
target_set = B_SET
elif name == 'creatures':
target_set = C_SET
model.eval()
if batch_size > len(files):
print(('Warning: batch size is bigger than the data size. '
'Setting batch size to data size'))
batch_size = len(files)
pred_arr = np.empty((len(files), dims))
preds_final_arr = {}
logits_arr = torch.zeros(345).cuda()
for i in tqdm(range(0, len(files), batch_size)):
if verbose:
print('\rPropagating batch %d/%d' % (i + 1, n_batches),
end='', flush=True)
start = i
end = i + batch_size
images = np.array([imread(str(f)).astype(np.float32) for f in files[start:end]])
images = images/255.
images = 1-images
images[images<0.1] = 0
# Reshape to (n_images, 3, height, width)
if len(images.shape) == 4:
images = images.transpose((0, 3, 1, 2))
elif len(images.shape) == 3:
images = np.expand_dims(images, 1)
batch = torch.from_numpy(images).type(torch.FloatTensor)
if cuda:
batch = batch.cuda()
batch = F.interpolate(batch, size=(299, 299), mode='bilinear', align_corners=False)
# store the model predictions
logits = model.inception(batch)
_, final_preds = torch.max(logits, 1)
logits = F.softmax(logits, 1)
for logit, final_pred in zip(logits, final_preds):
logits_arr += logit
pred_class = ID2CLASS[final_pred.item()]
if pred_class in preds_final_arr:
preds_final_arr[pred_class] += 1
else:
preds_final_arr[pred_class] = 1
pred = model(batch)[0]
# If model output is not scalar, apply global spatial average pooling.
# This happens if you choose a dimensionality not equal 2048.
if pred.size(2) != 1 or pred.size(3) != 1:
pred = adaptive_avg_pool2d(pred, output_size=(1, 1))
pred_arr[start:end] = pred.cpu().data.numpy().reshape(pred.size(0), -1)
# calculate CS and SDS
characteristic_count = 0.
total_count = 0.
for class_name in preds_final_arr:
total_count += preds_final_arr[class_name]
if class_name not in target_set:
continue
characteristic_count += preds_final_arr[class_name]
CS = characteristic_count/total_count
probs_all = logits_arr / total_count
# import ipdb;ipdb.set_trace()
if name == 'creatures':
C_prob = sum([probs_all[cl_id].item() for cl_id in range(345) if ID2CLASS[cl_id] in C_SET])
CCS = sum([-probs_all[cl_id].item()*np.log(probs_all[cl_id].item()/C_prob) for cl_id in range(345) if ID2CLASS[cl_id] in C_SET])
else:
CCS = 0.
if verbose:
print(' done')
return pred_arr, CS, CCS