How to get AWS GPU instance details using Python-tensorflow - python-3.x

I created an AWS GPU instance with g4dn.xlarge instance type.
I installed Python and Jupyter-notebook as well.
When I am trying to load the GPU details in the Jupyter notebook with the below code:
import tensorflow as tf
tf.config.list_physical_devices()
Output:
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]
I tried few other methods as well
1
import tensorflow as tf
gpus = tf.config.list_physical_devices('GPU')
for gpu in gpus:
print("Name:", gpu.name, " Type:", gpu.device_type)
2
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
3
from tensorflow.python.client import device_lib
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
get_available_gpus()
All the codes will respond with no GPU.
what could be the possible options to access the GPU?

Since you said you installed Python yourself, it's likely you didn't start with a deep learning AMI with all the drivers installed, so you'd have to install Nvidia drivers, CUDA, and cudnn. But trying to install Nvidia drivers on an AWS EC2 instance can be tough...
Solution: start with the deep learning AMIs:
https://aws.amazon.com/machine-learning/amis/

Related

How to run Caffe2 on Macbook Pro M1 GPU

I was able to run PyTorch with Macbook Pro M1 Max GPU. However Caffe2 does not use the GPUs.
import torch
torch.device("mps")
from caffe2.python import core
WARNING:root:This caffe2 python run failed to load cuda module:No module named 'caffe2.python.caffe2_pybind11_state_gpu',and AMD hip module:No module named 'caffe2.python.caffe2_pybind11_state_hip'.Will run in CPU only mode.
I created the PyTorch and Caffe2 from the nightly code using
export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
BUILD_CAFFE2=1 MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py install
Any suggestions on how to solve this?

Python => AutoML: XGBoost is not available; skipping it

I am getting this warning when running h2o AutoML. I have version 3.32.1.2 installed, and running it on python 3.8.
AutoML progress: |
11:30:52.773: AutoML: XGBoost is not available; skipping it.
CODE:
import h2o
h2o.init()
h2o_df = h2o.H2OFrame(df)
train, test = h2o_df.split_frame(ratios=[.75])
# Identify predictors and response
x = train.columns
y = "TERM_DEPOSIT"
x.remove(y)
from h2o.automl import H2OAutoML
aml = H2OAutoML(max_runtime_secs=600,
#exclude_algos=['DeepLearning'],
seed=1,
#stopping_metric='logloss',
#sort_metric='logloss',
balance_classes=False,
project_name='Completed'
)
%time aml.train(x=x, y=y, training_frame=train)
XGBoost is not supported on Windows, see the limitations in the H2O documentation.
If you are not using Windows and you didn't find another reason in the documentation mentioned above, you can try to reinstall h2o, e.g.,
pip install --force-reinstall https://h2o-release.s3.amazonaws.com/h2o/rel-zipf/2/Python/h2o-3.32.1.2-py2.py3-none-any.whl
I think I found the answer to this warning. I am running a windows machine.
https://twitter.com/ledell/status/1148512129659625472?lang=en
If you're on Windows, XGBoost is not supported 😿 so the parts of the tutorial that use XGBoost can be replaced by h2o.gbm(). The AutoML process will also exclude XGBoost models.

Import tensorflow module is slow in tensorflow 2

