This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
# Imports | |
import numpy as np | |
import tensorflow as tf | |
import matplotlib.pyplot as plt | |
tf.logging.set_verbosity(tf.logging.INFO) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Loading the data (MNIST) | |
mnist = tf.contrib.learn.datasets.load_dataset("mnist") | |
train_data = mnist.train.images # Returns np.array | |
train_labels = np.asarray(mnist.train.labels, dtype=np.int32) | |
eval_data = mnist.test.images # Returns np.array | |
eval_labels = np.asarray(mnist.test.labels, dtype=np.int32) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
index = 7 | |
plt.imshow(train_data[index].reshape(28, 28)) | |
print ("y = " + str(np.squeeze(train_labels[index]))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print ("number of training examples = " + str(train_data.shape[0])) | |
print ("number of evaluation examples = " + str(eval_data.shape[0])) | |
print ("X_train shape: " + str(train_data.shape)) | |
print ("Y_train shape: " + str(train_labels.shape)) | |
print ("X_test shape: " + str(eval_data.shape)) | |
print ("Y_test shape: " + str(eval_labels.shape)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cnn_model_fn(features, labels, mode): | |
# Input Layer | |
input_height, input_width = 28, 28 | |
input_channels = 1 | |
input_layer = tf.reshape(features["x"], [-1, input_height, input_width, input_channels]) | |
# Convolutional Layer #1 and Pooling Layer #1 | |
conv1_1 = tf.layers.conv2d(inputs=input_layer, filters=64, kernel_size=[3, 3], padding="same", activation=tf.nn.relu) | |
conv1_2 = tf.layers.conv2d(inputs=conv1_1, filters=64, kernel_size=[3, 3], padding="same", activation=tf.nn.relu) | |
pool1 = tf.layers.max_pooling2d(inputs=conv1_2, pool_size=[2, 2], strides=2, padding="same") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn, | |
model_dir="/tmp/mnist_vgg13_model") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": train_data}, | |
y=train_labels, | |
batch_size=100, | |
num_epochs=100, | |
shuffle=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mnist_classifier.train(input_fn=train_input_fn, | |
steps=None, | |
hooks=None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
eval_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": eval_data}, | |
y=eval_labels, | |
num_epochs=1, | |
shuffle=False) | |
eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn) | |
print(eval_results) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numbers | |
import random | |
from torchvision.transforms import functional as F | |
try: | |
import accimage | |
except ImportError: | |
accimage = None | |
from PIL import Image | |
OlderNewer