Convolutional Neural Networks (CNNs) are responsible for the major breakthroughs in image recognition made in the past few years. In this chapter we will cover:

  • Implementing a Simpler CNN
  • Implementing an Advanced CNN
  • Retraining Existing CNN models
  • Applying Stylenet/Neural-Style
  • Implementing DeepDream

The CIFAR-10 and CIFAR-100 are labeled subsets of the 80 million (八千万) tiny images dataset. They were collected by Alex Krizhevsky, Vinod Nair, and Geoffrey Hinton.

Click here: https://www.cs.toronto.edu/~kriz/cifar.html

Visual dictionary

Click on top of the map to visualize the images in that region of the visual dictionary.

We present a visualization of all the nouns in the English language arranged by semantic meaning. 根据语义可视化名词

Each of the tiles in the mosaic is an arithmetic average of images relating to one of 53,464 nouns. 与五万多个名词之一相关的一个由一大堆图像算数平均后的像素点

The images for each word were obtained using Google's Image Search and other engines. A total of 7,527,697 images were used, each tile being the average of 140 images.

大概就是如此,如下:

The CIFAR-10 dataset

The CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images per class.

There are training images and test images.

Here are the classes in the dataset, as well as 10 random images from each:

airplane
automobile
bird
cat
deer
dog
frog
horse
ship
truck

The classes are completely mutually exclusive. There is no overlap between automobiles and trucks. "Automobile" includes sedans, SUVs, things of that sort. "Truck" includes only big trucks. Neither includes pickup trucks.

This recipe is an adapted version of the official TensorFlow CIFAR-10 tutorial, which is
available under the See also section at the end of this chapter. We have condensed the
tutorial into one script and will go through it line-by-line and explain all the code that is
necessary. We also revert some constants and parameters to the original cited paper values,
which we will point out in the following appropriated steps.

前言:使用我的四核i7小笔记本训练了一次数据,CPU满负荷,生怕机器就此挂了。

  1. Starting Training
  2. Generation 50: Loss = 1.99133
  3. Generation 100: Loss = 1.82077
  4. Generation 150: Loss = 2.08245
  5. Generation 200: Loss = 1.60232
  6. Generation 250: Loss = 1.73661
  7. Generation 300: Loss = 1.71986
  8. Generation 350: Loss = 1.45252
  9. Generation 400: Loss = 1.51505
  10. Generation 450: Loss = 1.50190
  11. Generation 500: Loss = 1.43989
  12. --- Test Accuracy = 49.22%.
  13. Generation 550: Loss = 1.34899
  14. Generation 600: Loss = 1.28325
  15. Generation 650: Loss = 1.45376
  16. Generation 700: Loss = 1.22179
  17. Generation 750: Loss = 1.40790
  18. Generation 800: Loss = 1.23635
  19. Generation 850: Loss = 1.36577
  20. Generation 900: Loss = 1.29193
  21. Generation 950: Loss = 1.16195
  22. Generation 1000: Loss = 1.30807
  23. --- Test Accuracy = 53.91%.
  24. Generation 1050: Loss = 1.53120
  25. Generation 1100: Loss = 1.19605
  26. Generation 1150: Loss = 1.07220
  27. Generation 1200: Loss = 1.03782
  28. Generation 1250: Loss = 1.22976
  29. Generation 1300: Loss = 0.96371
  30. Generation 1350: Loss = 1.06199
  31. Generation 1400: Loss = 1.04158
  32. Generation 1450: Loss = 1.09863
  33. Generation 1500: Loss = 1.00462
  34. --- Test Accuracy = 61.72%.
  35. Generation 1550: Loss = 0.93589
  36. Generation 1600: Loss = 0.94716
  37. Generation 1650: Loss = 0.97767
  38. Generation 1700: Loss = 0.89214
  39. Generation 1750: Loss = 0.93194
  40. Generation 1800: Loss = 0.78864
  41. Generation 1850: Loss = 0.79083
  42. Generation 1900: Loss = 1.16496
  43. Generation 1950: Loss = 0.95690
  44. Generation 2000: Loss = 0.71276
  45. --- Test Accuracy = 64.84%.
  46. Generation 2050: Loss = 0.90579
  47. Generation 2100: Loss = 0.82735
  48. Generation 2150: Loss = 0.89798
  49. Generation 2200: Loss = 0.90343
  50. Generation 2250: Loss = 0.83713
  51. Generation 2300: Loss = 0.85635
  52. Generation 2350: Loss = 0.83437
  53. Generation 2400: Loss = 0.83430
  54. Generation 2450: Loss = 0.87104
  55. Generation 2500: Loss = 0.84299
  56. --- Test Accuracy = 74.22%.
  57. Generation 2550: Loss = 0.79991
  58. Generation 2600: Loss = 0.80746
  59. Generation 2650: Loss = 0.82027
  60. Generation 2700: Loss = 0.84372
  61. Generation 2750: Loss = 0.81977
  62. Generation 2800: Loss = 0.77057
  63. Generation 2850: Loss = 0.75629
  64. Generation 2900: Loss = 0.82681
  65. Generation 2950: Loss = 0.88289
  66. Generation 3000: Loss = 0.94536
  67. --- Test Accuracy = 71.09%.
  68. Generation 3050: Loss = 0.76870
  69. Generation 3100: Loss = 0.80715
  70. Generation 3150: Loss = 0.80056
  71. Generation 3200: Loss = 0.78387
  72. Generation 3250: Loss = 0.59328
  73. Generation 3300: Loss = 0.84897
  74. Generation 3350: Loss = 0.67461
  75. Generation 3400: Loss = 0.64628
  76. Generation 3450: Loss = 0.64160
  77. Generation 3500: Loss = 0.63691
  78. --- Test Accuracy = 70.31%.
  79. Generation 3550: Loss = 0.63177
  80. Generation 3600: Loss = 0.74349
  81. Generation 3650: Loss = 0.64307
  82. Generation 3700: Loss = 0.61021
  83. Generation 3750: Loss = 0.64688
  84. Generation 3800: Loss = 0.63159
  85. Generation 3850: Loss = 0.78472
  86. Generation 3900: Loss = 0.75076
  87. Generation 3950: Loss = 0.53717
  88. Generation 4000: Loss = 0.46514
  89. --- Test Accuracy = 65.62%.
  90. Generation 4050: Loss = 0.68460
  91. Generation 4100: Loss = 0.58425
  92. Generation 4150: Loss = 0.47215
  93. Generation 4200: Loss = 0.58976
  94. Generation 4250: Loss = 0.64681
  95. Generation 4300: Loss = 0.77239
  96. Generation 4350: Loss = 0.58956
  97. Generation 4400: Loss = 0.70569
  98. Generation 4450: Loss = 0.66185
  99. Generation 4500: Loss = 0.46662
  100. --- Test Accuracy = 76.56%.
  101. Generation 4550: Loss = 0.49475
  102. Generation 4600: Loss = 0.54739
  103. Generation 4650: Loss = 0.52838
  104. Generation 4700: Loss = 0.81228
  105. Generation 4750: Loss = 0.49100
  106. Generation 4800: Loss = 0.51341
  107. Generation 4850: Loss = 0.47875
  108. Generation 4900: Loss = 0.37848
  109. Generation 4950: Loss = 0.52750
  110. Generation 5000: Loss = 0.53570
  111. --- Test Accuracy = 63.28%.
  112. Generation 5050: Loss = 0.63138
  113. Generation 5100: Loss = 0.49153
  114. Generation 5150: Loss = 0.54037
  115. Generation 5200: Loss = 0.72630
  116. Generation 5250: Loss = 0.44166
  117. Generation 5300: Loss = 0.51812
  118. Generation 5350: Loss = 0.51912
  119. Generation 5400: Loss = 0.54622
  120. Generation 5450: Loss = 0.41648
  121. Generation 5500: Loss = 0.57976
  122. --- Test Accuracy = 71.88%.
  123. Generation 5550: Loss = 0.55666
  124. Generation 5600: Loss = 0.44564
  125. Generation 5650: Loss = 0.46812
  126. ... 未完,但担心机器挂了

部分日志

