Skip to content

Commit

Permalink
general code format consistency, examples exit when model(s) cannot b…
Browse files Browse the repository at this point in the history
…e loaded, example_keyword_spotting hides label on low-probability and prints key words, improved example_style_transfer responsiveness and added new images, added note to readme about dylib loading error, util function comments now oxygen format for IDEs
  • Loading branch information
danomatika committed Feb 9, 2021
1 parent 2b2649f commit 90a807d
Show file tree
Hide file tree
Showing 28 changed files with 741 additions and 705 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ If you find any bugs or suggestions please log them to GitHub as well.
Known Issues
------------

### dyld: Library not loaded: @rpath/libtensorflow.2.dylib

On macOS, the libtensorflow dynamic libraries (dylibs) need to be copied into the .app bundle. This error indicates the library loader cannot find the dylibs when the app starts and the build process is missing a step. Please check the "macOS" subsection under the "Installation & Build" section.

### EXC_BAD_INSTRUCTION Crash on macOS

The pre-built libtensorflow downloaded to `libs/tensorflow` comes with AVX (Advanced Vector Extensions) enabled which is an extension to the Intel x86 instruction set for fast vector math. CPUs older than circa 2013 may not support this and the application will simply crash with error such as:
Expand All @@ -401,6 +405,7 @@ EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
This problem may also be seen when using libtensorflow installed via Homebrew.

The only solution is to build libtensorflow from source with AVX disabled use a machine with a newer CPU. To check if your CPU supports AVX use:

