简体   繁体   中英

“NameError: name 'np' is not defined” when trying to run a Keras segmentation model called PspNet

The thing is as follows: After installing all the packages and dependencies. I've ran the following code:

import numpy as np
from sys import exit
from sys import argv,exit
from keras import optimizers
from keras import backend as K
import keras_segmentation

And everything was OK. But, then I ran:

model = keras_segmentation.models.pspnet.vgg_pspnet(n_classes = 4)

And got this error:

2019-11-13 19:01:15.403630: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
  File "semantic_sg.py", line 27, in <module>
    model = keras_segmentation.models.pspnet.vgg_pspnet(n_classes = 4)
  File "C:\Users\rodgu\AppData\Local\Continuum\anaconda3\envs\tensorflow_env\lib\site-packages\keras_segmentation\models\pspnet.py", line 91, in vgg_pspnet
    model =  _pspnet( n_classes , get_vgg_encoder ,  input_height=input_height, input_width=input_width  )
  File "C:\Users\rodgu\AppData\Local\Continuum\anaconda3\envs\tensorflow_env\lib\site-packages\keras_segmentation\models\pspnet.py", line 63, in _pspnet
    pooled = pool_block(  o , p  )
  File "C:\Users\rodgu\AppData\Local\Continuum\anaconda3\envs\tensorflow_env\lib\site-packages\keras_segmentation\models\pspnet.py", line 34, in pool_block
    pool_size = strides = [int(np.round( float(h) /  pool_factor)), int(np.round(  float(w )/  pool_factor))]
NameError: name 'np' is not defined

Which is crazy, because I definitely have numpy installed. I ran the same code with a print(np.version.version) within and got: 1.17.3 (this also implies that numpy works everywhere except within the Keras function). I reinstalled numpy, and still nothing. I've been stuck here for days.

Thank's a lot!

Thanks to Aditya Shankar, I made it work. You must downgrade your python and then your tensorflow in order to use keras-segmentation package (at least, as far as I know). (Also, be sure to install any other dependencies listed in the setup.py file)

conda create -n lower_env pip python=3.6
conda activate lower_env
pip install --ignore-installed --upgrade tensorflow
conda install -c anaconda keras
pip install tensorflow==1.4
conda install -c conda-forge shapely
pip install imgaug>=0.2.9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM