How to use filter_type method in localstack

Best Python code snippet using localstack_python

Denoising_ResNet.py

Source:Denoising_ResNet.py Github

copy

Full Screen

1"""2ResNet with Denoising Blocks in PyTorch on CIAFR10.34Reference:5[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun6 Deep Residual Learning for Image Recognition. arXiv:1512.033857[2] Cihang Xie, Yuxin Wu, Laurens van der Maaten, Alan Yuille, Kaiming He8 Feature Denoising for Improving Adversarial Robustness. arXiv:1812.03411910Explanation:11[1] If 'whether_denoising' is True, a ResNet with two denoising blocks will be created.12 In contrast 'whether_denoising' is False, a normal ResNet will be created.13[2] 'filter_type' decides which denoising operation the denoising block will apply.14 Now it includes 'Median_Filter' 'Mean_Filter' and 'Gaussian_Filter'.15[3] 'ksize' means the kernel size of the filter.16"""1718import torch19import torch.nn as nn20import torch.nn.functional as F21import kornia2223class BasicBlock(nn.Module):24 expansion = 12526 def __init__(self, in_planes, planes, stride=1):27 super(BasicBlock, self).__init__()28 self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)29 self.bn1 = nn.BatchNorm2d(planes)30 self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)31 self.bn2 = nn.BatchNorm2d(planes)3233 self.shortcut = nn.Sequential()34 if stride != 1 or in_planes != self.expansion*planes:35 self.shortcut = nn.Sequential(36 nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),37 nn.BatchNorm2d(self.expansion*planes)38 )3940 def forward(self, x):41 out = F.relu(self.bn1(self.conv1(x)))42 out = self.bn2(self.conv2(out))43 out += self.shortcut(x)44 out = F.relu(out)45 return out4647class Bottleneck(nn.Module):48 expansion = 44950 def __init__(self, in_planes, planes, stride=1):51 super(Bottleneck, self).__init__()52 self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)53 self.bn1 = nn.BatchNorm2d(planes)54 self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)55 self.bn2 = nn.BatchNorm2d(planes)56 self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=False)57 self.bn3 = nn.BatchNorm2d(self.expansion*planes)5859 self.shortcut = nn.Sequential()60 if stride != 1 or in_planes != self.expansion*planes:61 self.shortcut = nn.Sequential(62 nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),63 nn.BatchNorm2d(self.expansion*planes)64 )6566 def forward(self, x):67 out = F.relu(self.bn1(self.conv1(x)))68 out = F.relu(self.bn2(self.conv2(out)))69 out = self.bn3(self.conv3(out))70 out += self.shortcut(x)71 out = F.relu(out)72 return out7374class denoising_block(nn.Module):75 def __init__(self, in_planes, ksize, filter_type):76 super(denoising_block, self).__init__()77 self.in_planes = in_planes78 self.ksize = ksize79 self.filter_type = filter_type80 self.conv = nn.Conv2d(in_channels=in_planes, out_channels=in_planes, kernel_size=1, stride=1, padding=0)8182 def forward(self, x):83 if self.filter_type == 'Median_Filter':84 x_denoised = kornia.median_blur(x, (self.ksize, self.ksize))85 elif self.filter_type == 'Mean_Filter':86 x_denoised = kornia.box_blur(x, (self.ksize, self.ksize))87 elif self.filter_type == 'Gaussian_Filter':88 x_denoised = kornia.gaussian_blur2d(x, (self.ksize, self.ksize), (0.3 * ((x.shape[3] - 1) * 0.5 - 1) + 0.8, 0.3 * ((x.shape[2] - 1) * 0.5 - 1) + 0.8))89 new_x = x + self.conv(x_denoised)90 return new_x9192class ResNet(nn.Module):93 def __init__(self, block, num_blocks, num_classes=10, whether_denoising=False, filter_type="Mean_Filter", ksize=3):94 super(ResNet, self).__init__()95 if whether_denoising:96 self.denoising_block1 = denoising_block(in_planes=64, ksize=ksize, filter_type=filter_type)97 self.denoising_block2 = denoising_block(in_planes=64, ksize=ksize, filter_type=filter_type)98 self.whether_denoising = whether_denoising99 self.in_planes = 64100 self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)101 self.bn1 = nn.BatchNorm2d(64)102 self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)103 self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)104 self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)105 self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)106 self.linear = nn.Linear(512*block.expansion, num_classes)107108 def _make_layer(self, block, planes, num_blocks, stride):109 strides = [stride] + [1]*(num_blocks-1)110 layers = []111 for stride in strides:112 layers.append(block(self.in_planes, planes, stride))113 self.in_planes = planes * block.expansion114 return nn.Sequential(*layers)115116 def forward(self, x):117 out = F.relu(self.bn1(self.conv1(x)))118 if self.whether_denoising:119 out = self.denoising_block1(out)120 out = self.layer1(out)121 if self.whether_denoising:122 out = self.denoising_block2(out)123 out = self.layer2(out)124 out = self.layer3(out)125 out = self.layer4(out)126 out = F.avg_pool2d(out, 4)127 out = out.view(out.size(0), -1)128 out = self.linear(out)129 return out130131132def ResNet18(whether_denoising=False, filter_type="Mean_Filter", ksize=3):133 return ResNet(BasicBlock, [2, 2, 2, 2], num_classes=10, whether_denoising=whether_denoising, filter_type=filter_type, ksize=ksize)134135def ResNet34(whether_denoising=False, filter_type="Mean_Filter", ksize=3):136 return ResNet(BasicBlock, [3, 4, 6, 3], num_classes=10, whether_denoising=whether_denoising, filter_type=filter_type, ksize=ksize)137138def ResNet50(whether_denoising=False, filter_type="Mean_Filter", ksize=3):139 return ResNet(Bottleneck, [3, 4, 6, 3], num_classes=10, whether_denoising=whether_denoising, filter_type=filter_type, ksize=ksize)140141def ResNet101(whether_denoising=False, filter_type="Mean_Filter", ksize=3):142 return ResNet(Bottleneck, [3, 4, 23, 3], num_classes=10, whether_denoising=whether_denoising, filter_type=filter_type, ksize=ksize)143144def ResNet152(whether_denoising=False, filter_type="Mean_Filter", ksize=3):145 return ResNet(Bottleneck, [3, 8, 36, 3], num_classes=10, whether_denoising=whether_denoising, filter_type=filter_type, ksize=ksize)146147def test():148 from torch.autograd import Variable149 net = ResNet18(whether_denoising=True, filter_type="Median_Filter", ksize=3)150 x = Variable(torch.randn(1, 3, 32, 32), requires_grad=True)151 y = net(x)152 print(y)153154def denoising_block_test(filter_type):155 from torch.autograd import Variable156 denoising_block1 = denoising_block(in_planes=32, ksize=3, filter_type=filter_type)157 x = Variable(torch.ones(2, 64, 32, 32), requires_grad=True)158 y = denoising_block1(x)159 print(y)160 y.backward(x)161 print(x.grad)162163# test() ...

