Tensorflow no module named official - python

I am trying to use the nets from the official mnist directory of tensorflows model repository. On my windows system I receive this error:
C:\Users\ry\Desktop\NNTesting\models\official\mnist>mnist_test.py
Traceback (most recent call last):
File "C:\Users\ry\Desktop\NNTesting\models\official\mnist\mnist_test.py",line 24, in <module>
from official.mnist import mnist
ModuleNotFoundError: No module named 'official'
I have followed their official directions and set my python path using
set PYTHONPATH="PYTHONPATH:"%cd%"
and can confirm that
PYTHONPATH="$PYTHONPATH:C:\Users\ry\Desktop\NNTesting\models"
and I have also installed the dependencies successfully. Does anyone have experience using these models on a windows system and can help me with this pathing issue? I'm not sure what I have done incorrectly here.
Thanks

pip install tf-models-official

For Google Colab I needed to add the the model dir also to the Systems path:
!git clone https://github.com/tensorflow/models.git
import os
os.environ['PYTHONPATH'] += ":/content/models"
import sys
sys.path.append("/content/models")

if anyone has this problem make sure that the python path variable doesn't have quotations in it. For some reason, the readme has quotations around it.
Here is the correct way to set it
PYTHONPATH=path\to\models

I had exactly the same question as you did, and the following solution solved this problem.
There is an error in the tensorflow/models/official README.md
https://github.com/tensorflow/models/tree/master/official
Wrong
export PYTHONPATH="$PYTHONPATH:/path/to/models"
Correct
export PYTHONPATH=$PYTHONPATH:/path/to/models

The Official Models are made available as a Python module. To run the models and associated scripts, add the top-level /models folder to the Python path with the command: export PYTHONPATH="$PYTHONPATH:/path/to/models"
FROM README

I had the same problem. Did you use windows 10? Make sure you run the command prompt as "administrator". I used it in VS code at first, no warning, and didn't work. But it worked when I run a separate prompt window as "administrator".
set PYTHONPATH=path\to\models
then run the model.

I was setting up to run the NMT model and ran into the same problem.
It took me bit to figure out exactly which folder should be added to PYTHONPATH.
I tried several folders inside my example's directory with no luck.
I finally understood what that import was trying to tell me...
"from official.transformer.utils import tokenizer"
means
"add the parent of directory 'official' to PYTHONPATH".
For me, this was just the top-level 'models-master' directory that I obtained from GitHub. Once I added /path/to/models-master, I was past this obstacle.

Go to models folder and do
export PYTHONPATH=$PYTHONPATH:$PWD

add the model directory to PYTHONPATH.
import os
os.environ['PYTHONPATH'] += ':/content/models/research/:/content/models/research/slim/'

jupyter. Clone from official git and manually appended the path to sys.
!git clone https://github.com/tensorflow/models.git
import sys
sys.path.append("C:/Windows/System32/models")
sys.path

Related

How to import own modules from repo on Databricks?

I have connected a Github repository to my Databricks workspace, and am trying to import a module that's in this repo into a notebook also within the repo. The structure is as such:
Repo_Name
Checks.py
Test.ipynb
The path to this repo is in my sys.path(), yet I still get ModuleNotFoundError: No module named 'Checks'. When I try to do import Checks. This link explains that you should be able to import any modules that are in the PATH. Does anyone know why it might still not be working?
I have tried doing the same and got a similar error even after following the procedure as given in the link provided in the question.
I have the following python files in my GIT repo (3 files with .py extension).
Now when I add the path /Workspace/Repos/<username>/repro0812 to sys.path and try to import the sample module from this repo, it throws the same error.
This is because, for some reason, this file is not being rendered as a python file. When I open the repo, you can actually see the difference.
There was no problem while I import the other 2 python modules check and sample2. The following is an image for refernce.
Check and make sure that the file is being considered as a .py file after adding your repo.

Import [Module] could not be resolved (PylancereportMissingImports), with module in the same folder/directory

The first few lines of the code of evaluation.py:
import os
import torch
from torch.nn import functional as F
from torch.utils.data import DataLoader
import numpy as np
from dataset import CLSDataset # warning is reported here
from tqdm import tqdm
The structure of the folder:
./
|-dataset.py
|-dictionary.py
|-evaluation.py
|-model.py
|-models/
|-[some files]
|-__pycache__
|-train.py
Notice that dataset.py is in the same folder as that of evaluation.py and https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#unresolved-import-warnings says that The language server treats the workspace root (i.e. folder you have opened) as the main root of user module imports. But it still throws an warning of "Import dataset could not be resolved".
I tried to add the
{
"python.analysis.extraPaths": ["./"]
}
on the settings.json of both local and remote files, but it does not help.
In VSCode, go to the main window and do the following:
Do Ctrl+Shift+P (for Windows) and Command+Shift+P (for Mac)
Scroll and go to Python Select Interpreter
Now select whichever python version is installed in your system.
You're good to go!
Do upvote if it helps you!
Dataset is a relative import.
Add an __init__.py file your directory (a file with no content). Then try:
from .dataset import CLSDataset
That being said, Python imports are a tricky business. It looks like we’re only getting a glimpse of one part of workspace setup.
(I am surprised that your code runs as-is.)
Consider scaffolding your project with cookiecutter. Also try out the vs code python project tutorials. You don’t have to remake your entire project but these will give you a starting point for understanding where your current project goes awry.
I recently had the same error, just try restarting the IDE. You probably installed it thought the code was already open. Or you haven't installed the module at all.
Edit:
I'm sorry I understood it wrong, please send the code.
Edit2:
You need to add the .py at the end of the import if it is a .py file

