Error while importing tensorflow as tf on Colab - python

I had been using a simple "import tensorflow as tf" command to import tensorflow on my Colab notebook for months. However, just recently the same code and notebooks started giving me the error "from future imports must occur at the beginning of the file".
The tensorflow package version I found on using "!pip list" was "2.8.2+zzzcolab20220719082949".
The main thing confusing me is how a notebook/code that was working perfectly 2 weeks back suddenly starts giving errors.
The code for the cell I ran which gave this error was:
import sys
from tflite_model_maker import image_classifier
from tflite_model_maker.image_classifier import DataLoader
from tflite_model_maker.image_classifier import EfficientNetLite0Spec
from tflite_model_maker.image_classifier import EfficientNetLite4Spec
from __future__ import absolute_import, division, print_function, unicode_literals
import matplotlib.pylab as plt
import tensorflow as tf
The error I got was -
File "", line 9
import tensorflow as tf
^
SyntaxError: from future imports must occur at the beginning of the file

You only have to move your __future__ import at the top of your code to make it work:
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
from tflite_model_maker import image_classifier
from tflite_model_maker.image_classifier import DataLoader
from tflite_model_maker.image_classifier import EfficientNetLite0Spec
from tflite_model_maker.image_classifier import EfficientNetLite4Spec
import matplotlib.pylab as plt
import tensorflow as tf
Please have a look at the future-statements docs:
A future statement must appear near the top of the module. The only
lines that can appear before a future statement are:
the module docstring (if any),
comments,
blank lines, and
other future statements.
In your case you have other imports before your __future__, which is not allowed.

Related

module 'pandas' has no attribute 'DataFrame' when using alibi package in Python

I'm hoping someone can help me. When I run the following setup I get the error below. It seems like the alibi package is causing it. The other posts related to this problem are either because of case sensitive spellings of 'DataFrame' or .py filenames.
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR) # suppress deprecation messages
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.utils import to_categorical
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import os
from sklearn.datasets import load_boston
from alibi.explainers import CounterFactualProto
AttributeError: module 'pandas' has no attribute 'DataFrame'

ModuleNotFoundError: No module named 'tensorflow.python.compiler.tensorrt'

My code:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
from tensorflow.python.compiler.tensorrt import trt_convert as trt
# import tensorflow.contrib.slim as slim
When I run it, get this error:
ModuleNotFoundError: No module named 'tensorflow.python.compiler.tensorrt'
Also, instead of from tensorflow.python.compiler.tensorrt import trt_convert as trt I used from tensorflow.python.compiler.tensorrt import trt but it did not change anything.
I use TensorFlow version 1.14.
Also worth mentioning https://forums.developer.nvidia.com/t/error-converting-tensorflow-model-to-tensorrt-on-windows-10/83892 its not supported on windows 10
In version that lower then 1.14.1 the right import is:
import tensorflow.contrib.tensorrt as trt
try to change
from tensorflow.python.compiler.mlcompute import mlcompute
for setting the GPU device in Metal plugin. We honor the Tensorflow's device placement logic. So depending on the supported operations in Metal plugin the layers will be mapped to GPU by TF's device placer. You can use tf.debugging.set_log_device_placement(
True) to dump out the layers mapped to GPU. I just removed the mlcompute import below and was able to train the network.

Cannot set up tensorflow gpu object detection

I tried to follow a tutorial on how to setup tensorflow gpu (https://www.youtube.com/watch?v=tPq6NIboLSc) but on the jupyter step i am getting this error :https://pastebin.com/gm3g8cC5
(ive set in in a pastebin because its very long)
I dont know what to do, ive also tried others tutorial but none of the others works, they also got errors ( but they are differents) at the jupyter step.
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
from distutils.version import StrictVersion
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")
from object_detection.utils import ops as utils_ops
if StrictVersion(tf.__version__) < StrictVersion('1.12.0'):
raise ImportError('Please upgrade your TensorFlow installation to v1.12.*.')
Here is the part where the errors pops.
I dont know what is my tensorflow version since when i am trying to import it i am getting OSError: [WinError 193] %1 is not a valid Win32 application

In-code solution to remove warnings in my Python code

I have a deep learning python code (Anaconda3, Ubuntu 16.04) which uses TensorFlow for some machine learning algorithms on video inputs. It imports the following packages:
import time, os, glob, subprocess
import skvideo.io
import numpy as np
import tensorflow as tf
import time, cv2, librosa
import skvideo.io
import numpy as np
import tensorflow as tf
from sklearn.externals import joblib
When running, it get the following warning. How can I remove this warning (or hide it from printing)?
/home/user/anaconda3/lib/python3.5/site-packages/sklearn/base.py:311: UserWarning: Trying to unpickle estimator SVC from version 0.18.1 when using version 0.19.1. This might lead to breaking code or invalid results. Use at your own risk.
UserWarning)

How to get matplotlib working in Python3 on OS X?

I am trying to do Google's deep learning course on Udemy. For assignment one I need to verify that the following modules are working on my machine, but can't get matplotlib.pyplot working. The python code I must get to compile is the following:
# These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
import tarfile
from IPython.display import display, Image
from scipy import ndimage
from sklearn.linear_model import LogisticRegression
from six.moves.urllib.request import urlretrieve
from six.moves import cPickle as pickle
# Config the matplotlib backend as plotting inline in IPython
%matplotlib inline
When I compile and run this like so:
python3.6 nn_assignment_1.py
I get the following error:
Traceback (most recent call last):
File "nn_assignment_1.py", line 9, in <module>
from IPython import display, Image
ImportError: cannot import name 'Image'
Any ideas how to get matplotlib working for python 3 here? I have been banging my head against the keyboard for hours trying to figure this out.

Categories

Resources