Related: Import TensorFlow contrib module is slow in TensorFlow 1.2.1 also: What can cause the TensorFlow import to be so slow?
I am using an ssd and importing TensorFlow. I have 4 ghz 8 core pc with 16 gb ram (Processor AMD FX(tm)-8350 Eight-Core Processor, 4000 Mhz, 4 Core(s), 8 Logical Processor(s)). TensorFlow takes 10-12 seconds to import.
Is there any way to selectively import parts of TensorFlow?
Would a RAM disk help?
Is there any more work being done on stuff like this or: Slow to import tensorflow.contrib with Python 3 because inspect.stack is slow #11829?
Edit: Python 3.6.8 :: Anaconda, Inc. on windoze 8.1.
Dos box, cygwin bash are slow at 12 seconds.
Vs code bash/power shell is fastest at 8 seconds.
Importing in a .py file like: import tensorflow as tf.
Not sure what environments are.
Edit 2:
PS D:\ray\dev\ml\ss1> conda info --envs
# conda environments:
#
base * D:\Anaconda3
first D:\Anaconda3\envs\first
d:\Anaconda3
first d:\Anaconda3\envs\first
Edit 3: Using the code below, i get 9-10 seconds in a command prompt:
(tf2) D:\ray\dev\ml\ss1>python timeimport.py
import tensorflow: 1 units, 9.796 seconds. 0.1 units/second.
version: 2.0.0
(tf2) D:\ray\dev\ml\ss1>python timeimport.py
import tensorflow: 1 units, 9.448 seconds. 0.11 units/second.
version: 2.0.0
(tf2) D:\ray\dev\ml\ss1>python timeimport.py
import tensorflow: 1 units, 9.421 seconds. 0.11 units/second.
version: 2.0.0
from __future__ import absolute_import, division, print_function, unicode_literals
from contextlib import contextmanager
from timeit import default_timer as timer
#contextmanager
def timing(description: str,units=1,title="",before="") -> None:
if before!="":
print(before,flush=True)
start = timer()
yield
dt = timer() - start
frequency=0 if units is None else (units/dt)
if units is None:
if title is None: print(f"{description}: {dt} seconds.",flush=True)
else: print(f"{title} {description}: {dt} seconds.",flush=True)
else:
#"{0:.2f}".format(a)
dt=round(dt,3)
frequency=round(frequency,2)
print(f"{title} {description}: {str(units)} units, {dt} seconds. {str(frequency)} units/second.",flush=True)
return dt
with timing("import tensorflow",1):
import tensorflow as tf
print("version:",tf.__version__)
Edit 4: turning of windows degender, i get 8-9 seconds instead of 9-10 seconds.
Edit 5: i found a workaround:
make a notebook with:
import tensorflow as tf
print(tf.__version__)
import tensorflow_datasets as tfds
import code.py
then in your code.py:
print("enter imported code")
import tensorflow as tf
print(tf.__version__)
# !pip install -q tensorflow-datasets
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt
import numpy as np
#tfds.list_builders()
ds = tfds.load('mnist', split='train', shuffle_files=True)
...
so you run the notebook once and it takes 10 seconds. next time it goes like the wind.
I want to start off by saying that I'm using a 3 Ghz quad core and it does not take me any where near ten seconds to import TensorFlow in Python. Could you elaborate on what environment you're having issues importing it with (i.e. Windows/Mac/Linux in terminal/console/command prompt/Anaconda etc.)?
You didn't specify how you are trying to import Tensorflow, but considering you tagged it with python-3.x I'm assuming you are importing Tensorflow with Python. I'm sure this won't be a popular answer but maybe consider using Tensorflow with a compiled language like c++. It is well known that interpreted languages such as Python are considerably slower than compiled languages and if speed is paramount then it would seem obvious to use TensorFlow in its native language.
When I import tensorflow from a Thinkpad T560(i7 6600U with integrated GPU) in my office, it takes over 10 secs(about 15 secs).
But recently I got a new Laptop Lenovo Y7000 at home(i7 10750H and GTX 1650 Card, SAMSUNG SSD), and I installed CUDA dependencies and able to run TensorFlow with CUDA successfully.
I would say now I feel like I switch from an old carriage to a bullet train.

working with Keras on a Jupyter lab using Tensorflow

I'm trying to use keras on a jupyter lab using tensorflow (python 3.6) through miniconda.
Jupyter lab keeps give me a restarting error(Kernel Restarting The kernel for Desktop/Transfer/Untitled.ipynb appears to have died. It will restart automatically.)
Because the kernel restarting error is evident whenever I try to import keras. The code I used is (from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array, array_to_img).
Code
Therefore, I assume that there is a problem between keras and kernel even though I already installed ipykernel in my tensorflow directory ((tensorflow) C:\Users\user).
Pip_list
Still couldn't figure out this kernel and keras problem :(.

Running python code on CUDA

While trying to run this code https://wltrimbl.github.io/2014-06-10-spelman/intermediate/python/04-multiprocessing.html on my GPU system which has 300 cores, i used the comment with tf.device('/GPU:0') on the beginning of code. But found that it does not run on GPU. Then i tried
import tensorflow as tf
tf.device('/GPU:0'): # for run in GPU
init = tf.initialize_all_variables()
# initializing all variables
sess = tf.Session(
config=tf.ConfigProto(
intra_op_parallelism_threads=1))
Does this code run in GPU? or is there any method for run a python code on GPU.
No, it won't run without a GPU optimized version of TensorFlow.
Python multiprocesing is for CPU only.
TensorFlow GPU is available (see here https://www.nvidia.com/en-us/data-center/gpu-accelerated-applications/tensorflow/). The Tensorflow GPU implementation is using CUDA with cuDNN under the hood.
To run your own Python script on GPU, you need to use a library like PyCUDA or Cupy which use the CUDA API under the hood as well.

Resources