Skip to content

Commit

Permalink
retrain
Browse files Browse the repository at this point in the history
  • Loading branch information
jiweeo committed Dec 6, 2017
1 parent 1b52500 commit 2339d1c
Show file tree
Hide file tree
Showing 33 changed files with 198 additions and 77 deletions.
68 changes: 49 additions & 19 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 38 additions & 11 deletions SiameseNetworkDataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,52 @@
import torch
import numpy as np
from PIL import Image

import random
class SiameseNetworkDataset(Dataset):
def __init__(self, root_dir, label, transform=None):
self.root_dir = root_dir
self.label = label
def __init__(self, imageFolderDataset, transform=None):
# self.root_dir = root_dir
# self.label = label
# self.transform=transform
self.imageFolderDataset=imageFolderDataset
self.transform=transform
def __getitem__(self, index):

img0 = Image.open(self.root_dir + '/pair1/' + str(index) + '.bmp')
img1 = Image.open(self.root_dir + '/pair2/' + str(index) + '.bmp')
# img0 = Image.open(self.root_dir + '/pair1/' + str(index) + '.bmp')
# img1 = Image.open(self.root_dir + '/pair2/' + str(index) + '.bmp')
#
# img0=img0.convert("L")
# img1=img1.convert("L")
#
# if self.transform is not None:
# img0=self.transform(img0)
# img1=self.transform(img1)
#
# return img0,img1, torch.from_numpy(np.array([int(self.label[index])],dtype=np.float32))

img0_tuple=random.choice(self.imageFolderDataset.imgs)
should_get_same_patch=random.randint(0,1)
image0=Image.open(img0_tuple[0])
if should_get_same_patch:
image1=image0
else:
img1_tuple = random.choice(self.imageFolderDataset.imgs)
while img1_tuple[0]!=img0_tuple[0]:
break
image1=Image.open(img1_tuple[0])

x=random.randint(0,359)
image1=image1.rotate(x)

img0=img0.convert("L")
img1=img1.convert("L")
image0 = image0.convert('L')
image1 = image1.convert('L')

if self.transform is not None:
img0=self.transform(img0)
img1=self.transform(img1)
image0=self.transform(image0)
image1=self.transform(image1)
return image0, image1, torch.from_numpy(np.array([should_get_same_patch],dtype=np.float32))

return img0,img1, torch.from_numpy(np.array([int(self.label[index])],dtype=np.float32))
def __len__(self):
return len(self.label)
return len(self.imageFolderDataset.imgs)


24 changes: 13 additions & 11 deletions computeLearnedSIFT.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@
% compute 128-D discriptor

patch = im(locsDoG(i,2)+1-bound:locsDoG(i,2)+bound,locsDoG(i,1)+1-bound:locsDoG(i,1)+bound);
%patch = GaussianPyramid(locsDoG(i,2)+1-bound:locsDoG(i,2)+bound,locsDoG(i,1)+1-bound:locsDoG(i,1)+bound,level);

% compute major gradient
tmp_patch = GaussianPyramid(locsDoG(i,2)+1-patch_half_size:locsDoG(i,2)+patch_half_size,locsDoG(i,1)+1-patch_half_size:locsDoG(i,1)+patch_half_size,level);
tmp_patch = imresize(tmp_patch, [norm_patch_size norm_patch_size]);
g_histogram = computeGradient(tmp_patch);
[~,major_g_index] = max(g_histogram);
%tmp_patch = GaussianPyramid(locsDoG(i,2)+1-patch_half_size:locsDoG(i,2)+patch_half_size,locsDoG(i,1)+1-patch_half_size:locsDoG(i,1)+patch_half_size,level);
%tmp_patch = imresize(tmp_patch, [norm_patch_size norm_patch_size]);
%g_histogram = computeGradient(tmp_patch);
%[~,major_g_index] = max(g_histogram);
% rotate patch
patch = imrotate(patch,-45*(major_g_index-1));
[h,w] = size(patch);
h = floor(h/2);
w = floor(w/2);
patch = patch(h+1-patch_half_size:h+patch_half_size,w+1-patch_half_size:w+patch_half_size);
patch = imresize(patch, [norm_patch_size norm_patch_size]);
patches(end,:,:)=patch;
%patch = imrotate(patch,-45*(major_g_index-1));
%[h,w] = size(patch);
%h = floor(h/2);
%w = floor(w/2);
%patch = patch(h+1-patch_half_size:h+patch_half_size,w+1-patch_half_size:w+patch_half_size);
%patch = imresize(patch, [norm_patch_size norm_patch_size]);
patches(end,:,:)=imresize(patch,[64,64]);
patches=[patches;zeros(1,64,64)];
end
end
Expand Down
19 changes: 12 additions & 7 deletions compute_desc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ def compute_desc():
net=SiameseNetwork()

net.load_state_dict(torch.load('model.pt'))
transform=transforms.ToTensor()
net.train(False)
# transform=transforms.ToTensor()
imgs=scipy.io.loadmat('patch.mat')['patches']
imgs=imgs.reshape(imgs.shape[0],1,imgs.shape[1],imgs.shape[2])
imgs=np.delete(imgs,[imgs.shape[0]-1],axis=0)
X=torch.from_numpy(imgs)
_,output=net(Variable(X.float()),Variable(X.float()))
N=len(imgs)
mat=np.zeros([N-1,128])
for i in range(N-1):
img=imgs[i]
img=img.reshape(1,1,img.shape[0],img.shape[1])
X=torch.from_numpy(img)

#print 'time:'+str(end-start)
mat=output.data.numpy()
_,output=net(Variable(X.float()),Variable(X.float()))

#print 'time:'+str(end-start)
mat[i]=output.data.numpy()
scipy.io.savemat('learned_desc.mat',mdict={'desc':mat})
# print mat.tolist()
return 0
Expand Down
20 changes: 20 additions & 0 deletions cropdata.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
img=imread('../data/book.jpg');

img=imrotate(img,-90);

img=imresize(img,0.1);

img=rgb2gray(img);

[h,w]=size(img);

patch=zeros(64,64);
counter=0;
for x=1:10:w-63
for y=1:10:h-63
patch=img(y:y+64-1,x:x+64-1);
imwrite(patch,sprintf('book/%d.jpg',counter));
counter=counter+1;
end
end

17 changes: 17 additions & 0 deletions cropdata.m~
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
img=imread('../data/book.jpg');

img=imrotate(img,-90);

img=imresize(img,0.1);

img=rgb2gray(img);

[h,w]=size(img);

patch=zeros(64,64);
for x=1:2:w-63
for y=1:2:h-63
patch=img(y:y+64-1,x:x+64-1);
end
end

Loading

0 comments on commit 2339d1c

Please sign in to comment.