Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

port to python3 #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.pth
*.pyc
launch.json
File renamed without changes
6 changes: 3 additions & 3 deletions model/PieAPPv0pt1_PT.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self,batch_size,num_patches):
self.num_patches = num_patches

def flatten(self,matrix): # takes NxCxHxW input and outputs NxHWC
return matrix.view((self.batch_size*self.num_patches,-1))
return matrix.reshape(int(self.batch_size*self.num_patches),-1)

def compute_features(self,input):
#conv1 -> relu -> conv2 -> relu -> pool2 -> conv3 -> relu
Expand All @@ -57,12 +57,12 @@ def compute_score(self,image_A_patches, image_ref_patches):
diff_coarse = ref_coarse - A_coarse
# per patch score: fc1_score -> relu -> fc2_score
per_patch_score = self.ref_score_subtract(0.01*self.fc2_score(F.relu(self.fc1_score(diff_ms))))
per_patch_score.view((-1,self.num_patches))
per_patch_score.reshape(-1,int(self.num_patches))
# per patch weight: fc1_weight -> relu -> fc2_weight
const = Variable(torch.from_numpy(0.000001*np.ones((1,))).float(), requires_grad=False)
const_cuda = const.cuda()
per_patch_weight = self.fc2_weight(F.relu(self.fc1_weight(diff_coarse)))+const_cuda
per_patch_weight.view((-1,self.num_patches))
per_patch_weight.reshape(-1,int(self.num_patches))
product_val = torch.mul(per_patch_weight,per_patch_score)
dot_product_val = torch.sum(product_val)
norm_factor = torch.sum(per_patch_weight)
Expand Down
15 changes: 9 additions & 6 deletions test_PieAPP_PT.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

######## check for model and download if not present
if not os.path.isfile('weights/PieAPPv0.1.pth'):
print "downloading dataset"
print("downloading dataset")
os.system("bash scripts/download_PieAPPv0.1_PT_weights.sh")
if not os.path.isfile('weights/PieAPPv0.1.pth'):
print "PieAPPv0.1.pth not downloaded"
print("PieAPPv0.1.pth not downloaded")
sys.exit()

######## variables
Expand Down Expand Up @@ -58,7 +58,10 @@

######## initialize the model
PieAPP_net = PieAPP(batch_size,num_patches_per_dim)
PieAPP_net.load_state_dict(torch.load('weights/PieAPPv0.1.pth'))
weights = torch.load('weights/PieAPPv0.1.pth')
# modify ref_score_subtract.weight so instead of have torch.Size([1]) it has torch.Size([1, 1])
weights['ref_score_subtract.weight'] = weights['ref_score_subtract.weight'].unsqueeze(0)
PieAPP_net.load_state_dict(weights)

if use_gpu == 1:
PieAPP_net.cuda()
Expand All @@ -67,8 +70,8 @@
weight_accum = 0.0

# iterate through smaller size sub-images (to prevent memory overload)
for x_iter in range(0, -(-num_x//num_patches)):
for y_iter in range(0, -(-num_y//num_patches)):
for x_iter in range(0, -(-num_x//num_patches_per_dim)):
for y_iter in range(0, -(-num_y//num_patches_per_dim)):
# compute the size of the subimage
if (num_patches_per_dim*(x_iter + 1) >= num_x):
size_slice_cols = cols - x_loc[num_patches_per_dim*x_iter]
Expand Down Expand Up @@ -100,4 +103,4 @@
score_accum += np.sum(np.multiply(curr_err, curr_weights))
weight_accum += np.sum(curr_weights)

print 'PieAPP value of '+args.A_path+ ' with respect to: '+str(score_accum/weight_accum)
print('PieAPP value of '+args.A_path+ ' with respect to: '+str(score_accum/weight_accum))