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
| # adapted from: https://github.com/lucidrains/vit-pytorch/blob/main/vit_pytorch/vit.py | |
| import torch | |
| import torch.nn.functional as F | |
| from torch import nn | |
| from torch.utils import checkpoint | |
| from einops import rearrange, repeat | |
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
| using Microsoft.Extensions.DependencyModel; | |
| using System; | |
| using System.Linq; | |
| using System.Runtime.Loader; | |
| using System.Reflection; | |
| using System.Collections.Generic; | |
| namespace ConsoleApp1 | |
| { | |
| class Program |
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
| --[[ | |
| torch.chunk2() | |
| returns a table with nChunk entries, even in the case that the tensor has < nChunk entries in the specified dimension. | |
| Behaviour of the originial torch.chunk() function: | |
| th> torch.rand(11):chunk(5) | |
| { | |
| 1 : DoubleTensor - size: 3 | |
| 2 : DoubleTensor - size: 3 |
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
| function YUV422toYUV(input, w, h) | |
| local yuv_result = torch.ByteTensor(3, h, w) | |
| local x = input:view(h, w, 2) | |
| yuv_result[1] = x[{{},{},1}] -- copy Y part | |
| local u = yuv_result[2] | |
| local v = yuv_result[3] | |
| local uv = x[{{},{},2}]:reshape(h,w/2,2) | |
| local u_ = uv[{{},{},1}] | |
| local v_ = uv[{{},{},2}] | |
| u:view(h,w/2, 2)[{{},{},1}] = u_ |
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
| local function pca(X) | |
| -- PCA ------------------------------------------------------------------------- | |
| -- X is m x n | |
| local mean = torch.mean(X, 1) -- 1 x n | |
| local m = X:size(1) | |
| local Xm = X - torch.ones(m, 1) * mean | |
| Xm:div(math.sqrt(m - 1)) | |
| local v,s,_ = torch.svd(Xm:t()) | |
| s:cmul(s) -- n | |
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
| require 'cunn' | |
| --test cuda & non-cuda version | |
| function testVolumetricFullConvolution() | |
| local input = torch.rand(1,2,10,10,10) * 2 - 1 | |
| local a = nn.VolumetricFullConvolution(2,3, 3,3,3, 1,1,1) | |
| local b = nn.VolumetricFullConvolution(2,3, 3,3,3, 1,1,1) | |
| b:cuda() | |
| b.weight = a.weight:cuda() |
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
| x = torch.DoubleTensor() | |
| y = torch.FloatTensor() | |
| function testfunc(a,b) | |
| return a + b | |
| end | |
| f = {} | |
| f[x:type()] = {} | |
| f[y:type()] = {} |
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
| local SpatialUnpooling, parent = torch.class('nn.SpatialUnpooling', 'nn.Module') | |
| function SpatialUnpooling:__init(kW, kH, dW, dH, padW, padH) | |
| parent.__init(self) | |
| self.dW = dW or kW | |
| self.dH = dH or kH | |
| self.padW = padW or 0 | |
| self.padH = padH or 0 | |
| self.indices = torch.LongTensor() | |
| self._indexTensor = torch.LongTensor() |
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
| local SpatialUnpooling, parent = torch.class('nn.SpatialUnpooling', 'nn.Module') | |
| function SpatialUnpooling:__init(kW, kH, dW, dH, padW, padH) | |
| parent.__init(self) | |
| self.dW = dW or kW | |
| self.dH = dH or kH | |
| self.padW = padW or 0 | |
| self.padH = padH or 0 | |
| self.indices = torch.LongTensor() | |
| self._indexTensor = torch.LongTensor() |
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
| require 'nn' | |
| x = torch.rand(1,5,5) | |
| a = nn.SpatialConvolution(1,1,3,3) | |
| a.bias:zero() | |
| ay1 =torch.xcorr2(x,a.weight,'V') | |
| ay2 = a:forward(x) | |
| b = nn.SpatialFullConvolution(1,1,3,3) | |
| b.bias:zero() |