ImportError: No module named keras.preprocessing - python-3.x

Following the tutorial:
http://www.pyimagesearch.com/2016/08/10/imagenet-classification-with-python-and-keras/#comment-419896
Using these files:
https://github.com/fchollet/deep-learning-models
I get 2 separate errors depending on how I execute:
Running in PyCharm:
Using TensorFlow backend.
usage: test_imagenet.py [-h] -i IMAGE
test_imagenet.py: error: the following arguments are required: -i/--image
Running in cmd line:
C:\Users\AppData\Local\Programs\Python\Python35\Scripts>python deep-learning-models/test_imagenet.py --image deep-learning-models/images/dog.jpg
Traceback (most recent call last):
File "deep-learning-models/test_imagenet.py", line 2, in <module>
from keras.preprocessing import image as image_utils
ImportError: No module named keras.preprocessing
How do I resolve?

Its best if you solve this problem outside running the above script... Here is what you can try in your command line environment to make sure it works outside your script:
>>> import keras
Using TensorFlow backend.
>>> keras.__version__
'1.2.1'
>>> keras.preprocessing
<module 'keras.preprocessing' from '/usr/local/lib/python2.7/dist-packages/keras/preprocessing/__init__.pyc'>
>>> from keras.preprocessing import image as image_utils
>>>
Make sure you have latest version of keras installed. If you get above working then it could be the environment issue where above script is not able to find the keras package. However if above does not work or work partially you would need to install keras again by removing it first..
$ pip install keras --user

Every dependency in a python project need to be installed using pip or easy_install or from the source code. You will have to install the keras module as mentioned here.

This happened to me. It turned out I was working in a pyvenv which wasn't activated. Just run source bin/activate on Linux/Mac or Scripts\activate.bat on Windows

from keras.models import Sequential
from keras import legacy_tf_layer
from keras.preprocessing import image as image_utils
from keras.preprcessing.text import Toknizer
import pandas as pd
from sklearn.model_selection import train_test_spli

Related

spacy.training module not found in collab

Code is running properly in local windows machine but gives error while importing spacy.training in collab
what can be the problem?
from spacy.training import Example
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-ebc5aa3cff21> in <module>()
3 from spacy.util import minibatch, compounding
4 from pathlib import Path
----> 5 from spacy.training import Example
6 # TRAINING THE MODEL
7 with nlp.disable_pipes(*unaffected_pipes):
ModuleNotFoundError: No module named 'spacy.training'
I believe You have spacy 2 on your colab.
You need to import spacy with all the modules again on colab.
It is independent from Windows spacy.
You can check your version of spacy and update it using below instructions:
Run this command on your Jupyter cell:
!pip show spacy
OR
spacy.__version__
Once you check the version, you can update to a latest version by:
!pip install -U spacy
Once, you get spacy 3 your problem will be resolved.
Example was introduced in spacy version 3.
Link to Example :
https://spacy.io/api/example#_title
Make sure to Restart Runtime from the menu bar in colab.

ImportError: cannot import name 'Graph'

I am on windows using Anaconda with python 3.6, I have installed keras by:
conda install -c conda-forge keras
and whene i try this code
from keras.models import Graph
I get this error message:
ImportError Traceback (most recent call last)
<ipython-input-4-74d2d1fd7bad> in <module>()
----> 1 from keras.models import Graph
ImportError: cannot import name 'Graph'
This is not a Python issue. This is because the latest version of keras has removed Graph module from models. You can surely check the documentation.
If you check this Github issue (Why Keras remove graph model?), you can use the following line of code:
from .legacy.models import Graph
However, it is suggested to use functional API instead. Please check The Functional API

Error while importing datasets from sklearn

import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import svm
it gives the following error-
Traceback (most recent call last):
File "/home/songoku/PycharmProjects/untitled/sklearn.py", line 4, in <module>
from sklearn import datasets
File "/home/songoku/PycharmProjects/untitled/sklearn.py", line 4, in <module>
from sklearn import datasets
ImportError: cannot import name 'datasets'
Rename /home/songoku/PycharmProjects/untitled/sklearn.py to something that differs from the SKLearn module name: sklearn.py
Try checking the version of sklearn first. The modules like data-sets and svm is not a part of sklearn packages (0.19 <) i.e less than version number 0.19.0.
You can check this on the command line: pip3 list
it shows you the version of all libraries installed by pip3.
Note: Always double check if you istalled the libraries as a part of anaconda or pip3

sklearn.linear_model not found in TensorFlow Udacity course

I'm following the instructions of the Deep Learning course by Google with TensorFlow. Unfortunately I'm stuck with this workbook right now.
I work in the docker vm with all the assignment code loaded as described here.
When I do all the imports everything works except for following line:
from sklearn.linear_model import LogisticRegression
it throws the following error:
>>> from sklearn.linear_model import LogisticRegression
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named sklearn.linear_model
This SO answer sounds promising, but I did not find the source directory of sklearn.
Any help greatly aprreciated.
You can install and upgrade sklearn from the shell with pip. That may or may not be the problem - but at least you'll know its installed.
sudo pip install --upgrade scikit-learn
In your Jupyter notebook:
import pip
pip.main(['install', 'sklearn'])
Please take note that when you are writing your code, you'll import the sklearn package using import sklearn, but when installing the package it with, say, conda, you should do the following:
conda install scikit-learn

Scikit Learn import error: 'cross_val_predict' is not defined

I am trying to run some simple codes of scikit-learn in python, and while executing this, I encountered this error:
from sklearn.cross_validation import cross_val_predict
Traceback (most recent call last):
File "", line 1, in
from sklearn.cross_validation import cross_val_predict
ImportError: cannot import name cross_val_predict
I have downloaded scikit learn from this page: http://sourceforge.net/projects/scikit-learn/?source=typ_redirect and the other modules like sklearn.linear_model works well. It seems that I can also import successfully the module cross_validation.. I cannot understand!
Yes ! It seems works now ! In fact, instead of using the .exe files that I downloaded from internet (which turned out to be perhaps incompatible), I finally successed to install many modules by the standard "pip install". The only thing that I could not use "pip install" is the package "Scipy", so I had to replace it by a .exe file. Thank you for all your help !

Resources