Skip to content

Instantly share code, notes, and snippets.

"""Modified VGG16 to compute perceptual loss.
This class is mostly copied from pytorch/examples.
See, fast_neural_style in https://github.com/pytorch/examples.
"""
import torch
from torchvision import models
class VGG_OUTPUT_STYLE(object):
def __init__(self, relu1_2, relu2_2, relu3_3, relu4_3):
def normalised_squared_euclidean_distance(m_1, m_2):
return torch.mean(torch.square(m_1 - m_2))
def vgg_feature_loss(prediction, target, loss_model):
p_features = loss_model.forward_features(prediction).relu2_2
t_features = loss_model.forward_features(target).relu2_2
return normalised_squared_euclidean_distance(p_features, t_features)
def mge_loss(prediction, target):
x_filter = torch.tensor([[-1, -2, -1],
[0, 0, 0],
[1, 2, 1.0]]).unsqueeze(0).repeat(3, 1, 1).unsqueeze(0).cuda() / 8
y_filter = torch.tensor([[-1, 0, 1],
[-2, 0, 2],
[1, 0, 1.0]]).unsqueeze(0).repeat(3, 1, 1).unsqueeze(0).cuda() / 8
replication_pad = nn.ReplicationPad2d(1)
gx_prediction = F.conv2d(replication_pad(prediction), x_filter)
import torch.nn as nn
import torch.nn.functional as F
from upsample import UpsampleBlock
from pytorchlayer.opt_conv_layer import OptConvLayer
class SuperResolutionModel(nn.Module):
in_channels = 3
out_channels = 3
upsample_block_depth = [4, 4, 4]
import torch
import torch.nn as nn
import torch.nn.functional as F
from pytorchlayer.opt_conv_layer import OptConvLayer
# makes a matrix sparse by padding within: e.g. [1,2,3] -> [0,1,0,2,0,3,0]
def pad_within(x, stride=2):
w = x.new_zeros(stride, stride)
w[0, 0] = 1
return F.conv_transpose2d(x, w.expand(x.size(1), 1, stride, stride), stride=stride, groups=x.size(1))
@Ed-Optalysys
Ed-Optalysys / scrape_img.py
Last active June 29, 2021 08:36
Python & selenium image web scraper
from selenium import webdriver
from PIL import Image
import os
import time
import io
import requests
import hashlib
def fetch_image_urls(query: str, max_links_to_fetch: int, wd: webdriver, sleep_between_interactions: int = 1):
def scroll_to_end(wd):
@Ed-Optalysys
Ed-Optalysys / BayesianConv2d.py
Last active January 27, 2021 14:47
Bayesian CNN Layer in PyTorch
# Bayesian conv 2d implementation, adds dropout to kernel values
class BayesianConv2d(torch.nn.Module):
def __init__(self, in_ch, out_ch, kernel_size=(3, 3), bias=True, stride=1, padding=0, dilation=1, groups=1, drop_rate=0.2):
super(BayesianConv2d, self).__init__()
self.p = 1 - drop_rate
assert(0 <= self.p <= 1)
self.stride = stride
self.dilation = dilation
self.padding = padding
@Ed-Optalysys
Ed-Optalysys / BayesianDropout.py
Created January 27, 2021 13:38
Bayesian Dropout Layer
# Bayesian dropout layer: Applies random or optionally pre defined dropout to input during both training and evaluation
class BayesianDropout(torch.nn.Module):
def __init__(self, drop_rate):
super(BayesianDropout, self).__init__()
self.p = drop_rate
def forward(self, x, mask=None):
if mask is None:
return F.dropout(x, self.p, training=True, inplace=False)