Skip to content

Instantly share code, notes, and snippets.

View anderzzz's full-sized avatar
🌅
Robotu, robotu, compute!

Anders Ohrn anderzzz

🌅
Robotu, robotu, compute!
  • Switzerland
View GitHub Profile
class MemoryBank(object):
'''Memory bank
Args:
n_vectors (int): Number of vectors the memory bank should hold
dim_vector (int): Dimension of the vectors the memory bank should hold
memory_mixing_rate (float, optional): Fraction of new vector to add to currently stored vector. The value
should be between 0.0 and 1.0, the greater the value the more rapid the update. The mixing rate can be
set during calling `update_memory`.
def forward(self, codes, indices):
'''Forward pass for the local aggregation loss module'''
assert codes.shape[0] == len(indices)
codes = codes.type(torch.DoubleTensor)
code_data = normalize(codes.detach().numpy(), axis=1)
# Compute and collect arrays of indices that define the constants in the loss function. Note that
# no gradients are computed for these data values in backward pass
self.memory_bank.update_memory(code_data, indices)
@anderzzz
anderzzz / LALoss.py
Created November 3, 2020 10:27
Init of Local Aggregation Loss
import torch
from torch import nn
import torch.nn.functional as F
import numpy as np
from sklearn.neighbors import NearestNeighbors
from sklearn.cluster import KMeans
from sklearn.preprocessing import normalize
from scipy.spatial.distance import cosine as cosine_distance
@anderzzz
anderzzz / ae.py
Created October 30, 2020 15:03
auto encoder basic methods
class AutoEncoderVGG(nn.Module):
'''Auto-Encoder based on the VGG-16 with batch normalization template model. The class is comprised of
an encoder and a decoder.
Args:
pretrained_params (bool, optional): If the network should be populated with pre-trained VGG parameters.
Defaults to True.
'''
channels_in = EncoderVGG.channels_in
@anderzzz
anderzzz / decoder_forward.py
Created October 30, 2020 14:56
Forward method for decoder VGG
def forward(self, x, pool_indices):
'''Execute the decoder on the code tensor input
Args:
x (Tensor): code tensor obtained from encoder
pool_indices (list): Pool indices Pytorch tensors in order the pooling modules in the encoder
Returns:
x (Tensor): decoded image tensor
@anderzzz
anderzzz / decoder_init.py
Created October 30, 2020 14:41
Initialization part of decoder based on VGG
class DecoderVGG(nn.Module):
'''Decoder of code based on the architecture of VGG-16 with batch normalization.
Args:
encoder: The encoder instance of `EncoderVGG` that is to be inverted into a decoder
'''
channels_in = EncoderVGG.channels_code
channels_out = 3
@anderzzz
anderzzz / encode_forward.py
Created October 30, 2020 14:27
Forward method for encoder
def forward(self, x):
'''Execute the encoder on the image input
Args:
x (Tensor): image tensor
Returns:
x_code (Tensor): code tensor
pool_indices (list): Pool indices tensors in order of the pooling modules
@anderzzz
anderzzz / _encodify.py
Created October 30, 2020 14:23
Encodification method to handle pooling indices
def _encodify_(self, encoder):
'''Create list of modules for encoder based on the architecture in VGG template model.
In the encoder-decoder architecture, the unpooling operations in the decoder require pooling
indices from the corresponding pooling operation in the encoder. In VGG template, these indices
are not returned. Hence the need for this method to extent the pooling operations.
Args:
encoder : the template VGG model
@anderzzz
anderzzz / encoder_init.py
Created October 30, 2020 14:15
First part of encoder based on VGG16
import torch
from torch import nn
from torchvision import models
class EncoderVGG(nn.Module):
'''Encoder of image based on the architecture of VGG-16 with batch normalization.
Args:
pretrained_params (bool, optional): If the network should be populated with pre-trained VGG parameters.
Defaults to True.
@anderzzz
anderzzz / retransforms.py
Created October 20, 2020 08:08
Pre-processing classes
class StandardTransform(object):
'''Standard Image Transforms, typically instantiated and provided to the DataSet class
'''
def __init__(self, min_dim=299, to_tensor=True, square=False,
normalize=True, norm_mean=[0.485, 0.456, 0.406], norm_std=[0.229, 0.224, 0.225]):
self.transforms = []
self.transforms.append(transforms.ToPILImage())
self.transforms.append(transforms.Resize(min_dim))