NameError: name 'max_pool2D' is not defined

1.9k views Asked by At

I'm trying to run a code I acquired from Github for Light Field reconstruction using a CNN constructed with tensorflow. I've created a virtual environment and installed all the required packages given in the requirements.txt file, which are:

easydict==1.9

imageio==2.4.1

numpy==1.15.4

scikit-image==0.14.1

scipy==1.2.0

tensorflow-gpu==1.14.0

Python version = 3.7

Tensorflow is CUDA enabled with CUDA 10 and cudnn 7.6.5.

When I run the code, I get the following name error:

WARNING:tensorflow:From user\Documents\LFRNet-master\tensorlayer\layers\core.py:55: The name tf.GraphKeys is deprecated. Please use tf.compat.v1.GraphKeys instead.

WARNING:tensorflow:From user\Documents\LFRNet-master\tensorlayer\layers\pooling.py:66: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

[TL] [!] example_data/valid/bead/VCD_bead_40x_[m30-30]_step1um/ exists ...
user\anaconda3\envs\Tensorflow Final\lib\site-packages\imageio\plugins\_tifffile.py:7285: UserWarning: module 'imageio.plugins._tifffile' has no attribute 'decode_packbits'
  Functionality might be degraded or be slow.

  warnings.warn("%s%s" % (e, warn))
user\anaconda3\envs\Tensorflow Final\lib\site-packages\imageio\plugins\_tifffile.py:7285: UserWarning: module 'imageio.plugins._tifffile' has no attribute 'decode_lzw'
  Functionality might be degraded or be slow.

  warnings.warn("%s%s" % (e, warn))
user\anaconda3\envs\Tensorflow Final\lib\site-packages\imageio\plugins\_tifffile.py:7285: UserWarning: module 'imageio.plugins._tifffile' has no attribute 'unpack_ints'
  Functionality might be degraded or be slow.

  warnings.warn("%s%s" % (e, warn))
user\envs\Tensorflow Final\lib\site-packages\imageio\plugins\_tifffile.py:7285: UserWarning: module 'imageio.plugins._tifffile' has no attribute 'reverse_bitorder'
  Functionality might be degraded or be slow.

  warnings.warn("%s%s" % (e, warn))
read 1 from example_data/valid/bead/
WARNING:tensorflow:From eval.py:64: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From user\Documents\LFRNet-master\model\unet.py:166: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead.

WARNING:tensorflow:From user\Documents\LFRNet-master\tensorlayer\layers\core.py:410: The name tf.get_variable_scope is deprecated. Please use tf.compat.v1.get_variable_scope instead.

[TL] InputLayer  unet/lf_extra: (1, 84, 83, 121)
[TL] Conv2d unet/conv1/conv: shape:(5, 5, 121, 128) strides:(1, 1, 1, 1) pad:SAME act:identity
WARNING:tensorflow:From user\Documents\LFRNet-master\tensorlayer\layers\convolution.py:1481: The name tf.get_variable is deprecated. Please use tf.compat.v1.get_variable instead.

WARNING:tensorflow:From user\anaconda3\envs\Tensorflow Final\lib\site-packages\tensorflow\python\ops\init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:

Call initializer instance with the dtype argument instead of passing it to the constructor


[TL] SubpixelConv2d  upsale0/subpixel: scale: 2 n_out_channel: None act: identity
[TL] Conv2d unet/interp/upsale0/conv: shape:(3, 3, 32, 64) strides:(1, 1, 1, 1) pad:SAME act:identity
[TL] SubpixelConv2d  upsale1/subpixel: scale: 2 n_out_channel: None act: identity
[TL] Conv2d unet/interp/upsale1/conv: shape:(3, 3, 16, 32) strides:(1, 1, 1, 1) pad:SAME act:identity
[TL] SubpixelConv2d  upsale2/subpixel: scale: 2 n_out_channel: None act: identity
[TL] Conv2d unet/interp/upsale2/conv: shape:(3, 3, 8, 16) strides:(1, 1, 1, 1) pad:SAME act:identity
[TL] SubpixelConv2d  upsale3/subpixel: scale: 2 n_out_channel: None act: identity
[TL] Conv2d unet/interp/upsale3/conv: shape:(3, 3, 4, 8) strides:(1, 1, 1, 1) pad:SAME act:identity
[TL] Conv2d unet/encoder/conv0/conv: shape:(3, 3, 8, 64) strides:(1, 2, 2, 1) pad:SAME act:identity
encoder 0 : (1, 672, 664, 64)
[TL] Conv2d unet/encoder/conv1/conv: shape:(3, 3, 64, 64) strides:(1, 1, 1, 1) pad:SAME act:identity
Traceback (most recent call last):
  File "eval.py", line 128, in <module>
    infer(ckpt, batch_size=batch_size, use_cpu=use_cpu)
  File "eval.py", line 69, in infer
    net = UNet_B(t_image, n_slices, [height * n_num, width * n_num], is_train=True, reuse=False, name='unet')
  File "user\Documents\LFRNet-master\model\unet.py", line 189, in UNet_B
    n = max_pool2d(n, filter_size=2, stride=2)
NameError: name 'max_pool2d' is not defined

