#! /usr/bin/python # -*- coding: utf-8 -*- import dragon as D from dragon.core.ops import vision_ops from dragon.core.ops import activation_ops def padding_format(padding): """ Checks that the padding format correspond format. Parameters ---------- padding : str Must be one of the following:"same", "SAME", "VALID", "valid" Returns ------- str "SAME" or "VALID" """ if padding in ["SAME", "same"]: padding = "SAME" elif padding in ["VALID", "valid"]: padding = "VALID" elif padding == None: padding = None else: raise Exception("Unsupported padding: " + str(padding)) return padding def preprocess_1d_format(data_format, padding): """ Checks that the 1-D dataformat format correspond format. Parameters ---------- data_format : str Must be one of the following:"channels_last","NWC","NCW","channels_first" padding : str Must be one of the following:"same","valid","SAME","VALID" Returns ------- str "NWC" or "NCW" and "SAME" or "VALID" """ if data_format in ["channels_last", "NWC"]: data_format = "NWC" elif data_format in ["channels_first", "NCW"]: data_format = "NCW" elif data_format == None: data_format = None else: raise Exception("Unsupported data format: " + str(data_format)) padding = padding_format(padding) return data_format, padding def preprocess_2d_format(data_format, padding): """ Checks that the 2-D dataformat format correspond format. Parameters ---------- data_format : str Must be one of the following:"channels_last","NHWC","NCHW","channels_first" padding : str Must be one of the following:"same","valid","SAME","VALID" Returns ------- str "NHWC" or "NCHW" and "SAME" or "VALID" """ if data_format in ["channels_last", "NHWC", "nhwc"]: data_format = "NHWC" elif data_format in ["channels_first", "NCHW", "nchw"]: data_format = "NCHW" elif data_format == None: data_format = None else: raise Exception("Unsupported data format: " + str(data_format)) padding = padding_format(padding) return data_format, padding def preprocess_3d_format(data_format, padding): """ Checks that the 3-D dataformat format correspond format. Parameters ---------- data_format : str Must be one of the following:"channels_last","NDHWC","NCDHW","channels_first" padding : str Must be one of the following:"same","valid","SAME","VALID" Returns ------- str "NDHWC" or "NCDHW" and "SAME" or "VALID" """ if data_format in ['channels_last', 'NDHWC']: data_format = 'NDHWC' elif data_format in ['channels_first', 'NCDHW']: data_format = 'NCDHW' elif data_format == None: data_format = None else: raise Exception("Unsupported data format: " + str(data_format)) padding = padding_format(padding) return data_format, padding def nchw_to_nhwc(x): """ Channels first to channels last Parameters ---------- x : tensor channels first tensor data Returns ------- channels last tensor data """ pass def nhwc_to_nchw(x): """ Channles last to channels first Parameters ---------- x : tensor channels last tensor data Returns ------- channels first tensor data """ pass class ReLU(object): def __init__(self): pass def __call__(self, x): return D.nn.relu(x) def relu(x): """ Computes rectified linear: max(features, 0). Parameters ---------- x : tensor Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64, qint8. Returns ------- A Tensor. Has the same type as features. """ return D.nn.relu(x) class ReLU6(object): def __init__(self): pass def __call__(self, x): return D.nn.relu6(x) def relu6(x): """ Computes Rectified Linear 6: min(max(features, 0), 6). Parameters ---------- x : tensor Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64, qint8. Returns ------- A Tensor with the same type as features. """ return D.nn.relu6(x) class LeakyReLU(object): def __init__(self, alpha=0.2): self.alpha = alpha def __call__(self, x): return D.nn.leaky_relu(x, alpha=self.alpha) def leaky_relu(x): """ Compute the Leaky ReLU activation function. Parameters ---------- x : tensor representing preactivation values. Must be one of the following types: float16, float32, float64, int32, int64. Returns ------- The activation value. """ return D.nn.leaky_relu(x) class Softplus(object): def __init__(self): pass def __call__(self, x): raise NotImplementedError def softplus(x): """ Computes softplus: log(exp(features) + 1). Parameters ---------- x : tensor Must be one of the following types: half, bfloat16, float32, float64. Returns ------- A Tensor. Has the same type as features. """ raise NotImplementedError class Tanh(object): def __init__(self): pass def __call__(self, x): return activation_ops.tanh(x) def tanh(x): """ Computes hyperbolic tangent of x element-wise. Parameters ---------- x : tensor Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128. Returns ------- A Tensor. Has the same type as x. """ return activation_ops.tanh(x) class Sigmoid(object): def __init__(self): pass def __call__(self, x): return activation_ops.sigmoid(x) def sigmoid(x): """ Computes sigmoid of x element-wise. Parameters ---------- x : tensor A Tensor with type float16, float32, float64, complex64, or complex128. Returns ------- A Tensor with the same type as x. """ return activation_ops.sigmoid(x) class Softmax(object): def __init__(self): pass def __call__(self, x): return D.nn.softmax(x) def softmax(logits, axis=None): """ Computes softmax activations. Parameters ---------- logits : tensor Must be one of the following types: half, float32, float64. axis : int The dimension softmax would be performed on. The default is -1 which indicates the last dimension. Returns ------- A Tensor. Has the same type and shape as logits. """ return D.nn.softmax(logits) class Dropout(object): def __init__(self, keep, seed=1): self.keep = 1 - keep self.seed = seed def __call__(self, inputs): return D.nn.dropout(inputs, prob=self.keep) class BiasAdd(object): """ Adds bias to value. Parameters ---------- x : tensor A Tensor with type float, double, int64, int32, uint8, int16, int8, complex64, or complex128. bias : tensor Must be the same type as value unless value is a quantized type, in which case a different quantized type may be used. Returns ------- A Tensor with the same type as value. """ def __init__(self, data_format='NHWC'): self.data_format = data_format def __call__(self, x, bias): inputs = [x, bias] return vision_ops.bias_add(inputs, data_format=self.data_format) def bias_add(x, bias): """ Adds bias to value. Parameters ---------- x : tensor A Tensor with type float, double, int64, int32, uint8, int16, int8, complex64, or complex128. bias : tensor Must be the same type as value unless value is a quantized type, in which case a different quantized type may be used. data_format : A string. 'N...C' and 'NC...' are supported. name : str A name for the operation (optional). Returns ------- A Tensor with the same type as value. """ inputs = [x, bias] return vision_ops.bias_add(inputs, data_format='NHWC') class Conv1D(object): pass # raise NotImplementedError def conv1d(input, filters, stride, padding, data_format='NWC', dilations=None, name=None): """ Computes a 1-D convolution given 3-D input and filter tensors. Parameters ---------- input : tensor A 3D Tensor. Must be of type float16, float32, or float64 filters : tensor A 3D Tensor. Must have the same type as input. stride : int of list An int or list of ints that has length 1 or 3. The number of entries by which the filter is moved right at each step. padding : string 'SAME' or 'VALID' data_format : string An optional string from "NWC", "NCW". Defaults to "NWC", the data is stored in the order of [batch, in_width, in_channels]. The "NCW" format stores data as [batch, in_channels, in_width]. dilations : int or list An int or list of ints that has length 1 or 3 which defaults to 1. The dilation factor for each dimension of input. If set to k > 1, there will be k-1 skipped cells between each filter element on that dimension. Dilations in the batch and depth dimensions must be 1. name : string A name for the operation (optional). Returns ------- A Tensor. Has the same type as input. """ pass class Conv2D(object): def __init__(self, strides, padding, data_format='NHWC', dilations=None, out_channel=None, k_size=None): self.data_format, self.padding = preprocess_2d_format(data_format, padding) self.ksize = k_size[0] if self.data_format is 'NHWC': self.dg_stride = strides[1] self.dg_dilation = dilations[1] elif self.data_format is 'NCHW': self.dg_stride = strides[2] self.dg_dilation = dilations[2] def __call__(self, inputs, filters): outputs = vision_ops.conv2d( [inputs, filters], kernel_shape=self.ksize, strides=self.dg_stride, padding=self.padding, dilations=self.dg_dilation, data_format=self.data_format, ) return outputs def conv2d(input, filters, strides, padding, data_format='NCHW', dilations=None): """ Computes a 2-D convolution given 4-D input and filters tensors. Parameters ---------- input : tensor Must be one of the following types: half, bfloat16, float32, float64. A 4-D tensor. The dimension order is interpreted according to the value of data_format, see below for details. filters : tensor Must have the same type as input. A 4-D tensor of shape [filter_height, filter_width, in_channels, out_channels] strides : int of list The stride of the sliding window for each dimension of input. If a single value is given it is replicated in the H and W dimension. By default the N and C dimensions are set to 1. The dimension order is determined by the value of data_format, see below for details. padding : string "SAME" or "VALID" data_format : string "NHWC", "NCHW". Defaults to "NCHW". dilations : list or ints list of ints that has length 1, 2 or 4, defaults to 1. The dilation factor for each dimension ofinput. Returns ------- A Tensor. Has the same type as input. """ raise NotImplementedError class Conv3D(object): pass # raise NotImplementedError def conv3d(input, filters, strides, padding, data_format='NDHWC', dilations=None, name=None): """ Computes a 3-D convolution given 5-D input and filters tensors. Parameters ---------- input : tensor Must be one of the following types: half, bfloat16, float32, float64. Shape [batch, in_depth, in_height, in_width, in_channels]. filters : tensor Must have the same type as input. Shape [filter_depth, filter_height, filter_width, in_channels, out_channels]. in_channels must match between input and filters. strides : list of ints A list of ints that has length >= 5. 1-D tensor of length 5. The stride of the sliding window for each dimension of input. Must have strides[0] = strides[4] = 1. padding : string A string from: "SAME", "VALID". The type of padding algorithm to use. data_format : string An optional string from: "NDHWC", "NCDHW". Defaults to "NDHWC". The data format of the input and output data. With the default format "NDHWC", the data is stored in the order of: [batch, in_depth, in_height, in_width, in_channels]. Alternatively, the format could be "NCDHW", the data storage order is: [batch, in_channels, in_depth, in_height, in_width]. dilations : list of ints Defaults to [1, 1, 1, 1, 1]. 1-D tensor of length 5. The dilation factor for each dimension of input. If set to k > 1, there will be k-1 skipped cells between each filter element on that dimension. The dimension order is determined by the value of data_format, see above for details. Dilations in the batch and depth dimensions must be 1. name : string A name for the operation (optional). Returns ------- A Tensor. Has the same type as input. """ raise NotImplementedError def lrn(inputs, depth_radius, bias, alpha, beta): """ Local Response Normalization. Parameters ---------- inputs : tensor Must be one of the following types: half, bfloat16, float32. 4-D. depth_radius : int Defaults to 5. 0-D. Half-width of the 1-D normalization window. bias : float Defaults to 1. An offset (usually positive to avoid dividing by 0). alpha : float Defaults to 1. A scale factor, usually positive. beta : float Defaults to 0.5. An exponent. Returns ------- A Tensor. Has the same type as input. """ pass def moments(x, axes, shift=None, keepdims=False): """ Calculates the mean and variance of x. Parameters ---------- x : tensor A Tensor axes : ints Axes along which to compute mean and variance. shift : int Not used in the current implementation. keepdims : bool produce moments with the same dimensionality as the input. Returns ------- Two Tensor objects: mean and variance. """ pass class MaxPool(object): def __init__(self, ksize, strides, padding, data_format=None): self.data_format, self.padding = preprocess_2d_format(data_format, padding) self.ksize = ksize self.strides = strides def __call__(self, inputs): return vision_ops.pool2d( inputs, kernel_shape=self.ksize, strides=self.strides, padding=self.padding, mode='MAX', global_pooling=False, data_format=self.data_format, ) def max_pool(input, ksize, strides, padding, data_format=None): """ Performs the max pooling on the input. Parameters ---------- input : tensor Tensor of rank N+2, of shape [batch_size] + input_spatial_shape + [num_channels] if data_format does not start with "NC" (default), or [batch_size, num_channels] + input_spatial_shape if data_format starts with "NC". Pooling happens over the spatial dimensions only. ksize : int or list of ints An int or list of ints that has length 1, N or N+2. The size of the window for each dimension of the input tensor. strides : int or list of ints An int or list of ints that has length 1, N or N+2. The stride of the sliding window for each dimension of the input tensor. padding : string 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details. Returns ------- A Tensor of format specified by data_format. The max pooled output tensor. """ pass class AvgPool(object): def __init__(self, ksize, strides, padding, data_format=None): self.data_format, self.padding = preprocess_2d_format(data_format, padding) self.filter_size = ksize self.strides = strides def __call__(self, inputs): return vision_ops.pool2d( inputs, kernel_shape=self.filter_size, strides=self.strides, padding=self.padding, mode='AVG', global_pooling=False, data_format=self.data_format, ) def avg_pool(input, ksize, strides, padding): """ Performs the avg pooling on the input. Parameters ---------- input : tensor Tensor of rank N+2, of shape [batch_size] + input_spatial_shape + [num_channels] if data_format does not start with "NC" (default), or [batch_size, num_channels] + input_spatial_shape if data_format starts with "NC". Pooling happens over the spatial dimensions only. ksize : int or list of ints An int or list of ints that has length 1, N or N+2. The size of the window for each dimension of the input tensor. strides : int or list of ints An int or list of ints that has length 1, N or N+2. The stride of the sliding window for each dimension of the input tensor. padding : string 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details. Returns ------- A Tensor of format specified by data_format. The average pooled output tensor. """ pass def max_pool3d(input, ksize, strides, padding, data_format=None, name=None): """ Performs the max pooling on the input. Parameters ---------- input : tensor A 5-D Tensor of the format specified by data_format. ksize : int or list of ints An int or list of ints that has length 1, 3 or 5. The size of the window for each dimension of the input tensor. strides : int or list of ints An int or list of ints that has length 1, 3 or 5. The stride of the sliding window for each dimension of the input tensor. padding : string 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details. data_format : string "NDHWC", "NCDHW". Defaults to "NDHWC". The data format of the input and output data. With the default format "NDHWC", the data is stored in the order of: [batch, in_depth, in_height, in_width, in_channels]. Alternatively, the format could be "NCDHW", the data storage order is: [batch, in_channels, in_depth, in_height, in_width]. name : string A name for the operation (optional). Returns ------- A Tensor of format specified by data_format. The max pooled output tensor. """ pass def avg_pool3d(input, ksize, strides, padding, data_format=None, name=None): """ Performs the average pooling on the input. Parameters ---------- input : tensor A 5-D Tensor of shape [batch, height, width, channels] and type float32, float64, qint8, quint8, or qint32. ksize : int or list of ints An int or list of ints that has length 1, 3 or 5. The size of the window for each dimension of the input tensor. strides : int or list of ints An int or list of ints that has length 1, 3 or 5. The stride of the sliding window for each dimension of the input tensor. padding : string 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details. data_format : string 'NDHWC' and 'NCDHW' are supported. name : string Optional name for the operation. Returns ------- A Tensor with the same type as value. The average pooled output tensor. """ pass def pool(input, window_shape, pooling_type, strides=None, padding='VALID', data_format=None, dilations=None, name=None): """ Performs an N-D pooling operation. Parameters ---------- input : tensor Tensor of rank N+2, of shape [batch_size] + input_spatial_shape + [num_channels] if data_format does not start with "NC" (default), or [batch_size, num_channels] + input_spatial_shape if data_format starts with "NC". Pooling happens over the spatial dimensions only. window_shape : int Sequence of N ints >= 1. pooling_type : string Specifies pooling operation, must be "AVG" or "MAX". strides : ints Sequence of N ints >= 1. Defaults to [1]*N. If any value of strides is > 1, then all values of dilation_rate must be 1. padding : string The padding algorithm, must be "SAME" or "VALID". Defaults to "SAME". See the "returns" section of tf.ops.convolution for details. data_format : string Specifies whether the channel dimension of the input and output is the last dimension (default, or if data_format does not start with "NC"), or the second dimension (if data_format starts with "NC"). For N=1, the valid values are "NWC" (default) and "NCW". For N=2, the valid values are "NHWC" (default) and "NCHW". For N=3, the valid values are "NDHWC" (default) and "NCDHW". dilations : list of ints Dilation rate. List of N ints >= 1. Defaults to [1]*N. If any value of dilation_rate is > 1, then all values of strides must be 1. name : string Optional. Name of the op. Returns ------- Tensor of rank N+2, of shape [batch_size] + output_spatial_shape + [num_channels] """ pass class DepthwiseConv2d(object): def __init__(self, strides, padding, data_format=None, dilations=None, ksize=None, channel_multiplier=1): self.data_format, self.padding = preprocess_2d_format(data_format, padding) self.stride = strides self.dilations = dilations def __call__(self, input, filter): raise NotImplementedError("Not implemented depthwiseconv2d") def depthwise_conv2d(input, filter, strides, padding, data_format=None, dilations=None, name=None): """ Depthwise 2-D convolution. Parameters ---------- input : tensor 4-D with shape according to data_format. filter : tensor 4-D with shape [filter_height, filter_width, in_channels, channel_multiplier]. strides : list 1-D of size 4. The stride of the sliding window for each dimension of input. padding : string 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details. data_format : string The data format for input. Either "NHWC" (default) or "NCHW". dilations : list 1-D of size 2. The dilation rate in which we sample input values across the height and width dimensions in atrous convolution. If it is greater than 1, then all values of strides must be 1. name : string A name for this operation (optional). Returns ------- A 4-D Tensor with shape according to data_format. E.g., for "NHWC" format, shape is [batch, out_height, out_width, in_channels * channel_multiplier]. """ pass def conv1d_transpose( input, filters, output_shape, strides, padding='SAME', data_format='NWC', dilations=None, name=None ): """ The transpose of conv1d. Parameters ---------- input : tensor A 3-D Tensor of type float and shape [batch, in_width, in_channels] for NWC data format or [batch, in_channels, in_width] for NCW data format. filters : tensor A 3-D Tensor with the same type as value and shape [filter_width, output_channels, in_channels]. filter's in_channels dimension must match that of value. output_shape : tensor A 1-D Tensor, containing three elements, representing the output shape of the deconvolution op. strides : list An int or list of ints that has length 1 or 3. The number of entries by which the filter is moved right at each step. padding : string 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details. data_format : string 'NWC' and 'NCW' are supported. dilations : list An int or list of ints that has length 1 or 3 which defaults to 1. The dilation factor for each dimension of input. If set to k > 1, there will be k-1 skipped cells between each filter element on that dimension. Dilations in the batch and depth dimensions must be 1. name : string Optional name for the returned tensor. Returns ------- A Tensor with the same type as value. """ pass def conv2d_transpose( input, filters, output_shape, strides, padding='SAME', data_format='NHWC', dilations=None, name=None ): """ The transpose of conv2d. Parameters ---------- input : tensor A 4-D Tensor of type float and shape [batch, height, width, in_channels] for NHWC data format or [batch, in_channels, height, width] for NCHW data format. filters : tensor A 4-D Tensor with the same type as input and shape [height, width, output_channels, in_channels]. filter's in_channels dimension must match that of input. output_shape : tensor A 1-D Tensor representing the output shape of the deconvolution op. strides : list An int or list of ints that has length 1, 2 or 4. The stride of the sliding window for each dimension of input. If a single value is given it is replicated in the H and W dimension. By default the N and C dimensions are set to 0. The dimension order is determined by the value of data_format, see below for details. padding : string 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details. data_format : string 'NHWC' and 'NCHW' are supported. dilations : list An int or list of ints that has length 1, 2 or 4, defaults to 1. name : string Optional name for the returned tensor. Returns ------- A Tensor with the same type as input. """ pass def conv3d_transpose( input, filters, output_shape, strides, padding='SAME', data_format='NDHWC', dilations=None, name=None ): """ The transpose of conv3d. Parameters ---------- input : tensor A 5-D Tensor of type float and shape [batch, height, width, in_channels] for NHWC data format or [batch, in_channels, height, width] for NCHW data format. filters : tensor A 5-D Tensor with the same type as value and shape [height, width, output_channels, in_channels]. filter's in_channels dimension must match that of value. output_shape : tensor A 1-D Tensor representing the output shape of the deconvolution op. strides : list An int or list of ints that has length 1, 3 or 5. padding : string 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details. data_format : string 'NDHWC' and 'NCDHW' are supported. dilations : list of ints An int or list of ints that has length 1, 3 or 5, defaults to 1. name : string Optional name for the returned tensor. Returns ------- A Tensor with the same type as value. """ pass class BatchNorm(object): def __init__(self): pass def __call__(self, *args, **kwargs): pass