Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic Raspberry Pi example #2947

Merged
merged 3 commits into from Jun 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -291,7 +291,7 @@ - (void)runCNNOnFrame:(CVPixelBufferRef)pixelBuffer {
in + (in_y * image_width * image_channels) + (in_x * image_channels);
float *out_pixel = out_row + (x * wanted_channels);
for (int c = 0; c < wanted_channels; ++c) {
out_pixel[c] = (in_pixel[c] / input_std) - input_mean;
out_pixel[c] = (in_pixel[c] - input_mean) / input_std;
}
}
}
Expand Down
Expand Up @@ -202,7 +202,7 @@ bool PortableReadFileToProto(const std::string& file_name,
tensorflow::uint8* in_pixel = in_row + (in_x * image_channels);
float* out_pixel = out_row + (x * wanted_channels);
for (int c = 0; c < wanted_channels; ++c) {
out_pixel[c] = (in_pixel[c] / input_std) - input_mean;
out_pixel[c] = (in_pixel[c] - input_mean) / input_std;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions tensorflow/contrib/pi_examples/.gitignore
@@ -0,0 +1,3 @@
tensorflow_inception_graph.pb
imagenet_comp_graph_label_strings.txt
tensorflow_inception_stripped.pb
39 changes: 39 additions & 0 deletions tensorflow/contrib/pi_examples/README.md
@@ -0,0 +1,39 @@
# TensorFlow Raspberry Pi Examples

This folder contains examples of how to build applications for the Raspberry Pi using TensorFlow.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:
"This folder contains an example of a Raspberry Pi application that uses TensorFlow", as there's only one example.

## Building the Examples

- Follow the Raspberry Pi section of the instructions at [tensorflow/contrib/makefile](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/makefile) to compile a static library containing the core TensorFlow code.

- Install libjpeg, so we can load image files:

```
sudo apt-get install -y libjpeg-dev
```

- To download the example model you'll need, run these commands:

```bash
curl https://storage.googleapis.com/download.tensorflow.org/models/inception_dec_2015_stripped.zip \
-o /tmp/inception_dec_2015_stripped.zip
unzip /tmp/inception_dec_2015_stripped.zip \
-d tensorflow/contrib/pi_examples/label_image/data/
```

- From the root of the TensorFlow source tree, run `make -f tensorflow/contrib/pi_examples/label_image/Makefile` to build a basic example.

## Usage

Run `tensorflow/contrib/pi_examples/label_image/gen/bin/label_image` to try out image labeling with the default Grace Hopper image. You should several lines of output, with "Military Uniform" shown as the top result, something like this:

```bash
I tensorflow/contrib/pi_examples/label_image/label_image.cc:384] Running model succeeded!
I tensorflow/contrib/pi_examples/label_image/label_image.cc:284] military uniform (866): 0.624293
I tensorflow/contrib/pi_examples/label_image/label_image.cc:284] suit (794): 0.0473981
I tensorflow/contrib/pi_examples/label_image/label_image.cc:284] academic gown (896): 0.0280926
I tensorflow/contrib/pi_examples/label_image/label_image.cc:284] bolo tie (940): 0.0156956
I tensorflow/contrib/pi_examples/label_image/label_image.cc:284] bearskin (849): 0.0143348
```

Once you've verified that is working, you can supply your own images with `--image=your_image.jpg`, or even with graphs you've trained yourself with the TensorFlow for Poets tutorial using `--graph=your_graph.pb --input=Mul:0 --output=softmax:0`.
83 changes: 83 additions & 0 deletions tensorflow/contrib/pi_examples/label_image/Makefile
@@ -0,0 +1,83 @@
# This Makefile compiles the label_image example for the Raspberry Pi.
# See tensorflow/contrib/pi_examples/README.md for full build instructions.

# Find where we're running from, so we can store generated files here.
SCRIPT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))

# The location of the tensorflow/contrib/makefile directory.
TFMAKEFILE_DIR := $(SCRIPT_DIR)/../../makefile

# Where compiled objects are stored.
GENDIR := $(SCRIPT_DIR)/gen/
OBJDIR := $(GENDIR)obj/
LIBDIR := $(GENDIR)lib/
BINDIR := $(GENDIR)bin/

# The expected locations of the TensorFlow library.
TFLIBDIR := $(TFMAKEFILE_DIR)/gen/lib
TFLIBS := $(TFLIBDIR)/libtensorflow-core.a

# Where the downloads have been stored.
DOWNLOADSDIR := $(TFMAKEFILE_DIR)/downloads

# The location of the compiled protobuf headers generated by TensorFlow.
PBTGENDIR := $(TFMAKEFILE_DIR)/gen/proto_text/
PROTOGENDIR := $(TFMAKEFILE_DIR)/gen/proto/

# The name of the output program we're compiling.
EXECUTABLE_NAME := $(BINDIR)/label_image

# Settings for the target compiler.
CXX := gcc
OPTFLAGS := -O0
CXXFLAGS := --std=c++11 $(OPTFLAGS)
LDFLAGS := \
-L/usr/local/lib \
-L$(TFLIBDIR) \
-Wl,--no-whole-archive
INCLUDES := \
-I/usr/local/include \
-I. \
-I$(DOWNLOADSDIR) \
-I$(DOWNLOADSDIR)/eigen-latest/ \
-I$(PROTOGENDIR) \
-I$(PBTGENDIR)
LIBS := \
-lstdc++ \
-lprotobuf \
-Wl,--allow-multiple-definition \
-Wl,--whole-archive \
-ltensorflow-core \
-Wl,--no-whole-archive \
-ldl \
-lpthread \
-lm \
-ljpeg
LIBFLAGS :=

EXECUTABLE_SRCS := tensorflow/contrib/pi_examples/label_image/label_image.cc

# File names of the intermediate files target compilation generates.
EXECUTABLE_OBJS := $(addprefix $(OBJDIR), $(EXECUTABLE_SRCS:.cc=.o))

.PHONY: clean

# The target that's compiled if there's no command-line arguments.
all: $(EXECUTABLE_NAME)

# Rules for target compilation.

$(EXECUTABLE_NAME): $(EXECUTABLE_OBJS) $(TFLIBS)
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(INCLUDES) \
-o $(EXECUTABLE_NAME) $(EXECUTABLE_OBJS) \
$(LIBFLAGS) $(LIB_PATH) $(LDFLAGS) $(LIBS)

# Matches on C++ source files.
$(OBJDIR)%.o: %.cc
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

# Gets rid of all generated files.
clean:
rm -rf $(GENDIR)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.