Yes, I know there are a lot of warnings beforehand, but I'm trying to change the code as little as possible and just get it to run first. I'm not sure if any of the warnings are the reason I'm getting the NameError.

The max_pool2d is a part of the UNET algorithm used within the code, which is:

def UNet_B(lf_extra, n_slices, output_size, 
        n_pyramid_levels=4,
        n_base_filters=64,
        using_batch_norm=False,
        is_train=False,
        last_act=tf.nn.relu,
        reuse=False, name='unet'):
'''U-net based VCD-Net for sparse light field reconstruction, faster than UNet_A
Params:
    lf_extra: tf.tensor 
        In shape of [batch, height, width, n_num^2], the extracted views from the light field image
    n_slices: int
        The slices number of the 3-D reconstruction.
    output_size: list of int
        Lateral size of the 3-D reconstruction, i.e., [height, width].
    using_batch_norm: boolean
        Whether using batch normalization after each convolutional layer. 
    is_train: boolean, only valid when using_batch_norm=True.
        Sees tl.layers.BatchNormLayer.
    last_act: tensorflow activation functions
        Acivation function applied to the final layer.
    reuse: boolean 
        Whether to reuse the variables or not. See tf.variable_scope() for details.
    name: string
        The name of the variable scope.
Return:
    The 3-D reconstruction in shape of [batch, height, width, depth=n_slices]
'''    
n_interp = 4
channels_interp = 128
act = tf.nn.relu

 
with tf.variable_scope(name, reuse=reuse):
    n = InputLayer(lf_extra, 'lf_extra')
    # n = conv2d(n, n_filter=channels_interp, filter_size=5, name='conv1')
    n = conv(n, n_filter=channels_interp, filter_size=5, act=act, using_batch_norm=using_batch_norm, is_train=is_train, name='conv1')

    ## Up-scale input
    with tf.variable_scope('interp'): 
        for i in range(n_interp):
            channels_interp = channels_interp / 2
            n = upscale(n, out_channels=channels_interp, scale=2, mode='subpixel', name='upsale%d' % i)
            
    pyramid_channels = [n_base_filters * i for i in range(1, n_pyramid_levels + 1)] # output channels number of each conv layer in the encoder
    encoder_layers = []
    with tf.variable_scope('encoder'):
        # n = conv2d(n, n_filter=64, filter_size=3, stride=2, name='conv0')
        n = conv(n, n_filter=64, filter_size=3, stride=2, act=act, using_batch_norm=using_batch_norm, is_train=is_train, name='conv0')

        for idx, nc in enumerate(pyramid_channels):
            encoder_layers.append(n) # append n0, n1, n2, n3, n4 (but without n5)to the layers list
            print('encoder %d : %s' % (idx, str(n.outputs.get_shape())))
            n = LReluLayer(n, name='relu%d' % (idx + 1))
            # n = conv2d(n, n_filter=nc, filter_size=3, name='conv%d' % (idx + 1)) 
            n = conv(n, n_filter=nc, filter_size=3, act=act, using_batch_norm=using_batch_norm, is_train=is_train, name='conv%d' % (idx + 1)) 
            n = max_pool2d(n, filter_size=2, stride=2)

    nl = len(encoder_layers)        
    with tf.variable_scope('decoder'):
        _, h, w, _ = encoder_layers[-1].outputs.shape.as_list()
        n = ReluLayer(n, name='relu1')
        n = upscale(n, out_channels=pyramid_channels[-1], out_size=(h, w), mode='upconv', name='upsale1')
        
        for idx in range(nl - 1, -1, -1): # idx = 4,3,2,1,0
            if idx > 0:
                _, h, w, _ = encoder_layers[idx - 1].outputs.shape.as_list()
                out_size = (h, w)
                out_channels = pyramid_channels[idx-1]
                
            else:
                out_size = output_size
                out_channels = n_base_filters
            
            print('decoder %d : %s' % (idx, str(n.outputs.get_shape())))
            n = ConcatLayer([encoder_layers[idx], n], concat_dim=-1, name='concat%d' % (nl - idx))
            n = ReluLayer(n, name='relu%d' % (nl - idx + 1))
            n = upscale(n, out_channels=out_channels, out_size=out_size, mode='upconv', name='upscale%d' % (nl - idx + 1))
            #n = DropoutLayer(n, keep=0.5, is_fix=True, is_train=is_train, name='dropout1')

        n = conv2d(n, n_filter=n_slices, filter_size=3, act=last_act, name='out')  
        return n

If I try to import tensorflow.nn.max_pool2s, I get a barrage of other errors. I'm fairly new to tensorflow, so there's a good chance I'm messing up somewhere myself, but every solution I've tried so far has failed, including importing tensorflor.nn.max_poold2d and changing "filter_size" argument to "kernel_size" (which, again, leads to a fountain of other errors).

Any sort of help would be greatly appreciated, as I've spent a really long time trying to run this code.

1

There are 1 answers

0
Whole Brain On

You actually have to import tf.nn.max_pool2d. There is no way around this.

Check this doc from you tensorflow version to get the parameters exact names : https://github.com/tensorflow/docs/blob/r1.14/site/en/api_docs/python/tf/nn/max_pool2d.md