Skip to content

Instantly share code, notes, and snippets.

View ShairozS's full-sized avatar

Shairoz Sohail ShairozS

View GitHub Profile
import torch
import numpy as np
class Tester:
##########################
# #
# Initialization #
# #
##########################
@ShairozS
ShairozS / predict.py
Created February 7, 2021 04:28
Predict() method for contrastive model
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:
@ShairozS
ShairozS / generate_embeddings.py
Created December 31, 2020 20:14
Generate embeddings using pretrained VGG16
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))
@ShairozS
ShairozS / SiameseModel.py
Created December 20, 2020 17:39
Basic Siamese Model for Contrastive Learning
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
'''
@ShairozS
ShairozS / main.py
Created August 4, 2020 03:08
Convert scanned documents into text files and organize by topic
#################################################################
# 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
@ShairozS
ShairozS / get_topic_label.py
Created August 4, 2020 00:50
Retrieve the label of a topic on a trained Gensim LDA model
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]))
@ShairozS
ShairozS / find_document_topic.py
Created August 4, 2020 00:47
Use a trained Gensim LDA model to classify the topics in a list of text
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 = []
@ShairozS
ShairozS / preprocess_for_lda.py
Created August 3, 2020 23:24
Preprocess Text for LDA Modeling
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,
@ShairozS
ShairozS / read_and_return_text.py
Created August 3, 2020 23:12
Read a folder of text documents
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:
'''
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