TensorFlow installation and demo at Windows10 64bit

Source: Internet
Author: User

The current popular deep learning library has Caffe,keras,theano, this article uses the Google Open source has used to make Alphago's deep learning system TensorFlow.

1: Install TensorFlow

The earliest TensorFlow only supported Mac and Linux systems and currently supports Windows systems, but requires a python3.5 (64bit) version. TensorFlow has CPU and GPU versions, because this document uses a server that is Nvidia graphics card, so install the GPU version, type on the cmd command line

Pip Install--upgrade Tensorflow-gpu

If the error "cannot remove entries from nonexistent file" appears, execute the following command

"Pip Install--upgrade-i Setuptools", the following interface appears successfully

2: Installing Cuda Library

Using the GPU to run TensorFlow also requires the Cuda and CUDNN libraries to be configured,

Download the WIN10 (64bit) version of the Cuda installation package with the following link, which is approximately 1.2G https://developer.nvidia.com/cuda-downloads

Install Cuda_8.0.61_win10.exe, configure system variables after completion, add three paths to Cuda_path in system variables, C:\Program files\nvidia GPU Computing toolkit\cuda\v8.0

C:\Program Files\nvidia GPU Computing toolkit\cuda\v8.0\bin

C:\Program Files\nvidia GPU Computing toolkit\cuda\v8.0\bin\lib\x64

3: Install the CUDNN library

Download the CUDNN library using the link below

Https://developer.nvidia.com/cudnn

After downloading the decompression, in order to run the TensorFlow can also be loaded into the library, we want to copy the extracted files to CUDA corresponding folder C:\Program files\nvidia GPU Computing toolkit\cuda\v8.0

4: Test Installation

Create a new py file in Pycharm type

Import TensorFlow as TF

Hello = tf.constant (' Hello World, tensorflow! ')

Sess = tf. Session ()

Print (Sess.run (hello))

If you can output ' Hello, tensorflow! ' This means that the configuration is successful, as shown below.

1: Import the necessary modules

Import Sys

Import Cv2

Import NumPy as NP

Import TensorFlow as TF

2: Defining the basic components of CNN

According to the definition of LENET5, using 32*32 image Input, the basic components of CNN include convolution C1, sampling layer S1, convolution C2, sampling layer S2, full bonding layer 1, classification layer 2

3: Training CNN

Reduce the input image to 32*32 size, using the Resize function in OpenCV

Its transformation parameters are

Cv_inter_nn-Nearest neighbor interpolation,

Cv_inter_linear-bilinear interpolation (default use)

Cv_inter_area-resampling using pixel relationships. This method avoids ripples when the image shrinks. When the image is zoomed in, it resembles the Cv_inter_nn method:

Cv_inter_cubic-cubic interpolation.

Output=cv2.resize (IMG, (32,32), Interpolation=cv2. Inter_cubic)

The core code is as follows:

Class Cnnetwork:

num_classes = 2 #分两类

Image_size = 28

Image_pixels = image_size*image_size*3

def inference (Images_placeholder, Keep_prob):

def weight_variable (Shape):

Initial = Tf.truncated_normal (Shape, stddev=0.1)

Return TF. Variable (initial)

def bias_variable (Shape):

Initial = Tf.constant (0.1, Shape=shape)

Return TF. Variable (initial)

def conv2d (x, W):

return tf.nn.conv2d (x, W, strides=[1, 1, 1, 1], padding= ' same ')

def max_pool_2x2 (x):

return Tf.nn.max_pool (x, Ksize=[1, 2, 2, 1),

Strides=[1, 2, 2, 1], padding= ' same ')

X_image = Tf.reshape (Images_placeholder, [-1, 28, 28, 1])

With Tf.name_scope (' conv1 ') as scope:

W_CONV1 = Weight_variable ([5, 5, 3, 32])

B_CONV1 = Bias_variable ([32])

H_CONV1 = Tf.nn.relu (conv2d (X_image, W_CONV1) + b_conv1)

With Tf.name_scope (' pool1 ') as scope:

H_pool1 = max_pool_2x2 (H_CONV1)

With Tf.name_scope (' conv2 ') as scope:

W_conv2 = Weight_variable ([5, 5, 32, 64])

B_conv2 = Bias_variable ([64])

H_conv2 = Tf.nn.relu (conv2d (h_pool1, w_conv2) + b_conv2)

With Tf.name_scope (' pool2 ') as scope:

H_pool2 = max_pool_2x2 (h_conv2)

With Tf.name_scope (' FC1 ') as scope:

W_FC1 = Weight_variable ([7*7*64, 1024])

B_FC1 = Bias_variable ([1024])

H_pool2_flat = Tf.reshape (H_pool2, [-1, 7*7*64])

H_FC1 = Tf.nn.relu (Tf.matmul (H_pool2_flat, W_FC1) + b_fc1)

H_fc1_drop = Tf.nn.dropout (H_FC1, Keep_prob)

With Tf.name_scope (' FC2 ') as scope:

W_FC2 = Weight_variable ([1024x768, num_classes])

B_FC2 = Bias_variable ([num_classes])

With Tf.name_scope (' Softmax ') as scope:

Y_conv=tf.nn.softmax (Tf.matmul (H_fc1_drop, W_FC2) + B_FC2)

Return Y_conv

if __name__ = = ' __main__ ':

Test_image = []

filenames = []

For I in range (1, Len (SYS.ARGV)):

img = Cv2.imread (sys.argv[i])

img = Cv2.resize (IMG, (28, 28))

Test_image.append (Img.flatten (). Astype (Np.float32)/255.0)

Filenames.append (Sys.argv[i])

Test_image = Np.asarray (test_image)

Images_placeholder = Tf.placeholder ("float", shape= (None, Image_pixels))

Labels_placeholder = Tf.placeholder ("float", shape= (None, num_classes))

Keep_prob = Tf.placeholder ("float")

Logits = Inference (Images_placeholder, Keep_prob)

Sess = tf. InteractiveSession ()

Saver = Tf.train.Saver ()

Sess.run (Tf.initialize_all_variables ())

Saver.restore (Sess, "model.ckpt")

For I in range (len (test_image)):

pred = Np.argmax (Logits.eval (feed_dict={

Images_placeholder: [Test_image[i]],

keep_prob:1.0}) [0])

Pred2 = Logits.eval (feed_dict={

Images_placeholder: [Test_image[i]],

keep_prob:1.0}) [0]

Print filenames[i],pred, "{0:10.8f}". Format (Pred2[0]), "{0:10.8f}". Format (Pred2[1])

TensorFlow installation and demo at Windows10 64bit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.