1. 初始化.

  1. # More Advanced CNN Model: CIFAR-10
  2. #---------------------------------------
  3. #
  4. # In this example, we will download the CIFAR-10 images
  5. # and build a CNN model with dropout and regularization
  6. #
  7. # CIFAR is composed ot 50k train and 10k test
  8. # images that are 32x32.
  9.  
  10. import os
  11. import tarfile
  12. import matplotlib.pyplot as plt
  13. import tensorflow as tf
  14. from six.moves import urllib
  15. from tensorflow.python.framework import ops
  16. ops.reset_default_graph()
  17.  
  18. # Change Directory
  19. abspath = os.path.abspath(__file__)
  20. dname = os.path.dirname(abspath)
  21. os.chdir(dname)
  22.  
  23. # Start a graph session
  24. sess = tf.Session()

2. 参数设置.

  1. # Set model parameters
  2. batch_size = 128
  3. data_dir = 'temp'
  4. output_every = 50
  5. generations = 20000
  6. eval_every = 500
  7. image_height = 32
  8. image_width = 32
  9. crop_height = 24
  10. crop_width = 24
  11. num_channels = 3
  12. num_targets = 10
  13. extract_folder = 'cifar-10-batches-bin'
  14.  
  15. # Exponential Learning Rate Decay Params
  16. learning_rate = 0.1
  17. lr_decay = 0.1
  18. num_gens_to_wait = 250.
  19.  
  20. # Extract model parameters
  21. image_vec_length = image_height * image_width * num_channels
  22. record_length = 1 + image_vec_length # ( + 1 for the 0-9 label)

3. Create folder then load data. 

  1. # Create folder then load data
  2. data_dir = 'temp'
  3. if not os.path.exists(data_dir):
  4. os.makedirs(data_dir)
  5. cifar10_url = 'http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz'
  6.  
  7. # Check if file exists, otherwise download it
  8. data_file = os.path.join(data_dir, 'cifar-10-binary.tar.gz')
  9. if os.path.isfile(data_file):
  10. pass
  11. else:
  12. # Download file
  13. def progress(block_num, block_size, total_size):
  14. progress_info = [cifar10_url, float(block_num * block_size) / float(total_size) * 100.0]
  15. print('\r Downloading {} - {:.2f}%'.format(*progress_info), end="")
  16. filepath, _ = urllib.request.urlretrieve(cifar10_url, data_file, progress)
  17. # Extract file
  18. tarfile.open(filepath, 'r:gz').extractall(data_dir)

4. Get data then declare model.

  1. # Get data
  2. print('Getting/Transforming Data.')
  3. # Initialize the data pipeline
  4. images, targets = input_pipeline(batch_size, train_logical=True)  // --> 4.1
  5. # Get batch test images and targets from pipline
  6. test_images, test_targets = input_pipeline(batch_size, train_logical=False)
  7.  
  8. # Declare Model
  9. print('Creating the CIFAR10 Model.')
  10. with tf.variable_scope('model_definition') as scope:
  11. # Declare the training network model
  12. model_output = cifar_cnn_model(images, batch_size)
  13. # This is very important!!! We must set the scope to REUSE the variables,
  14. # Otherwise, when we set the test network model, it will create new random variables.
  15. # Otherwise we get random evaluations on the test batches.
  16. scope.reuse_variables()
  17. test_output = cifar_cnn_model(test_images, batch_size)

4.1 序列加载 - input_pipeline

  1. # Create a CIFAR image pipeline from reader
  2. def input_pipeline(batch_size, train_logical=True):
  3. if train_logical:
  4. files = [os.path.join(data_dir, extract_folder, 'data_batch_{}.bin'.format(i)) for i in range(1,6)]
  5. else:
  6. files = [os.path.join(data_dir, extract_folder, 'test_batch.bin')]
  7. filename_queue = tf.train.string_input_producer(files)
  8. image, label = read_cifar_files(filename_queue)  // --> 4.2
  9.  
  10. # min_after_dequeue defines how big a buffer we will randomly sample
  11. # from -- bigger means better shuffling but slower start up and more
  12. # memory used.
  13. # capacity must be larger than min_after_dequeue and the amount larger
  14. # determines the maximum we will prefetch. Recommendation:
  15. # min_after_dequeue + (num_threads + a small safety margin) * batch_size
  16. min_after_dequeue = 5000
  17. capacity = min_after_dequeue + 3 * batch_size
  18. example_batch, label_batch = tf.train.shuffle_batch([image, label],
  19. batch_size=batch_size,
  20. capacity=capacity,
  21. min_after_dequeue=min_after_dequeue)
  22.  
  23. return(example_batch, label_batch)

4.2 图片预处理 - read_cifar_files

对图片样本数据做了一些预处理工作。

  1. # Define CIFAR reader
  2. def read_cifar_files(filename_queue, distort_images = True):
  3. reader = tf.FixedLengthRecordReader(record_bytes=record_length)
  4. key, record_string = reader.read(filename_queue)
  5. record_bytes = tf.decode_raw(record_string, tf.uint8)
  6. image_label = tf.cast(tf.slice(record_bytes, [0], [1]), tf.int32)
  7.  
  8. # Extract image
  9. image_extracted = tf.reshape(tf.slice(record_bytes, [1], [image_vec_length]),
  10. [num_channels, image_height, image_width])
  11.  
  12. # Reshape image
  13. image_uint8image = tf.transpose(image_extracted, [1, 2, 0])
  14. reshaped_image = tf.cast(image_uint8image, tf.float32)
  15. # Randomly Crop image
  16. final_image = tf.image.resize_image_with_crop_or_pad(reshaped_image, crop_width, crop_height)
  17. # 一定的预处理步骤
  18. if distort_images:
  19. # Randomly flip the image horizontally, change the brightness and contrast
  20. final_image = tf.image.random_flip_left_right(final_image)
  21. final_image = tf.image.random_brightness(final_image,max_delta=63)
  22. final_image = tf.image.random_contrast(final_image,lower=0.2, upper=1.8)
  23.  
  24. # Normalize whitening
  25. final_image = tf.image.per_image_standardization(final_image)
  26. return(final_image, image_label)

4.3 定义模型 

Next, we can declare our model function.

The model we will use has 2 convolutional layers, followed by 3 fully connected layers.

To make variable declaration easier, we'll start by declaring two variable functions. (ori: 32x32 --> 8x8)

  • The two convolutional layers will create 64 features each.
  • The 1st fully connected layer will connect the 2nd convolutional layer with 384 hidden nodes.
  • The 2nd fully connected operation will connect those 384 hidden nodes to 192 hidden nodes.  
  • The final hidden layer operation will then connect the 192 nodes to the 10 output classes we are trying to predict.

