Skip to content

Commit

Permalink
-initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Seb-Good committed Apr 30, 2019
1 parent 59ec803 commit 936c279
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 22 deletions.
3 changes: 3 additions & 0 deletions mnistazure/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@

# Projects path
DATA_PATH = os.path.join(WORKING_DIR, 'data')

# Tensorboard path
TENSORBOARD_PATH = os.path.join(WORKING_DIR, 'tensorboard')
8 changes: 3 additions & 5 deletions mnistazure/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"""

# 3rd party imports
import os
import json
import tensorflow as tf

# Local imports
Expand All @@ -14,13 +12,13 @@

class Network(object):

def __init__(self, height, width, channels, labels, seed=0):
def __init__(self, height, width, channels, num_labels, seed=0):

# Set input parameters
self.height = height
self.width = width
self.channels = channels
self.labels = labels
self.num_labels = num_labels
self.seed = seed

def inference(self, input_layer, is_training):
Expand Down Expand Up @@ -65,7 +63,7 @@ def inference(self, input_layer, is_training):
'probabilities': tf.nn.softmax(logits, name="softmax_tensor")}

# Check output dimensions
assert net.shape[1] == self.labels
assert logits.shape[1] == self.num_labels

return logits, predictions

Expand Down
8 changes: 4 additions & 4 deletions notebooks/generate_dataset.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -32,7 +32,7 @@
"import matplotlib.pylab as plt\n",
"\n",
"# Import local Libraries\n",
"sys.path.insert(0, '/home/sebastiangoodfellow/Documents/Code/mnist-azure')\n",
"sys.path.insert(0, r'C:\\Users\\sebig\\Documents\\Code\\mnist-azure')\n",
"from mnistazure.config import DATA_PATH"
]
},
Expand All @@ -45,7 +45,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -62,7 +62,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down
108 changes: 96 additions & 12 deletions notebooks/train_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"import matplotlib.pylab as plt\n",
"\n",
"# Import local Libraries\n",
"sys.path.insert(0, '/home/sebastiangoodfellow/Documents/Code/mnist-azure')\n",
"from mnistazure.config import DATA_PATH\n",
"sys.path.insert(0, r'C:\\Users\\sebig\\Documents\\Code\\mnist-azure')\n",
"from mnistazure.config import DATA_PATH, TENSORBOARD_PATH\n",
"from mnistazure.generator import DataGenerator\n",
"from mnistazure.graph import Graph\n",
"from mnistazure.network import Network"
Expand Down Expand Up @@ -65,10 +65,7 @@
"seed = 0\n",
"\n",
"# Number of unique labels\n",
"num_labels = 10\n",
"\n",
"# Save path\n",
"save_path = '/home/sebastiangoodfellow/Desktop/tensoreboard/mnistazure'"
"num_labels = 10"
]
},
{
Expand All @@ -80,7 +77,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -89,7 +86,7 @@
"<PrefetchDataset shapes: ((?, 28, 28, 1), (?,)), types: (tf.float32, tf.int32)>"
]
},
"execution_count": 3,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -114,18 +111,105 @@
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "InvalidArgumentError",
"evalue": "Input to reshape is a tensor with 2352 values, but the requested shape has 784\n\t [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32](convert_image, Reshape/shape)]]\n\t [[Node: next_batch/IteratorGetNext = IteratorGetNext[output_shapes=[[?,28,28,1], [?]], output_types=[DT_FLOAT, DT_INT32], _device=\"/job:localhost/replica:0/task:0/device:CPU:0\"](iterator/IteratorFromStringHandle)]]\n\t [[Node: gradients/ConvNet/flatten/Reshape_grad/Shape-1-1-VecPermuteNCHWToNHWC-LayoutOptimizer/_23 = _HostRecv[client_terminated=false, recv_device=\"/job:localhost/replica:0/task:0/device:GPU:0\", send_device=\"/job:localhost/replica:0/task:0/device:CPU:0\", send_device_incarnation=1, tensor_name=\"edge_180_g...tOptimizer\", tensor_type=DT_INT32, _device=\"/job:localhost/replica:0/task:0/device:GPU:0\"]()]]",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mInvalidArgumentError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m~\\Anaconda3\\envs\\mnist-azure\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_do_call\u001b[1;34m(self, fn, *args)\u001b[0m\n\u001b[0;32m 1321\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1322\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1323\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mOpError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\Anaconda3\\envs\\mnist-azure\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_run_fn\u001b[1;34m(feed_dict, fetch_list, target_list, options, run_metadata)\u001b[0m\n\u001b[0;32m 1306\u001b[0m return self._call_tf_sessionrun(\n\u001b[1;32m-> 1307\u001b[1;33m options, feed_dict, fetch_list, target_list, run_metadata)\n\u001b[0m\u001b[0;32m 1308\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\Anaconda3\\envs\\mnist-azure\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_call_tf_sessionrun\u001b[1;34m(self, options, feed_dict, fetch_list, target_list, run_metadata)\u001b[0m\n\u001b[0;32m 1408\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_session\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0moptions\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtarget_list\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1409\u001b[1;33m run_metadata)\n\u001b[0m\u001b[0;32m 1410\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mInvalidArgumentError\u001b[0m: Input to reshape is a tensor with 2352 values, but the requested shape has 784\n\t [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32](convert_image, Reshape/shape)]]\n\t [[Node: next_batch/IteratorGetNext = IteratorGetNext[output_shapes=[[?,28,28,1], [?]], output_types=[DT_FLOAT, DT_INT32], _device=\"/job:localhost/replica:0/task:0/device:CPU:0\"](iterator/IteratorFromStringHandle)]]\n\t [[Node: gradients/ConvNet/flatten/Reshape_grad/Shape-1-1-VecPermuteNCHWToNHWC-LayoutOptimizer/_23 = _HostRecv[client_terminated=false, recv_device=\"/job:localhost/replica:0/task:0/device:GPU:0\", send_device=\"/job:localhost/replica:0/task:0/device:CPU:0\", send_device_incarnation=1, tensor_name=\"edge_180_g...tOptimizer\", tensor_type=DT_INT32, _device=\"/job:localhost/replica:0/task:0/device:GPU:0\"]()]]",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[1;31mInvalidArgumentError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-4-da1d95ac3061>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 48\u001b[0m feed_dict={graph.batch_size: batch_size, graph.is_training: True,\n\u001b[0;32m 49\u001b[0m \u001b[0mgraph\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlearning_rate\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mlearning_rate\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 50\u001b[1;33m graph.mode_handle: handle_train})\n\u001b[0m\u001b[0;32m 51\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 52\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\Anaconda3\\envs\\mnist-azure\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36mrun\u001b[1;34m(self, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m 898\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 899\u001b[0m result = self._run(None, fetches, feed_dict, options_ptr,\n\u001b[1;32m--> 900\u001b[1;33m run_metadata_ptr)\n\u001b[0m\u001b[0;32m 901\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mrun_metadata\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 902\u001b[0m \u001b[0mproto_data\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtf_session\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTF_GetBuffer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrun_metadata_ptr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\Anaconda3\\envs\\mnist-azure\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_run\u001b[1;34m(self, handle, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m 1133\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mfinal_fetches\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mfinal_targets\u001b[0m \u001b[1;32mor\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mhandle\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mfeed_dict_tensor\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1134\u001b[0m results = self._do_run(handle, final_targets, final_fetches,\n\u001b[1;32m-> 1135\u001b[1;33m feed_dict_tensor, options, run_metadata)\n\u001b[0m\u001b[0;32m 1136\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1137\u001b[0m \u001b[0mresults\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\Anaconda3\\envs\\mnist-azure\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_do_run\u001b[1;34m(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m 1314\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mhandle\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1315\u001b[0m return self._do_call(_run_fn, feeds, fetches, targets, options,\n\u001b[1;32m-> 1316\u001b[1;33m run_metadata)\n\u001b[0m\u001b[0;32m 1317\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1318\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_do_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_prun_fn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeeds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetches\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\Anaconda3\\envs\\mnist-azure\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_do_call\u001b[1;34m(self, fn, *args)\u001b[0m\n\u001b[0;32m 1333\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1334\u001b[0m \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1335\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnode_def\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mop\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmessage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1336\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1337\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_extend_graph\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mInvalidArgumentError\u001b[0m: Input to reshape is a tensor with 2352 values, but the requested shape has 784\n\t [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32](convert_image, Reshape/shape)]]\n\t [[Node: next_batch/IteratorGetNext = IteratorGetNext[output_shapes=[[?,28,28,1], [?]], output_types=[DT_FLOAT, DT_INT32], _device=\"/job:localhost/replica:0/task:0/device:CPU:0\"](iterator/IteratorFromStringHandle)]]\n\t [[Node: gradients/ConvNet/flatten/Reshape_grad/Shape-1-1-VecPermuteNCHWToNHWC-LayoutOptimizer/_23 = _HostRecv[client_terminated=false, recv_device=\"/job:localhost/replica:0/task:0/device:GPU:0\", send_device=\"/job:localhost/replica:0/task:0/device:CPU:0\", send_device_incarnation=1, tensor_name=\"edge_180_g...tOptimizer\", tensor_type=DT_INT32, _device=\"/job:localhost/replica:0/task:0/device:GPU:0\"]()]]"
]
}
],
"source": [
"# Initialize network\n",
"network = Network(height=image_shape[0], width=image_shape[1], channels=image_shape[2], labels=num_labels, seed=0)\n",
"network = Network(height=image_shape[0], width=image_shape[1], \n",
" channels=image_shape[2], num_labels=num_labels, seed=0)\n",
"\n",
"# Initialize graph\n",
"graph = Graph(network=network, save_path=save_path, data_path=DATA_PATH, max_to_keep=max_to_keep)\n",
"graph = Graph(network=network, save_path=TENSORBOARD_PATH, \n",
" data_path=DATA_PATH, max_to_keep=max_to_keep)\n",
"\n",
"# Learning rate\n",
"learning_rate = 1e-3\n",
"\n",
"# Number of epochs\n",
"epochs = 100\n",
"\n",
"# Batch size\n",
"batch_size = 32\n",
"\n",
"with tf.Session() as sess:\n",
" \n",
" # Initialize variables\n",
" sess.run(graph.init_global)"
" sess.run(graph.init_global)\n",
" \n",
" # Get number of training batches\n",
" num_train_batches = graph.generator_train.num_batches.eval(\n",
" feed_dict={graph.batch_size: batch_size})\n",
" \n",
" # Get number of batch steps per epoch\n",
" steps_per_epoch = int(np.ceil(num_train_batches / 1))\n",
" \n",
" # Get mode handle for training\n",
" handle_train = sess.run(graph.generator_train.iterator.string_handle())\n",
" \n",
" # Initialize the train dataset iterator at the beginning of each epoch\n",
" sess.run(fetches=[graph.generator_train.iterator.initializer],\n",
" feed_dict={graph.batch_size: batch_size})\n",
" \n",
" # Loop through epochs\n",
" for epoch in range(epochs):\n",
"\n",
" # Initialize metrics\n",
" sess.run(fetches=[graph.init_metrics_op])\n",
"\n",
" # Loop through train dataset batches\n",
" for batch in range(steps_per_epoch):\n",
" \n",
" _, _, _, _ = sess.run(fetches=[graph.train_op, graph.update_metrics_op,\n",
" graph.train_summary_metrics_op, graph.global_step],\n",
" feed_dict={graph.batch_size: batch_size, graph.is_training: True,\n",
" graph.learning_rate: learning_rate,\n",
" graph.mode_handle: handle_train})\n",
"\n",
" \n",
" # Initialize the train dataset iterator at the end of each epoch\n",
" sess.run(fetches=[graph.generator_train.iterator.initializer],\n",
" feed_dict={graph.batch_size: batch_size})"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"25088"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"28*28*32"
]
},
{
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tensorflow==1.8.0
numpy==1.16.3
opencv3==3.1.0
matplotlib==3.0.3
opencv==3.4.2
2 changes: 2 additions & 0 deletions tensorboard/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore

0 comments on commit 936c279

Please sign in to comment.