Best Python code snippet using robotframework-appiumlibrary_python
shake_shake.py
Source:shake_shake.py
1# Copyright 2018 The TensorFlow Authors All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14# ==============================================================================15"""Builds the Shake-Shake Model."""16from __future__ import absolute_import17from __future__ import division18from __future__ import print_function19import custom_ops as ops20import tensorflow as tf21def _shake_shake_skip_connection(x, output_filters, stride):22 """Adds a residual connection to the filter x for the shake-shake model."""23 curr_filters = int(x.shape[3])24 if curr_filters == output_filters:25 return x26 stride_spec = ops.stride_arr(stride, stride)27 # Skip path 128 path1 = tf.nn.avg_pool(29 x, [1, 1, 1, 1], stride_spec, 'VALID', data_format='NHWC')30 path1 = ops.conv2d(path1, int(output_filters / 2), 1, scope='path1_conv')31 # Skip path 232 # First pad with 0's then crop33 pad_arr = [[0, 0], [0, 1], [0, 1], [0, 0]]34 path2 = tf.pad(x, pad_arr)[:, 1:, 1:, :]35 concat_axis = 336 path2 = tf.nn.avg_pool(37 path2, [1, 1, 1, 1], stride_spec, 'VALID', data_format='NHWC')38 path2 = ops.conv2d(path2, int(output_filters / 2), 1, scope='path2_conv')39 # Concat and apply BN40 final_path = tf.concat(values=[path1, path2], axis=concat_axis)41 final_path = ops.batch_norm(final_path, scope='final_path_bn')42 return final_path43def _shake_shake_branch(x, output_filters, stride, rand_forward, rand_backward,44 is_training):45 """Building a 2 branching convnet."""46 x = tf.nn.relu(x)47 x = ops.conv2d(x, output_filters, 3, stride=stride, scope='conv1')48 x = ops.batch_norm(x, scope='bn1')49 x = tf.nn.relu(x)50 x = ops.conv2d(x, output_filters, 3, scope='conv2')51 x = ops.batch_norm(x, scope='bn2')52 if is_training:53 x = x * rand_backward + tf.stop_gradient(x * rand_forward -54 x * rand_backward)55 else:56 x *= 1.0 / 257 return x58def _shake_shake_block(x, output_filters, stride, is_training):59 """Builds a full shake-shake sub layer."""60 batch_size = tf.shape(x)[0]61 # Generate random numbers for scaling the branches62 rand_forward = [63 tf.random_uniform(64 [batch_size, 1, 1, 1], minval=0, maxval=1, dtype=tf.float32)65 for _ in range(2)66 ]67 rand_backward = [68 tf.random_uniform(69 [batch_size, 1, 1, 1], minval=0, maxval=1, dtype=tf.float32)70 for _ in range(2)71 ]72 # Normalize so that all sum to 173 total_forward = tf.add_n(rand_forward)74 total_backward = tf.add_n(rand_backward)75 rand_forward = [samp / total_forward for samp in rand_forward]76 rand_backward = [samp / total_backward for samp in rand_backward]77 zipped_rand = zip(rand_forward, rand_backward)78 branches = []79 for branch, (r_forward, r_backward) in enumerate(zipped_rand):80 with tf.variable_scope('branch_{}'.format(branch)):81 b = _shake_shake_branch(x, output_filters, stride, r_forward, r_backward,82 is_training)83 branches.append(b)84 res = _shake_shake_skip_connection(x, output_filters, stride)85 return res + tf.add_n(branches)86def _shake_shake_layer(x, output_filters, num_blocks, stride,87 is_training):88 """Builds many sub layers into one full layer."""89 for block_num in range(num_blocks):90 curr_stride = stride if (block_num == 0) else 191 with tf.variable_scope('layer_{}'.format(block_num)):92 x = _shake_shake_block(x, output_filters, curr_stride,93 is_training)94 return x95def build_shake_shake_model(images, num_classes, hparams, is_training):96 """Builds the Shake-Shake model.97 Build the Shake-Shake model from https://arxiv.org/abs/1705.07485.98 Args:99 images: Tensor of images that will be fed into the Wide ResNet Model.100 num_classes: Number of classed that the model needs to predict.101 hparams: tf.HParams object that contains additional hparams needed to102 construct the model. In this case it is the `shake_shake_widen_factor`103 that is used to determine how many filters the model has.104 is_training: Is the model training or not.105 Returns:106 The logits of the Shake-Shake model.107 """108 depth = 26109 k = hparams.shake_shake_widen_factor # The widen factor110 n = int((depth - 2) / 6)111 x = images112 x = ops.conv2d(x, 16, 3, scope='init_conv')113 x = ops.batch_norm(x, scope='init_bn')114 with tf.variable_scope('L1'):115 x = _shake_shake_layer(x, 16 * k, n, 1, is_training)116 with tf.variable_scope('L2'):117 x = _shake_shake_layer(x, 32 * k, n, 2, is_training)118 with tf.variable_scope('L3'):119 x = _shake_shake_layer(x, 64 * k, n, 2, is_training)120 x = tf.nn.relu(x)121 x = ops.global_avg_pool(x)122 # Fully connected123 logits = ops.fc(x, num_classes)...
shake.js
Source:shake.js
1/*globals define*/2define([3 'utils'4], function( Utils ) {5 'use strict';6 function Shake() {7 this.magnitude = 0;8 this.duration = 0;9 this.time = 0;10 // Variables pertaining to the current shake cycle.11 // Change shake angle at frequency (20 fps).12 this.shakePeriod = 1 / 20;13 this.shakeTime = 0;14 this.shakeMagnitude = 0;15 this.shakeAngle = 0;16 }17 Shake.prototype.applyTransform = function( ctx ) {18 if ( !this.magnitude ) {19 return;20 }21 ctx.translate(22 Math.cos( this.shakeAngle ) * this.shakeMagnitude,23 Math.sin( this.shakeAngle ) * this.shakeMagnitude24 );25 };26 Shake.prototype.update = function( dt ) {27 if ( !this.magnitude ) {28 return;29 }30 this.time += dt;31 if ( this.time > this.duration ) {32 this.magnitude = 0;33 this.time = 0;34 return;35 }36 this.shakeTime += dt;37 // A new oscillation!38 if ( this.shakeTime > this.shakePeriod ) {39 this.shakeTime = 0;40 this.shakeAngle = Math.random() * Utils.PI2;41 // Lerp scale magnitude down to zero.42 var scale = ( this.duration - this.time ) / this.duration;43 this.shakeMagnitude = this.magnitude * scale;44 }45 };46 Shake.prototype.shake = function( magnitude, duration ) {47 this.magnitude = magnitude || 0;48 this.duration = duration || 0;49 this.update(0);50 };51 Object.defineProperty( Shake.prototype, 'frequency', {52 get: function() {53 return 1 / this.shakePeriod;54 },55 set: function( frequency ) {56 this.shakePeriod = 1 / frequency;57 }58 });59 return Shake;...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!