【input --> 1st conv + pooling --> 2nd conv + pooling --> fully connected --> 384 -- fully connected --> 192 -- fully connected --> 10】

  1. # Define the model architecture, this will return logits from images
  2. def cifar_cnn_model(input_images, batch_size, train_logical=True):
  3. def truncated_normal_var(name, shape, dtype):
  4. return(tf.get_variable(name=name, shape=shape, dtype=dtype, initializer=tf.truncated_normal_initializer(stddev=0.05)))
  5. def zero_var(name, shape, dtype):
  6. return(tf.get_variable(name=name, shape=shape, dtype=dtype, initializer=tf.constant_initializer(0.0)))
  7.  
  8. # First Convolutional Layer
  9. with tf.variable_scope('conv1') as scope:
  10. # Conv_kernel is 5x5 for all 3 colors and we will create 64 features
  11. conv1_kernel = truncated_normal_var(name='conv_kernel1', shape=[5, 5, 3, 64], dtype=tf.float32)
  12. # We convolve across the image with a stride size of 1
  13. conv1 = tf.nn.conv2d(input_images, conv1_kernel, [1, 1, 1, 1], padding='SAME')
  14. # Initialize and add the bias term
  15. conv1_bias = zero_var(name='conv_bias1', shape=[64], dtype=tf.float32)
  16. conv1_add_bias = tf.nn.bias_add(conv1, conv1_bias)
  17. # ReLU element wise
  18. relu_conv1 = tf.nn.relu(conv1_add_bias)
  19.  
  20. # Max Pooling
  21. pool1 = tf.nn.max_pool(relu_conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],padding='SAME', name='pool_layer1')
  22.  
  23. # Local Response Normalization (parameters from paper)
  24. # paper: http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks
  25. norm1 = tf.nn.lrn(pool1, depth_radius=5, bias=2.0, alpha=1e-3, beta=0.75, name='norm1')
  26.  
  27. # Second Convolutional Layer
  28. with tf.variable_scope('conv2') as scope:
  29. # Conv kernel is 5x5, across all prior 64 features and we create 64 more features
  30. conv2_kernel = truncated_normal_var(name='conv_kernel2', shape=[5, 5, 64, 64], dtype=tf.float32)
  31. # Convolve filter across prior output with stride size of 1
  32. conv2 = tf.nn.conv2d(norm1, conv2_kernel, [1, 1, 1, 1], padding='SAME')
  33. # Initialize and add the bias
  34. conv2_bias = zero_var(name='conv_bias2', shape=[64], dtype=tf.float32)
  35. conv2_add_bias = tf.nn.bias_add(conv2, conv2_bias)
  36. # ReLU element wise
  37. relu_conv2 = tf.nn.relu(conv2_add_bias)
  38.  
  39. # Max Pooling
  40. pool2 = tf.nn.max_pool(relu_conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool_layer2')
  41.  
  42. # Local Response Normalization (parameters from paper)
  43. norm2 = tf.nn.lrn(pool2, depth_radius=5, bias=2.0, alpha=1e-3, beta=0.75, name='norm2')
  44.  
  45. # Reshape output into a single matrix for multiplication for the fully connected layers
  46. reshaped_output = tf.reshape(norm2, [batch_size, -1])
  47. reshaped_dim = reshaped_output.get_shape()[1].value
  48.  
  49. # First Fully Connected Layer
  50. with tf.variable_scope('full1') as scope:
  51. # Fully connected layer will have 384 outputs.
  52. full_weight1 = truncated_normal_var(name='full_mult1', shape=[reshaped_dim, 384], dtype=tf.float32)
  53. full_bias1 = zero_var(name='full_bias1', shape=[384], dtype=tf.float32)
  54. full_layer1 = tf.nn.relu(tf.add(tf.matmul(reshaped_output, full_weight1), full_bias1))
  55.  
  56. # Second Fully Connected Layer
  57. with tf.variable_scope('full2') as scope:
  58. # Second fully connected layer has 192 outputs.
  59. full_weight2 = truncated_normal_var(name='full_mult2', shape=[384, 192], dtype=tf.float32)
  60. full_bias2 = zero_var(name='full_bias2', shape=[192], dtype=tf.float32)
  61. full_layer2 = tf.nn.relu(tf.add(tf.matmul(full_layer1, full_weight2), full_bias2))
  62.  
  63. # Final Fully Connected Layer -> 10 categories for output (num_targets)
  64. with tf.variable_scope('full3') as scope:
  65. # Final fully connected layer has 10 (num_targets) outputs.
  66. full_weight3 = truncated_normal_var(name='full_mult3', shape=[192, num_targets], dtype=tf.float32)
  67. full_bias3 = zero_var(name='full_bias3', shape=[num_targets], dtype=tf.float32)
  68. final_output = tf.add(tf.matmul(full_layer2, full_weight3), full_bias3)
  69.  
  70. return(final_output)

5. Loss and accuracy.

  1. # Loss function
  2. def cifar_loss(logits, targets):
  3. # Get rid of extra dimensions and cast targets into integers
  4. targets = tf.squeeze(tf.cast(targets, tf.int32))
  5. # Calculate cross entropy from logits and targets
  6. cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, targets)
  7. # Take the average loss across batch size
  8. cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
  9. return(cross_entropy_mean)
  10.  
  11. # Accuracy function
  12. def accuracy_of_batch(logits, targets):
  13. # Make sure targets are integers and drop extra dimensions
  14. targets = tf.squeeze(tf.cast(targets, tf.int32))
  15. # Get predicted values by finding which logit is the greatest
  16. batch_predictions = tf.cast(tf.argmax(logits, 1), tf.int32)
  17. # Check if they are equal across the batch
  18. predicted_correctly = tf.equal(batch_predictions, targets)
  19. # Average the 1's and 0's (True's and False's) across the batch size
  20. accuracy = tf.reduce_mean(tf.cast(predicted_correctly, tf.float32))
  21. return(accuracy)
  22.  
  23. # Train step
  24. def train_step(loss_value, generation_num):
  25. # Our learning rate is an exponential decay after we wait a fair number of generations
  26. model_learning_rate = tf.train.exponential_decay(learning_rate, generation_num,
  27. num_gens_to_wait, lr_decay, staircase=True)
  28. # Create optimizer
  29. my_optimizer = tf.train.GradientDescentOptimizer(model_learning_rate)
  30. # Initialize train step
  31. train_step = my_optimizer.minimize(loss_value)
  32. return(train_step)
  33.  
  34. # Declare loss function
  35. print('Declare Loss Function.')
  36. loss = cifar_loss(model_output, targets)
  37.  
  38. # Create accuracy function
  39. accuracy = accuracy_of_batch(test_output, test_targets)
  40.  
  41. # Create training operations
  42. print('Creating the Training Operation.')
  43. generation_num = tf.Variable(0, trainable=False)
  44. train_op = train_step(loss, generation_num)
  45.  
  46. # Initialize Variables
  47. print('Initializing the Variables.')
  48. init = tf.initialize_all_variables()
  49. sess.run(init)
  50.  
  51. # Initialize queue (This queue will feed into the model, so no placeholders necessary)
  52. tf.train.start_queue_runners(sess=sess)

