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
| ################################################################# | |
| # This script takes in an input folder of scanned documents # | |
| # and reads these documents, seperates them into topics # | |
| # and outputs raw .txt files into the output folder, seperated # | |
| # by topic # | |
| ################################################################# | |
| import os | |
| from PIL import Image | |
| import base64 |
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 SiameseModel(nn.Module): | |
| ''' | |
| Implementation of Koch et al (2015) "Siamese Neural Networks for One-shot Image Recognition" | |
| args: | |
| in_channels (int): The number of input channels in the input images | |
| distance (function): A function of two vectors that returns the scalar distance between them | |
| ''' |
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
| from torchvision import transforms | |
| from torchvision.datasets import MNIST | |
| import torchvision.models as models | |
| import torch | |
| BATCH_SIZE = 100 | |
| def stack(tensor, times=3): | |
| return(torch.cat([tensor]*times, dim=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
| def predict(self, x, y, threshold=1.0): | |
| ''' | |
| Generate similarity scores for a batch of paired examples | |
| Args: | |
| x (torch.Tensor): An input image (B, C, 105, 105) | |
| y (torch.Tensor): The paired input image (B, C, 105, 105) | |
| threshold (float): The threshold for similarity - set to model margin (default = 1.0), set to None for raw distance outputs | |
| returns: |
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 torch | |
| import numpy as np | |
| class Tester: | |
| ########################## | |
| # # | |
| # Initialization # | |
| # # | |
| ########################## |
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 LabeledContrastiveDataset(Dataset): | |
| """ | |
| Take a folder containing sub-folders of images, where the sub-folder name is the image class, and generate | |
| pairs of images with the same class label for contrastive learning. This procedure fixes the batch size since | |
| every batch contains all classes and every batch element is a unique pairing of each class. | |
| """ | |
| def __init__(self, folder, transforms=None): | |
| labels = os.listdir(folder) |
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 torch | |
| import numpy as np | |
| class Trainer: | |
| ########################## | |
| # # | |
| # Initialization # | |
| # # | |
| ########################## |
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 torch | |
| import numpy as np | |
| class Trainer: | |
| ########################## | |
| # # | |
| # Initialization # |
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: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf | |
| args: | |
| distance (function): A function that returns the distance between two tensors - should be a valid metric over R; default= L2 distance | |
| margin (scalar): The margin value between positive and negative class ; default=1.0 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.