Best Python code snippet using slash
modules.py
Source:modules.py
1import numpy as np2import tensorflow as tf3class NeuralNetwork(object):4 def conv2d(self, input, filter_shape, scope_name, strides=[1, 1, 1, 1]):5 xavier_initializer = tf.contrib.layers.xavier_initializer6 with tf.variable_scope(scope_name):7 W = tf.get_variable(initializer=xavier_initializer(uniform=False),8 name="W",9 shape=filter_shape)10 b = tf.get_variable(initializer=xavier_initializer(uniform=False),11 name="b",12 shape=[filter_shape[3]])13 out = tf.nn.conv2d(input, W, padding="SAME", strides=strides)14 out = tf.nn.bias_add(out, b)15 return out16 def conv2d_relu(self, input, filter_shape, scope_name, strides=[1, 1, 1, 1]):17 xavier_initializer = tf.contrib.layers.xavier_initializer18 with tf.variable_scope(scope_name):19 W = tf.get_variable(initializer=xavier_initializer(uniform=False),20 name="W",21 shape=filter_shape)22 b = tf.get_variable(initializer=xavier_initializer(uniform=False),23 name="b",24 shape=[filter_shape[3]])25 out = tf.nn.conv2d(input, W, padding="SAME", strides=strides)26 out = tf.nn.bias_add(out, b)27 out = tf.contrib.layers.batch_norm(out)28 out = tf.nn.relu(out, name="out")29 return out30 def maxpool2d(self, input, scope_name, pool_shape=[1, 2, 2, 1], strides=[1, 2, 2, 1]):31 with tf.variable_scope(scope_name):32 out = tf.nn.max_pool(input,33 ksize=pool_shape,34 name="out",35 padding="SAME",36 strides=strides)37 return out38 def dropout(self, input, keep_prob, scope_name):39 with tf.variable_scope(scope_name):40 out = tf.nn.dropout(input, keep_prob, name="out")41 return out42 def fc(self, input, output_shape, scope_name):43 xavier_initializer = tf.contrib.layers.xavier_initializer44 with tf.variable_scope(scope_name):45 input_shape = input.shape[1:]46 input_shape = int(np.prod(input_shape))47 W = tf.get_variable(name="W",48 shape=[input_shape, output_shape],49 initializer=xavier_initializer(uniform=False))50 b = tf.get_variable(name="b",51 shape=[output_shape],52 initializer=xavier_initializer(uniform=False))53 input = tf.reshape(input, [-1, input_shape])54 # out = tf.nn.relu(tf.add(tf.matmul(input, W), b), name="out")55 out = tf.add(tf.matmul(input, W), b, name="out")56 return out57 def deconv2d(self, input, filter_shape, num_outputs, scope_name, strides=[1, 1]):58 xavier_initializer = tf.contrib.layers.xavier_initializer59 xavier_initializer_conv2d = tf.contrib.layers.xavier_initializer_conv2d60 with tf.variable_scope(scope_name):61 out = tf.contrib.layers.conv2d_transpose(input,62 # activation_fn=tf.nn.relu,63 activation_fn=None,64 biases_initializer=xavier_initializer(uniform=False),65 kernel_size=filter_shape,66 num_outputs=num_outputs,67 padding="SAME",68 stride=strides,69 weights_initializer=xavier_initializer_conv2d(uniform=False))70 out = tf.identity(out, name="out")71 return out72 def upsample(self, input, scope_name, factor=[2, 2]):73 size = [int(input.shape[1] * factor[0]), int(input.shape[2] * factor[1])]74 with tf.variable_scope(scope_name):75 out = tf.image.resize_bilinear(input, size=size, align_corners=None, name="out")76 return out77class ConvEncoder(NeuralNetwork):78 def __init__(self, input_shape, keep_prob, scope_name="encoder"):79 self.input_shape = input_shape80 self.keep_prob = keep_prob81 self.scope_name = scope_name82 def build_graph(self, input):83 with tf.variable_scope(self.scope_name):84 conv1 = self.conv2d_relu(input, filter_shape=[3, 3, 1, 8], scope_name="conv1") # (232, 196, 8)85 pool1 = self.maxpool2d(conv1, scope_name="pool1") # (116, 98, 8)86 drop1 = self.dropout(pool1, keep_prob=self.keep_prob, scope_name="drop1")87 conv2 = self.conv2d_relu(drop1, filter_shape=[5, 5, 8, 16], scope_name="conv2") # (116, 98, 16)88 pool2 = self.maxpool2d(conv2, scope_name="pool2") # (58, 49, 16)89 drop2 = self.dropout(pool2, keep_prob=self.keep_prob, scope_name="drop2")90 drop2 = tf.reshape(drop2, shape=[-1, 58*49*16]) # (45472,)91 fc1 = self.fc(drop2, output_shape=1024, scope_name="fc1")92 drop3 = self.dropout(fc1, keep_prob=self.keep_prob, scope_name="drop3")93 fc2 = self.fc(drop3, output_shape=256, scope_name="fc2")94 out = tf.identity(fc2, name="out")95 return out96class DeconvDecoder(NeuralNetwork):97 def __init__(self, keep_prob, output_shape, scope_name="decoder"):98 self.keep_prob = keep_prob99 self.output_shape = output_shape100 self.scope_name = scope_name101 def build_graph(self, input):102 with tf.variable_scope(self.scope_name):103 fc1 = self.fc(input, output_shape=1024, scope_name="fc1")104 drop1 = self.dropout(fc1, keep_prob=self.keep_prob, scope_name="drop1")105 fc2 = self.fc(drop1, output_shape=58*49*16, scope_name="fc2")106 drop2 = self.dropout(fc2, keep_prob=self.keep_prob, scope_name="drop2")107 drop2 = tf.reshape(drop2, shape=[-1, 58, 49, 16])108 up1 = self.upsample(drop2, scope_name="up1", factor=[2, 2]) # (116, 98, 16)109 deconv1 = self.deconv2d(up1, filter_shape=[5, 5], num_outputs=8, scope_name="deconv1") # (116, 98, 8)110 up2 = self.upsample(deconv1, scope_name="up2", factor=[2, 2])111 deconv2 = self.deconv2d(up2, filter_shape=[3, 3], num_outputs=1, scope_name="deconv2") # (232, 196, 1)112 out = tf.identity(deconv2, name="out")113 return out114class UNet(NeuralNetwork):115 def __init__(self, input_shape, keep_prob, output_shape, scope_name="unet"):116 self.input_shape = input_shape117 self.keep_prob = keep_prob118 self.output_shape = output_shape119 self.scope_name = scope_name120 def build_graph(self, input):121 with tf.variable_scope(self.scope_name):122 # Conv123 conv1 = self.conv2d_relu(input, filter_shape=[3, 3, 1, 64], scope_name="conv1") # (b, 232, 196, 64)124 drop1 = self.dropout(conv1, keep_prob=self.keep_prob, scope_name="drop1")125 conv2 = self.conv2d_relu(drop1, filter_shape=[3, 3, 64, 64], scope_name="conv2") # (b, 232, 196, 64)126 drop2 = self.dropout(conv2, keep_prob=self.keep_prob, scope_name="drop2")127 pool1 = self.maxpool2d(drop2, scope_name="pool1") # (b, 116, 98, 64)128 conv3 = self.conv2d_relu(pool1, filter_shape=[3, 3, 64, 128], scope_name="conv3") # (b, 116, 98, 128)129 drop3 = self.dropout(conv3, keep_prob=self.keep_prob, scope_name="drop3")130 conv4 = self.conv2d_relu(drop3, filter_shape=[3, 3, 128, 128], scope_name="conv4") # (b, 116, 98, 128)131 drop4 = self.dropout(conv4, keep_prob=self.keep_prob, scope_name="drop4")132 pool2 = self.maxpool2d(conv4, scope_name="pool2") # (b, 58, 49, 128)133 conv5 = self.conv2d_relu(pool2, filter_shape=[3, 3, 128, 256], scope_name="conv5") # (b, 58, 49, 256)134 drop5 = self.dropout(conv5, keep_prob=self.keep_prob, scope_name="drop5")135 conv6 = self.conv2d_relu(drop5, filter_shape=[3, 3, 256, 256], scope_name="conv6") # (b, 58, 49, 256)136 drop6 = self.dropout(conv6, keep_prob=self.keep_prob, scope_name="drop6")137 # Deconv138 up1 = self.upsample(drop6, scope_name="up1", factor=[2, 2]) # (b, 116, 98, 256)139 deconv1 = self.deconv2d(up1, filter_shape=[2, 2], num_outputs=128, scope_name="deconv1") # (b, 116, 98, 128)140 concat1 = tf.concat([drop4, deconv1], axis=3) # (b, 116, 98, 256)141 conv7 = self.conv2d_relu(concat1, filter_shape=[3, 3, 256, 128], scope_name="conv7") # (b, 116, 98, 128)142 drop7 = self.dropout(conv7, keep_prob=self.keep_prob, scope_name="drop7")143 conv8 = self.conv2d_relu(drop7, filter_shape=[3, 3, 128, 128], scope_name="conv8") # (b, 116, 98, 128)144 drop8 = self.dropout(conv8, keep_prob=self.keep_prob, scope_name="drop8")145 up2 = self.upsample(drop8, scope_name="up2", factor=[2, 2]) # (b, 232, 196, 128)146 deconv2 = self.deconv2d(up2, filter_shape=[2, 2], num_outputs=64, scope_name="deconv2") # (b, 232, 196, 64)147 concat2 = tf.concat([drop2, deconv2], axis=3) # (b, 232, 196, 128)148 conv9 = self.conv2d_relu(concat2, filter_shape=[3, 3, 128, 64], scope_name="conv9") # (b, 232, 196, 64)149 drop9 = self.dropout(conv9, keep_prob=self.keep_prob, scope_name="drop9")150 conv10 = self.conv2d_relu(drop9, filter_shape=[3, 3, 64, 64], scope_name="conv10") # (b, 232, 196, 64)151 drop10 = self.dropout(conv10, keep_prob=self.keep_prob, scope_name="drop10")152 conv11 = self.conv2d(drop10, filter_shape=[1, 1, 64, 1], scope_name="conv11") # (b, 232, 196, 1)153 out = tf.identity(conv11, name="out")154 return out155class MetaUNet(NeuralNetwork):156 def __init__(self, input_shape, keep_prob, output_shape, scope_name="metaunet"):157 self.input_shape = input_shape158 self.keep_prob = keep_prob159 self.output_shape = output_shape160 self.scope_name = scope_name161 def build_graph(self, input):162 with tf.variable_scope(self.scope_name):163 # Conv164 conv1 = self.conv2d_relu(input, filter_shape=[3, 3, 5, 32], scope_name="conv1") # (b, 232, 196, 64)165 drop1 = self.dropout(conv1, keep_prob=self.keep_prob, scope_name="drop1")166 conv2 = self.conv2d_relu(drop1, filter_shape=[3, 3, 32, 32], scope_name="conv2") # (b, 232, 196, 64)167 drop2 = self.dropout(conv2, keep_prob=self.keep_prob, scope_name="drop2")168 pool1 = self.maxpool2d(drop2, scope_name="pool1") # (b, 116, 98, 64)169 conv3 = self.conv2d_relu(pool1, filter_shape=[3, 3, 32, 64], scope_name="conv3") # (b, 116, 98, 128)170 drop3 = self.dropout(conv3, keep_prob=self.keep_prob, scope_name="drop3")171 conv4 = self.conv2d_relu(drop3, filter_shape=[3, 3, 64, 64], scope_name="conv4") # (b, 116, 98, 128)172 drop4 = self.dropout(conv4, keep_prob=self.keep_prob, scope_name="drop4")173 pool2 = self.maxpool2d(conv4, scope_name="pool2") # (b, 58, 49, 128)174 conv5 = self.conv2d_relu(pool2, filter_shape=[3, 3, 64, 128], scope_name="conv5") # (b, 58, 49, 256)175 drop5 = self.dropout(conv5, keep_prob=self.keep_prob, scope_name="drop5")176 conv6 = self.conv2d_relu(drop5, filter_shape=[3, 3, 128, 128], scope_name="conv6") # (b, 58, 49, 256)177 drop6 = self.dropout(conv6, keep_prob=self.keep_prob, scope_name="drop6")178 # Deconv179 up1 = self.upsample(drop6, scope_name="up1", factor=[2, 2]) # (b, 116, 98, 256)180 deconv1 = self.deconv2d(up1, filter_shape=[2, 2], num_outputs=64, scope_name="deconv1") # (b, 116, 98, 128)181 concat1 = tf.concat([drop4, deconv1], axis=3) # (b, 116, 98, 256)182 conv7 = self.conv2d_relu(concat1, filter_shape=[3, 3, 128, 64], scope_name="conv7") # (b, 116, 98, 128)183 drop7 = self.dropout(conv7, keep_prob=self.keep_prob, scope_name="drop7")184 conv8 = self.conv2d_relu(drop7, filter_shape=[3, 3, 64, 64], scope_name="conv8") # (b, 116, 98, 128)185 drop8 = self.dropout(conv8, keep_prob=self.keep_prob, scope_name="drop8")186 up2 = self.upsample(drop8, scope_name="up2", factor=[2, 2]) # (b, 232, 196, 128)187 deconv2 = self.deconv2d(up2, filter_shape=[2, 2], num_outputs=32, scope_name="deconv2") # (b, 232, 196, 64)188 concat2 = tf.concat([drop2, deconv2], axis=3) # (b, 232, 196, 128)189 conv9 = self.conv2d_relu(concat2, filter_shape=[3, 3, 64, 32], scope_name="conv9") # (b, 232, 196, 64)190 drop9 = self.dropout(conv9, keep_prob=self.keep_prob, scope_name="drop9")191 conv10 = self.conv2d_relu(drop9, filter_shape=[3, 3, 32, 32], scope_name="conv10") # (b, 232, 196, 64)192 drop10 = self.dropout(conv10, keep_prob=self.keep_prob, scope_name="drop10")193 conv11 = self.conv2d(drop10, filter_shape=[1, 1, 32, 1], scope_name="conv11") # (b, 232, 196, 1)194 out = tf.identity(conv11, name="out")195 ...
private_link_scope.py
Source:private_link_scope.py
1# --------------------------------------------------------------------------------------------2# Copyright (c) Microsoft Corporation. All rights reserved.3# Licensed under the MIT License. See License.txt in the project root for license information.4# --------------------------------------------------------------------------------------------5from knack.log import get_logger6logger = get_logger(__name__)7# region Private Link Scope8def show_private_link_scope(client, resource_group_name, scope_name):9 return client.get(resource_group_name=resource_group_name,10 scope_name=scope_name)11def delete_private_link_scope(client, resource_group_name, scope_name):12 return client.begin_delete(resource_group_name=resource_group_name,13 scope_name=scope_name)14def list_private_link_scope(client, resource_group_name=None):15 if not resource_group_name:16 return client.list()17 return client.list_by_resource_group(resource_group_name=resource_group_name)18def create_private_link_scope(client, resource_group_name, scope_name, location='Global', tags=None):19 from azure.mgmt.monitor.models import AzureMonitorPrivateLinkScope20 private_link_scope = AzureMonitorPrivateLinkScope(location=location, tags=tags)21 return client.create_or_update(resource_group_name=resource_group_name,22 scope_name=scope_name,23 azure_monitor_private_link_scope_payload=private_link_scope)24def update_private_link_scope(client, resource_group_name, scope_name, tags):25 from azure.mgmt.monitor.models import TagsResource26 tags = TagsResource(tags=tags)27 return client.update_tags(resource_group_name=resource_group_name,28 scope_name=scope_name,29 private_link_scope_tags=tags)30# endregion31# region Private Link Scope Resource32def show_private_link_scope_resource(client, resource_group_name, scope_name, resource_name):33 return client.get(resource_group_name=resource_group_name,34 scope_name=scope_name,35 name=resource_name)36def delete_private_link_scope_resource(client, resource_group_name, scope_name, resource_name):37 return client.begin_delete(resource_group_name=resource_group_name,38 scope_name=scope_name,39 name=resource_name)40def list_private_link_scope_resource(client, resource_group_name, scope_name):41 return client.list_by_private_link_scope(resource_group_name=resource_group_name,42 scope_name=scope_name)43def create_private_link_scope_resource(client, resource_group_name, scope_name, resource_name,44 linked_resource_id):45 from azure.mgmt.monitor.models import ScopedResource46 scoped_resource = ScopedResource(linked_resource_id=linked_resource_id)47 return client.begin_create_or_update(resource_group_name=resource_group_name,48 scope_name=scope_name,49 name=resource_name,50 parameters=scoped_resource)51# endregion52# region Private Link Resource53def list_private_link_resource(client, resource_group_name, scope_name):54 return client.list_by_private_link_scope(resource_group_name=resource_group_name,55 scope_name=scope_name)56def show_private_link_resource(client, resource_group_name, scope_name, group_name):57 return client.get(resource_group_name=resource_group_name,58 scope_name=scope_name,59 group_name=group_name)60# endregion61# region Private Endpoint Connection62def show_private_endpoint_connection(client, resource_group_name, scope_name, private_endpoint_connection_name):63 return client.get(resource_group_name=resource_group_name,64 scope_name=scope_name,65 private_endpoint_connection_name=private_endpoint_connection_name)66def delete_private_endpoint_connection(client, resource_group_name, scope_name, private_endpoint_connection_name):67 return client.begin_delete(resource_group_name=resource_group_name,68 scope_name=scope_name,69 private_endpoint_connection_name=private_endpoint_connection_name)70def list_private_endpoint_connection(client, resource_group_name, scope_name):71 return client.list_by_private_link_scope(resource_group_name=resource_group_name,72 scope_name=scope_name)73# pylint: disable=line-too-long, unused-argument74def _update_private_endpoint_connection_status(cmd, client, resource_group_name, scope_name,75 private_endpoint_connection_name, is_approved=True, description=None):76 private_endpoint_connection = client.get(resource_group_name=resource_group_name, scope_name=scope_name,77 private_endpoint_connection_name=private_endpoint_connection_name)78 old_status = private_endpoint_connection.private_link_service_connection_state.status79 new_status = "Approved" if is_approved else "Rejected"80 if old_status == new_status:81 logger.warning('The status has been satisfied. Skip this command.')82 return None83 private_endpoint_connection.private_link_service_connection_state.status = new_status84 private_endpoint_connection.private_link_service_connection_state.description = description85 from azure.mgmt.monitor.models import PrivateEndpointConnection86 private_endpoint_connection = PrivateEndpointConnection(private_link_service_connection_state=private_endpoint_connection.private_link_service_connection_state)87 return client.begin_create_or_update(resource_group_name=resource_group_name,88 scope_name=scope_name,89 private_endpoint_connection_name=private_endpoint_connection_name,90 parameters=private_endpoint_connection)91def approve_private_endpoint_connection(cmd, client, resource_group_name, scope_name,92 private_endpoint_connection_name, description=""):93 return _update_private_endpoint_connection_status(94 cmd, client, resource_group_name=resource_group_name, scope_name=scope_name, is_approved=True,95 private_endpoint_connection_name=private_endpoint_connection_name, description=description96 )97def reject_private_endpoint_connection(cmd, client, resource_group_name, scope_name,98 private_endpoint_connection_name, description=""):99 return _update_private_endpoint_connection_status(100 cmd, client, resource_group_name=resource_group_name, scope_name=scope_name, is_approved=False,101 private_endpoint_connection_name=private_endpoint_connection_name, description=description102 )...
name.py
Source:name.py
1# This file is part of PeachPy package and is licensed under the Simplified BSD license.2# See license.rst for the full text of the license.3import six4class Name:5 def __init__(self, name=None, prename=None):6 assert name is None or isinstance(name, str)7 assert prename is None or isinstance(prename, str)8 assert name is None or prename is None, \9 "Either name or prename, but not both, can be specified"10 self.name = name11 self.prename = prename12 def __str__(self):13 if self.name is not None:14 return self.name15 elif self.prename is not None:16 return "<" + self.prename + ">"17 else:18 return "<?>"19 def __repr__(self):20 return str(self)21 def __hash__(self):22 return hash(self.name) ^ id(self)23 def __eq__(self, other):24 return isinstance(other, Name) and (self is other or self.name == other.name)25 def __ne__(self, other):26 return not isinstance(other, Name) or (self is not other and self.name != other.name)27 @staticmethod28 def check_name(name):29 """Verifies that the name is appropriate for a symbol"""30 if not isinstance(name, str):31 raise TypeError("Invalid name %s: string required" % str(name))32 import re33 if not re.match("^[_a-zA-Z]\\w*$", name):34 raise ValueError("Invalid name: " + name)35 if name.startswith("__"):36 raise ValueError("Invalid name %s: names starting with __ are reserved for PeachPy purposes" % name)37class Namespace:38 def __init__(self, scope_name):39 assert scope_name is None or isinstance(scope_name, Name)40 # Name of the namespace.41 self.scope_name = scope_name42 # Map from name string to either Name or Namespace object43 self.names = dict()44 # Map from prename string to a set of Name and Namespace objects45 self.prenames = dict()46 def __str__(self):47 return str(self.scope_name)48 def __hash__(self):49 return hash(self.scope_name)50 def __eq__(self, other):51 return isinstance(other, Namespace) and self.scope_name == other.scope_name52 def __ne__(self, other):53 return not isinstance(other, Namespace) or self.scope_name != other.scope_name54 def add_scoped_name(self, scoped_name):55 assert isinstance(scoped_name, tuple)56 scope_name, subscoped_name = scoped_name[0], scoped_name[1:]57 assert isinstance(scope_name, Name)58 scope = scope_name59 if subscoped_name:60 scope = Namespace(scope_name)61 scope.add_scoped_name(subscoped_name)62 if scope_name.name:63 assert scope_name.prename is None64 if scope_name.name in self.names:65 if subscoped_name and isinstance(self.names[scope_name.name], Namespace):66 self.names[scope_name.name].add_scoped_name(subscoped_name)67 else:68 raise ValueError("Name %s already exists" % scope_name.name)69 else:70 self.names[scope_name.name] = scope71 else:72 assert scope_name.name is None73 self.prenames.setdefault(scope_name.prename, set())74 if subscoped_name:75 for subscope in iter(self.prenames[scope_name.prename]):76 if isinstance(subscope, Namespace) and subscope.scope_name is scope_name:77 subscope.add_scoped_name(subscoped_name)78 return79 self.prenames[scope_name.prename].add(scope)80 def assign_names(self):81 # Step 1: assign names to symbols with prenames with no conflicts82 for prename in six.iterkeys(self.prenames):83 if prename is not None:84 if len(self.prenames[prename]) == 1 and prename not in self.names:85 name_object = next(iter(self.prenames[prename]))86 self.names[prename] = name_object87 if isinstance(name_object, Namespace):88 name_object = name_object.scope_name89 name_object.name = prename90 # Step 2: assign names to symbols with conflicting prenames91 for prename, prename_objects in six.iteritems(self.prenames):92 if prename is not None:93 suffix = 094 suffixed_name = prename + str(suffix)95 for name_object in iter(prename_objects):96 # Check that the name wasn't already assigned at Step 197 if name_object.name is None:98 # Generate a non-conflicting name by appending a suffix99 while suffixed_name in self.names:100 suffix += 1101 suffixed_name = prename + str(suffix)102 self.names[suffixed_name] = name_object103 if isinstance(name_object, Namespace):104 name_object = name_object.scope_name105 name_object.name = suffixed_name106 # Step 3: assign names to symbols without prenames107 if None in self.prenames:108 unnamed_objects = self.prenames[None]109 suffix = 0110 suffixed_name = "__local" + str(suffix)111 for name_object in iter(unnamed_objects):112 # Generate a non-conflicting name by appending a suffix113 while suffixed_name in self.names:114 suffix += 1115 suffixed_name = "__local" + str(suffix)116 self.names[suffixed_name] = name_object117 if isinstance(name_object, Namespace):118 name_object = name_object.scope_name119 name_object.name = suffixed_name120 pass121 @property122 def name(self):123 return self.scope_name.name124 @property125 def prename(self):...
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!!