If you installed tensorflow version 2 but use v1 programs, pleas do the following, in yoru program:
import tensorflow as tf
replace with
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
////
>pip list #to see all your installed python versions
////////////////////////////////////////////////////////////////////////////
1 . Tensor_windows (installation through Anaconda3)
Installation guide, see below
https://www.tensorflow.org/tutorials
error fix when running https://github.com/keras-team/keras/tree/master/
fix: keras --> tensorflow.keras (for https://github.com/keras-team/keras)
fix:from keras.utils.data_utils import get_file --> from tensorflow.python.keras.utils.data_utils import get_file
fix : > conda install pillow
fix: in variational_autoencoder_deconv : use: vae.save_weights('vae_cnn_mnist.tf') #instead of vae.save_weights('vae_cnn_mnist.h5')
If the tensorflow tutorials cannot run, try to downgrade to an older tf version >>pip install tensorflow==2.0
https://www.tensorflow.org/tutorialsInstallation of tensorflow ,using win10 , anaconda
check the Tensor-flow version by :
conda> conda activate tf-gpu
conda> python
>>>import tensorflow as tf; print(tf.__version__) #tensorflow version
conda> python --version #python version
------------** if you use tensorflow2 on old project s, e.g. imagenet etc, use the following:------------------
#import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
---------------------changes needed for new tensor-flow-with-keras------------------------------
from tensorflow.keras.models import Sequential
from tensorflow.python.keras.layers.recurrent import LSTM
from tensorflow.python.keras.optimizers import Adam
or
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers.recurrent import LSTM
from tensorflow.python.keras.optimizers import Adam
-------------------------------------------------------------------------
#for music genre lstm_genre_classifier_keras.py from (https://github.com/ruohoruotsi/LSTM-Music-Genre-Classification)
from keras.models import Sequential
from python.keras.layers.recurrent import LSTM
from python.keras.layers import Dense
from keras.optimizers import Adam
need to change to if you use Tensorflow 2.x instead of tensorflow1
from tensorflow.python.keras.models import Sequential #add tensorflow
from tensorflow.python.keras.layers.recurrent import LSTM #add tensorflow
from tensorflow.python.keras.layers import Dense #add tensorflow
from tensorflow.keras.optimizers import Adam #add tensorflow instead of tensorflow.python
Installation guide
https://www.pugetsystems.com/labs/hpc/How-to-Install-TensorFlow-with-GPU-Support-on-Windows-10-Without-Installing-CUDA-UPDATED-1419/
1) Go to the Anaconda downloads page https://www.anaconda.com/distribution
e.g. anaconda 2020.9 for windows 64-bit and python 3.8 version (or the latest)
----------------------------------------
2) in win10/start/ type anacond powershell prompt(right click for administrator mode)
-------------------------------------------
3) In Administrator: Anaconda Powershell Prompt: (base)>
----------------------------------------------------
4) In Administrator: Anaconda Powershell Prompt: run the followings
conda update conda
conda update anaconda
conda update python
conda update --all
Downloading and Extracting Packages
python-3.8.5 | 15.7 MB | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
####################################################
In Administrator: Anaconda Powershell Prompt: (base)> you see
Python 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
Type "help", "copyright", "credits" or "license" for more information.
>>>
If you see above , you are fine. (control-D to exit)
---------------------------------------------------------
5a) If you have gpu, create the kernel (tf-gpu)
conda create --name tf-gpu
>conda activate tf-gpu
conda install tensorflow-gpu
(Not discussed here , see the document :https://www.pugetsystems.com/labs/hpc/How-to-Install-TensorFlow-with-GPU-Support-on-Windows-10-Without-Installing-CUDA-UPDATED-1419/#Step2)PythonEnvironmentSetupwithAnacondaPython
or ---------------------
5b) if you don't have gpu, use cpu only, create the kernel (tf-cpu)
conda create --name tf-cpu
>conda activate tf-cpu
>conda install tensorflow==1.15 # this is good for cpu only, then python version becomes lower
#If necessary, check the Tensor-flow version by :
conda> python
will get Python 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
>>>import tensorflow as tf
>>>print(tf.__version__)
will get: 1.15.0
conda> python --version #python version
will get : Python 3.7.9
5-final), (after 5a or 5b) after installation of tensorflow-python , test if it is there
(tf-cpu) >python
>>> import tensorflow as tf
>>> print(tf.__version__)
1.15.0
Control-D to exit python
-------------------------------------------
6) Optional install jupter notdebook (see another link)
----------------------------------------------------
7)An Example Convolution Neural Network training using Keras with TensorFlow.
Assume you quit and start your pc again,
8) Find the path of tensorflow
C:\Users\khwong2\Anaconda3\pkgs\tensorflow-1.15.0....
9) Spyder is a good editor for python/tensorflow (optional but recommendation)
If you have completed the above steps 1-8 in https://sites.google.com/site/hongslinks/tensor_windows, run win10-start / anaconda navigator (Anaconda 3) ,
you can see you can install spyder (conda>pip install spyder) and jupyter (conda>pip install jupyter). Clink the icon to start the installation you need.
10) opencv-installation, see https://sites.google.com/site/hongslinks/opencv-1/opencv,
10a) conda>pip install opencv-contrib-python # for opencv (with cv2.aruco etc), not pip install opencv-python because they conflict each other
10b) ## In anacondas (right click administrator ): > conda install -c conda-forge dlib #if you are using python 3.7
#older method: In anacondas (right click administrator ): conda install -c menpo dlib # to install the latest dlib face track
10c) pip install imutils #used for dlib programs
Conclusion
That MNIST digits training example was a model with 1.2 million training parameters and a dataset with 60,000 images. **It took 80 seconds utilizing the NVIDIA GTX 980 on my old test system! For reference it took 1345 seconds using all cores at 100% on the Intel i7-4770 CPU in that machine. That's an 17 fold speedup on the GPU. That's why you use GPU's for this stuff!**
Note: I used the same procedure for doing the CPU version. I created a new "env" naming it "tf-CPU" and installed the CPU only version of TensorFlow i.e. `conda install tensorflow` without the "-gpu" part. I then ran the same Jupyter notebook using a "kernel" created for that env.
11) example
####### testing program to make sure your system is working ##
#Can use the following procedure to run the mnist code:
#cut and paste the following and save it as mnist1.py in c:\\temp1
# In win10>start, type Anaconda prompt and run as administrator (better), you will see
#(base) C:\WINDOWS\system32>conda activate tf-cpu # use 'tf-gpu' if you have gpu (see above for installation)
#(tf-cpu) C:\WINDOWS\system32>cd c:\\temp1
#(tf-cpu) c:\temp1>dir
#(tf-cpu) c:\temp1>python mnist1.py
# Alternatively, use spyder3 to run it.
####### mnist_test1.py begins ####################
from __future__ import print_function
import tensorflow.keras as keras
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras import backend as K
batch_size = 128
num_classes = 10
epochs = 12
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# mnist_test1.py end
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
################# mnist_test1.py end ###########################################
=========== older instructions ========================================
#-------------------------------------------
https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html
#-------------------------------------------
In some example, you need to normalize the data as follows:
train_images = train_images.astype('float32') #//fix by khw
test_images = test_images.astype('float32')#//fix by khw
train_images /= 255#//fix by khw
test_images /= 255#//fix by khw
#-------------------------------------------
https://keras.io/getting-started/sequential-model-guide/
-----------------------------------------
https://keras.io/getting-started/sequential-model-guide/
#Install tensorflow without GPU (2020 Feb 13), use CPU (slower)
In anaconda prompt (administrator mode): >conda install tensorflow
>conda create --name tf-gpu
>conda activate tf-cpu
In anaconda install spyder , run spyder,
-) Open Spyder and make your first test file:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
-) Run it in Spyder and it will work
-----------Run tensorflow on spyder (2020 Feb 13)
After installing Tensorflow using Anaconda based on Installing TensorFlow on Windows you must change your Environment for Spyder.
1) Open Anaconda Navigator
2) In top left corner you see Selector: "Applications on: base(root)"
3) Change: "base root" for "Tensorflow" it assumes that it was already installed based on link above
4) Install Spyder
5) Open Spyder and make your first test file:
010 import tensorflow as tf
020 hello = tf.constant('Hello, TensorFlow!')
030 sess = tf.Session()
040 print(sess.run(hello))
6) Run it in Spyder and it will work
current issues;
https://keras.io/preprocessing/image/ how to create a data-set
https://www.myzhar.com/blog/tutorials/tutorial-nvidia-gpu-cuda-compute-capability/
operation of alexnet
----- test procedure -------
191004
run tensorflow cnn_keras_mnist1.py on
office 1012 pc with nvidia 1070-8G card
1) conda activate tf-gpu
2) run anaconda prompt --- righ-click run as administartor
3) cd C:\Users\khwong\projects\tested_ok\cnn\
4) python cnn_keras_mnist1.py
5) will run ok
ref------------
(tf-gpu) C:\Users\khwong\projects\tested_ok\cnn\cnn_keras_mnist>python cnn_keras_mnist1.py
--------------- installation--------------------------
conda activate tf-gpu
Uninstall all python, tensorflow, anacodas versions , restart your machine, 2019 june 10
Update GPU driver (https://www.nvidia.com/Download/index.aspx?lang=en-us)
install anacondas (windows / python) https://www.anaconda.com/distribution/#download-section
Follow this: install GPU-tensor-flow on win10 https://www.pugetsystems.com/labs/hpc/How-to-Install-TensorFlow-with-GPU-Support-on-Windows-10-Without-Installing-CUDA-UPDATED-1419/#Step2)PythonEnvironmentSetupwithAnacondaPython
# ================-2019 oct 13 ===========================
#############################################################
import tensorflow.keras as keras #khwong
#from tensorflow.keras.layers import Activation, Dense, Input #khwong
from keras.callbacks import LambdaCallback
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.optimizers import RMSprop
from keras.utils.data_utils import get_file
#from keras.layers.recurrent import LSTM #no good, use the following
from tensorflow.python.keras.layers.recurrent import LSTM #khwong
#############################################################
#*************************************************************************************************************
modification needed if you use tensorflow.keras (in new tensor flow versions) instead of keras
#-----------------------------------2019.6.8------------
keras--> tensorflow.keras
keras.utils -->tensorflow.python.keras
tensorflow.keras.preprocessing.image --> keras.preprocessing.image
#*************************************************************************************************************
Keras problem:
from python.keras.utils import np_utils
-->
from tensorflow.python.keras.utils import np_utils
#*************************************************************************************************************
TENSORBOARD:
If tensorboard does not work
e.g.error message :
File "c:\programdata\anaconda_3\lib\site-packages\tensorboard\db.py", line 48, in TensorBase
def __init__(self, db_connection_provider, retrier_factory=util.Retrier):
AttributeError: module 'tensorboard.util' has no attribute 'Retrier'
#*************************************************************************************************************
In anaconda(admin)>
pip uninstall tensorboard
pip install tensorboard
=======================
use tensorboard --logdir log_file_directory --host=127.0.0.1
use tensorboard --logdir C:\Users\khwong\projects\tensorflow-master\tensorflow\examples\tutorials\word2vec\log --host=127.0.0.1
then use chrome to view http://127.0.0.1:6006
=GGGGGGGGGG GPU GPU GPU GPU GPU-tensor-flow on Win10 =======
Install GPU-tensor-flow on Win10
Update GPU driver (https://www.nvidia.com/Download/index.aspx?lang=en-us)
Follow this: install GPU-tensor-flow on win10 https://www.pugetsystems.com/labs/hpc/How-to-Install-TensorFlow-with-GPU-Support-on-Windows-10-Without-Installing-CUDA-UPDATED-1419/#Step2)PythonEnvironmentSetupwithAnacondaPython
In win10/search/Anacodas Prompt (right-click-admin)pip install --upgrade tensorflow-tensorboard #to upgrade tensorboard
################### testing program: MNIST ##################################################3
see cnn_mnist1a_ok.py below
################### hasion mnist ##################################################3
see fashion4.py (simple) , or fashio10.py (complex) shown below
=CCCCCCCCCCC CPU CPU CPU CPU CPU-tensor-flow on Win10 ==========
-----------------------------------------------------------------------------------
Install tensor flow CPU (without GPU) https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.htm
1) Install anaconda, (assume you have python 3.6/ or 3.7)
2) https://www.anaconda.com/distribution/ ( download 64-Bit Graphical Installer (637 MB), run windows-graphical for python 3.7 version.
a. After installation, windows search( Anacodna3) will see all directories,
b. Add these to windows path “C:\Users\khwon\Anaconda3; C:\Users\khwon\Anaconda3\Scripts”
3) In windows cmd,” >> where conda” , will see “C:\Users\khwon\Anaconda3\Scripts\conda.exe”
4) Install tensorflow
a. In windows-search, type Anaconda prompt (right click run as administrator), then you will see the Anaconda window, run
>>conda create -n tensorflow_cpu pip python=3.6
5) After installation, then, >> activate tensorflow_cpu
6) You will see: (tensorflow_cpu) C:\WINDOWS\system32>
7) Run >>pip install --ignore-installed --upgrade tensorflow
8) Test,
9) >>> import tensorflow as tf
10)>>> hello = tf.constant('Hello, TensorFlow!')
11)>>> sess = tf.Session()
12)>>> print(sess.run(hello))
13)b'Hello, TensorFlow!'
#-----type as follows--------------
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
================= tensorflow test and demo =====================
try
http://tensorflow.org/tutorials/
Now you have tensorflow , and can run tutorial
http://tensorflow.org/tutorials/
14) In win110-cmd(administrator) >install the following using pip
pip install pillow
pip install lxml
pip install jupyter
pip install matplotlib
pip install opencv-python
Please follow the below steps before running models in this repo:
In windows-search, type Anaconda prompt (right click run as administrator), then you will see the Anaconda window, run
Will get you to “Anaconda window”
1) pip install tensorflow
2) conda install keras
3) Now use “Anaconda Navigator “/ spyder to test various programs in
4) https://www.tensorflow.org/guide/keras
To use the system, start with win10(search
-------------------------------------
http://tensorflow.org/tutorials/
Now you have tensorflow , and can run tutorial
http://tensorflow.org/tutorials/
14) In win110-cmd(administrator) >install the following using pip
pip install pillow
pip install lxml
pip install jupyter
pip install matplotlib
pip install opencv-python
Please follow the below steps before running models in this repo:
In windows-search, type Anaconda prompt (right click run as administrator), then you will see the Anaconda window, run
Will get you to “Anaconda window”
1) pip install tensorflow
2) conda install keras
3) Now use “Anaconda Navigator “/ spyder to test various programs in
4) https://www.tensorflow.org/guide/keras
To use the system, start with win10(search) "Anaconda Navigator"/ use "spyder" to edit/run python programs
////////////////////////////////////// updated steps ///////////////////
#install tensorflow model gardens, 2021.10.8
cd C:\project\
conda>conda install git
conda>git clone https://github.com/tensorflow/models.git
////////// run mnist ///////
in conda
(base) conda > activate tf-gpu
cd C:\project\models\official\vision\image_classification
(tf-cpu) C:\project\models\official\vision\image_classification>python mnist_test.py # it runs ok
(tf-cpu) C:\project\models\official\vision\image_classification>python classifier_trainer_test.py #it runs ok
(tf-cpu) C:\project\models\official\nlp\keras_nlp\encoders>python bert_encoder_test.py #tried, not totally successful
(tf-cpu) C:\project\models\official\nlp\keras_nlp\layers>python on_device_embedding_test.py #runs ok
#########
(tf-cpu) C:\project\models\official\nlp\transformer>python transformer_main_test.py # runs for a while, error at the end??
(tf-cpu) C:\project\models\official\nlp\transformer>python compute_bleu_test.py #ok
(tf-cpu) C:\project\models\official\nlp\bert>python model_training_utils_test.py #ok