How to use test_forward method in uiautomator

Best Python code snippet using uiautomator

test_forward.py

Source:test_forward.py Github

copy

Full Screen

...47 + DARKNET_LIB + '?raw=true'48_download(DARKNETLIB_URL, DARKNET_LIB)49LIB = __darknetffi__.dlopen('./' + DARKNET_LIB)5051def test_forward(net):52 '''Test network with given input image on both darknet and tvm'''53 def get_darknet_output(net, img):54 return LIB.network_predict_image(net, img)5556 def get_tvm_output(net, img):57 '''Compute TVM output'''58 dtype = 'float32'59 batch_size = 160 sym, params = frontend.darknet.from_darknet(net, dtype)61 data = np.empty([batch_size, img.c, img.h, img.w], dtype)62 i = 063 for c in range(img.c):64 for h in range(img.h):65 for k in range(img.w):66 data[0][c][h][k] = img.data[i]67 i = i + 16869 target = 'llvm'70 shape_dict = {'data': data.shape}71 #with nnvm.compiler.build_config(opt_level=2):72 graph, library, params = nnvm.compiler.build(sym, target, shape_dict, dtype, params=params)73 ######################################################################74 # Execute on TVM75 # ---------------76 # The process is no different from other examples.77 from tvm.contrib import graph_runtime78 ctx = tvm.cpu(0)79 m = graph_runtime.create(graph, library, ctx)80 # set inputs81 m.set_input('data', tvm.nd.array(data.astype(dtype)))82 m.set_input(**params)83 m.run()84 # get outputs85 out_shape = (net.outputs,)86 tvm_out = m.get_output(0, tvm.nd.empty(out_shape, dtype)).asnumpy()87 return tvm_out8889 test_image = 'dog.jpg'90 img_url = 'https://github.com/siju-samuel/darknet/blob/master/data/' + test_image +'?raw=true'91 _download(img_url, test_image)92 img = LIB.letterbox_image(LIB.load_image_color(test_image.encode('utf-8'), 0, 0), net.w, net.h)93 darknet_output = get_darknet_output(net, img)94 darknet_out = np.zeros(net.outputs, dtype='float32')95 for i in range(net.outputs):96 darknet_out[i] = darknet_output[i]97 tvm_out = get_tvm_output(net, img)98 np.testing.assert_allclose(darknet_out, tvm_out, rtol=1e-3, atol=1e-3)99100def test_forward_extraction():101 '''test extraction model'''102 model_name = 'extraction'103 cfg_name = model_name + '.cfg'104 weights_name = model_name + '.weights'105 cfg_url = 'https://github.com/pjreddie/darknet/blob/master/cfg/' + cfg_name + '?raw=true'106 weights_url = 'http://pjreddie.com/media/files/' + weights_name + '?raw=true'107 _download(cfg_url, cfg_name)108 _download(weights_url, weights_name)109 net = LIB.load_network(cfg_name.encode('utf-8'), weights_name.encode('utf-8'), 0)110 test_forward(net)111 LIB.free_network(net)112113def test_forward_alexnet():114 '''test alexnet model'''115 model_name = 'alexnet'116 cfg_name = model_name + '.cfg'117 weights_name = model_name + '.weights'118 cfg_url = 'https://github.com/pjreddie/darknet/blob/master/cfg/' + cfg_name + '?raw=true'119 weights_url = 'http://pjreddie.com/media/files/' + weights_name + '?raw=true'120 _download(cfg_url, cfg_name)121 _download(weights_url, weights_name)122 net = LIB.load_network(cfg_name.encode('utf-8'), weights_name.encode('utf-8'), 0)123 test_forward(net)124 LIB.free_network(net)125126def test_forward_resnet50():127 '''test resnet50 model'''128 model_name = 'resnet50'129 cfg_name = model_name + '.cfg'130 weights_name = model_name + '.weights'131 cfg_url = 'https://github.com/pjreddie/darknet/blob/master/cfg/' + cfg_name + '?raw=true'132 weights_url = 'http://pjreddie.com/media/files/' + weights_name + '?raw=true'133 _download(cfg_url, cfg_name)134 _download(weights_url, weights_name)135 net = LIB.load_network(cfg_name.encode('utf-8'), weights_name.encode('utf-8'), 0)136 test_forward(net)137 LIB.free_network(net)138139def test_forward_yolo():140 '''test yolo model'''141 model_name = 'yolo'142 cfg_name = model_name + '.cfg'143 weights_name = model_name + '.weights'144 cfg_url = 'https://github.com/pjreddie/darknet/blob/master/cfg/' + cfg_name + '?raw=true'145 weights_url = 'http://pjreddie.com/media/files/' + weights_name + '?raw=true'146 _download(cfg_url, cfg_name)147 _download(weights_url, weights_name)148 net = LIB.load_network(cfg_name.encode('utf-8'), weights_name.encode('utf-8'), 0)149 test_forward(net)150 LIB.free_network(net)151152def test_forward_convolutional():153 '''test convolutional layer'''154 net = LIB.make_network(1)155 layer = LIB.make_convolutional_layer(1, 224, 224, 3, 32, 1, 3, 2, 0, 1, 0, 0, 0, 0)156 net.layers[0] = layer157 net.w = net.h = 224158 LIB.resize_network(net, 224, 224)159 test_forward(net)160 LIB.free_network(net)161162def test_forward_dense():163 '''test fully connected layer'''164 net = LIB.make_network(1)165 layer = LIB.make_connected_layer(1, 75, 20, 1, 0, 0)166 net.layers[0] = layer167 net.w = net.h = 5168 LIB.resize_network(net, 5, 5)169 test_forward(net)170 LIB.free_network(net)171172def test_forward_maxpooling():173 '''test maxpooling layer'''174 net = LIB.make_network(1)175 layer = LIB.make_maxpool_layer(1, 224, 224, 3, 2, 2, 0)176 net.layers[0] = layer177 net.w = net.h = 224178 LIB.resize_network(net, 224, 224)179 test_forward(net)180 LIB.free_network(net)181182def test_forward_avgpooling():183 '''test avgerage pooling layer'''184 net = LIB.make_network(1)185 layer = LIB.make_avgpool_layer(1, 224, 224, 3)186 net.layers[0] = layer187 net.w = net.h = 224188 LIB.resize_network(net, 224, 224)189 test_forward(net)190 LIB.free_network(net)191192def test_forward_batch_norm():193 '''test batch normalization layer'''194 net = LIB.make_network(1)195 layer = LIB.make_convolutional_layer(1, 224, 224, 3, 32, 1, 3, 2, 0, 1, 1, 0, 0, 0)196 for i in range(32):197 layer.rolling_mean[i] = np.random.rand(1)198 layer.rolling_variance[i] = np.random.rand(1)199 net.layers[0] = layer200 net.w = net.h = 224201 LIB.resize_network(net, 224, 224)202 test_forward(net)203 LIB.free_network(net)204205def test_forward_shortcut():206 '''test shortcut layer'''207 net = LIB.make_network(3)208 layer_1 = LIB.make_convolutional_layer(1, 224, 224, 3, 32, 1, 3, 2, 0, 1, 0, 0, 0, 0)209 layer_2 = LIB.make_convolutional_layer(1, 111, 111, 32, 32, 1, 1, 1, 0, 1, 0, 0, 0, 0)210 layer_3 = LIB.make_shortcut_layer(1, 0, 111, 111, 32, 111, 111, 32)211 layer_3.activation = 1212 net.layers[0] = layer_1213 net.layers[1] = layer_2214 net.layers[2] = layer_3215 net.w = net.h = 224216 LIB.resize_network(net, 224, 224)217 test_forward(net)218 LIB.free_network(net)219220def test_forward_reorg():221 '''test reorg layer'''222 net = LIB.make_network(2)223 layer_1 = LIB.make_convolutional_layer(1, 222, 222, 3, 32, 1, 3, 2, 0, 1, 0, 0, 0, 0)224 layer_2 = LIB.make_reorg_layer(1, 110, 110, 32, 2, 0, 0, 0)225 net.layers[0] = layer_1226 net.layers[1] = layer_2227 net.w = net.h = 222228 LIB.resize_network(net, 222, 222)229 test_forward(net)230 LIB.free_network(net)231232def test_forward_region():233 '''test region layer'''234 net = LIB.make_network(2)235 layer_1 = LIB.make_convolutional_layer(1, 224, 224, 3, 8, 1, 3, 2, 0, 1, 0, 0, 0, 0)236 layer_2 = LIB.make_region_layer(1, 111, 111, 2, 2, 1)237 layer_2.softmax = 1238 net.layers[0] = layer_1239 net.layers[1] = layer_2240 net.w = net.h = 224241 LIB.resize_network(net, 224, 224)242 test_forward(net)243 LIB.free_network(net)244245if __name__ == '__main__':246 test_forward_resnet50()247 test_forward_alexnet()248 test_forward_extraction()249 test_forward_yolo()250 test_forward_convolutional()251 test_forward_maxpooling()252 test_forward_avgpooling()253 test_forward_batch_norm()254 test_forward_shortcut()255 test_forward_dense()256 test_forward_reorg() ...

