You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

subclassing.py 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from __future__ import absolute_import, division, print_function
  2. import tensorflow as tf
  3. from tensorflow.keras import Model, layers
  4. import numpy as np
  5. # MNIST dataset parameters.
  6. num_classes = 10 # total classes (0-9 digits).
  7. # Training parameters.
  8. learning_rate = 0.001
  9. training_steps = 100
  10. batch_size = 128
  11. display_step = 10
  12. # Network parameters.
  13. conv1_filters = 32 # number of filters for 1st conv layer.
  14. conv2_filters = 64 # number of filters for 2nd conv layer.
  15. fc1_units = 1024 # number of neurons for 1st fully-connected layer.
  16. # Prepare MNIST data.
  17. from tensorflow.keras.datasets import mnist
  18. (x_train, y_train), (x_test, y_test) = mnist.load_data()
  19. # Convert to float32.
  20. x_train, x_test = np.array(x_train, np.float32), np.array(x_test, np.float32)
  21. # Normalize images value from [0, 255] to [0, 1].
  22. x_train, x_test = x_train / 255., x_test / 255.
  23. # Use tf.data API to shuffle and batch data.
  24. train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))
  25. train_data = train_data.repeat().shuffle(5000).batch(batch_size).prefetch(1)
  26. # Create TF Model.
  27. class ConvNet(Model):
  28. # Set layers.
  29. def __init__(self):
  30. super(ConvNet, self).__init__()
  31. # Convolution Layer with 32 filters and a kernel size of 5.
  32. self.conv1 = layers.Conv2D(32, kernel_size=5, activation=tf.nn.relu)
  33. # Max Pooling (down-sampling) with kernel size of 2 and strides of 2.
  34. self.maxpool1 = layers.MaxPool2D(2, strides=2)
  35. # Convolution Layer with 64 filters and a kernel size of 3.
  36. self.conv2 = layers.Conv2D(64, kernel_size=3, activation=tf.nn.relu)
  37. # Max Pooling (down-sampling) with kernel size of 2 and strides of 2.
  38. self.maxpool2 = layers.MaxPool2D(2, strides=2)
  39. # Flatten the data to a 1-D vector for the fully connected layer.
  40. self.flatten = layers.Flatten()
  41. # Fully connected layer.
  42. self.fc1 = layers.Dense(1024)
  43. # Apply Dropout (if is_training is False, dropout is not applied).
  44. self.dropout = layers.Dropout(rate=0.5)
  45. # Output layer, class prediction.
  46. self.out = layers.Dense(num_classes)
  47. # Set forward pass.
  48. def call(self, x, is_training=False):
  49. x = tf.reshape(x, [-1, 28, 28, 1])
  50. x = self.conv1(x)
  51. x = self.maxpool1(x)
  52. x = self.conv2(x)
  53. x = self.maxpool2(x)
  54. x = self.flatten(x)
  55. x = self.fc1(x)
  56. x = self.dropout(x)
  57. x = self.out(x)
  58. if not is_training:
  59. # tf cross entropy expect logits without softmax, so only
  60. # apply softmax when not training.
  61. x = tf.nn.softmax(x)
  62. return x
  63. '''
  64. # Build neural network model.
  65. conv_net = ConvNet()
  66. # Cross-Entropy Loss.
  67. # Note that this will apply 'softmax' to the logits.
  68. def cross_entropy_loss(x, y):
  69. # Convert labels to int 64 for tf cross-entropy function.
  70. y = tf.cast(y, tf.int64)
  71. # Apply softmax to logits and compute cross-entropy.
  72. loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=x)
  73. # Average loss across the batch.
  74. return tf.reduce_mean(loss)
  75. # Accuracy metric.
  76. def accuracy(y_pred, y_true):
  77. # Predicted class is the index of highest score in prediction vector (i.e. argmax).
  78. correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.cast(y_true, tf.int64))
  79. return tf.reduce_mean(tf.cast(correct_prediction, tf.float32), axis=-1)
  80. # Stochastic gradient descent optimizer.
  81. optimizer = tf.optimizers.Adam(learning_rate)
  82. # Optimization process.
  83. def run_optimization(x, y):
  84. # Wrap computation inside a GradientTape for automatic differentiation.
  85. with tf.GradientTape() as g:
  86. # Forward pass.
  87. pred = conv_net(x, is_training=True)
  88. # Compute loss.
  89. loss = cross_entropy_loss(pred, y)
  90. # Variables to update, i.e. trainable variables.
  91. trainable_variables = conv_net.trainable_variables
  92. # Compute gradients.
  93. gradients = g.gradient(loss, trainable_variables)
  94. # Update W and b following gradients.
  95. optimizer.apply_gradients(zip(gradients, trainable_variables))
  96. # Run training for the given number of steps.
  97. for step, (batch_x, batch_y) in enumerate(train_data.take(training_steps), 1):
  98. # Run the optimization to update W and b values.
  99. run_optimization(batch_x, batch_y)
  100. if step % display_step == 0:
  101. pred = conv_net(batch_x)
  102. loss = cross_entropy_loss(pred, batch_y)
  103. acc = accuracy(pred, batch_y)
  104. print("step: %i, loss: %f, accuracy: %f" % (step, loss, acc))
  105. # Test model on validation set.
  106. pred = conv_net(x_test)
  107. print("Test Accuracy: %f" % accuracy(pred, y_test))
  108. conv_net.save_weights('weights.h5')
  109. '''
  110. conv_net = ConvNet()
  111. conv_net.build(x_test.shape)
  112. conv_net.load_weights('weights.h5')
  113. # Test model on validation set.
  114. pred = conv_net(x_test)
  115. # print("Test Accuracy: %f" % accuracy(pred, y_test))
  116. # Visualize predictions.
  117. import matplotlib.pyplot as plt
  118. # Predict 5 images from validation set.
  119. n_images = 5
  120. test_images = x_test[:n_images]
  121. predictions = conv_net(test_images)
  122. # Display image and model prediction.
  123. for i in range(n_images):
  124. plt.imshow(np.reshape(test_images[i], [28, 28]), cmap='gray')
  125. plt.show()
  126. print("Model prediction: %i" % np.argmax(predictions.numpy()[i]))