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
| VGG_types = { | |
| 'VGG11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], | |
| 'VGG13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], | |
| 'VGG16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'], | |
| 'VGG19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'], | |
| } | |
| class VGG_net(nn.Module): | |
| def __init__(self, in_channels=3, num_classes=1000): | |
| super(VGG_net, self).__init__() | |
| self.in_channels = in_channels |
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 AlexNet(nn.Module): | |
| def __init__(self): | |
| super(AlexNet, self).__init__() | |
| self.features = nn.Sequential( | |
| nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2), | |
| nn.ReLU(), | |
| nn.LocalResponseNorm(size=5,alpha=0.0001,beta=0.75,k=2), | |
| nn.MaxPool2d(kernel_size=3, stride=2), | |
| nn.Conv2d(64, 192, kernel_size=5, padding=2), | |
| nn.ReLU(), |
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) |
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
| # 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 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
| # 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
| #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
| # 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
| #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): |