6. Training.

  1. # Train CIFAR Model
  2. print('Starting Training')
  3. train_loss = []
  4. test_accuracy = []
  5. for i in range(generations):
  6. _, loss_value = sess.run([train_op, loss])
  7.  
  8. if (i+1) % output_every == 0:
  9. train_loss.append(loss_value)
  10. output = 'Generation {}: Loss = {:.5f}'.format((i+1), loss_value)
  11. print(output)
  12.  
  13. if (i+1) % eval_every == 0:
  14. [temp_accuracy] = sess.run([accuracy])
  15. test_accuracy.append(temp_accuracy)
  16. acc_output = ' --- Test Accuracy = {:.2f}%.'.format(100.*temp_accuracy)
  17. print(acc_output)
  18.  
  19. # Print loss and accuracy
  20. # Matlotlib code to plot the loss and accuracies
  21. eval_indices = range(0, generations, eval_every)
  22. output_indices = range(0, generations, output_every)
  23.  
  24. # Plot loss over time
  25. plt.plot(output_indices, train_loss, 'k-')
  26. plt.title('Softmax Loss per Generation')
  27. plt.xlabel('Generation')
  28. plt.ylabel('Softmax Loss')
  29. plt.show()
  30.  
  31. # Plot accuracy over time
  32. plt.plot(eval_indices, test_accuracy, 'k-')
  33. plt.title('Test Accuracy')
  34. plt.xlabel('Generation')
  35. plt.ylabel('Accuracy')
  36. plt.show()
  1. # More Advanced CNN Model: CIFAR-10
  2. #---------------------------------------
  3. #
  4. # In this example, we will download the CIFAR-10 images
  5. # and build a CNN model with dropout and regularization
  6. #
  7. # CIFAR is composed ot 50k train and 10k test
  8. # images that are 32x32.
  9.  
  10. import os
  11. import tarfile
  12. import matplotlib.pyplot as plt
  13. import tensorflow as tf
  14. from six.moves import urllib
  15. from tensorflow.python.framework import ops
  16. ops.reset_default_graph()
  17.  
  18. # Change Directory
  19. abspath = os.path.abspath(__file__)
  20. dname = os.path.dirname(abspath)
  21. os.chdir(dname)
  22.  
  23. # Start a graph session
  24. sess = tf.Session()
  25.  
  26. # Set model parameters
  27. batch_size = 128
  28. data_dir = 'temp'
  29. output_every = 50
  30. generations = 20000
  31. eval_every = 500
  32. image_height = 32
  33. image_width = 32
  34. crop_height = 24
  35. crop_width = 24
  36. num_channels = 3
  37. num_targets = 10
  38. extract_folder = 'cifar-10-batches-bin'
  39.  
  40. # Exponential Learning Rate Decay Params
  41. learning_rate = 0.1
  42. lr_decay = 0.1
  43. num_gens_to_wait = 250.
  44.  
  45. # Extract model parameters
  46. image_vec_length = image_height * image_width * num_channels
  47. record_length = 1 + image_vec_length # ( + 1 for the 0-9 label)
  48.  
  49. # Create folder then load data
  50. data_dir = 'temp'
  51. if not os.path.exists(data_dir):
  52. os.makedirs(data_dir)
  53. cifar10_url = 'http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz'
  54.  
  55. # Check if file exists, otherwise download it
  56. data_file = os.path.join(data_dir, 'cifar-10-binary.tar.gz')
  57. if os.path.isfile(data_file):
  58. pass
  59. else:
  60. # Download file
  61. def progress(block_num, block_size, total_size):
  62. progress_info = [cifar10_url, float(block_num * block_size) / float(total_size) * 100.0]
  63. print('\r Downloading {} - {:.2f}%'.format(*progress_info), end="")
  64. filepath, _ = urllib.request.urlretrieve(cifar10_url, data_file, progress)
  65. # Extract file
  66. tarfile.open(filepath, 'r:gz').extractall(data_dir)
  67.  
  68. # Define CIFAR reader
  69. def read_cifar_files(filename_queue, distort_images = True):
  70. reader = tf.FixedLengthRecordReader(record_bytes=record_length)
  71. key, record_string = reader.read(filename_queue)
  72. record_bytes = tf.decode_raw(record_string, tf.uint8)
  73. image_label = tf.cast(tf.slice(record_bytes, [0], [1]), tf.int32)
  74.  
  75. # Extract image
  76. image_extracted = tf.reshape(tf.slice(record_bytes, [1], [image_vec_length]),
  77. [num_channels, image_height, image_width])
  78.  
  79. # Reshape image
  80. image_uint8image = tf.transpose(image_extracted, [1, 2, 0])
  81. reshaped_image = tf.cast(image_uint8image, tf.float32)
  82. # Randomly Crop image
  83. final_image = tf.image.resize_image_with_crop_or_pad(reshaped_image, crop_width, crop_height)
  84.  
  85. if distort_images:
  86. # Randomly flip the image horizontally, change the brightness and contrast
  87. final_image = tf.image.random_flip_left_right(final_image)
  88. final_image = tf.image.random_brightness(final_image,max_delta=63)
  89. final_image = tf.image.random_contrast(final_image,lower=0.2, upper=1.8)
  90.  
  91. # Normalize whitening
  92. final_image = tf.image.per_image_standardization(final_image)
  93. return(final_image, image_label)
  94.  
  95. # Create a CIFAR image pipeline from reader
  96. def input_pipeline(batch_size, train_logical=True):
  97. if train_logical:
  98. files = [os.path.join(data_dir, extract_folder, 'data_batch_{}.bin'.format(i)) for i in range(1,6)]
  99. else:
  100. files = [os.path.join(data_dir, extract_folder, 'test_batch.bin')]
  101.  
  102. filename_queue = tf.train.string_input_producer(files)
  103. image, label = read_cifar_files(filename_queue)
  104.  
  105. # min_after_dequeue defines how big a buffer we will randomly sample
  106. # from -- bigger means better shuffling but slower start up and more
  107. # memory used.
  108. # capacity must be larger than min_after_dequeue and the amount larger
  109. # determines the maximum we will prefetch. Recommendation:
  110. # min_after_dequeue + (num_threads + a small safety margin) * batch_size
  111. min_after_dequeue = 5000
  112. capacity = min_after_dequeue + 3 * batch_size
  113. example_batch, label_batch = tf.train.shuffle_batch([image, label],
  114. batch_size=batch_size,
  115. capacity=capacity,
  116. min_after_dequeue=min_after_dequeue)
  117.  
  118. return(example_batch, label_batch)
  119.  
  120. # Define the model architecture, this will return logits from images
  121. def cifar_cnn_model(input_images, batch_size, train_logical=True):
  122. def truncated_normal_var(name, shape, dtype):
  123. return(tf.get_variable(name=name, shape=shape, dtype=dtype, initializer=tf.truncated_normal_initializer(stddev=0.05)))
  124. def zero_var(name, shape, dtype):
  125. return(tf.get_variable(name=name, shape=shape, dtype=dtype, initializer=tf.constant_initializer(0.0)))
  126.  
  127. # First Convolutional Layer
  128. with tf.variable_scope('conv1') as scope:
  129. # Conv_kernel is 5x5 for all 3 colors and we will create 64 features
  130. conv1_kernel = truncated_normal_var(name='conv_kernel1', shape=[5, 5, 3, 64], dtype=tf.float32)
  131. # We convolve across the image with a stride size of 1
  132. conv1 = tf.nn.conv2d(input_images, conv1_kernel, [1, 1, 1, 1], padding='SAME')
  133. # Initialize and add the bias term
  134. conv1_bias = zero_var(name='conv_bias1', shape=[64], dtype=tf.float32)
  135. conv1_add_bias = tf.nn.bias_add(conv1, conv1_bias)
  136. # ReLU element wise
  137. relu_conv1 = tf.nn.relu(conv1_add_bias)
  138.  
  139. # Max Pooling
  140. pool1 = tf.nn.max_pool(relu_conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],padding='SAME', name='pool_layer1')
  141.  
  142. # Local Response Normalization (parameters from paper)
  143. # paper: http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks
  144. norm1 = tf.nn.lrn(pool1, depth_radius=5, bias=2.0, alpha=1e-3, beta=0.75, name='norm1')
  145.  
  146. # Second Convolutional Layer
  147. with tf.variable_scope('conv2') as scope:
  148. # Conv kernel is 5x5, across all prior 64 features and we create 64 more features
  149. conv2_kernel = truncated_normal_var(name='conv_kernel2', shape=[5, 5, 64, 64], dtype=tf.float32)
  150. # Convolve filter across prior output with stride size of 1
  151. conv2 = tf.nn.conv2d(norm1, conv2_kernel, [1, 1, 1, 1], padding='SAME')
  152. # Initialize and add the bias
  153. conv2_bias = zero_var(name='conv_bias2', shape=[64], dtype=tf.float32)
  154. conv2_add_bias = tf.nn.bias_add(conv2, conv2_bias)
  155. # ReLU element wise
  156. relu_conv2 = tf.nn.relu(conv2_add_bias)
  157.  
  158. # Max Pooling
  159. pool2 = tf.nn.max_pool(relu_conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool_layer2')
  160.  
  161. # Local Response Normalization (parameters from paper)
  162. norm2 = tf.nn.lrn(pool2, depth_radius=5, bias=2.0, alpha=1e-3, beta=0.75, name='norm2')
  163.  
  164. # Reshape output into a single matrix for multiplication for the fully connected layers
  165. reshaped_output = tf.reshape(norm2, [batch_size, -1])
  166. reshaped_dim = reshaped_output.get_shape()[1].value
  167.  
  168. # First Fully Connected Layer
  169. with tf.variable_scope('full1') as scope:
  170. # Fully connected layer will have 384 outputs.
  171. full_weight1 = truncated_normal_var(name='full_mult1', shape=[reshaped_dim, 384], dtype=tf.float32)
  172. full_bias1 = zero_var(name='full_bias1', shape=[384], dtype=tf.float32)
  173. full_layer1 = tf.nn.relu(tf.add(tf.matmul(reshaped_output, full_weight1), full_bias1))
  174.  
  175. # Second Fully Connected Layer
  176. with tf.variable_scope('full2') as scope:
  177. # Second fully connected layer has 192 outputs.
  178. full_weight2 = truncated_normal_var(name='full_mult2', shape=[384, 192], dtype=tf.float32)
  179. full_bias2 = zero_var(name='full_bias2', shape=[192], dtype=tf.float32)
  180. full_layer2 = tf.nn.relu(tf.add(tf.matmul(full_layer1, full_weight2), full_bias2))
  181.  
  182. # Final Fully Connected Layer -> 10 categories for output (num_targets)
  183. with tf.variable_scope('full3') as scope:
  184. # Final fully connected layer has 10 (num_targets) outputs.
  185. full_weight3 = truncated_normal_var(name='full_mult3', shape=[192, num_targets], dtype=tf.float32)
  186. full_bias3 = zero_var(name='full_bias3', shape=[num_targets], dtype=tf.float32)
  187. final_output = tf.add(tf.matmul(full_layer2, full_weight3), full_bias3)
  188.  
  189. return(final_output)
  190.  
  191. # Loss function
  192. def cifar_loss(logits, targets):
  193. # Get rid of extra dimensions and cast targets into integers
  194. targets = tf.squeeze(tf.cast(targets, tf.int32))
  195. # Calculate cross entropy from logits and targets
  196. cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, targets)
  197. # Take the average loss across batch size
  198. cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
  199. return(cross_entropy_mean)
  200.  
  201. # Train step
  202. def train_step(loss_value, generation_num):
  203. # Our learning rate is an exponential decay after we wait a fair number of generations
  204. model_learning_rate = tf.train.exponential_decay(learning_rate, generation_num,
  205. num_gens_to_wait, lr_decay, staircase=True)
  206. # Create optimizer
  207. my_optimizer = tf.train.GradientDescentOptimizer(model_learning_rate)
  208. # Initialize train step
  209. train_step = my_optimizer.minimize(loss_value)
  210. return(train_step)
  211.  
  212. # Accuracy function
  213. def accuracy_of_batch(logits, targets):
  214. # Make sure targets are integers and drop extra dimensions
  215. targets = tf.squeeze(tf.cast(targets, tf.int32))
  216. # Get predicted values by finding which logit is the greatest
  217. batch_predictions = tf.cast(tf.argmax(logits, 1), tf.int32)
  218. # Check if they are equal across the batch
  219. predicted_correctly = tf.equal(batch_predictions, targets)
  220. # Average the 1's and 0's (True's and False's) across the batch size
  221. accuracy = tf.reduce_mean(tf.cast(predicted_correctly, tf.float32))
  222. return(accuracy)
  223.  
  224. # Get data
  225. print('Getting/Transforming Data.')
  226. # Initialize the data pipeline
  227. images, targets = input_pipeline(batch_size, train_logical=True)
  228. # Get batch test images and targets from pipline
  229. test_images, test_targets = input_pipeline(batch_size, train_logical=False)
  230.  
  231. # Declare Model
  232. print('Creating the CIFAR10 Model.')
  233. with tf.variable_scope('model_definition') as scope:
  234. # Declare the training network model
  235. model_output = cifar_cnn_model(images, batch_size)
  236. # This is very important!!! We must set the scope to REUSE the variables,
  237. # otherwise, when we set the test network model, it will create new random
  238. # variables. Otherwise we get random evaluations on the test batches.
  239. scope.reuse_variables()
  240. test_output = cifar_cnn_model(test_images, batch_size)
  241.  
  242. # Declare loss function
  243. print('Declare Loss Function.')
  244. loss = cifar_loss(model_output, targets)
  245.  
  246. # Create accuracy function
  247. accuracy = accuracy_of_batch(test_output, test_targets)
  248.  
  249. # Create training operations
  250. print('Creating the Training Operation.')
  251. generation_num = tf.Variable(0, trainable=False)
  252. train_op = train_step(loss, generation_num)
  253.  
  254. # Initialize Variables
  255. print('Initializing the Variables.')
  256. init = tf.initialize_all_variables()
  257. sess.run(init)
  258.  
  259. # Initialize queue (This queue will feed into the model, so no placeholders necessary)
  260. tf.train.start_queue_runners(sess=sess)
  261.  
  262. # Train CIFAR Model
  263. print('Starting Training')
  264. train_loss = []
  265. test_accuracy = []
  266. for i in range(generations):
  267. _, loss_value = sess.run([train_op, loss])
  268.  
  269. if (i+1) % output_every == 0:
  270. train_loss.append(loss_value)
  271. output = 'Generation {}: Loss = {:.5f}'.format((i+1), loss_value)
  272. print(output)
  273.  
  274. if (i+1) % eval_every == 0:
  275. [temp_accuracy] = sess.run([accuracy])
  276. test_accuracy.append(temp_accuracy)
  277. acc_output = ' --- Test Accuracy = {:.2f}%.'.format(100.*temp_accuracy)
  278. print(acc_output)
  279.  
  280. # Print loss and accuracy
  281. # Matlotlib code to plot the loss and accuracies
  282. eval_indices = range(0, generations, eval_every)
  283. output_indices = range(0, generations, output_every)
  284.  
  285. # Plot loss over time
  286. plt.plot(output_indices, train_loss, 'k-')
  287. plt.title('Softmax Loss per Generation')
  288. plt.xlabel('Generation')
  289. plt.ylabel('Softmax Loss')
  290. plt.show()
  291.  
  292. # Plot accuracy over time
  293. plt.plot(eval_indices, test_accuracy, 'k-')
  294. plt.title('Test Accuracy')
  295. plt.xlabel('Generation')
  296. plt.ylabel('Accuracy')
  297. plt.show()

