TensorFlow MNIST example not running with fully_connected_feed.py - python

I am able to run the Deep MNIST Example fine, but when running fully_connected_feed.py, I am getting the following error:
File "fully_connected_feed.py", line 19, in <module>
from tensorflow.g3doc.tutorials.mnist import input_data ImportError: No module named
g3doc.tutorials.mnist
I am new to Python so could also just be a general setup problem.

This is a Python path issue. Assuming that the directory tensorflow/g3doc/tutorials/mnist is your current working directory (or in your Python path), the easiest way to resolve it is to change the following lines in fully_connected_feed.py from:
from tensorflow.g3doc.tutorials.mnist import input_data
from tensorflow.g3doc.tutorials.mnist import mnist
...to:
import input_data
import mnist

Another alternative is to link the 'g3doc' directory from the github repo into the tensorflow python wheel folder. That way you don't need to change the code.

Related

ModuleNotFoundError in Colaboratory

I am trying to run my project using the gpus but I can't get it to work.
I have ran the following commands:
from google.colab import drive
drive.mount('/content/gdrive')
%cd gdrive/MyDrive/project_folder
import sys
sys.path.append('/content/gdrive/MyDrive/project_folder')
I then try to run my main script from project_folder by using
! python property_prediction/predict.py
In the first line of predict.py I import a module from the folder 'project_folder' but that gives this error in colab:
File "property_prediction/predict.py", line 17, in <module>
from GP.kernels import Shortest_Path
ModuleNotFoundError: No module named 'GP
Why is it not finding the folder GP which contains my kernels script?
Try to replace your running command with:
!python -m property_prediction.predict
Or better:
from property_prediction.predict import predict # or whatever your main function is called
predict()
NB: This is of course assuming that you have a module named GP in the folder project_folder
If none of this work, you might be interested in reading this or other articles about imports with python (this is most likely not a problem of google collab)

ImportError: cannot import name 'convert_kernel'

When i try to use tensorflow to train model, i get this error message.
File "/Users/ABC/anaconda3/lib/python3.6/site-packages/keras/utils/layer_utils.py", line 7, in
from .conv_utils import convert_kernel
ImportError: cannot import name 'convert_kernel'
i have already install Keras
I got the same issue. The filename of my python code was "tensorflow.py". After I changed the name to "test.py". The issue was resolved.
I guess there is already a "tensorflow.py" in the tensorflow package. If anyone uses the same name, it may lead to the conflict.
If your python code is also called "tensorflow.py", you can try to use other names and see if it helps.

ModuleNotFoundError: No module named 'tensorflow.examples'

When I import tensorflow
import tensorflow as tf
I don't get an error. However, I do get the error below. I'm using spyder if that helps.
As per other questions, I ensured up to date (v1.8) tensorflow using both conda and then pip installs. This didn't resolve the issue. Please assist.
import tensorflow.examples.tutorials.mnist.input_data as input_data
ModuleNotFoundError: No module named 'tensorflow.examples'
I think you should use like bellow on tensorflow 2
import tensorflow_datasets
mnist = tensorflow_datasets.load('mnist')
Use the following, it will download the data. It is from the tensorflow documentation
import tensorflow as tf
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
Sometimes the TensorFlow examples are not pre-downloaded, so you might need to run the below command to install the examples from Github using the below code.
!pip install -q git+https://github.com/tensorflow/examples.git
To load the mnist dataset in Tensorflow 2.0:
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
Here is the reference:
TensorFlow 2 quickstart for beginners
Another method(also works for locally saved dataset):
DATA_URL = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz'
path = tf.keras.utils.get_file('mnist.npz', DATA_URL)
with np.load(path) as data:
train_examples = data['x_train']
train_labels = data['y_train']
test_examples = data['x_test']
test_labels = data['y_test']
Here is the reference:
Load NumPy data
Sometimes on downloading the TF, the example directory might not be available. You could rectify it by linking the 'example' directory from the GitHub repo into the tensorflow python wheel folder. That way you don't need to change the code.
If this doesn't work, try to replace import tensorflow.examples.tutorials.mnist.input_data as input_data as import input_data as mentioned in the link:
TensorFlow MNIST example not running with fully_connected_feed.py
Hope this helps!!!
Different approach
OS: Windows
copy from:
https://github.com/tensorflow/examples/tree/master/tensorflow_examples
to
[python folder]\Lib\site-packages\tensorflow_examples
use
import tensorflow_examples
example
from tensorflow_examples.models import pix2pix
but for datasets use:
pip install tensorflow_datasets
I solved this issue by adding **tutorial** directory into tensorflow_core, usually this issue pops up when lacking of this file
..\anaconda3\envs\tensorflow\Lib\site-packages\tensorflow_core\examples check this directory to see if you have tutorials file.
lack of tutorial file
If you do not have, then go to https://github.com/tensorflow/tensorflow download the zip file, and extract all (or open it).
download tutorial file
find tutorials file from tensorflow-master\tensorflow\examples\, and copy it to ..\anaconda3\envs\tensorflow\Lib\site-packages\tensorflow_core\examples.
Issue resolved.
run
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
im = mnist.train.images[1]
im = im.reshape(-1, 28)
plt.imshow(im)
I solved this issue on Mac, simply copy the official examples to tensorflow_core/examples directory.
pull the tensorflow code
git clone https://github.com/tensorflow/tensorflow
copy the examples to the system python3 directory
cp -a tensorflow/examples/ /usr/local/lib/python3.7/site-packages/tensorflow_core/examples/
You need to download the data sets to use it.
Command:
pip install tensorflow-datasets
Code part:
mnist_train = tfds.load(name="mnist", split="train")
You are done now. Happy coding! :)
You just need to download the lost files and copy them to the tensorflow-core/ examples.
for me on windows 10 is :
C:\Users\Amirreza\AppData\Local\Programs\Python\Python37\Lib\site-packages\tensorflow_core\examples
This folder has been deleted at September/2020. See their repository.
I used the commands:
git clone https://github.com/tensorflow/tensorflow.git
tensorflow> git checkout c31acd156c
I solved this issue by installing Keras according to this answer from different question:
I didn't have Keras installed and had to add it manualy
ImportError: No module named 'keras'