Full Screen

Full Screen

test_filters.py

Source:test_filters.py Github

copy

Full Screen

1import dask.array as da2import numpy as np3import pytest4from mps_motion_tracking import filters5@pytest.mark.parametrize(6 "filter_type, size, sigma",7 [8 (filters.Filters.median, 3, None),9 (filters.Filters.gaussian, None, 1),10 ],11)12def test_filter_vectors_numpy(filter_type, size, sigma):13 shape = (10, 9, 2)14 np.random.seed(1)15 vectors = 10 * np.ones(shape) + np.random.random(shape)16 filtered_vectors = filters.filter_vectors(17 vectors,18 filter_type=filter_type,19 size=size,20 sigma=sigma,21 )22 assert filtered_vectors.shape == shape23 assert 0 < np.abs(filtered_vectors - vectors).max() < 124@pytest.mark.parametrize(25 "filter_type, size, sigma",26 [27 (filters.Filters.median, None, None),28 (filters.Filters.gaussian, None, None),29 (filters.Filters.median, 0, None),30 (filters.Filters.gaussian, None, 0),31 (filters.Filters.median, None, 3),32 (filters.Filters.gaussian, 1, None),33 ("somefilter", None, None),34 ],35)36def test_filter_with_0_do_nothing(filter_type, size, sigma):37 shape = (10, 9, 2)38 np.random.seed(1)39 vectors = 10 * np.ones(shape) + np.random.random(shape)40 filtered_vectors = filters.filter_vectors(41 vectors,42 filter_type=filter_type,43 size=size,44 sigma=sigma,45 )46 assert filtered_vectors.shape == shape47 assert np.isclose(filtered_vectors, vectors).all()48@pytest.mark.parametrize(49 "filter_type, size, sigma",50 [51 (filters.Filters.median, 3, None),52 (filters.Filters.gaussian, None, 1),53 ],54)55def test_filter_vectors_dask(filter_type, size, sigma):56 shape = (10, 9, 2)57 np.random.seed(1)58 vectors = 10 * da.ones(shape) + da.random.random(shape)59 filtered_vectors = filters.filter_vectors(60 vectors,61 filter_type=filter_type,62 size=size,63 sigma=sigma,64 )65 assert filtered_vectors.shape == shape66 assert 0 < da.absolute(filtered_vectors - vectors).max().compute() < 167@pytest.mark.parametrize(68 "filter_type, size, sigma",69 [70 (filters.Filters.median, 3, None),71 (filters.Filters.gaussian, None, 1),72 ],73)74def test_filter_vectors_par_numpy(filter_type, size, sigma):75 shape = (10, 10, 5, 2)76 np.random.seed(1)77 vectors = 10 * np.ones(shape) + np.random.random(shape)78 filtered_vectors = filters.filter_vectors_par(79 vectors,80 filter_type=filter_type,81 size=size,82 sigma=sigma,83 )84 assert filtered_vectors.shape == shape...

Full Screen

Full Screen

filter_by_type.py

Source:filter_by_type.py Github

copy

Full Screen

...3# String4# If the string is greater than or equal to 50 characters print "Long sentence." If the string is shorter than 50 characters print "Short sentence."5# List6# If the length of the list is greater than or equal to 10 print "Big list!" If the list has fewer than 10 values print "Short list."7def filter_type(el):8 if isinstance(el, int):9 print str(el), "is a number"10 if el >= 100:11 print "that's a big number"12 else:13 print "that's a small number"14 15 if isinstance(el, str):16 print str(el), "is a string"17 if len(el) >= 50:18 print "long sentence"19 else:20 print "short sentence"21 if isinstance(el, list):22 print str(el), "is a list"23 if len(el) >= 10:24 print "that's a long list"25 else:26 print "that's a short list"27 28filter_type(45)29filter_type(100)30filter_type(455)31filter_type(0)32filter_type(-23)33filter_type("Rubber baby buggy bumpers")34filter_type("Experience is simply the name we give our mistakes")35filter_type("Tell me and I forget. Teach me and I remember. Involve me and I learn.")36filter_type("")37filter_type([1,7,4,21])38filter_type([3,5,7,34,3,2,113,65,8,89])39filter_type([4,34,22,68,9,13,3,5,7,9,2,12,45,923])40filter_type([])...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful