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 image loading capabilities (as 3D uint8 Tensors) #18

Merged
merged 1 commit into from
Jul 3, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
CFLAGS = -g -O3 -Wall
ERLANG_PATH = /usr/lib/erlang/erts-7.3/include
ERLANG_PATH = $(shell erl -eval 'io:format("~s", [lists:concat([code:root_dir(), "/erts-", erlang:system_info(version), "/include"])])' -s init stop -noshell)
LIBTENSORFLOW_PATH = /usr/local/lib
CFLAGS += -I$(ERLANG_PATH)
CFLAGS += -Ic_src
LDFLAGS += -L$(LIBTENSORFLOW_PATH)
ifeq ($(shell uname -s), Darwin)
LDFLAGS += -flat_namespace -undefined suppress
endif
LIB_SO_NAME = priv/Tensorflex.so
CFLAGS += -fPIC
NIF=c_src/Tensorflex.c

$(LIB_SO_NAME): $(NIF)
mkdir -p priv
$(CC) $(CFLAGS) -shared $(LDFLAGS) $^ -ltensorflow -o $@
$(CC) $(CFLAGS) -shared $(LDFLAGS) $^ -ltensorflow -ljpeg -o $@
72 changes: 72 additions & 0 deletions c_src/Tensorflex.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <stdlib.h>
#include <stddef.h>
#include <assert.h>
#include <jpeglib.h>
#include <stdint.h>

typedef struct
{
Expand Down Expand Up @@ -603,6 +605,75 @@ static ERL_NIF_TERM run_session(ErlNifEnv *env, int argc, const ERL_NIF_TERM arg

}

static ERL_NIF_TERM load_image_as_tensor(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
TF_Tensor *tensor;
TF_Tensor **tensor_resource_alloc = enif_alloc_resource(tensor_resource, sizeof(TF_Tensor *));

int error_check;
unsigned long input_size;
unsigned char *input_img;
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
unsigned long output_size;
unsigned char *output;
int row_stride, width, height, num_pixels;

ErlNifBinary filepath;
enif_inspect_binary(env,argv[0], &filepath);

char* file = enif_alloc(filepath.size+1);
memset(file, 0, filepath.size+1);
memcpy(file, (void *) filepath.data, filepath.size);

const char *dot = strrchr(file, '.');
if(!dot || dot == file) return enif_make_badarg(env);
if(!((strcmp((dot + 1),"jpg") == 0) || (strcmp((dot + 1),"jpeg") == 0))) return enif_make_badarg(env);
Copy link
Contributor

Choose a reason for hiding this comment

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

If you create Tensorflex.NIF (like we talked about elsewhere), we could move a bunch of this error validation to the Elixir code, which should make the code simpler and more idiomatic. :)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yes, I completely agree. I'm working on doing that next, and should hopefully be done soon. Thanks for the input! :)


FILE* f = fopen(file, "rb");
fseek(f, 0, SEEK_END);
input_size = ftell(f);
fseek(f, 0, SEEK_SET);

input_img = (unsigned char*) malloc(input_size);
fread(input_img, input_size, 1, f);
fclose(f);

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_mem_src(&cinfo, input_img, input_size);
error_check = jpeg_read_header(&cinfo, TRUE);

if (error_check != 1) return enif_make_badarg(env);
jpeg_start_decompress(&cinfo);
width = cinfo.output_width;
height = cinfo.output_height;
num_pixels = cinfo.output_components;
output_size = width * height * num_pixels;
output = (unsigned char*) malloc(output_size);
row_stride = width * num_pixels;

while (cinfo.output_scanline < cinfo.output_height) {
unsigned char *buf[1];
buf[0] = output + (cinfo.output_scanline) * row_stride;
jpeg_read_scanlines(&cinfo, buf, 1);
}

jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
free(input_img);

const int size_alloc = output_size * sizeof(float);
int64_t dims[3] = {width, height, num_pixels};

tensor = TF_NewTensor(TF_UINT8, dims, 3, output, size_alloc, tensor_deallocator, 0);
memcpy((void *) tensor_resource_alloc, (void *) &tensor, sizeof(TF_Tensor *));
ERL_NIF_TERM new_tensor = enif_make_resource(env, tensor_resource_alloc);
enif_release_resource(tensor_resource_alloc);
return enif_make_tuple2(env, enif_make_atom(env,"ok"), new_tensor);

}


static ErlNifFunc nif_funcs[] =
{
Expand All @@ -622,6 +693,7 @@ static ErlNifFunc nif_funcs[] =
{ "float64_tensor_alloc", 1, float64_tensor_alloc },
{ "float32_tensor_alloc", 1, float32_tensor_alloc },
{ "run_session", 5, run_session },
{ "load_image_as_tensor", 1, load_image_as_tensor },
};

ERL_NIF_INIT(Elixir.Tensorflex, nif_funcs, res_loader, NULL, NULL, NULL)
Expand Down
4 changes: 4 additions & 0 deletions lib/tensorflex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ defmodule Tensorflex do
def float64_tensor_alloc(_dims) do
raise "NIF float64_tensor_alloc/1 not implemented"
end

def load_image_as_tensor(_imagepath) do
raise "NIF load_image_as_tensor/1 not implemented"
end

def run_session(_graph, _input_tensor, _output_tensor, _input_opname, _output_opname) do
raise "NIF run_session/5 not implemented"
Expand Down