Spaces:
Runtime error
Runtime error
File size: 3,237 Bytes
de947e8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import logging, os
logging.disable(logging.WARNING)
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import tensorflow as tf
from network_configure import conf_basic_ops
"""This script defines basic operaters.
"""
def convolution_2D(inputs, filters, kernel_size, strides, use_bias, name=None):
"""Performs 2D convolution without activation function.
If followed by batch normalization, set use_bias=False.
"""
return tf.layers.conv2d(
inputs=inputs,
filters=filters,
kernel_size=kernel_size,
strides=strides,
padding='same',
use_bias=use_bias,
kernel_initializer=conf_basic_ops['kernel_initializer'],
name=name,
)
def convolution_3D(inputs, filters, kernel_size, strides, use_bias, name=None):
"""Performs 3D convolution without activation function.
If followed by batch normalization, set use_bias=False.
"""
return tf.layers.conv3d(
inputs=inputs,
filters=filters,
kernel_size=kernel_size,
strides=strides,
padding='same',
use_bias=use_bias,
kernel_initializer=conf_basic_ops['kernel_initializer'],
name=name,
)
def transposed_convolution_2D(inputs, filters, kernel_size, strides, use_bias, name=None):
"""Performs 2D transposed convolution without activation function.
If followed by batch normalization, set use_bias=False.
"""
return tf.layers.conv2d_transpose(
inputs=inputs,
filters=filters,
kernel_size=kernel_size,
strides=strides,
padding='same',
use_bias=use_bias,
kernel_initializer=conf_basic_ops['kernel_initializer'],
name=name,
)
def transposed_convolution_3D(inputs, filters, kernel_size, strides, use_bias, name=None):
"""Performs 3D transposed convolution without activation function.
If followed by batch normalization, set use_bias=False.
"""
return tf.layers.conv3d_transpose(
inputs=inputs,
filters=filters,
kernel_size=kernel_size,
strides=strides,
padding='same',
use_bias=use_bias,
kernel_initializer=conf_basic_ops['kernel_initializer'],
name=name,
)
def batch_norm(inputs, training, name=None):
"""Performs a batch normalization.
We set fused=True for a significant performance boost.
See https://www.tensorflow.org/performance/performance_guide#common_fused_ops
"""
return tf.layers.batch_normalization(
inputs=inputs,
momentum=conf_basic_ops['momentum'],
epsilon=conf_basic_ops['epsilon'],
center=True,
scale=True,
training=training,
fused=True,
name=name,
)
def relu(inputs, name=None):
return tf.nn.relu(inputs, name=name) if conf_basic_ops['relu_type'] == 'relu' \
else tf.nn.relu6(inputs, name=name)
|