完整代码

  1. # student @ user-ubuntu in ~/work/jeff/cnn_cifar [3:43:05]
  2. $ python cnn_cifar10.py
  3.  
  4. 2017-07-27 03:43:18.521404: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
  5. 2017-07-27 03:43:18.521432: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
  6. 2017-07-27 03:43:18.521439: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
  7. 2017-07-27 03:43:18.521461: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
  8. 2017-07-27 03:43:18.521482: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
  9. 2017-07-27 03:43:18.971638: I tensorflow/core/common_runtime/gpu/gpu_device.cc:940] Found device 0 with properties:
  10. name: GeForce GTX 1080 Ti
  11. major: 6 minor: 1 memoryClockRate (GHz) 1.582
  12. pciBusID 0000:82:00.0
  13. Total memory: 10.91GiB
  14. Free memory: 10.75GiB
  15. 2017-07-27 03:43:19.404592: W tensorflow/stream_executor/cuda/cuda_driver.cc:523] A non-primary context 0x2b7d1e0 exists before initializing the StreamExecutor. We haven't verified StreamExecutor works with that.
  16. 2017-07-27 03:43:19.406002: I tensorflow/core/common_runtime/gpu/gpu_device.cc:940] Found device 1 with properties:
  17. name: GeForce GTX 1080 Ti
  18. major: 6 minor: 1 memoryClockRate (GHz) 1.582
  19. pciBusID 0000:83:00.0
  20. Total memory: 10.91GiB
  21. Free memory: 10.75GiB
  22. 2017-07-27 03:43:19.407105: I tensorflow/core/common_runtime/gpu/gpu_device.cc:961] DMA: 0 1
  23. 2017-07-27 03:43:19.407122: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0: Y Y
  24. 2017-07-27 03:43:19.407127: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 1: Y Y
  25. 2017-07-27 03:43:19.407143: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:82:00.0)
  26. 2017-07-27 03:43:19.407150: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Creating TensorFlow device (/gpu:1) -> (device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:83:00.0)
  27. Getting/Transforming Data.
  28. Creating the CIFAR10 Model.
  29. Declare Loss Function.
  30. Creating the Training Operation.
  31. Initializing the Variables.
  32. WARNING:tensorflow:From /home/sean/virtualenv/tensorflow-py3.5/lib/python3.5/site-packages/tensorflow/python/util/tf_should_use.py:170: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
  33. Instructions for updating:
  34. Use `tf.global_variables_initializer` instead.
  35. Starting Training
  36.  
  37. Generation 50 : Loss = 1.98874
  38. Generation 100: Loss = 1.89331
  39. Generation 150: Loss = 2.14434
  40. Generation 200: Loss = 1.62269
  41. Generation 250: Loss = 1.73568
  42. Generation 300: Loss = 1.67207
  43. Generation 350: Loss = 1.47158
  44. Generation 400: Loss = 1.70603
  45. Generation 450: Loss = 1.70473
  46. Generation 500: Loss = 1.54680
  47. --- Test Accuracy = 50.00%.
  48. Generation 550: Loss = 1.47264
  49. Generation 600: Loss = 1.33211
  50. Generation 650: Loss = 1.28465
  51. Generation 700: Loss = 1.47795
  52. Generation 750: Loss = 1.54336
  53. Generation 800: Loss = 1.32847
  54. Generation 850: Loss = 1.30343
  55. Generation 900: Loss = 1.21732
  56. Generation 950: Loss = 1.38395
  57. Generation 1000: Loss = 1.18652
  58. --- Test Accuracy = 58.59%.
  59. Generation 1050: Loss = 1.18199
  60. Generation 1100: Loss = 1.26724
  61. Generation 1150: Loss = 1.18451
  62. Generation 1200: Loss = 1.38382
  63. Generation 1250: Loss = 1.10484
  64. Generation 1300: Loss = 1.22248
  65. Generation 1350: Loss = 1.03363
  66. Generation 1400: Loss = 1.19343
  67. Generation 1450: Loss = 1.21833
  68. Generation 1500: Loss = 1.28556
  69. --- Test Accuracy = 64.06%.
  70. Generation 1550: Loss = 1.05083
  71. Generation 1600: Loss = 1.12365
  72. Generation 1650: Loss = 0.98373
  73. Generation 1700: Loss = 0.98653
  74. Generation 1750: Loss = 0.89979
  75. Generation 1800: Loss = 1.15915
  76. Generation 1850: Loss = 0.97605
  77. Generation 1900: Loss = 0.98232
  78. Generation 1950: Loss = 0.93118
  79. Generation 2000: Loss = 0.84955
  80. --- Test Accuracy = 71.88%.
  81. Generation 2050: Loss = 1.01471
  82. Generation 2100: Loss = 1.03468
  83. Generation 2150: Loss = 1.03076
  84. Generation 2200: Loss = 1.12238
  85. Generation 2250: Loss = 0.72818
  86. Generation 2300: Loss = 0.92140
  87. Generation 2350: Loss = 0.96073
  88. Generation 2400: Loss = 0.90507
  89. Generation 2450: Loss = 0.84059
  90. Generation 2500: Loss = 1.00707
  91. --- Test Accuracy = 64.84%.
  92. Generation 2550: Loss = 0.77818
  93. Generation 2600: Loss = 0.91769
  94. Generation 2650: Loss = 0.83464
  95. Generation 2700: Loss = 0.82919
  96. Generation 2750: Loss = 0.71690
  97. Generation 2800: Loss = 0.69588
  98. Generation 2850: Loss = 0.81282
  99. Generation 2900: Loss = 0.86472
  100. Generation 2950: Loss = 0.68893
  101. Generation 3000: Loss = 0.68487
  102. --- Test Accuracy = 66.41%.
  103. Generation 3050: Loss = 0.77830
  104. Generation 3100: Loss = 0.98387
  105. Generation 3150: Loss = 0.67766
  106. Generation 3200: Loss = 0.83822
  107. Generation 3250: Loss = 0.72022
  108. Generation 3300: Loss = 0.61615
  109. Generation 3350: Loss = 0.80152
  110. Generation 3400: Loss = 0.61803
  111. Generation 3450: Loss = 0.69104
  112. Generation 3500: Loss = 0.86474
  113. --- Test Accuracy = 71.09%.
  114. Generation 3550: Loss = 0.56136
  115. Generation 3600: Loss = 0.83764
  116. Generation 3650: Loss = 0.75091
  117. Generation 3700: Loss = 0.57823
  118. Generation 3750: Loss = 0.52850
  119. Generation 3800: Loss = 0.53191
  120. Generation 3850: Loss = 0.65577
  121. Generation 3900: Loss = 0.70614
  122. Generation 3950: Loss = 0.57539
  123. Generation 4000: Loss = 0.61946
  124. --- Test Accuracy = 71.09%.
  125. Generation 4050: Loss = 0.50912
  126. Generation 4100: Loss = 0.59709
  127. Generation 4150: Loss = 0.63275
  128. Generation 4200: Loss = 0.73160
  129. Generation 4250: Loss = 0.69023
  130. Generation 4300: Loss = 0.68340
  131. Generation 4350: Loss = 0.54651
  132. Generation 4400: Loss = 0.66809
  133. Generation 4450: Loss = 0.54778
  134. Generation 4500: Loss = 0.49987
  135. --- Test Accuracy = 66.41%.
  136. Generation 4550: Loss = 0.52780
  137. Generation 4600: Loss = 0.47527
  138. Generation 4650: Loss = 0.56457
  139. Generation 4700: Loss = 0.49000
  140. Generation 4750: Loss = 0.62392
  141. Generation 4800: Loss = 0.53709
  142. Generation 4850: Loss = 0.46020
  143. Generation 4900: Loss = 0.58521
  144. Generation 4950: Loss = 0.52085
  145. Generation 5000: Loss = 0.56563
  146. --- Test Accuracy = 76.56%.
  147. Generation 5050: Loss = 0.62101
  148. Generation 5100: Loss = 0.68806
  149. Generation 5150: Loss = 0.56646
  150. Generation 5200: Loss = 0.55054
  151. Generation 5250: Loss = 0.70789
  152. Generation 5300: Loss = 0.48409
  153. Generation 5350: Loss = 0.48703
  154. Generation 5400: Loss = 0.53602
  155. Generation 5450: Loss = 0.53750
  156. Generation 5500: Loss = 0.44592
  157. --- Test Accuracy = 71.88%.
  158. Generation 5550: Loss = 0.61261
  159. Generation 5600: Loss = 0.57290
  160. Generation 5650: Loss = 0.52776
  161. Generation 5700: Loss = 0.49262
  162. Generation 5750: Loss = 0.44058
  163. Generation 5800: Loss = 0.62443
  164. Generation 5850: Loss = 0.38249
  165. Generation 5900: Loss = 0.39162
  166. Generation 5950: Loss = 0.49900
  167. Generation 6000: Loss = 0.60641
  168. --- Test Accuracy = 73.44%.
  169. Generation 6050: Loss = 0.52156
  170. Generation 6100: Loss = 0.50984
  171. Generation 6150: Loss = 0.62414
  172. Generation 6200: Loss = 0.56085
  173. Generation 6250: Loss = 0.45930
  174. Generation 6300: Loss = 0.41330
  175. Generation 6350: Loss = 0.46615
  176. Generation 6400: Loss = 0.48824
  177. Generation 6450: Loss = 0.61569
  178. Generation 6500: Loss = 0.54841
  179. --- Test Accuracy = 72.66%.
  180. Generation 6550: Loss = 0.45108
  181. Generation 6600: Loss = 0.36974
  182. Generation 6650: Loss = 0.42269
  183. Generation 6700: Loss = 0.31257
  184. Generation 6750: Loss = 0.39991
  185. Generation 6800: Loss = 0.34907
  186. Generation 6850: Loss = 0.34459
  187. Generation 6900: Loss = 0.39457
  188. Generation 6950: Loss = 0.29138
  189. Generation 7000: Loss = 0.40070
  190. --- Test Accuracy = 71.09%.
  191. Generation 7050: Loss = 0.31275
  192. Generation 7100: Loss = 0.37386
  193. Generation 7150: Loss = 0.57231
  194. Generation 7200: Loss = 0.33384
  195. Generation 7250: Loss = 0.39317
  196. Generation 7300: Loss = 0.27306
  197. Generation 7350: Loss = 0.42451
  198. Generation 7400: Loss = 0.44812
  199. Generation 7450: Loss = 0.40212
  200. Generation 7500: Loss = 0.33456
  201. --- Test Accuracy = 71.88%.
  202. Generation 7550: Loss = 0.33886
  203. Generation 7600: Loss = 0.35627
  204. Generation 7650: Loss = 0.37291
  205. Generation 7700: Loss = 0.36350
  206. Generation 7750: Loss = 0.61906
  207. Generation 7800: Loss = 0.44072
  208. Generation 7850: Loss = 0.53827
  209. Generation 7900: Loss = 0.40603
  210. Generation 7950: Loss = 0.34712
  211. Generation 8000: Loss = 0.37340
  212. --- Test Accuracy = 74.22%.
  213. Generation 8050: Loss = 0.34767
  214. Generation 8100: Loss = 0.27752
  215. Generation 8150: Loss = 0.39700
  216. Generation 8200: Loss = 0.32144
  217. Generation 8250: Loss = 0.29297
  218. Generation 8300: Loss = 0.25248
  219. Generation 8350: Loss = 0.31232
  220. Generation 8400: Loss = 0.37384
  221. Generation 8450: Loss = 0.19988
  222. Generation 8500: Loss = 0.35983
  223. --- Test Accuracy = 70.31%.
  224. Generation 8550: Loss = 0.24563
  225. Generation 8600: Loss = 0.26254
  226. Generation 8650: Loss = 0.28202
  227. Generation 8700: Loss = 0.38843
  228. Generation 8750: Loss = 0.36233
  229. Generation 8800: Loss = 0.18249
  230. Generation 8850: Loss = 0.28049
  231. Generation 8900: Loss = 0.21987
  232. Generation 8950: Loss = 0.27884
  233. Generation 9000: Loss = 0.26830
  234. --- Test Accuracy = 78.91%.
  235. Generation 9050: Loss = 0.21410
  236. Generation 9100: Loss = 0.18955
  237. Generation 9150: Loss = 0.21886
  238. Generation 9200: Loss = 0.35513
  239. Generation 9250: Loss = 0.23994
  240. Generation 9300: Loss = 0.30040
  241. Generation 9350: Loss = 0.27230
  242. Generation 9400: Loss = 0.24417
  243. Generation 9450: Loss = 0.23737
  244. Generation 9500: Loss = 0.24323
  245. --- Test Accuracy = 71.88%.
  246. Generation 9550: Loss = 0.28200
  247. Generation 9600: Loss = 0.37996
  248. Generation 9650: Loss = 0.14036
  249. Generation 9700: Loss = 0.25095
  250. Generation 9750: Loss = 0.24847
  251. Generation 9800: Loss = 0.31754
  252. Generation 9850: Loss = 0.17151
  253. Generation 9900: Loss = 0.19960
  254. Generation 9950: Loss = 0.24201
  255. Generation 10000: Loss = 0.25191
  256. --- Test Accuracy = 71.88%.
  257. Generation 10050: Loss = 0.21244
  258. Generation 10100: Loss = 0.31911
  259. Generation 10150: Loss = 0.25067
  260. Generation 10200: Loss = 0.17353
  261. Generation 10250: Loss = 0.17035
  262. Generation 10300: Loss = 0.20111
  263. Generation 10350: Loss = 0.24000
  264. Generation 10400: Loss = 0.28682
  265. Generation 10450: Loss = 0.27803
  266. Generation 10500: Loss = 0.22228
  267. --- Test Accuracy = 74.22%.
  268. Generation 10550: Loss = 0.20168
  269. Generation 10600: Loss = 0.18150
  270. Generation 10650: Loss = 0.12649
  271. Generation 10700: Loss = 0.21024
  272. Generation 10750: Loss = 0.13210
  273. Generation 10800: Loss = 0.21463
  274. Generation 10850: Loss = 0.19228
  275. Generation 10900: Loss = 0.20855
  276. Generation 10950: Loss = 0.09159
  277. Generation 11000: Loss = 0.19000
  278. --- Test Accuracy = 71.09%.
  279. Generation 11050: Loss = 0.16792
  280. Generation 11100: Loss = 0.18264
  281. Generation 11150: Loss = 0.20756
  282. Generation 11200: Loss = 0.23574
  283. Generation 11250: Loss = 0.25095
  284. Generation 11300: Loss = 0.19270
  285. Generation 11350: Loss = 0.19303
  286. Generation 11400: Loss = 0.16534
  287. Generation 11450: Loss = 0.29888
  288. Generation 11500: Loss = 0.17793
  289. --- Test Accuracy = 78.91%.
  290. Generation 11550: Loss = 0.15598
  291. Generation 11600: Loss = 0.12160
  292. Generation 11650: Loss = 0.26322
  293. Generation 11700: Loss = 0.10899
  294. Generation 11750: Loss = 0.11561
  295. Generation 11800: Loss = 0.16404
  296. Generation 11850: Loss = 0.18666
  297. Generation 11900: Loss = 0.15152
  298. Generation 11950: Loss = 0.22033
  299. Generation 12000: Loss = 0.17022
  300. --- Test Accuracy = 77.34%.
  301. Generation 12050: Loss = 0.06982
  302. Generation 12100: Loss = 0.11614
  303. Generation 12150: Loss = 0.22383
  304. Generation 12200: Loss = 0.14770
  305. Generation 12250: Loss = 0.12691
  306. Generation 12300: Loss = 0.13115
  307. Generation 12350: Loss = 0.15366
  308. Generation 12400: Loss = 0.10993
  309. Generation 12450: Loss = 0.12453
  310. Generation 12500: Loss = 0.11822
  311. --- Test Accuracy = 68.75%.
  312. Generation 12550: Loss = 0.08440
  313. Generation 12600: Loss = 0.10500
  314. Generation 12650: Loss = 0.09079
  315. Generation 12700: Loss = 0.17050
  316. Generation 12750: Loss = 0.16910
  317. Generation 12800: Loss = 0.16500
  318. Generation 12850: Loss = 0.10901
  319. Generation 12900: Loss = 0.06830
  320. Generation 12950: Loss = 0.06736
  321. Generation 13000: Loss = 0.15788
  322. --- Test Accuracy = 75.00%.
  323. Generation 13050: Loss = 0.13596
  324. Generation 13100: Loss = 0.11368
  325. Generation 13150: Loss = 0.15130
  326. Generation 13200: Loss = 0.16115
  327. Generation 13250: Loss = 0.08005
  328. Generation 13300: Loss = 0.37412
  329. Generation 13350: Loss = 0.08087
  330. Generation 13400: Loss = 0.05354
  331. Generation 13450: Loss = 0.14977
  332. Generation 13500: Loss = 0.06454
  333. --- Test Accuracy = 76.56%.
  334. Generation 13550: Loss = 0.10611
  335. Generation 13600: Loss = 0.14358
  336. Generation 13650: Loss = 0.30438
  337. Generation 13700: Loss = 0.12326
  338. Generation 13750: Loss = 0.12546
  339. Generation 13800: Loss = 0.05507
  340. Generation 13850: Loss = 0.10522
  341. Generation 13900: Loss = 0.14672
  342. Generation 13950: Loss = 0.08316
  343. Generation 14000: Loss = 0.04716
  344. --- Test Accuracy = 81.25%.
  345. Generation 14050: Loss = 0.05569
  346. Generation 14100: Loss = 0.07380
  347. Generation 14150: Loss = 0.08503
  348. Generation 14200: Loss = 0.09180
  349. Generation 14250: Loss = 0.12643
  350. Generation 14300: Loss = 0.28318
  351. Generation 14350: Loss = 0.10349
  352. Generation 14400: Loss = 0.08242
  353. Generation 14450: Loss = 0.14991
  354. Generation 14500: Loss = 0.09186
  355. --- Test Accuracy = 68.75%.
  356. Generation 14550: Loss = 0.05001
  357. Generation 14600: Loss = 0.09397
  358. Generation 14650: Loss = 0.15350
  359. Generation 14700: Loss = 0.05397
  360. Generation 14750: Loss = 0.07973
  361. Generation 14800: Loss = 0.04911
  362. Generation 14850: Loss = 0.14983
  363. Generation 14900: Loss = 0.04087
  364. Generation 14950: Loss = 0.09777
  365. Generation 15000: Loss = 0.06766
  366. --- Test Accuracy = 79.69%.
  367. Generation 15050: Loss = 0.08833
  368. Generation 15100: Loss = 0.05564
  369. Generation 15150: Loss = 0.14910
  370. Generation 15200: Loss = 0.09593
  371. Generation 15250: Loss = 0.02290
  372. Generation 15300: Loss = 0.09136
  373. Generation 15350: Loss = 0.05094
  374. Generation 15400: Loss = 0.06671
  375. Generation 15450: Loss = 0.06932
  376. Generation 15500: Loss = 0.05988
  377. --- Test Accuracy = 71.09%.
  378. Generation 15550: Loss = 0.03620
  379. Generation 15600: Loss = 0.11110
  380. Generation 15650: Loss = 0.09207
  381. Generation 15700: Loss = 0.12269
  382. Generation 15750: Loss = 0.07687
  383. Generation 15800: Loss = 0.13080
  384. Generation 15850: Loss = 0.06997
  385. Generation 15900: Loss = 0.08472
  386. Generation 15950: Loss = 0.09175
  387. Generation 16000: Loss = 0.10958
  388. --- Test Accuracy = 74.22%.
  389. Generation 16050: Loss = 0.06961
  390. Generation 16100: Loss = 0.04545
  391. Generation 16150: Loss = 0.08067
  392. Generation 16200: Loss = 0.09693
  393. Generation 16250: Loss = 0.10157
  394. Generation 16300: Loss = 0.12212
  395. Generation 16350: Loss = 0.09758
  396. Generation 16400: Loss = 0.11395
  397. Generation 16450: Loss = 0.06691
  398. Generation 16500: Loss = 0.07225
  399. --- Test Accuracy = 78.12%.
  400. Generation 16550: Loss = 0.06004
  401. Generation 16600: Loss = 0.06961
  402. Generation 16650: Loss = 0.18485
  403. Generation 16700: Loss = 0.08379
  404. Generation 16750: Loss = 0.06583
  405. Generation 16800: Loss = 0.07328
  406. Generation 16850: Loss = 0.06474
  407. Generation 16900: Loss = 0.09860
  408. Generation 16950: Loss = 0.05844
  409. Generation 17000: Loss = 0.05786
  410. --- Test Accuracy = 72.66%.
  411. Generation 17050: Loss = 0.07321
  412. Generation 17100: Loss = 0.12127
  413. Generation 17150: Loss = 0.05446
  414. Generation 17200: Loss = 0.03569
  415. Generation 17250: Loss = 0.05022
  416. Generation 17300: Loss = 0.11921
  417. Generation 17350: Loss = 0.04017
  418. Generation 17400: Loss = 0.04406
  419. Generation 17450: Loss = 0.06000
  420. Generation 17500: Loss = 0.11646
  421. --- Test Accuracy = 71.88%.
  422. Generation 17550: Loss = 0.14290
  423. Generation 17600: Loss = 0.04812
  424. Generation 17650: Loss = 0.06327
  425. Generation 17700: Loss = 0.13356
  426. Generation 17750: Loss = 0.09151
  427. Generation 17800: Loss = 0.13200
  428. Generation 17850: Loss = 0.16656
  429. Generation 17900: Loss = 0.05319
  430. Generation 17950: Loss = 0.07239
  431. Generation 18000: Loss = 0.07355
  432. --- Test Accuracy = 75.78%.
  433. Generation 18050: Loss = 0.09703
  434. Generation 18100: Loss = 0.02250
  435. Generation 18150: Loss = 0.02004
  436. Generation 18200: Loss = 0.12326
  437. Generation 18250: Loss = 0.15379
  438. Generation 18300: Loss = 0.03012
  439. Generation 18350: Loss = 0.05909
  440. Generation 18400: Loss = 0.05413
  441. Generation 18450: Loss = 0.06055
  442. Generation 18500: Loss = 0.04617
  443. --- Test Accuracy = 75.00%.
  444. Generation 18550: Loss = 0.11713
  445. Generation 18600: Loss = 0.10103
  446. Generation 18650: Loss = 0.08530
  447. Generation 18700: Loss = 0.07523
  448. Generation 18750: Loss = 0.06605
  449. Generation 18800: Loss = 0.04995
  450. Generation 18850: Loss = 0.04141
  451. Generation 18900: Loss = 0.04708
  452. Generation 18950: Loss = 0.04045
  453. Generation 19000: Loss = 0.03030
  454. --- Test Accuracy = 78.91%.
  455. Generation 19050: Loss = 0.02257
  456. Generation 19100: Loss = 0.01894
  457. Generation 19150: Loss = 0.06192
  458. Generation 19200: Loss = 0.15686
  459. Generation 19250: Loss = 0.03990
  460. Generation 19300: Loss = 0.07178
  461. Generation 19350: Loss = 0.07857
  462. Generation 19400: Loss = 0.06567
  463. Generation 19450: Loss = 0.04735
  464. Generation 19500: Loss = 0.12532
  465. --- Test Accuracy = 67.19%.
  466. Generation 19550: Loss = 0.02739
  467. Generation 19600: Loss = 0.04494
  468. Generation 19650: Loss = 0.16667
  469. Generation 19700: Loss = 0.08560
  470. Generation 19750: Loss = 0.11396
  471. Generation 19800: Loss = 0.08600
  472. Generation 19850: Loss = 0.04694
  473. Generation 19900: Loss = 0.06692
  474. Generation 19950: Loss = 0.03973
  475. Generation 20000: Loss = 0.01563
  476. --- Test Accuracy = 70.31%.
  477. (tensorflow-py3.5)
  478. # student @ user-ubuntu in ~/work/jeff/cnn_cifar [4:24:35]

