Skip to content

Commit 8c692a1

Browse files
committed
tensorflow net, examples and documentation
1 parent c589dcf commit 8c692a1

File tree

9 files changed

+851
-0
lines changed

9 files changed

+851
-0
lines changed

data/mnist/get_mnist.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env sh
2+
# This scripts downloads the mnist data and unzips it.
3+
4+
DIR="$( cd "$(dirname "$0")" ; pwd -P )"
5+
cd $DIR
6+
7+
echo "Downloading..."
8+
9+
for fname in train-images-idx3-ubyte train-labels-idx1-ubyte t10k-images-idx3-ubyte t10k-labels-idx1-ubyte
10+
do
11+
if [ ! -e $fname ]; then
12+
wget --no-check-certificate http://yann.lecun.com/exdb/mnist/${fname}.gz
13+
gunzip ${fname}.gz
14+
fi
15+
done

doc/creating-jars.md

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
Creating JAR files for JavaCPP
2+
==============================
3+
4+
This document describes how we create the JAR files for JavaCPP. The libraries
5+
are build in Ubuntu 14.04. We build binaries for Caffe and also TensorFlow,
6+
which requires Bazel.
7+
8+
Start an EC2 AMI with Ubuntu 14.04 and run these commands:
9+
10+
```
11+
wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1404/x86_64/cuda-repo-ubuntu1404_7.0-28_amd64.deb
12+
sudo dpkg -i cuda-repo-ubuntu1404_7.0-28_amd64.deb
13+
sudo apt-get update
14+
sudo apt-get upgrade -y
15+
sudo apt-get install cuda-7-0 -y
16+
```
17+
18+
Install CuDNN: Download `cudnn-7.0-linux-x64-v4.0-rc.tgzcudnn-7.0-linux-x64-v4.0-rc.tgz` from the CuDNN website and run:
19+
20+
```
21+
tar -zxf cudnn-7.0-linux-x64-v4.0-rc.tgz
22+
cd cuda
23+
sudo cp lib64/* /usr/local/cuda/lib64/
24+
sudo cp include/cudnn.h /usr/local/cuda/include/
25+
```
26+
27+
Install some development tools needed in subsequent steps:
28+
29+
```
30+
sudo apt-get install python-pip python-dev build-essential git zip zlib1g-dev cmake gfortran maven
31+
pip install numpy
32+
```
33+
34+
Install and activate the JDK 8:
35+
36+
```
37+
sudo add-apt-repository ppa:openjdk-r/ppa
38+
# When prompted you'll need to press ENTER to continue
39+
sudo apt-get update
40+
sudo apt-get install -y openjdk-8-jdk
41+
42+
sudo update-alternatives --config java
43+
sudo update-alternatives --config javac
44+
```
45+
46+
Install Bazel (needs JDK 8):
47+
```
48+
cd ~
49+
git clone https://github.com/bazelbuild/bazel.git
50+
cd bazel
51+
git checkout tags/0.1.4
52+
./compile.sh
53+
sudo cp output/bazel /usr/bin
54+
```
55+
56+
Install JavaCPP:
57+
```
58+
cd ~
59+
git clone https://github.com/bytedeco/javacpp.git
60+
cd javacpp
61+
mvn install
62+
```
63+
64+
For the following step to work, we had to do
65+
```
66+
locate DisableStupidWarnings.h
67+
```
68+
which gives the following output:
69+
```
70+
/home/ubuntu/.cache/bazel/_bazel_ubuntu/d557fe27c3b1f8a6b8a21796588f212a/external/eigen_archive/eigen-eigen-73a4995594c6/Eigen/src/Core/util/DisableStupidWarnings.h
71+
```
72+
You should adapt the following paths according to this output:
73+
```
74+
export CPLUS_INCLUDE_PATH="/home/ubuntu/.cache/bazel/_bazel_ubuntu/d557fe27c3b1f8a6b8a21796588f212a/external/eigen_archive/eigen-eigen-73a4995594c6/:$CPLUS_INCLUDE_PATH"
75+
export CPLUS_INCLUDE_PATH="/home/ubuntu/.cache/bazel/_bazel_ubuntu/d557fe27c3b1f8a6b8a21796588f212a/external/eigen_archive/:$CPLUS_INCLUDE_PATH"
76+
```
77+
78+
Install the JavaCPP presets:
79+
```
80+
cd ~
81+
git clone https://github.com/pcmoritz/javacpp-presets.git
82+
cd javacpp-presets
83+
bash cppbuild.sh install opencv caffe tensorflow
84+
mvn install --projects=.,opencv,caffe,tensorflow -Djavacpp.platform.dependency=false
85+
```
58 KB
Binary file not shown.
+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# This file is adapted from https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/alexnet/alexnet_benchmark.py
2+
3+
from datetime import datetime
4+
import math
5+
import time
6+
7+
from six.moves import xrange # pylint: disable=redefined-builtin
8+
import tensorflow as tf
9+
10+
BATCH_SIZE = 128
11+
IMAGE_SIZE = 227
12+
NUM_CHANNELS = 3
13+
SEED = 66478
14+
15+
def print_activations(t):
16+
print(t.op.name, ' ', t.get_shape().as_list())
17+
18+
19+
def inference(images):
20+
# conv1
21+
with tf.name_scope('conv1') as scope:
22+
kernel = tf.Variable(tf.truncated_normal([11, 11, 3, 64], dtype=tf.float32,
23+
stddev=1e-1, seed=SEED), name='weights')
24+
conv = tf.nn.conv2d(images, kernel, [1, 4, 4, 1], padding='SAME')
25+
biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32),
26+
trainable=True, name='biases')
27+
bias = tf.nn.bias_add(conv, biases)
28+
conv1 = tf.nn.relu(bias, name=scope)
29+
print_activations(conv1)
30+
31+
# lrn1
32+
# TODO(shlens, jiayq): Add a GPU version of local response normalization.
33+
34+
# pool1
35+
pool1 = tf.nn.max_pool(conv1,
36+
ksize=[1, 3, 3, 1],
37+
strides=[1, 2, 2, 1],
38+
padding='VALID',
39+
name='pool1')
40+
print_activations(pool1)
41+
42+
# conv2
43+
with tf.name_scope('conv2') as scope:
44+
kernel = tf.Variable(tf.truncated_normal([5, 5, 64, 192],
45+
dtype=tf.float32,
46+
stddev=1e-1,
47+
seed=SEED), name='weights')
48+
conv = tf.nn.conv2d(pool1, kernel, [1, 1, 1, 1], padding='SAME')
49+
biases = tf.Variable(tf.constant(0.0, shape=[192], dtype=tf.float32),
50+
trainable=True, name='biases')
51+
bias = tf.nn.bias_add(conv, biases)
52+
conv2 = tf.nn.relu(bias, name=scope)
53+
print_activations(conv2)
54+
55+
# pool2
56+
pool2 = tf.nn.max_pool(conv2,
57+
ksize=[1, 3, 3, 1],
58+
strides=[1, 2, 2, 1],
59+
padding='VALID',
60+
name='pool2')
61+
print_activations(pool2)
62+
63+
# conv3
64+
with tf.name_scope('conv3') as scope:
65+
kernel = tf.Variable(tf.truncated_normal([3, 3, 192, 384],
66+
dtype=tf.float32,
67+
stddev=1e-1,
68+
seed=SEED), name='weights')
69+
conv = tf.nn.conv2d(pool2, kernel, [1, 1, 1, 1], padding='SAME')
70+
biases = tf.Variable(tf.constant(0.0, shape=[384], dtype=tf.float32),
71+
trainable=True, name='biases')
72+
bias = tf.nn.bias_add(conv, biases)
73+
conv3 = tf.nn.relu(bias, name=scope)
74+
print_activations(conv3)
75+
76+
# conv4
77+
with tf.name_scope('conv4') as scope:
78+
kernel = tf.Variable(tf.truncated_normal([3, 3, 384, 256],
79+
dtype=tf.float32,
80+
stddev=1e-1,
81+
seed=SEED), name='weights')
82+
conv = tf.nn.conv2d(conv3, kernel, [1, 1, 1, 1], padding='SAME')
83+
biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32),
84+
trainable=True, name='biases')
85+
bias = tf.nn.bias_add(conv, biases)
86+
conv4 = tf.nn.relu(bias, name=scope)
87+
print_activations(conv4)
88+
89+
# conv5
90+
with tf.name_scope('conv5') as scope:
91+
kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256],
92+
dtype=tf.float32,
93+
stddev=1e-1,
94+
seed=SEED), name='weights')
95+
conv = tf.nn.conv2d(conv4, kernel, [1, 1, 1, 1], padding='SAME')
96+
biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32),
97+
trainable=True, name='biases')
98+
bias = tf.nn.bias_add(conv, biases)
99+
conv5 = tf.nn.relu(bias, name=scope)
100+
print_activations(conv5)
101+
102+
# pool5
103+
pool5 = tf.nn.max_pool(conv5,
104+
ksize=[1, 3, 3, 1],
105+
strides=[1, 2, 2, 1],
106+
padding='VALID',
107+
name='pool5')
108+
print_activations(pool5)
109+
110+
fc6W = tf.Variable(
111+
tf.truncated_normal([9216, 4096],
112+
stddev=0.1,
113+
seed=SEED),
114+
name="fc6W")
115+
fc6b = tf.Variable(tf.zeros([4096]), name="fc6b")
116+
fc6 = tf.nn.relu_layer(tf.reshape(pool5, [BATCH_SIZE, 9216]), fc6W, fc6b, name="fc6")
117+
118+
fc7W = tf.Variable(
119+
tf.truncated_normal([4096, 4096],
120+
stddev=0.1,
121+
seed=SEED),
122+
name="fc7W")
123+
fc7b = tf.Variable(tf.zeros([4096]), name="fc7b")
124+
fc7 = tf.nn.relu_layer(fc6, fc7W, fc7b, name="fc7")
125+
126+
fc8W = tf.Variable(
127+
tf.truncated_normal([4096, 1000],
128+
stddev=0.1,
129+
seed=SEED),
130+
name="fc8W")
131+
fc8b = tf.Variable(tf.zeros([1000]), name="fc8b")
132+
fc8 = tf.nn.xw_plus_b(fc7, fc8W, fc8b, name="fc8")
133+
134+
return fc8
135+
136+
137+
sess = tf.InteractiveSession(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
138+
139+
with tf.device('/gpu:0'):
140+
# Generate some dummy images.
141+
# Note that our padding definition is slightly different the cuda-convnet.
142+
# In order to force the model to start with the same activations sizes,
143+
# we add 3 to the image_size and employ VALID padding above.
144+
images = tf.placeholder(
145+
tf.float32,
146+
shape=(BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS),
147+
name="data")
148+
labels = tf.placeholder(tf.int32, shape=(BATCH_SIZE,), name="label")
149+
labels = tf.to_int64(labels)
150+
151+
# Build a Graph that computes the logits predictions from the
152+
# inference model.
153+
logits = inference(images)
154+
155+
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
156+
logits, labels), name="loss")
157+
158+
# Use simple momentum for the optimization.
159+
optimizer = tf.train.MomentumOptimizer(0.01,
160+
0.9).minimize(loss,
161+
name="train//step")
162+
163+
# Predictions for the current training minibatch.
164+
probs = tf.nn.softmax(logits, name="probs")
165+
prediction = tf.arg_max(probs, 1, name="prediction")
166+
correct_prediction = tf.equal(prediction, labels, name="correct_prediction")
167+
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"), name="accuracy")
168+
169+
# Build an initialization operation.
170+
tf.initialize_variables(tf.all_variables(), name="init//all_vars")
171+
172+
# this code traverses the graph and adds Assign nodes for each variable
173+
variables = [node for node in sess.graph_def.node if node.op == "Variable"]
174+
for v in variables:
175+
n = sess.graph.as_graph_element(v.name + ":0")
176+
dtype = tf.as_dtype(sess.graph.get_operation_by_name(v.name).get_attr("dtype"))
177+
update_placeholder = tf.placeholder(dtype, n.get_shape().as_list(), name=(v.name + "//update_placeholder"))
178+
tf.assign(n, update_placeholder, name=(v.name + "//assign"))
179+
180+
from google.protobuf.text_format import MessageToString
181+
print MessageToString(sess.graph_def)
182+
filename = "alexnet_graph.pb"
183+
s = sess.graph_def.SerializeToString()
184+
f = open(filename, "wb")
185+
f.write(s)
40.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)