Beispiel #1
0
def downsample(imgs, s, size=None):
    if size is None:
        size = utils.get_data_size(imgs)
    if size == 3:
        return np_downsample_3d(imgs, s)
    elif size == 2:
        return np_downsample_2d(imgs, s)
    elif size == 1:
        return np_downsample_1d(imgs, s)
    else:
        raise ValueError("Size should be 1,2,3 or None")
Beispiel #2
0
def up_sampler(x, s=2, size=None, smoothout=False):
    """Up sampling operation"""

    if size is None:
        size = utils.get_data_size(x)

    if not (size in [1, 2, 3]):
        raise ValueError("Wrong size parameter")

    bs = tf.shape(x)[0]
    dims = x.shape.as_list()[1:]

    if size == 3:
        filt = tf.constant(1, dtype=tf.float32, shape=[s, s, s, 1, 1])
        output_shape = [bs, dims[0] * s, dims[1] * s, dims[2] * s, dims[3]]
        return tf.nn.conv3d_transpose(x,
                                      filt,
                                      output_shape=output_shape,
                                      strides=[1, s, s, s, 1],
                                      padding='SAME')
    elif size == 2:
        filt = tf.constant(1, dtype=tf.float32, shape=[s, s, 1, 1])
        output_shape = [bs, dims[0] * s, dims[1] * s, 1]
        if dims[-1] == 1:
            x = tf.nn.conv2d_transpose(x,
                                       filt,
                                       output_shape=output_shape,
                                       strides=[1, s, s, 1],
                                       padding='SAME')

            if smoothout:
                paddings = tf.constant([[0, 0], [(s - 1) // 2, s // 2],
                                        [(s - 1) // 2, s // 2], [0, 0]])
                x = tf.pad(x, paddings, "SYMMETRIC")
                x = tf.nn.conv2d(x,
                                 filt / (s * s),
                                 strides=[1, 1, 1, 1],
                                 padding='VALID')
            return x
        else:
            res = []
            for sl in tf.split(x, dims[-1], axis=3):
                tx = tf.nn.conv2d_transpose(sl,
                                            filt,
                                            output_shape=output_shape,
                                            strides=[1, s, s, 1],
                                            padding='SAME')
                if smoothout:
                    paddings = tf.constant([[0, 0], [(s - 1) // 2, s // 2],
                                            [(s - 1) // 2, s // 2], [0, 0]])
                    tx = tf.pad(tx, paddings, "SYMMETRIC")
                    tx = tf.nn.conv2d(tx,
                                      filt / (s * s),
                                      strides=[1, 1, 1, 1],
                                      padding='VALID')
                res.append(tx)
            return tf.concat(res, axis=3)
    else:
        filt = tf.constant(1, dtype=tf.float32, shape=[s, 1, 1])
        output_shape = [bs, dims[0] * s, dims[1]]
        return tf.nn.conv1d_transpose(x,
                                      filt,
                                      output_shape=output_shape,
                                      strides=s,
                                      padding='SAME')
Beispiel #3
0
def down_sampler(x=None, s=2, size=None):
    '''
    Op to downsample 2D or 3D images by factor 's'.
    This method works for both inputs: tensor or placeholder
    '''

    if size is None:
        size = utils.get_data_size(x)

    if not (size in [1, 2, 3]):
        raise ValueError("Wrong size parameter")

    # The input to the downsampling operation is a placeholder.
    if x is None:
        if size == 3:
            addname = '3d_'
        elif size == 2:
            addname = '2d_'
        else:
            addname = '1d_'
        placeholder_name = 'down_sampler_in_' + addname + str(s)
        down_sampler_x = tf.placeholder(dtype=tf.float32,
                                        name=placeholder_name)
        op_name = 'down_sampler_out_' + addname + str(s)

    # The input to the downsampling operation is the input tensor x.
    else:
        down_sampler_x = x
        op_name = None

    if size == 3:
        filt = tf.constant(1 / (s * s * s),
                           dtype=tf.float32,
                           shape=[s, s, s, 1, 1])
        return tf.nn.conv3d(down_sampler_x,
                            filt,
                            strides=[1, s, s, s, 1],
                            padding='SAME',
                            name=op_name)

    elif size == 2:
        filt = tf.constant(1 / (s * s), dtype=tf.float32, shape=[s, s, 1, 1])
        ldim = down_sampler_x.shape[-1]
        if ldim == 1 or not (isinstance(ldim, int)):
            return tf.nn.conv2d(down_sampler_x,
                                filt,
                                strides=[1, s, s, 1],
                                padding='SAME',
                                name=op_name)
        else:
            res = []
            for sl in tf.split(down_sampler_x,
                               down_sampler_x.shape[-1],
                               axis=3):
                if op_name is not None:
                    op_name += 'I'
                res.append(
                    tf.nn.conv2d(sl,
                                 filt,
                                 strides=[1, s, s, 1],
                                 padding='SAME',
                                 name=op_name))
            return tf.concat(res, axis=3)
    else:
        filt = tf.constant(1 / s, dtype=tf.float32, shape=[s, 1, 1])
        return tf.nn.conv1d(down_sampler_x,
                            filt,
                            stride=s,
                            padding='SAME',
                            name=op_name)