ModuleNotFoundError: No module named 'nets' (TensorFlow)

Here is my error :
File "C:\Program Files\Python36\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\models\faster_rcnn_inception_resnet_v2_feature_extractor.py", line 28, in <module>
ModuleNotFoundError: No module named 'nets'
screenshot
I have already change the python path but it doesn't change anything
Maybe in a Windows environments, capital code couldn't check differently and differs from linux file system.
It has already BUILD file inside slim folder. Let's move BUILD to BUILD.old, then You can build slim package
c:\foo\bar\models\research> cd slim
c:\foo\bar\models\research\slim> move BUILD BUILD.old
c:\foo\bar\models\research\slim> python setup.py build
c:\foo\bar\models\research\slim> python setup.py install
on windows
in the C:\tensorflow\models\research\slim directory run
python setup.py build
python setup.py install
P.S
models/research/slim HAS ITS OWN setup.py!!!!!!!!!!!!!
use the one specific for slim
Try to copy nets or slim folder to .....\site-packages\object_detection-0.1-py3.5.egg
See if this solution works.
I met same problem with you, since we are both in Windows Environment. What am I doing is that, I added little codes in the header of model_builder_test.py.
import sys
sys.path.append("....../tutorial/models/research")
sys.path.append("....../tutorial/models/research/slim")
......
import tensorflow as tf
from google.protobuf import text_format
from object_detection.builders import model_builder
......
You need to make sure that the tensorflow/models/research/ and slim directories are added to PYTHONPATH (see the installation instructions).
Either run the following
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
or add it to the end of your ~/.bashrc so that it gets run automatically whenever you open a new terminal.

ImportError: No module named datasets

from datasets import dataset_utils ImportError: No module named datasets.
when i am writing this in python sript.
import tensorflow as tf
from datasets import dataset_utils
slim = tf.contrib.slim
But i am getting error.
from datasets import dataset_utils
ImportError: No module named datasets
I found this solution
How can jupyter access a new tensorflow module installed in the right path?
I did the same and i have dataset packages at path anaconda/lib/python2.7/site-packages/. Still i am getting same error.
pip install datasets
I solved it this way.
You can find the folder address on your device and append it to system path.
import sys
sys.path.append(r"D:\Python35\models\slim\datasets"); import dataset_utils
You'll need to do the same with 'nets' and 'preprocessing'
sys.path.append(r"D:\Python35\models\slim\nets"); import vgg
sys.path.append(r"D:\Python35\models\slim\preprocessing"); import vgg_preprocessing
Datasets is present in https://github.com/tensorflow/models/tree/master/slim/datasets
Since 'models' are not installable from pip (at the time of writing), they are not available in python load paths by default. So either we copy them or manually add to the path.
Here is how I setup env before running the code:
# git clone or wget
wget https://github.com/tensorflow/models/archive/master.zip -O models.zip
unzip models.zip
# add it to Python PATH
export PYTHONPATH=$PYTHONPATH:$PWD/models-master/slim
# now we are good to call `python mytensorflow.py`
It's using the datasets package in the TF-slim image models library, which is in:
git clone https://github.com/tensorflow/models/
Having done that though, in order to import the module as shown in the example on the slim image page, empty init.py have to be added to the models and models/slim directories.
go to https://github.com/nschaetti/EchoTorch/releases and download the latest release
install the latest release from the downloaded file (202006291 is the latest version at the moment):
$pip install ./EchoTorch-202006291.zip
test it out using narma10_esn.py (other examples may have some issues)
you may still need to install some more python packages not listed in the requirements file but it works once you do this.

Categories

Resources