```shell
# print all CPU features
sysctl -a | grep cpu.feat
Expand All @@ -415,11 +420,11 @@ Systems confirmed: Mac Pro (Mid 2012)

The pre-built libtensorflow dynamic libraries downloaded from the TensorFlow website require a minimum of macOS 10.14. On macOS 10.13 or lower, the project may build but will fail on run with a runtime loader error:

```
~~~
dyld: lazy symbol binding failed: Symbol not found: ____chkstk_darwin
Referenced from: /Users/na/of_v0.11.0_osx_release/addons/ofxTensorFlow2/example_basics/bin/example_basics.app/Contents/MacOS/./../Frameworks/libtensorflow.2.dylib (which was built for Mac OS X 10.15)
Expected in: /usr/lib/libSystem.B.dylib
```
~~~

The only solutions are:

Expand Down
2 changes: 1 addition & 1 deletion example_basics/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "ofApp.h"

//========================================================================
int main(){
int main() {

ofSetupOpenGL(1024, 768, OF_WINDOW); // <-------- setup the GL context

Expand Down
47 changes: 28 additions & 19 deletions example_basics/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@
#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
void ofApp::setup() {
ofSetFrameRate(60);
ofSetVerticalSync(true);
ofSetWindowTitle("example_basics");


// create an input tensor of an arbitrary shape and fill it
auto input = cppflow::fill({1, 2, 2, 3}, 1.0f);

// load the model
model.load("model");
// load the model, bail out on error
if(!model.load("model")) {
std::exit(EXIT_FAILURE);
}

// inference
auto output = model.runModel(input);
Expand All @@ -35,74 +36,82 @@ void ofApp::setup(){
ofxTF2::tensorToVector<float>(output, outputVector);
ofxTF2::tensorToVector<float>(input, inputVector);

// print summary to console
ofLog() << "Flatted Input:";
ofLog() << ofxTF2::vectorToString(inputVector);
ofLog() << "Flattened Output:";
ofLog() << ofxTF2::vectorToString(outputVector);

// load a font for displaying strings
font.load(OF_TTF_SANS, 14);
}

//--------------------------------------------------------------
void ofApp::update(){
void ofApp::update() {

}

//--------------------------------------------------------------
void ofApp::draw(){
font.drawString("Flattened Input: ", 20, 20);
void ofApp::draw() {

// draw summary to screen
font.drawString("Flattened Input:", 20, 20);
font.drawString(ofxTF2::vectorToString(inputVector), 40, 40);
font.drawString("Flattened Output: ", 20, 60);
font.drawString("Flattened Output:", 20, 60);
font.drawString(ofxTF2::vectorToString(outputVector), 40, 80);
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
void ofApp::keyPressed(int key) {

}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){
void ofApp::keyReleased(int key) {

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
void ofApp::mouseMoved(int x, int y) {

}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
void ofApp::mouseDragged(int x, int y, int button) {

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
void ofApp::mousePressed(int x, int y, int button) {

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
void ofApp::mouseReleased(int x, int y, int button) {

}

//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
void ofApp::mouseEntered(int x, int y) {

}

//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
void ofApp::mouseExited(int x, int y) {

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
void ofApp::windowResized(int w, int h) {

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
void ofApp::gotMessage(ofMessage msg) {

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
void ofApp::dragEvent(ofDragInfo dragInfo) {

}
5 changes: 2 additions & 3 deletions example_basics/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "ofMain.h"
#include "ofxTensorFlow2.h"

class ofApp : public ofBaseApp{
class ofApp : public ofBaseApp {

public:
void setup();
Expand All @@ -27,7 +27,7 @@ class ofApp : public ofBaseApp{

void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseMoved(int x, int y);
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
Expand All @@ -37,7 +37,6 @@ class ofApp : public ofBaseApp{
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);

private:
ofxTF2::Model model;
std::vector<float> inputVector;
std::vector<float> outputVector;
Expand Down
43 changes: 23 additions & 20 deletions example_basics_efficientnet/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@
#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
void ofApp::setup() {
ofSetFrameRate(60);
ofSetVerticalSync(true);
ofSetWindowTitle("example_effnet");
ofSetWindowTitle("example_basics_efficientnet");

// load the model
model.load("model");
// load the model, bail out on error
if(!model.load("model")) {
std::exit(EXIT_FAILURE);
}

// define and print image path relative to bin/data
std::string path(ofToDataPath("my_cat.jpg"));
ofLog() << "Loading image: " << path;

// use TensorFlow ops through the cppflow wrappers
// load a jpeg picture cast it to float and add a dimension for batches
// load a jpeg picture, cast it to float, and add a dimension for batches
auto input = cppflow::decode_jpeg(cppflow::read_file(path));
input = cppflow::cast(input, TF_UINT8, TF_FLOAT);
input = cppflow::expand_dims(input, 0);
Expand All @@ -44,11 +46,12 @@ void ofApp::setup(){
// access each element using ofxTF2 conversion functions
ofxTF2::tensorToVector<float>(output, outputVector);

// get and print tensor shape
// get and print tensor shape,
// "NHWC" -> Num_samples x Height x Width x Channels
auto shape = ofxTF2::getTensorShape(input);
ofLog() << "Input tensor has shape: "
<< ofxTF2::vectorToString(shape);
ofLog() << "Keep in mind: default format for images in TensorFlow is NHWC";
ofLog() << "Keep in mind: Default format for images in TensorFlow is NHWC";

// allocate the image and write to it
imgIn.allocate(shape[2], shape[1], OF_IMAGE_COLOR);
Expand All @@ -59,12 +62,12 @@ void ofApp::setup(){
}

//--------------------------------------------------------------
void ofApp::update(){
void ofApp::update() {
imgIn.update();
}

//--------------------------------------------------------------
void ofApp::draw(){
void ofApp::draw() {
font.drawString("[281] tabby cat: " + std::to_string(outputVector[281]), 0, 20);
font.drawString("[282] tiger cat: " + std::to_string(outputVector[282]), 0, 50);
font.drawString("[283] persian cat: " + std::to_string(outputVector[283]), 0, 80);
Expand All @@ -74,56 +77,56 @@ void ofApp::draw(){
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
void ofApp::keyPressed(int key) {

}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){
void ofApp::keyReleased(int key) {

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
void ofApp::mouseMoved(int x, int y) {

}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
void ofApp::mouseDragged(int x, int y, int button) {

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
void ofApp::mousePressed(int x, int y, int button) {

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
void ofApp::mouseReleased(int x, int y, int button) {

}

//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
void ofApp::mouseEntered(int x, int y) {

}

//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
void ofApp::mouseExited(int x, int y) {

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
void ofApp::windowResized(int w, int h) {

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
void ofApp::gotMessage(ofMessage msg) {

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
void ofApp::dragEvent(ofDragInfo dragInfo) {

}
9 changes: 4 additions & 5 deletions example_basics_efficientnet/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "ofMain.h"
#include "ofxTensorFlow2.h"

class ofApp : public ofBaseApp{
class ofApp : public ofBaseApp {

public:
void setup();
Expand All @@ -27,7 +27,7 @@ class ofApp : public ofBaseApp{

void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseMoved(int x, int y);
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
Expand All @@ -36,10 +36,9 @@ class ofApp : public ofBaseApp{
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);

private:
std::vector<float> outputVector;

ofxTF2::Model model;
ofImage imgIn;
std::vector<float> outputVector;
ofTrueTypeFont font;
};
2 changes: 1 addition & 1 deletion example_keyword_spotting/addons.make
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ofxTensorFlow2
ofxTensorFlow2
2 changes: 1 addition & 1 deletion example_keyword_spotting/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "ofApp.h"

//========================================================================
int main(){
int main() {

ofSetupOpenGL(500, 260, OF_WINDOW); // <-------- setup the GL context

Expand Down
Loading

0 comments on commit 90a807d

Please sign in to comment.