I am currently trying to run a U-Net model in Keras, using PyCharm and Anaconda on Windows. My first attempts kept on working well. However, now I'm getting an error when I try to acces keras:
My imports:
from unet_model import multi_unet_model
from keras.utils import normalize
import os
import glob
import cv2
import numpy as np
from matplotlib import pyplot as plt
The error:
AttributeError: module 'tensorflow.python.client.pywrap_tf_session' has no attribute 'cxx_version'
Maybe one of you encountered the same problem and can give me a hint how to solve it.
The problem occured after trying to move the model from cpu to gpu. Now the model can't be processed at all. Maybe I changed some references without knowing it.
Related
I've installed tensorflow and keras in every possible way I can think of, I have updated pip, used brew, tried a virtualenv but for some reason it won't let me import specific methods from tensorflow.keras (see image). What can I do?
Import issue
from tensorflow.python.keras.models import model_from_json
I am having trouble with a Jupyter script that I am using for a class at university. Useful information: I am using a MacBook Air (first time), macOS Monterey 12.0.1, M1 Apple chip, and I am working in a conda virtual environment with conda 4.11.0 and Python 3.9.7. This is the first part of the script:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from tensorflow import keras <--
import tensorflow as tf <--
from sklearn.utils import shuffle
from sklearn import preprocessing
from keras import regularizers <--
import random
from keras.utils.vis_utils import plot_model <--
import time
from IPython.display import Image
When I run it, I get this message:
The kernel appears to have died. It will restart automatically.
I tried commenting each row, and apparently, the problem is due to the ones that I highlighted with the arrow. So there is something wrong with TensorFlow. As suggested by my professor, I went on the terminal, typed ipython, then this:
In [1]: import tensorflow
zsh: illegal hardware instruction ipython
I looked it up on the internet, and I understood that there are some incompatibilities between some Python packages and the M1 Apple chip. I tried to follow this https://github.com/apple/tensorflow_macos, but when I use the command written there, this is what happens
(phdcourse) alessandroruggieri#Alessandros-MacBook-Air ~ % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/apple/tensorflow_macos/master/scripts/download_and_install.sh)"
ERROR: TensorFlow with ML Compute acceleration is only available on macOS 11.0 and later.
This is pretty weird, considering I have macOS 12.0.1 (as said at the beginning).
To conclude, I have seen some posts about similar issues on the internet, but they all look rather confusing, so I would really appreciate an easy and clear help.
Intellisense works fine on importing phrase
But when it comes with chaining method, it shows different suggestions
Python & Pylance extensions are installed.
From this issue on github
try adding this to the bottom of your tensorflow/__init__.py (in .venv/Lib/site-packages/tensorflow for me)
# Explicitly import lazy-loaded modules to support autocompletion.
# pylint: disable=g-import-not-at-top
if _typing.TYPE_CHECKING:
from tensorflow_estimator.python.estimator.api._v2 import estimator as estimator
from keras.api._v2 import keras
from keras.api._v2.keras import losses
from keras.api._v2.keras import metrics
from keras.api._v2.keras import optimizers
from keras.api._v2.keras import initializers
# pylint: enable=g-import-not-at-top
The problem is because keras is a special class that enables lazy loading and not a normal module.
Edit: With updates to tf, vscode, or something else I'm not having this issue and don't need to use the above fix anymore. I just have to use keras = tf.keras instead of from tensorflow import keras and I have Intellisense working now.
did you try clearing the cache on your system?
Try this
Don't import it directly like this
import tensorflow as tf
import tensorflow.keras as keras
Instead Do
import tensorflow as tf
keras = tf.keras
After this change, Everything was fixed and started showing better suggestions including function documentations
tensorflow.python.keras is for developers only and should not be used, but I think it is fine to be used as "type". I have also read it is a different version than the tensorflow.keras so have this in mind.
# Those are the imports, that actualy load the correct code
import tensorflow.keras as tfk
import tensorflow.keras.layers as layers
# This is for typehinting and intllisense
import tensorflow.python.keras as _tfk
import tensorflow.python.keras.layers as _layers
# This gets highlighted as error by my linter, but it runs
tfk: _tfk
layers: _layers
# from now on, the intellisense and docstrings work
# ...
While keras = tf.keras does the trick, I was dumbstruck that IntelliSense on my home machine wasn't working. Turns out, the Jupyter notebook I was using wasn't using the right Python interpreter (conda environment with tf and keras both # 2.11.0) due to a window reload or whatever.
This worked for me using conda with cuda and tensoflow:
import tensorflow as tf
from tensorflow import keras
from keras.api._v2 import keras as KerasAPI
KerasAPI.applications.ResNet50()
I try to do some PCA and TSNE analyses. I want to use sklearn to import the PCA and TSNE package.
Internet says that I should use these imports
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html
https://scikit-learn.org/stable/modules/generated/sklearn.manifold.TSNE.html
However, I get this message when I try to import those.
ImportError: cannot import name 'PCA' from 'sklearn.decomposition' (unknown location)
I did install sklearn and am able to import sklearn.decomposition and sklearn.manifold. However, If I I try to do this:
import sklearn.decomposition as decom
decom.PCA
I get the same ImportError. I tried reinstalling sklearn and updating it, but that doesn't solve the problem. Does one of you have a clue of what I'm doing wrong? I'm using Python 3.7.8 and use jupyter lab with MiniConda.
The documentation of sklearn is version 23.2 and that is the same version as my scikit-learn is.
EDIT: I created a totally new virtual environment and now my dir(decom) is totally different. And those packages for PCA and TSNE are working. Could somebody enlighten me why those are not working in the my current environment? Does it have to do with incompatible packages? I did not get any warning for it. It is sort of solved, yet I'm curious what the reason is and how I can fix it in the future.
I am following a deep learning tutorial book in Japanese and it is using MNIST for its handwritten images. It has the code from dataset.mnist import load_dataset, and when I tried it, it did not work, gave an error saying no such module named dataset.mnist. I have downloaded the modules dataset and mnist individually using pip. The book recommended to use Anaconda, but I have tried it to no success.
How can I use the module dataset.mnist?
The first question I want to ask you is which deep learning framework are you working with for solving your problem.
There are many deep learning frameworks. PyTorch, Tensorflow, and Keras are examples of such frameworks.
1) If you are working with PyTorch framework then you should import the torch framework using the command.
import torch
and then you can import MNIST dataset using the command
from torchvision.datasets import MNIST
2) For Keras framework use the following commands for importing MNIST dataset.
import keras
from keras.datasets import mnist
NOTE: This can be written as well for better understanding of your problem.
import keras
from keras.datasets as datasets
and then you can import MNIST dataset using thedatasets which is an alias of keras.datasets
Similarly, you can import MNIST dataset in other frameworks as well.
Hope this will be of your help.
Adding on to #SauravRai's Answer
For tensorflow :
from tensorflow.examples.tutorials.mnist import input_data
input_data.read_data_sets('my/directory')