Full Screen

Full Screen

test_vggtransformer.py

Source:test_vggtransformer.py Github

copy

Full Screen

...66class VGGTransformerEncoderTest(TestFairseqEncoderBase):67 def setUp(self):68 super().setUp()69 self.setUpInput(get_dummy_input(T=50, D=80, B=5))70 def test_forward(self):71 print("1. test standard vggtransformer")72 self.setUpEncoder(VGGTransformerEncoder(input_feat_per_channel=80))73 super().test_forward()74 print("2. test vggtransformer with limited right context")75 self.setUpEncoder(76 VGGTransformerEncoder(77 input_feat_per_channel=80, transformer_context=(-1, 5)78 )79 )80 super().test_forward()81 print("3. test vggtransformer with limited left context")82 self.setUpEncoder(83 VGGTransformerEncoder(84 input_feat_per_channel=80, transformer_context=(5, -1)85 )86 )87 super().test_forward()88 print("4. test vggtransformer with limited right context and sampling")89 self.setUpEncoder(90 VGGTransformerEncoder(91 input_feat_per_channel=80,92 transformer_context=(-1, 12),93 transformer_sampling=(2, 2),94 )95 )96 super().test_forward()97 print("5. test vggtransformer with windowed context and sampling")98 self.setUpEncoder(99 VGGTransformerEncoder(100 input_feat_per_channel=80,101 transformer_context=(12, 12),102 transformer_sampling=(2, 2),103 )104 )105class TransformerDecoderTest(TestFairseqDecoderBase):106 def setUp(self):107 super().setUp()108 dict = get_dummy_dictionary(vocab_size=DEFAULT_TEST_VOCAB_SIZE)109 decoder = TransformerDecoder(dict)110 dummy_encoder_output = get_dummy_encoder_output(encoder_out_shape=(50, 5, 256))...

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 uiautomator 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