module 'caffe' has no attribute 'set_mode_gpu'

I have installed caffe from source. I have used the Cmake for installation. I have updated the respective paths as well.
My caffe root directory is: /home/ashj/caffe
I have updated the PYTHON path as:
export PYTHONPATH=<caffe-home>/python:$PYTHONPATH
which is by using
**export PYTHONPATH=/home/ashj/caffe/python:$PYTHONPATH**
I could load import the module caffe. However I am not able to access any methods or any layers inside the caffe like set_mode_gpu(), set_mode_cpu() or layers or params. I am getting errors like:
When I used
import caffe
caffe.set_mode_gpu()
I am getting following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'caffe' has no attribute 'set_mode_gpu'
PS: I have also tried using caffe.__caffe.set_mode_gpu() as mentioned in this link. but it is not working for me.
My system specs: Ubuntu 18.04
TIA
Though may be late, but I encountered this same problem and found a way work around:
sys.path.insert(0, '/path/to/caffe/python')
import caffe
caffe.set_mode_gpu()
namely, add the caffe/pathon path to you sys.path before import caffe.
Wrote a post here for detailed analysis, hope helpful.
This problem may be the results of package caffe's path.
For me, if I do the following from the Ubuntu Terminal, everything goes fine:
but if I do from the Pycharm IDE, errors occur:
note that I tested the package caffe's path in both ways, and got different results:
- in the Ubuntu terminal, namely the way which goes fine, I got
'/home/CVAR-B/softwares/caffe/caffe/python/caffe/__init__.pyc'
which is the expected result;
- in the Pycharm IDE way, namely the way error occurs, I got
'/usr/local/lib/python2.7/dist-packages/caffe/__init__.pyc'
which is not the expected result.
In view of this discovery, I did this one more thing to handle the error:
sys.path.insert(0, '/path/to/caffe/python')
import caffe
caffe.set_mode_gpu()
namely, add the caffe/pathon path to you sys.path before import caffe.
and the result shows this can be a workaround:
(source: ax2x.com)
See the caffe.__file__'s result, now returns the expected path.
Try these steps and then set your python PATH:
You might've already done steps 1 and 3.
make all
make pycaffe
make distribute
mkdir ~/python
mv distribute/python/caffe ~/python
Set your PYTHONPATH after this — this should be some dir like caffe/python/caffe

Relative imports in Tensorflow Object detection API

I am trying to use the tensorflow object detection APIs for one of my personal projects. Here you can find the link to the repo for the same that needs to be cloned before using it. In this repo, there are multiple directories and are arranged in this fashion:
models
+ research
+ object_detection
+
+ ....
+ ....
Now the object_detection is the one that is of use to me. So, I create a new directory in the parent directory research for my own models and try to use the functionalities given in the object_detection directory. Within the object_detction directory is a sub-directory utils which contains a bunch of .py files for certain utilities. Here is a sample link for such a file in that directory. Now when I try to use this file in my code, it says no module found: object_detection. This is a relative import error but I am not getting how to resolve it. I just want to make sure that I can use each of the functionality without modifying the imports and this is doable because there is an __init__ file in each sub-directory but somehow this isn't working. Please help!!
As per the installation instructions, please add slim/ and models/ to your PYTHONPATH.
Just for clarification, I assume that you already installed tensorflow/models. Otherwise install it (and all its dependencies) either as explained here or in an virtual environment.
Either way you can import code from the object_detection API like so:
from object_detection import <myExample>
For your example that would be:
from object_detection.utils import label_map_util

Python: Installing and using Exscript module Windows x86

I have been banging my head against the wall trying to get Exscript installed. After multiple failed attempts at doing it manually, I installed ActivePython and had success running "pypm install Exscript" from the cmd prompt.
I am now going through the Exscript documentation (found here https://github.com/knipknap/exscript/wiki/Python-API-Tutorial) and if I run the first example script I get an error:
>>> from Exscript.util.interact import read_login
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
from Exscript.util.interact import read_login
ImportError: No module named interact
So, I understand that this is saying that there is no module interact. How can I check this? Is there a way I can manually add this module? I would love to know WHY this module didnt come with the package, but that may be impossible to answer :)
Any and all help is greatly appreciated. Thank you
EDIT -
import Exscript.util works but if I try import Exscript.util.Interact I get the error. When I look in util.py I see an entry that says "from FooLib import Interact". I first thought it may just be a capitalization error (Exscript.util.interact vs util.Interact) but neither of those work. I am not sure where to go from here... :(
EDIT -
I have posted this question on the developers forums, hopefully he will have an answer for us. https://github.com/knipknap/exscript/issues/15
EDIT -
The developer suggested that I was using an old version and told me to download the latest. I had struggled installing the module manually so I googled how to easily install py modules. I found a writeup on easy_install.exe. I ran "c:\Python26>easy_install C:\Users\support\Desktop\lou\knipknap-exscript-v2.1-70-gf5583f3.tar.gz" from the cmd prompt, the module was installed (no errors) and now when i run the script it works.
Next challenge will be how to get these scripts to run as stand-alone exe's on users computers without Python installed :)
THANK YOU to everyone to commented I truely appreciate your help.
Lou
One common way packages are installed is as directories. So check your site-packages directory for an Exscript directory, and inside that there should be a util directory, and inside that there should be an interact.py file. Look for similar spellings in case the tutorial misspelled something.

Categories