完整日志

[Tensorflow] Cookbook - Object Classification based on CIFAR-10的更多相关文章

  1. 【翻译】TensorFlow卷积神经网络识别CIFAR 10Convolutional Neural Network (CNN)| CIFAR 10 TensorFlow

    原网址:https://data-flair.training/blogs/cnn-tensorflow-cifar-10/ by DataFlair Team · Published May 21, ...

  2. 机器学习-TensorFlow应用之classification和ROC curve

    概述 前面几节讲的是linear regression的内容,这里咱们再讲一个非常常用的一种模型那就是classification,classification顾名思义就是分类的意思,在实际的情况是非 ...

  3. A Novel Multi-label Classification Based on PCA and ML-KNN

     ICIC Express Letters                  ICIC International ⓒ2010 ISSN 1881-803X Volume4, Number5, O ...

  4. 论文笔记:Visual Object Tracking based on Adaptive Siamese and Motion Estimation Network

    Visual Object Tracking based on Adaptive Siamese and Motion Estimation 本文提出一种利用上一帧目标位置坐标,在本帧中找出目标可能出 ...

  5. [Tensorflow] Cookbook - The Tensorflow Way

    本章介绍tf基础知识,主要包括cookbook的第一.二章节. 方针:先会用,后定制 Ref: TensorFlow 如何入门? Ref: 如何高效的学习 TensorFlow 代码? 顺便推荐该领域 ...

  6. tensorflow,object,detection,在model zoom,新下载的模型,WARNING:root:Variable [resnet_v1_50/block1/unit_3/bottleneck_v1/conv3/BatchNorm/gamma] is not available in checkpoint

    现象: WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/BatchNorm/beta] is not ava ...

  7. [Tensorflow] Cookbook - Retraining Existing CNNs models - Inception Model

    From: https://github.com/jcjohnson/cnn-benchmarks#alexnet 先大概了解模型,再看如果加载pre-training weight. 关于retai ...

  8. [Tensorflow] Cookbook - Neural Network

    In this chapter, we'll cover the following recipes: Implementing Operational Gates Working with Gate ...

  9. [Tensorflow] Cookbook - CNN

    Convolutional Neural Networks (CNNs) are responsible for the major breakthroughs in image recognitio ...

