-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsimilarity_ops.py
20 lines (19 loc) · 1016 Bytes
/
similarity_ops.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import numpy as np
from numpy import matlib as mb # matlib must be imported separately
def compute_spatial_similarity(conv1,conv2):
"""
Takes in the last convolutional layer from two images, computes the pooled output
feature, and then generates the spatial similarity map for both images.
"""
pool1 = np.mean(conv1,axis=0)
pool2 = np.mean(conv2,axis=0)
out_sz = (int(np.sqrt(conv1.shape[0])),int(np.sqrt(conv1.shape[0])))
conv1_normed = conv1 / np.linalg.norm(pool1) / conv1.shape[0]
conv2_normed = conv2 / np.linalg.norm(pool2) / conv2.shape[0]
im_similarity = np.zeros((conv1_normed.shape[0],conv1_normed.shape[0]))
for zz in range(conv1_normed.shape[0]):
repPx = mb.repmat(conv1_normed[zz,:],conv1_normed.shape[0],1)
im_similarity[zz,:] = np.multiply(repPx,conv2_normed).sum(axis=1)
similarity1 = np.reshape(np.sum(im_similarity,axis=1),out_sz)
similarity2 = np.reshape(np.sum(im_similarity,axis=0),out_sz)
return similarity1, similarity2