TypeError: conv2d_v2() got an unexpected keyword argument 'filter

Viewed 4649

Was trying to run the code from a course from Coursera on my device with pretrained VGG model. Getting this error while running the model. This is a question regarding neural style transfer with a pretrained model using a VGG model.

pp = pprint.PrettyPrinter(indent=4)
model = load_vgg_model("pretrained-model/imagenet-vgg-verydeep-19.mat")
pp.pprint(model)

TypeError                                 Traceback (most recent call last)
<ipython-input-7-2a76ad487818> in <module>
      1 pp = pprint.PrettyPrinter(indent=4)
----> 2 model = load_vgg_model("pretrained-model/imagenet-vgg-verydeep-19.mat")
      3 pp.pprint(model)

D:\College\Machine learning\Coursera\c4\jovyan\work\week4\Neural Style Transfer\nst_utils.py in load_vgg_model(path)
    127     graph = {}
    128     graph['input']   = tf.Variable(np.zeros((1, CONFIG.IMAGE_HEIGHT, CONFIG.IMAGE_WIDTH, CONFIG.COLOR_CHANNELS)), dtype = 'float32')
--> 129     graph['conv1_1']  = _conv2d_relu(graph['input'], 0, 'conv1_1')
    130     graph['conv1_2']  = _conv2d_relu(graph['conv1_1'], 2, 'conv1_2')
    131     graph['avgpool1'] = _avgpool(graph['conv1_2'])

D:\College\Machine learning\Coursera\c4\jovyan\work\week4\Neural Style Transfer\nst_utils.py in _conv2d_relu(prev_layer, layer, layer_name)
    116         model at 'layer'.
    117         """
--> 118         return _relu(_conv2d(prev_layer, layer, layer_name))
    119 
    120     def _avgpool(prev_layer):

D:\College\Machine learning\Coursera\c4\jovyan\work\week4\Neural Style Transfer\nst_utils.py in _conv2d(prev_layer, layer, layer_name)
    109         W = tf.constant(W)
    110         b = tf.constant(np.reshape(b, (b.size)))
--> 111         return tf.nn.conv2d(prev_layer, strides=[1, 1, 1, 1], padding='SAME') + b
    112 
    113     def _conv2d_relu(prev_layer, layer, layer_name):

D:\Applications\Anaconda\lib\site-packages\tensorflow\python\util\dispatch.py in wrapper(*args, **kwargs)
    199     """Call target, and fall back on dispatchers if there is a TypeError."""
    200     try:
--> 201       return target(*args, **kwargs)
    202     except (TypeError, ValueError):
    203       # Note: convert_to_eager_tensor currently raises a ValueError, not a

TypeError: conv2d_v2() got an unexpected keyword argument 'filter'

This is my function to load the model.

def load_vgg_model(path):
   
    vgg = scipy.io.loadmat(path)

    vgg_layers = vgg['layers']

    def _conv2d(prev_layer, layer, layer_name):

        W, b = _weights(layer, layer_name)
        W = tf.constant(W)
        b = tf.constant(np.reshape(b, (b.size)))
        return tf.nn.conv2d(prev_layer, filter=W, strides=[1, 1, 1, 1], padding='SAME') + b

    def _conv2d_relu(prev_layer, layer, layer_name):

        return _relu(_conv2d(prev_layer, layer, layer_name))

I am pretty confused as the same model runs on Coursera but doesn't run on my PC.

3 Answers

The Coursera course on Convolutional Neural Networks uses TensorFlow 1.2.1

I got into this same error today while trying to run this notebook on Google Collab.

The solution for me was:

pip uninstall tensorflow==2.3.0

pip install tensorflow==1.2.1

Try using

tf.nn.conv2d(prev_layer, filters=W, strides=[1, 1, 1, 1], padding='SAME') + b

Notice, filter is now plural, its filters

Try using:

tf.nn.conv2d(prev_layer, W, strides=[1, 1, 1, 1], padding='SAME') + b
Related