MNN/demo/exec/segment.cpp

144 lines
4.7 KiB
C++
Raw Normal View History

//
// segment.cpp
// MNN
//
// Created by MNN on 2019/07/01.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include <stdio.h>
2019-12-27 22:16:57 +08:00
#include <MNN/ImageProcess.hpp>
#define MNN_OPEN_TIME_TRACE
#include <algorithm>
#include <fstream>
#include <functional>
#include <memory>
#include <sstream>
#include <vector>
2019-12-27 22:16:57 +08:00
#include <MNN/expr/Expr.hpp>
#include <MNN/expr/ExprCreator.hpp>
#include <MNN/AutoTime.hpp>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
using namespace MNN;
using namespace MNN::CV;
using namespace MNN::Express;
int main(int argc, const char* argv[]) {
if (argc < 4) {
MNN_PRINT("Usage: ./segment.out model.mnn input.jpg output.jpg\n");
return 0;
}
- build: - unify schema building in core and converter; - add more build script for android; - add linux build script for python; - ops impl: - add floor mod support in binary; - use eltwise impl in add/max/sub/mul binary for optimization; - remove fake double support in cast; - fix 5d support for concat; - add adjX and adjY support for batch matmul; - optimize conv2d back prop filter; - add pad mode support for conv3d; - fix bug in conv2d & conv depthwise with very small feature map; - optimize binary without broacast; - add data types support for gather; - add gather ND support; - use uint8 data type in gather v2; - add transpose support for matmul; - add matrix band part; - add dim != 4 support for padding, reshape & tensor convert; - add pad type support for pool3d; - make ops based on TensorFlow Lite quantization optional; - add all & any support for reduction; - use type in parameter as output type in reduction; - add int support for unary; - add variable weight support for conv2d; - fix conv2d depthwise weights initialization; - fix type support for transpose; - fix grad outputs count for reduce grad and reshape grad; - fix priorbox & detection output; - fix metal softmax error; - python: - add runSessionWithCallBackInfo interface; - add max nodes limit (1400) for visualization tool; - fix save error in python3; - align default dim; - convert: - add extra design for optimization; - add more post converting optimizers; - add caffe v1 weights blob support; - add cast, unary, conv transpose support for onnx model; - optimize batchnorm, conv with variable weights, prelu, reshape, slice, upsample for onnx model; - add cos/sin/atan/tan support for unary for tensorflow model; - add any/all support for reduction for tensorflow model; - add elu, conv3d, pool3d support for tensorflow model; - optimize argmax, batchnorm, concat, batch to space, conv with variable weights, prelu, slice for tensorflow model; - others: - fix size computer lock; - fix thread pool deadlock; - add express & parameters in express; - rewrite blitter chooser without static map; - add tests for expr;
2019-10-29 13:37:26 +08:00
auto net = Variable::getInputAndOutput(Variable::loadMap(argv[1]));
if (net.first.empty()) {
MNN_ERROR("Invalid Model\n");
return 0;
}
- build: - unify schema building in core and converter; - add more build script for android; - add linux build script for python; - ops impl: - add floor mod support in binary; - use eltwise impl in add/max/sub/mul binary for optimization; - remove fake double support in cast; - fix 5d support for concat; - add adjX and adjY support for batch matmul; - optimize conv2d back prop filter; - add pad mode support for conv3d; - fix bug in conv2d & conv depthwise with very small feature map; - optimize binary without broacast; - add data types support for gather; - add gather ND support; - use uint8 data type in gather v2; - add transpose support for matmul; - add matrix band part; - add dim != 4 support for padding, reshape & tensor convert; - add pad type support for pool3d; - make ops based on TensorFlow Lite quantization optional; - add all & any support for reduction; - use type in parameter as output type in reduction; - add int support for unary; - add variable weight support for conv2d; - fix conv2d depthwise weights initialization; - fix type support for transpose; - fix grad outputs count for reduce grad and reshape grad; - fix priorbox & detection output; - fix metal softmax error; - python: - add runSessionWithCallBackInfo interface; - add max nodes limit (1400) for visualization tool; - fix save error in python3; - align default dim; - convert: - add extra design for optimization; - add more post converting optimizers; - add caffe v1 weights blob support; - add cast, unary, conv transpose support for onnx model; - optimize batchnorm, conv with variable weights, prelu, reshape, slice, upsample for onnx model; - add cos/sin/atan/tan support for unary for tensorflow model; - add any/all support for reduction for tensorflow model; - add elu, conv3d, pool3d support for tensorflow model; - optimize argmax, batchnorm, concat, batch to space, conv with variable weights, prelu, slice for tensorflow model; - others: - fix size computer lock; - fix thread pool deadlock; - add express & parameters in express; - rewrite blitter chooser without static map; - add tests for expr;
2019-10-29 13:37:26 +08:00
auto input = net.first.begin()->second;
auto info = input->getInfo();
if (nullptr == info) {
MNN_ERROR("The model don't have init dim\n");
return 0;
}
auto shape = input->getInfo()->dim;
shape[0] = 1;
input->resize(shape);
- build: - unify schema building in core and converter; - add more build script for android; - add linux build script for python; - ops impl: - add floor mod support in binary; - use eltwise impl in add/max/sub/mul binary for optimization; - remove fake double support in cast; - fix 5d support for concat; - add adjX and adjY support for batch matmul; - optimize conv2d back prop filter; - add pad mode support for conv3d; - fix bug in conv2d & conv depthwise with very small feature map; - optimize binary without broacast; - add data types support for gather; - add gather ND support; - use uint8 data type in gather v2; - add transpose support for matmul; - add matrix band part; - add dim != 4 support for padding, reshape & tensor convert; - add pad type support for pool3d; - make ops based on TensorFlow Lite quantization optional; - add all & any support for reduction; - use type in parameter as output type in reduction; - add int support for unary; - add variable weight support for conv2d; - fix conv2d depthwise weights initialization; - fix type support for transpose; - fix grad outputs count for reduce grad and reshape grad; - fix priorbox & detection output; - fix metal softmax error; - python: - add runSessionWithCallBackInfo interface; - add max nodes limit (1400) for visualization tool; - fix save error in python3; - align default dim; - convert: - add extra design for optimization; - add more post converting optimizers; - add caffe v1 weights blob support; - add cast, unary, conv transpose support for onnx model; - optimize batchnorm, conv with variable weights, prelu, reshape, slice, upsample for onnx model; - add cos/sin/atan/tan support for unary for tensorflow model; - add any/all support for reduction for tensorflow model; - add elu, conv3d, pool3d support for tensorflow model; - optimize argmax, batchnorm, concat, batch to space, conv with variable weights, prelu, slice for tensorflow model; - others: - fix size computer lock; - fix thread pool deadlock; - add express & parameters in express; - rewrite blitter chooser without static map; - add tests for expr;
2019-10-29 13:37:26 +08:00
auto output = net.second.begin()->second;
if (nullptr == output->getInfo()) {
MNN_ERROR("Alloc memory or compute size error\n");
return 0;
}
2019-12-27 22:16:57 +08:00
{
int size_w = 0;
int size_h = 0;
int bpp = 0;
if (info->order == NHWC) {
bpp = shape[3];
size_h = shape[1];
size_w = shape[2];
} else {
bpp = shape[1];
size_h = shape[2];
size_w = shape[3];
}
if (bpp == 0)
bpp = 1;
if (size_h == 0)
size_h = 1;
if (size_w == 0)
size_w = 1;
MNN_PRINT("input: w:%d , h:%d, bpp: %d\n", size_w, size_h, bpp);
2019-12-27 22:16:57 +08:00
auto inputPatch = argv[2];
int width, height, channel;
auto inputImage = stbi_load(inputPatch, &width, &height, &channel, 4);
if (nullptr == inputImage) {
MNN_ERROR("Can't open %s\n", inputPatch);
return 0;
}
MNN_PRINT("origin size: %d, %d\n", width, height);
Matrix trans;
// Set scale, from dst scale to src
trans.setScale((float)(width-1) / (size_w-1), (float)(height-1) / (size_h-1));
ImageProcess::Config config;
config.filterType = CV::BILINEAR;
// float mean[3] = {103.94f, 116.78f, 123.68f};
// float normals[3] = {0.017f, 0.017f, 0.017f};
float mean[3] = {127.5f, 127.5f, 127.5f};
float normals[3] = {0.00785f, 0.00785f, 0.00785f};
::memcpy(config.mean, mean, sizeof(mean));
::memcpy(config.normal, normals, sizeof(normals));
config.sourceFormat = RGBA;
config.destFormat = RGB;
2019-12-27 22:16:57 +08:00
std::shared_ptr<ImageProcess> pretreat(ImageProcess::create(config));
pretreat->setMatrix(trans);
pretreat->convert((uint8_t*)inputImage, width, height, 0, input->writeMap<float>(), size_w, size_h, 4, 0, halide_type_of<float>());
stbi_image_free(inputImage);
input->unMap();
}
{
- build: - unify schema building in core and converter; - add more build script for android; - add linux build script for python; - ops impl: - add floor mod support in binary; - use eltwise impl in add/max/sub/mul binary for optimization; - remove fake double support in cast; - fix 5d support for concat; - add adjX and adjY support for batch matmul; - optimize conv2d back prop filter; - add pad mode support for conv3d; - fix bug in conv2d & conv depthwise with very small feature map; - optimize binary without broacast; - add data types support for gather; - add gather ND support; - use uint8 data type in gather v2; - add transpose support for matmul; - add matrix band part; - add dim != 4 support for padding, reshape & tensor convert; - add pad type support for pool3d; - make ops based on TensorFlow Lite quantization optional; - add all & any support for reduction; - use type in parameter as output type in reduction; - add int support for unary; - add variable weight support for conv2d; - fix conv2d depthwise weights initialization; - fix type support for transpose; - fix grad outputs count for reduce grad and reshape grad; - fix priorbox & detection output; - fix metal softmax error; - python: - add runSessionWithCallBackInfo interface; - add max nodes limit (1400) for visualization tool; - fix save error in python3; - align default dim; - convert: - add extra design for optimization; - add more post converting optimizers; - add caffe v1 weights blob support; - add cast, unary, conv transpose support for onnx model; - optimize batchnorm, conv with variable weights, prelu, reshape, slice, upsample for onnx model; - add cos/sin/atan/tan support for unary for tensorflow model; - add any/all support for reduction for tensorflow model; - add elu, conv3d, pool3d support for tensorflow model; - optimize argmax, batchnorm, concat, batch to space, conv with variable weights, prelu, slice for tensorflow model; - others: - fix size computer lock; - fix thread pool deadlock; - add express & parameters in express; - rewrite blitter chooser without static map; - add tests for expr;
2019-10-29 13:37:26 +08:00
//auto originOrder = output->getInfo()->order;
output = _Convert(output, NHWC);
//output = _Softmax(output, -1);
auto outputInfo = output->getInfo();
auto width = outputInfo->dim[2];
auto height = outputInfo->dim[1];
auto channel = outputInfo->dim[3];
std::shared_ptr<Tensor> wrapTensor(ImageProcess::createImageTensor<uint8_t>(width, height, 4, nullptr));
MNN_PRINT("Mask: w=%d, h=%d, c=%d\n", width, height, channel);
auto outputHostPtr = output->readMap<float>();
for (int y = 0; y < height; ++y) {
auto rgbaY = wrapTensor->host<uint8_t>() + 4 * y * width;
auto sourceY = outputHostPtr + y * width * channel;
for (int x=0; x<width; ++x) {
auto sourceX = sourceY + channel * x;
int index = 0;
float maxValue = sourceX[0];
auto rgba = rgbaY + 4 * x;
for (int c=1; c<channel; ++c) {
if (sourceX[c] > maxValue) {
index = c;
maxValue = sourceX[c];
}
}
rgba[0] = 255;
rgba[2] = 0;
rgba[1] = 0;
rgba[3] = 255;
if (15 == index) {
rgba[2] = 255;
rgba[3] = 0;
}
}
}
output->unMap();
stbi_write_png(argv[3], width, height, 4, wrapTensor->host<uint8_t>(), 4 * width);
}
return 0;
}