Skip to content

Instantly share code, notes, and snippets.

View ShairozS's full-sized avatar

Shairoz Sohail ShairozS

View GitHub Profile
@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 / 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 / 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 / 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:
import torch
import numpy as np
class Tester:
##########################
# #
# Initialization #
# #
##########################
@ShairozS
ShairozS / labeled_contrastive_dataset.py
Created April 13, 2021 01:31
Labeled Contrastive Dataset (PyTorch)
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)
@ShairozS
ShairozS / trainers.py
Created April 13, 2021 19:03
Generic trainer for Pytorch model
import torch
import numpy as np
class Trainer:
##########################
# #
# Initialization #
# #
##########################
@ShairozS
ShairozS / trainers.py
Created April 13, 2021 19:30
Trainer for Pytorch contrastive model
import torch
import numpy as np
class Trainer:
##########################
# #
# Initialization #
@ShairozS
ShairozS / losses.py
Created April 13, 2021 19:52
Contrastive loss
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
@ShairozS
ShairozS / train_mnist.ipynb
Created April 15, 2021 22:18
Train MNIST (Contrastive Learning)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.