This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
face_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml') | |
eye_cascade=cv2.CascadeClassifier('haarcascade_eye.xml') | |
cap=cv2.VideoCapture(0) | |
while True: | |
ret,img=cap.read() | |
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
routes = [] | |
#write a function to find path | |
def find_path(node, cities, path, distance): | |
#add way point | |
path.append(node) | |
#calcualte path length | |
if len(path) > 1: | |
distance += cities[path[-2]][node] | |
#if path contains all cities and is not a dead end | |
#add path from the first city |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#preprocessing and loading the dataset | |
class SiameseDataset(): | |
def __init__(self,training_csv=None,training_dir=None,transform=None): | |
# used to prepare the labels and images path | |
self.train_df=pd.read_csv(training_csv) | |
self.train_df.columns =["image1","image2","label"] | |
self.train_dir = training_dir | |
self.transform = transform | |
def __getitem__(self,index): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load the the dataset from raw image folders | |
siamese_dataset = SiameseDataset(training_csv,training_dir, | |
transform=transforms.Compose([transforms.Resize((105,105)), | |
transforms.ToTensor() | |
]) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#create a siamese network | |
class SiameseNetwork(nn.Module): | |
def __init__(self): | |
super(SiameseNetwork, self).__init__() | |
# Setting up the Sequential of CNN Layers | |
self.cnn1 = nn.Sequential( | |
nn.Conv2d(1, 96, kernel_size=11,stride=1), | |
nn.ReLU(inplace=True), | |
nn.LocalResponseNorm(5,alpha=0.0001,beta=0.75,k=2), | |
nn.MaxPool2d(3, stride=2), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Declare Siamese Network | |
net = SiameseNetwork().cuda() | |
# Decalre Loss Function | |
criterion = ContrastiveLoss() | |
# Declare Optimizer | |
optimizer = th.optim.Adam(net.parameters(), lr=1e-3, weight_decay=0.0005) | |
#train the model | |
def train(): | |
loss=[] | |
counter=[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ContrastiveLoss(torch.nn.Module): | |
""" | |
Contrastive loss function. | |
Based on: | |
""" | |
def __init__(self, margin=1.0): | |
super(ContrastiveLoss, self).__init__() | |
self.margin = margin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load the test dataset | |
test_dataset = SiameseDataset(training_csv=testing_csv,training_dir=testing_dir, | |
transform=transforms.Compose([transforms.Resize((105,105)), | |
transforms.ToTensor() | |
]) | |
) | |
test_dataloader = DataLoader(test_dataset,num_workers=6,batch_size=1,shuffle=True) | |
#test the network | |
count=0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
def longestConsecutive(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: int | |
""" | |
nums = set(nums) | |
best = 0 | |
for x in nums: | |
if x-1 not in nums: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Lenet(nn.Module): | |
def __init__(self): | |
super(Lenet,self).__init__() | |
self.tanh = nn.Tanh() | |
self.pool = nn.AvgPool2d(kernel_size=(2,2),stride=(2,2)) | |
self.conv1 = nn.Conv2d(in_channels=1,out_channels=6,kernel_size=(5,5),stride=(1,1)) | |
self.conv2 = nn.Conv2d(in_channels=6,out_channels=16,kernel_size=(5,5),stride=(1,1)) | |
self.conv3 = nn.Conv2d(in_channels=16,out_channels=120,kernel_size=(5,5),stride=(1,1)) | |
self.linear1 = nn.Linear(120,84) | |
self.linear2 = nn.Linear(84,10) |
OlderNewer