1

I'm not sure if the title of this question is correct, mods please feel free to change it.

I'm working through cs231n Convolutional Neural Networks for Visual Recognition course online and I've hit a weird error. This is my first attempt at any real Python programming so it could be a simple error, or something more complicated.

I'm using Python 2.7, Anaconda, and Windows 7.

When trying to run a script I get the following error;

NameError: global name 'im2col_cython' is not defined

My understanding so far is that this occurs because this fails (and calls the exception);

try:
  from cs231n.im2col_cython import col2im_cython, im2col_cython
  from cs231n.im2col_cython import col2im_6d_cython
except ImportError:
  print 'run the following from the cs231n directory and try again:'
  print 'python setup.py build_ext --inplace'
  print 'You may also need to restart your iPython kernel'

I've tried to figure out why this may be the case. First off I have to run setup.py to turn im2col_cython.pyx into other files. This seems to run but does at one point have the warning;

warning: extension name 'im2col_cython' does not match fully qualified name 'cs231n.im2col_cython' of 'im2col_cython.pyx'

My figuring here is that it's an issue to do with the fact that the folder tree I have looks like this; Assignment 2 ->cs231n

Inside cs231n is where the setup.py and im2col_cython.pyx files are located. I've installed cython, and I have vc for python2.7 installed. When I run setup.py is creates 2 new files;

im2col_cython.c
im2col_cython.pyd

But I have no idea if that's what it's meant to do or anything. I'm in way over my head with this (0 Python knowledge), but I'm keen to learn!

EDIT 1 The script that imports im2col_cython is here

The im2col_cython file is here

EDIT 2

When including the following in the exception clause;

  import traceback; traceback.print_exc()

I get this output;

Traceback (most recent call last):
  File "fast_layers.py", line 3, in <module>
    from cs231n.im2col_cython import col2im_cython, im2col_cython
ImportError: No module named cs231n.im2col_cython
FraserOfSmeg
  • 1,128
  • 2
  • 23
  • 41
  • did you restart your iPython kernel whatever that is? – Matthias Apr 07 '17 at 15:23
  • These look like similar errors http://stackoverflow.com/questions/39398154/run-cython-python-setup-py-build-ext-inplace-cs231n http://stackoverflow.com/questions/38993332/global-name-col2im-6d-cython-is-not-defined-cs231n but I don't know how useful the answers actually are. It might be worth looking at them anyway if you haven't already. – DavidW Apr 07 '17 at 15:24
  • also, did you look at the `cs231n.im2col_cython` file itself and see if there could be a reason `im2col_cython` is not defined? – Matthias Apr 07 '17 at 15:24
  • 1
    @Matthias, I've restarted my Anaconda (which I believe restarts the iPython Kernel). I've looked in the file and it seems to have a function named im2col_cython – FraserOfSmeg Apr 07 '17 at 15:47
  • @DavidW that does look like a suspiciously like the issue I'm having. But the only answer to the question doesn't work for me (if i remove the im2col_cython.pyx file the setup.py doesn't work as it returns an error: im2col_cython.pyx doesn't match any files. – FraserOfSmeg Apr 07 '17 at 15:49
  • guess I'd have to see more of the code then (or more of the stack trace if there is any) – Matthias Apr 07 '17 at 16:00
  • @Matthias I've provided links to the files on github. – FraserOfSmeg Apr 07 '17 at 16:14
  • @FraserOfSmeg Can you add a line in the `except ImportError:` block of: `import traceback; traceback.print_exc()`. I can promise it won't fix the problem, but it's possible that it might generate a more informative error message... – DavidW Apr 07 '17 at 16:17
  • For what it's worth I can't get it to work easily either... – DavidW Apr 07 '17 at 16:40
  • @DavidW Thanks, it does make me feel less like a fool! I've added the traceback.print info to the question! – FraserOfSmeg Apr 07 '17 at 17:46

2 Answers2

0

Add this in the file fast_layer.py before import cs231n.im2col_cython:

import pyximport
pyximport.install()

The problem you mentioned should be fixed.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
0

I also meet this problem. I tryed the above solution,and it was solved. I don't know why, here are my steps.

I used the Google Colab to run the experience, and the code stored in Googel Drive.The Colab is Python3.10,None GPU.

Solution: I add the two lines to the top of the code.

import pyximport
pyximport.install()

code are cs231n ConvolutionNetworks of spring 2023:

# Add here two lines
# import pyximport
# pyximport.install()

# Rel errors should be around e-9 or less.
from cs231n.fast_layers import conv_forward_fast, conv_backward_fast
from time import time
np.random.seed(231)
x = np.random.randn(100, 3, 31, 31)
w = np.random.randn(25, 3, 3, 3)
b = np.random.randn(25,)
dout = np.random.randn(100, 25, 16, 16)
conv_param = {'stride': 2, 'pad': 1}

t0 = time()
out_naive, cache_naive = conv_forward_naive(x, w, b, conv_param)
t1 = time()
out_fast, cache_fast = conv_forward_fast(x, w, b, conv_param)
t2 = time()
···

And I tryed to delete the two line, the code also can run.

Attempt

When I run the code, it was wrong.

NameError: name 'col2im_6d_cython' is not defined

I found many advices about it.

Firstly, I tryed to delete the files im2col_cython.c and im2col_cython.cpython-310-x86_64-linux-gnu.so.Then, I restart run the code:

%cd /content/drive/My\ Drive/$FOLDERNAME/cs231n/
!python setup.py build_ext --inplace
%cd /content/drive/My\ Drive/$FOLDERNAME/

But, this problem also exist.

Secondly, I go to the file fast_layers.py to chage the import link; but it have no effort.

Lastly, I add the two code to the top, it's OK. Then I delete it, it's also run well.

Here are the outputs:

Testing conv_forward_fast:
Naive: 0.302786s
Fast: 0.013577s
Speedup: 22.300660x
Difference:  4.926407851494105e-11

Testing conv_backward_fast:
Naive: 1.151665s
Fast: 0.020583s
Speedup: 55.951166x
dx difference:  1.006330206620509e-11
dw difference:  1.6835243644808872e-13
db difference:  0.0