Skip to content

Commit c2ef0ba

Browse files
authored
Merge pull request #36 from anshuman23/dev
Fixed bugs in C code
2 parents 6d88572 + 9553847 commit c2ef0ba

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

Diff for: c_src/Tensorflex.c

+26-17
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ typedef union
2626

2727
static int get_number(ErlNifEnv* env, ERL_NIF_TERM term, double* dp);
2828
static Matrix* alloc_matrix(ErlNifEnv* env, unsigned nrows, unsigned ncols);
29-
static Matrix* realloc_matrix(ErlNifEnv* env, Matrix* mx, unsigned nrows, unsigned ncols);
3029
static void matrix_destr(ErlNifEnv* env, void* obj);
3130

3231
static ErlNifResourceType* resource_type = NULL;
@@ -193,25 +192,32 @@ static ERL_NIF_TERM matrix_to_lists(ErlNifEnv* env, int argc, const ERL_NIF_TERM
193192

194193
static ERL_NIF_TERM append_to_matrix(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
195194
{
196-
mx_t mx;
197-
unsigned j;
195+
Matrix* mx_ret = NULL;
196+
unsigned j, it_i, it_j;
198197
unsigned nrows, ncols;
199198
ERL_NIF_TERM list, row, ret;
199+
mx_t mx;
200200

201201
if (!enif_get_resource(env, argv[0], resource_type, &mx.vp)) return enif_make_badarg(env);
202202

203203
nrows = mx.p->nrows;
204204
ncols = mx.p->ncols;
205205

206-
mx.p = realloc_matrix(env, mx.p, nrows+1, ncols);
206+
mx_ret = alloc_matrix(env, nrows+1, ncols);
207+
for(it_i = 0; it_i < nrows; it_i++) {
208+
for(it_j = 0; it_j < ncols; it_j++){
209+
POS(mx_ret, it_i, it_j) = POS(mx.p, it_i, it_j);
210+
}
211+
}
212+
207213
list = argv[1];
208214
if (!enif_get_list_cell(env, list, &row, &list)) {
209215
goto badarg;
210216
}
211217
for (j = 0; j<ncols; j++) {
212218
ERL_NIF_TERM v;
213219
if (!enif_get_list_cell(env, row, &v, &row) ||
214-
!get_number(env, v, &POS(mx.p,nrows,j))) {
220+
!get_number(env, v, &POS(mx_ret,nrows,j))) {
215221
goto badarg;
216222
}
217223
}
@@ -223,8 +229,8 @@ static ERL_NIF_TERM append_to_matrix(ErlNifEnv* env, int argc, const ERL_NIF_TER
223229
goto badarg;
224230
}
225231

226-
ret = enif_make_resource(env, mx.p);
227-
enif_release_resource(mx.p);
232+
ret = enif_make_resource(env, mx_ret);
233+
enif_release_resource(mx_ret);
228234
return ret;
229235

230236
badarg:
@@ -250,14 +256,6 @@ static Matrix* alloc_matrix(ErlNifEnv* env, unsigned nrows, unsigned ncols)
250256
return mx;
251257
}
252258

253-
static Matrix* realloc_matrix(ErlNifEnv* env, Matrix* mx, unsigned nrows, unsigned ncols)
254-
{
255-
mx->nrows = nrows;
256-
mx->ncols = ncols;
257-
mx->data = enif_realloc(mx->data, nrows*ncols*sizeof(double));
258-
return mx;
259-
}
260-
261259
static void matrix_destr(ErlNifEnv* env, void* obj)
262260
{
263261
Matrix* mx = (Matrix*) obj;
@@ -495,8 +493,14 @@ static ERL_NIF_TERM float64_tensor(ErlNifEnv *env, int argc, const ERL_NIF_TERM
495493
}
496494
}
497495

498-
tensor = TF_NewTensor(TF_DOUBLE, dims, ndims, mx1.p->data, (size_alloc) * sizeof(double), tensor_deallocator, 0);
496+
double *data = enif_alloc((mx1.p->nrows)*(mx1.p->ncols)*sizeof(double));
497+
for (i = 0; i < mx1.p->nrows; i++) {
498+
for (j = 0; j < mx1.p->ncols; j++) {
499+
data[(i)*(mx1.p->ncols) + (j)] = (double) POS(mx1.p, i, j);
500+
}
501+
}
499502

503+
tensor = TF_NewTensor(TF_DOUBLE, dims, ndims, data, (size_alloc) * sizeof(double), tensor_deallocator, 0);
500504
}
501505

502506
memcpy((void *) tensor_resource_alloc, (void *) &tensor, sizeof(TF_Tensor *));
@@ -775,7 +779,12 @@ static ERL_NIF_TERM load_image_as_tensor(ErlNifEnv *env, int argc, const ERL_NIF
775779
const int size_alloc = output_size * sizeof(unsigned char);
776780
int64_t dims[3] = {width, height, num_pixels};
777781

778-
tensor = TF_NewTensor(TF_UINT8, dims, 3, output, size_alloc, tensor_deallocator, 0);
782+
uint8_t *data = enif_alloc(output_size*sizeof(uint8_t));
783+
for(int it = 0; it < output_size; it++){
784+
data[it] = (uint8_t)output[it];
785+
}
786+
787+
tensor = TF_NewTensor(TF_UINT8, dims, 3, data, size_alloc, tensor_deallocator, 0);
779788
memcpy((void *) tensor_resource_alloc, (void *) &tensor, sizeof(TF_Tensor *));
780789
ERL_NIF_TERM new_tensor = enif_make_resource(env, tensor_resource_alloc);
781790
enif_release_resource(tensor_resource_alloc);

Diff for: test/cropped_panda.jpg

2.62 KB
Loading

Diff for: test/tensorflex_test.exs

+11
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ defmodule TensorflexTest do
188188
_m = Tensorflex.load_csv_as_matrix("./test/sample1.csv", header: :no_header, delimiter: ",")
189189
end
190190
end
191+
192+
test "image-to-tensor loading function check" do
193+
{:ok, img_tensor} = Tensorflex.load_image_as_tensor("./test/cropped_panda.jpg")
194+
{:ok, :tf_uint8} = Tensorflex.tensor_datatype img_tensor
195+
end
196+
197+
test "image-to-tensor function incorrect usage check" do
198+
assert_raise ArgumentError, fn ->
199+
{:ok, _tensor} = Tensorflex.load_image_as_tensor("./test/sample1.csv")
200+
end
201+
end
191202
end
192203

193204
end

0 commit comments

Comments
 (0)