随机推荐

  1. CocosCreator的Sprite的更换

    先上图,左侧是运行的效果, cc.Class({ extends: cc.Component, /* * cocos creator动态更换纹理 *方法一,预先在编辑器里设置好所有的纹理,绑定到对应的 ...

  2. 基于Python的SQLAlchemy的操作

    安装 在Python使用SQLAlchemy的首要前提是安装相应的模块,当然作为python的优势,可以到python安装目录下的scripts下,同时按住shift+加上鼠标左键,从而在菜单中打开命 ...

  3. 碰到在Windows中访问局域网文件夹, 提示无法访问时的解决办法

    运行:gpedit.msc 找到下图的位置, 启用即可

  4. python 数字

    python数字: 本文主要讲解常用的数字类型转换, 数字函数和随机数字函数. # coding:utf-8 # python数字类型转换 def data_conversion(): "& ...

  5. AIX上解压缩.tar.Z, .tar.gz, .zip及.tgz

    在AIX上最常见的压缩文件就是.tar檔了,而除了tar文件以外,有时会遇到数据是用其它的压缩文件格式,所以偶顺手整理了一些常见的压缩文件格式,在AIX要怎么解压缩 : ·    .tar.Z fil ...

  6. [Beego模型] 六、事务处理

    [Beego模型] 一.ORM 使用方法 [Beego模型] 二.CRUD 操作 [Beego模型] 三.高级查询 [Beego模型] 四.使用SQL语句进行查询 [Beego模型] 五.构造查询 [ ...

  7. POJ 1595 素数打表水题

    [题意简述]:给出N和C,让我们求出N以内的包含N的素数,然后依据若N以内的素数为奇数个,就将中间2*c-1个素数输出:若为偶数个.就将中间2*c个素数输出. [分析]:仅仅要题意理解就简单了. 详见 ...

  8. ds18b20采集温度并上报服务器

    交叉编译器:arm-linux-gcc-4.5.4 Linux内核版本:Linux-3.0 主机操作系统:Centos 6.5 开发板:FL2440 温度传感器:ds18b20 注:此程序的客户端是在 ...

  9. C# ConcurrentQueue实现

    我们从C# Queue 和Stack的实现知道Queue是用数组来实现的,数组的元素不断的通过Array.Copy从一个数组移动到另一个数组,ConcurrentQueue我们需要关心2点:1线程安全 ...

  10. 为什么和什么是 DevOps?

    原文地址 本文内容 为什么 DevOps 什么是 DevOps DevOps 所带来的好处 如何将 DevOps 落到实处? 关于 DevOps 的澄清 参考资料 编写软件之所以难,是因为没有哪两个软 ...