keras import fails "no module named contrib.ctc" - keras

On OSX El CApitan 10.11.6 I've installed theano & tensorflow fine. But the keras install is not happy as seen from the trace below. I've seen this reported on github issues but with no solution there, only a suggestion to post here on SO.
>>>import theano
>>>
>>> import tensorflow
>>>
>>> import keras
File "/Users/petercotton/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 6, in <module>
import tensorflow.contrib.ctc as ctc
ImportError: No module named contrib.ctc`
Suggestions appreciated.

This is a problem with Keras, not TensorFlow and a known issue.
A workaround is mentioned here but that would mean that you have to modify Keras code (keras/backend/tensorflow_backend.py).
Luckily, it seems that this issue is fixed in the master branch of Keras.

Related

python3 keras import error with both tensorflow and theano

With python3 (version 3.6.8) and keras
the simple script:
import keras
gives an error:
Using TensorFlow backend.
Ungültiger Maschinenbefehl (Speicherabzug geschrieben)
(in english it would be something like: "invalid machine command (memory image written)")
So I tried to use theano instead:
import os
os.environ['KERAS_BACKEND'] = 'theano'
from keras import backend as K
With python3 it shows this output:
Using Theano backend.
Ungültiger Maschinenbefehl (Speicherabzug geschrieben)
How could I get further information about the problem?
Try
from tensorflow import keras
If the problem persists, try going through the documentation on how to install and how to use it.
Keras - Tensorflow
Keras Overview - Tensorflow
Keras.io

Error: Module 'tensorflow' has no attribute 'gfile' error while running tensorflow object detection api tutorial

I am trying to use the object detection tutorial from tensor flow api. I am using python 3 and tensor flow version 2. But getting the below error.I tried several ways:
File "C:\Aniruddhya\object_detection\object_detection\utils\label_map_util.py", line 137, in load_labelmap
with tf.gfile.GFile(path, 'r') as fid:
AttributeError: module 'tensorflow' has no attribute 'gfile'
can someone help me to run this?
code link: https://drive.google.com/drive/u/3/folders/1XHpnr5rsENzOOSzoWNTvRqhEbLKXaenL
It's not called that in TensorFlow 2. You might be using a TensorFlow 1 tutorial.
Version 1
tf.gfile.GFile
https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/io/gfile/GFile
Version 2
tf.io.gfile.GFile
https://www.tensorflow.org/api_docs/python/tf/io/gfile/GFile
If you have Tensorflow version 2. You can use the next module compatible with the version 1, too.
import tensorflow.compat.v1 as tf
I solved this problem by reinstalling tensor using the previous version: sudo pip3 install tensorflow==1.14.0
You may optionally downgrade to previous version of tensorflow:
!pip install tensorflow==1.12.0
import tensorflow as tf
print(tf.__version__)
otherwise , make if tf.io.gfile and import tf.io

Problems setting up FastAi working on Colab

I'm trying to work through a tutorial on CycleGANs using the Colab platform but I'm struggling to find a way through the 'simple' part of just importing libraries.
I'm just trying to import the following:
from fastai.conv_learner import *
from fastai.dataset import *
from cgan.options.train_options import *
from sklearn.model_selection import train_test_split
from cgan.options.train_options import TrainOptions
from cgan.data.data_loader import CreateDataLoader
from cgan.models.models import create_model
from cgan.util.visualizer import Visualizer
from google_images_download import google_images_download
I'm currently stuck with an error on the first line:
----> 7 class IntermediateLayerGetter(nn.ModuleDict):
8 """
9 Module wrapper that returns intermediate layers from a model
AttributeError: module 'torch.nn' has no attribute 'ModuleDict'
The code can be found online: https://colab.research.google.com/drive/1dMQWStzLfAHDTGuKaUNQn1aOBWeJw4FN
Did you check this issue it sorts partially the problem.
Afterthat I received and this error (Detected that PyTorch and torchvision were compiled with different CUDA versions. PyTorch has CUDA Version=9.0 and torchvision has CUDA Version=10.0. Please reinstall the torchvision that matches your PyTorch install.) and solve it with
pip install torch==1.0.1 -f https://download.pytorch.org/whl/cu100/stable
I hope it helps :)

How to import WordEmbeddingSimilarityIndex function from gensim module?

When i try to import WordEmbeddingSimilarityIndex, it's giving me the following error:
>> from gensim.models import WordEmbeddingSimilarityIndex
ImportError: cannot import name 'WordEmbeddingSimilarityIndex
The same issue occurs for SparseTermSimilarityMatrix function:
>> from gensim.similarities import SparseTermSimilarityMatrix
ImportError: cannot import name 'SparseTermSimilarityMatrix
Note: I have installed and imported gensim, gensim.models and gensim.similarities. But still it's giving me the ImportError while importing the above mentioned functions.
Can you tell me what I am doing wrong, please?
Fix is change "models" to "similarities"
from gensim.similarities import WordEmbeddingSimilarityIndex
it works in gensim 4.0.1
Try to check the version of gensim that you are using. Usually, the older versions of gensim cause this issue.
from gensim.models import WordEmbeddingSimilarityIndex
print(gensim.__version__)
if the gensim version is 3.6.x or older update it to 3.7.x or latest version by running the below command. Once you update gensim version should get rid of this issue.
pip install --upgrade gensim

ImportError: No module named keras.preprocessing

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

Resources