[FIXED] I got attribute error while doing convolution

Issue

I was trying to test my model for making a convolution using MNIST data

keep_prob=1.0
ActivatedUnits = convolve1(sampleimage)
                           
filters = ActivatedUnits.shape[3]
plt.figure(1, figsize=(20,20))
n_columns = 6
n_rows = np.math.ceil(filters / n_columns) + 1
for i in range(filters):
    plt.subplot(n_rows, n_columns, i+1)
    plt.title('Filter ' + str(i))
    plt.imshow(ActivatedUnits[0,:,:,i], interpolation="nearest", cmap="gray")

error message:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_19/3245363145.py in <module>
      1 #ActivatedUnits = sess.run(convolve1,feed_dict={x:np.reshape(sampleimage,[1,784],order='F'),keep_prob:1.0})
      2 keep_prob=1.0
----> 3 ActivatedUnits = convolve1(sampleimage)
      4 
      5 filters = ActivatedUnits.shape[3]

/tmp/ipykernel_19/1030351309.py in convolve1(x)
      1 def convolve1(x):
      2     return(
----> 3         tf.nn.conv2d(x, W_conv1, strides=[1, 1, 1, 1], padding='SAME') + b_conv1)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    204     """Call target, and fall back on dispatchers if there is a TypeError."""
    205     try:
--> 206       return target(*args, **kwargs)
    207     except (TypeError, ValueError):
    208       # Note: convert_to_eager_tensor currently raises a ValueError, not a

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py in conv2d_v2(input, filters, strides, padding, data_format, dilations, name)
   2286                 data_format=data_format,
   2287                 dilations=dilations,
-> 2288                 name=name)
   2289 
   2290 

/opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    204     """Call target, and fall back on dispatchers if there is a TypeError."""
    205     try:
--> 206       return target(*args, **kwargs)
    207     except (TypeError, ValueError):
    208       # Note: convert_to_eager_tensor currently raises a ValueError, not a

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py in conv2d(input, filter, strides, padding, use_cudnn_on_gpu, data_format, dilations, name, filters)
   2377   dilations = _get_sequence(dilations, 2, channel_index, "dilations")
   2378 
-> 2379   shape = input.shape
   2380   # shape object may lack ndims, e.g., if input is an np.ndarray.  In that case,
   2381   # we fall back to len(shape).

AttributeError: 'list' object has no attribute 'shape'

I got this error. I am not getting the error this indicated
https://www.kaggle.com/code/gajnaselvi/cnn-convolution-neural-network this is my Kaggle notebook. Please help me 🙂

Solution

As Dr. Snoopy said, this error raised due to the sample image isn’t a NumPy array.
therefore I added a line :

sampleimage=np.array(sampleimage)

to my code:

keep_prob=1.0
ActivatedUnits = convolve1(sampleimage)
                           
filters = ActivatedUnits.shape[3]
plt.figure(1, figsize=(20,20))
n_columns = 6
n_rows = np.math.ceil(filters / n_columns) + 1
for i in range(filters):
    plt.subplot(n_rows, n_columns, i+1)
    plt.title('Filter ' + str(i))
    plt.imshow(ActivatedUnits[0,:,:,i], interpolation="nearest", cmap="gray")

Thanks a lot, sir! 🙂

Answered By – gsv

Answer Checked By – Senaida (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published