piiswrong closed pull request #11114: [MXNET-502] Fixing broken feature_extract
cpp example
URL: https://github.com/apache/incubator-mxnet/pull/11114
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/cpp-package/example/feature_extract/Makefile
b/cpp-package/example/feature_extract/Makefile
index cc76d0506aa..f598183bd16 100644
--- a/cpp-package/example/feature_extract/Makefile
+++ b/cpp-package/example/feature_extract/Makefile
@@ -16,15 +16,13 @@
# under the License.
CXX=g++
-BLAS=-L /opt/openblas/lib -lopenblas -DMSHADOW_USE_CBLAS=1 -DMSHADOW_USE_MKL=0
+BLAS=-L /opt/openblas/lib -lopenblas -DMSHADOW_USE_CBLAS=1 -DMSHADOW_USE_MKL=0
CUDA=-DMSHADOW_USE_CUDA=1
OPENCV_CFLAGS=`pkg-config --cflags opencv`
OPENCV_LDFLAGS=`pkg-config --libs opencv`
-#COMMFLAGS=-static -static-libgcc -static-libstdc++
-
-CFLAGS=$(COMMFLAGS) -I ../../include -Wall -O3 -msse3 -funroll-loops
-Wno-unused-parameter -Wno-unknown-pragmas -fopenmp
-LDFLAGS=$(COMMFLAGS) -L ../../lib/linux -lmxnet $(BLAS) $(CUDA) -lgomp -pthread
+CFLAGS=$(COMMFLAGS) -I../../../3rdparty/nnvm/include
-I../../../3rdparty/dmlc-core/include -I ../../include -I ../../../include
-Wall -O3 -msse3 -funroll-loops -Wno-unused-parameter -Wno-unknown-pragmas
-fopenmp
+LDFLAGS=$(COMMFLAGS) -L ../../../lib -lmxnet $(BLAS) $(CUDA) -lgomp -pthread
all: feature_extract prepare_data_with_opencv
@@ -34,7 +32,7 @@ feature_extract: ./feature_extract.cpp
-rm -f $(basename $@).o
prepare_data_with_opencv: ./prepare_data_with_opencv.cpp
- $(CXX) -c -std=c++0x $(OPENCV_CFLAGS) $^
+ $(CXX) -c -std=c++0x $(OPENCV_CFLAGS) $^
$(CXX) $(basename $@).o -o $@ $(OPENCV_LDFLAGS)
-rm -f $(basename $@).o
diff --git a/cpp-package/example/feature_extract/README.md
b/cpp-package/example/feature_extract/README.md
index 4367a0c2efe..87cfcb4021f 100644
--- a/cpp-package/example/feature_extract/README.md
+++ b/cpp-package/example/feature_extract/README.md
@@ -1,8 +1,12 @@
This example shows how to extract features with a pretrained model.
-You can first get a pretrained model from
<https://github.com/dmlc/mxnet-model-gallery/blob/master/imagenet-1k-inception-bn.md>,
-then prepare 2 pictures 1.jpg and 2.jpg to extract by executing `run.sh`.
+Execute `run.sh` to:
+- Download a pretrained model
+- Download sample pictures (`dog.jpg` and `cat.jpg`)
+- Compile the files
+- Execute the featurization on `dog.jpg` and `cat.jpg`
+
Note:
1. The filename of network parameters may vary, line 67 in
`feature_extract.cpp` should be updated accordingly.
-2. As the build system has changed a lot, to build this example, you need to
put the compiled library `libmxnet.so` in `../lib/linux`.
+2. You need to build MXNet from source to get access to the `lib/libmxnet.so`
or point `LD_LIBRARY_PATH` to where it is installed in your system
diff --git a/cpp-package/example/feature_extract/feature_extract.cpp
b/cpp-package/example/feature_extract/feature_extract.cpp
index 1886c576400..c23623e23bd 100644
--- a/cpp-package/example/feature_extract/feature_extract.cpp
+++ b/cpp-package/example/feature_extract/feature_extract.cpp
@@ -58,13 +58,13 @@ class FeatureExtractor {
LG<<layer_name;
}
*/
- net = Symbol::Load("./model/Inception_BN-symbol.json")
+ net = Symbol::Load("./model/Inception-BN-symbol.json")
.GetInternals()["global_pool_output"];
}
/*Fill the trained paramters into the model, a.k.a. net, executor*/
void LoadParameters() {
map<string, NDArray> paramters;
- NDArray::Load("./model/Inception_BN-0039.params", 0, ¶mters);
+ NDArray::Load("./model/Inception-BN-0126.params", 0, ¶mters);
for (const auto &k : paramters) {
if (k.first.substr(0, 4) == "aux:") {
auto name = k.first.substr(4, k.first.size() - 4);
@@ -99,7 +99,7 @@ class FeatureExtractor {
data.Slice(0, 1) -= mean_img;
data.Slice(1, 2) -= mean_img;
args_map["data"] = data;
- /*bind the excutor*/
+ /*bind the executor*/
executor = net.SimpleBind(global_ctx, args_map, map<string, NDArray>(),
map<string, OpReqType>(), aux_map);
executor->Forward(false);
@@ -117,7 +117,7 @@ NDArray Data2NDArray() {
NDArray ret(Shape(2, 3, 224, 224), global_ctx, false);
ifstream inf("./img.dat", ios::binary);
vector<float> data(2 * 3 * 224 * 224);
- inf.read(reinterpret_cast<char *>data.data(), 2 * 3 * 224 * 224 *
sizeof(float));
+ inf.read(reinterpret_cast<char *>(data.data()), 2 * 3 * 224 * 224 *
sizeof(float));
inf.close();
ret.SyncCopyFromCPU(data.data(), 2 * 3 * 224 * 224);
NDArray::WaitAll();
diff --git a/cpp-package/example/feature_extract/prepare_data_with_opencv.cpp
b/cpp-package/example/feature_extract/prepare_data_with_opencv.cpp
index a7b4cba0a64..fe32e896adb 100644
--- a/cpp-package/example/feature_extract/prepare_data_with_opencv.cpp
+++ b/cpp-package/example/feature_extract/prepare_data_with_opencv.cpp
@@ -29,7 +29,7 @@ using namespace std;
/*read images and store them the NDArray format that MXNet.cpp can handle*/
void Mat2Array() {
- string file_name_list[] = {"./1.jpg", "./2.jpg"};
+ string file_name_list[] = {"./dog.jpg", "./cat.jpg"};
std::vector<float> array;
for (auto &t : file_name_list) {
@@ -45,7 +45,7 @@ void Mat2Array() {
}
}
ofstream outf("./img.dat", ios::binary);
- outf.write(reinterpret_cast<char *>array.data(), array.size() *
sizeof(float));
+ outf.write(reinterpret_cast<char *>(array.data()), array.size() *
sizeof(float));
outf.close();
}
diff --git a/cpp-package/example/feature_extract/run.sh
b/cpp-package/example/feature_extract/run.sh
index dc6665604b1..616445dbd67 100755
--- a/cpp-package/example/feature_extract/run.sh
+++ b/cpp-package/example/feature_extract/run.sh
@@ -15,15 +15,19 @@
# specific language governing permissions and limitations
# under the License.
-### To run the this example,
-###
-### 1.
-### Get Inseption-BN model first, from here
-### https://github.com/dmlc/mxnet-model-gallery
-###
-### 2.
-### Then Prepare 2 pictures, 1.jpg 2.jpg to extract
+# Downloading the data and model
+mkdir -p model
+wget -nc http://data.dmlc.ml/mxnet/models/imagenet/inception-bn.tar.gz
+wget -nc -O cat.jpg
https://github.com/dmlc/web-data/blob/master/mxnet/doc/tutorials/python/predict_image/cat.jpg?raw=true
+wget -nc -O dog.jpg
https://github.com/dmlc/web-data/blob/master/mxnet/doc/tutorials/python/predict_image/dog.jpg?raw=true
+wget -nc -O model/mean_224.nd
https://github.com/dmlc/web-data/raw/master/mxnet/example/feature_extract/mean_224.nd
+tar -xvzf inception-bn.tar.gz -C model --skip-old-files
+# Building
make
+
+# Preparing the data
./prepare_data_with_opencv
-LD_LIBRARY_PATH=../../lib/linux ./feature_extract
+
+# Running the featurization
+LD_LIBRARY_PATH=../../../lib ./feature_extract
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services