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
| 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
| 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
| 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
| ################################################################# | |
| # 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
| def topic_label(ldamodel, topicnum): | |
| alltopics = ldamodel.show_topics(formatted=False) | |
| topic = alltopics[topicnum] | |
| topic = dict(topic[1]) | |
| return(max(topic, key=lambda key: topic[key])) |
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 find_topic(textlist, dictionary, lda): | |
| ''' | |
| https://stackoverflow.com/questions/16262016/how-to-predict-the-topic-of-a-new-query-using-a-trained-lda-model-using-gensim | |
| For each query ( document in the test file) , tokenize the | |
| query, create a feature vector just like how it was done while training | |
| and create text_corpus | |
| ''' | |
| text_corpus = [] |
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 gensim import corpora, models, similarities | |
| from gensim.parsing.preprocessing import remove_stopwords, preprocess_string | |
| def preprocess(document): | |
| clean = remove_stopwords(document) | |
| clean = preprocess_string(document) | |
| return(clean) | |
| def run_lda(textlist, | |
| num_topics=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
| def read_and_return(foldername, fileext='.txt'): | |
| ''' | |
| Read all text files with fileext from foldername, and place them into a list of tuples as | |
| [(filename, text), ... , (filename, text)] | |
| ''' | |
| allfiles = os.listdir(foldername) | |
| allfiles = [os.path.join(foldername, f) for f in allfiles if f.endswith(fileext)] | |
| alltext = [] | |
| for filename in allfiles: | |
| with open(filename, 'r') as f: |
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
| ''' | |
| Read in a list of scanned images (as .png files > 50x50px) and output a set of .txt files containing the text content of these scans | |
| ''' | |
| from functions import preprocess, image_to_text | |
| from PIL import Image | |
| import os | |
| from spellchecker import SpellChecker |