Skip to content

Commit

Permalink
[Example] Change user scenario
Browse files Browse the repository at this point in the history
In this patch, user draws smile face and sad face respectively for 5
times.

It is splited half to trainset/validationSet and used for training.

**Self evaluation:**
1. Build test: [X]Passed [ ]Failed [ ]Skipped
2. Run test: [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: Jihoon Lee <[email protected]>
  • Loading branch information
zhoonit authored and jijoongmoon committed Aug 10, 2020
1 parent 418dc57 commit 13ae17b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 79 deletions.
12 changes: 5 additions & 7 deletions Applications/Tizen_native/CustomShortcut/inc/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@

#define EDJ_PATH "edje/main.edj"
#define TRAIN_SET_PATH "trainingSet.dat"
#define VALIDATION_SET_PATH "validationSet.dat"

#define MAX_TRIES 5
#define MAX_TRAIN_TRIES 5
#define MAX_TRIES 10

typedef enum _DRAW_MODE {
INFER = 0,
TRAIN_SMILE,
TRAIN_SAD,
} DRAW_MODE;
#define FEATURE_SIZE 62720
#define NUM_CLASS 2
typedef struct appdata {
Evas_Object *win;
Evas_Object *conform;
Expand All @@ -60,7 +59,6 @@ typedef struct appdata {

cairo_surface_t *cr_surface; /**< cairo surface for the canvas */
cairo_t *cr; /**< cairo engine for the canvas */
DRAW_MODE mode; /**< drawing mode of current canvas */
int tries; /**< tells how many data has been labeled */

/**< ML related */
Expand Down
31 changes: 4 additions & 27 deletions Applications/Tizen_native/CustomShortcut/res/edje/main.edc
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,17 @@ collections {
group {
name: "home";
parts {
PART_TITLE("home/title", "Select actions...")
PART_BUTTON("home/to_train", "train", 0.55, 0.3, 0.9, 0.7)
PART_BUTTON("home/to_test", "test", 0.1, 0.3, 0.45, 0.7)
PART_TITLE("home/title", "NNTrainer Demo")
PART_BUTTON("home/proceed", "start draw", 0.1, 0.3, 0.9, 0.7)
}
programs {
PROGRAM_BUTTON("home/to_train", "routes/to", "select")
PROGRAM_BUTTON("home/to_test", "routes/to", "draw:inference")
PROGRAM_BUTTON("home/proceed", "routes/to", "draw")
}
}
group {
name: "select";
parts {
PART_TITLE("select/title", "Select target emoji...")
PART_BUTTON("select/sad", "😢", 0.1, 0.3, 0.45, 0.7)
PART_BUTTON("select/smile", "😊", 0.55, 0.3, 0.9, 0.7)
}
programs {
PROGRAM_BUTTON("select/smile", "routes/to", "draw:smile")
PROGRAM_BUTTON("select/sad", "routes/to", "draw:sad")
}
}
// this group is meant to be used for train / test, if it is not applicable, this group is reserved for training part
group {
name: "draw";
parts {
PART_TITLE("draw/title", "draw your symbol")
PART_TITLE("draw/title", "draw for 😊")
part {
name: "draw/canvas";
type: SWALLOW;
Expand All @@ -143,12 +128,4 @@ collections {
PART_BUTTON("train_result/go_back", "go back", 0.1, 0.3, 0.9, 0.7)
}
}
group {
name: "test_result";
parts {
PART_TITLE("test_result/title", "test is successfully done")
PART_BUTTON("test_result/go_back", "test result: 😊", 0.1, 0.3, 0.9, 0.7)
// reserve a text area to show the guess from the model
}
}
}
35 changes: 25 additions & 10 deletions Applications/Tizen_native/CustomShortcut/src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,40 @@ static void _on_data_receive(ml_tensors_data_h data,
goto CLEAN;
}

file = fopen(ad->pipe_dst, ad->tries == 1 ? "wb" : "wb+");
LOG_I("current tries %d", ad->tries);

if (ad->tries == MAX_TRAIN_TRIES || ad->tries == 0)
file = fopen(ad->pipe_dst, "wb+");
else
file = fopen(ad->pipe_dst, "ab+");

if (file == NULL) {
LOG_E("cannot open file");
goto CLEAN;
}

if (write_result = fwrite(raw_data, data_size, 1, file) < 0) {
if ((write_result = fwrite(raw_data, 1, data_size, file)) < 0) {
LOG_E("write error happend");
}

if (write_result < data_size) {
LOG_E("data was not fully written to file");
LOG_E("data was not fully written to file, result = %d, data_size = %d",
write_result, data_size);
}

if (ad->mode != INFER) {
LOG_D("writing label");
label = ad->mode == TRAIN_SMILE ? 1 : 0;
if (fwrite(&label, sizeof(float), 1, file) < 0) {
LOG_E("write error happend");
};
}
bool target_label = ad->tries % 2;
LOG_D("writing one-hot encoded label");
label = target_label;
if (fwrite(&label, sizeof(float), 1, file) < 0) {
LOG_E("write error happend");
};

label = !target_label;
if (fwrite(&label, sizeof(float), 1, file) < 0) {
LOG_E("write error happend");
};

LOG_I("file dst: %s size: %ld", ad->pipe_dst, ftell(file));

if (fclose(file) < 0) {
LOG_E("there was error closing");
Expand Down Expand Up @@ -245,6 +257,7 @@ int data_extract_feature(appdata_s *ad, const char *dst, bool append) {

void data_train_model() {
ml_train_model_h model;

char model_conf_path[PATH_MAX];
char label_path[PATH_MAX];
int status = ML_ERROR_NONE;
Expand Down Expand Up @@ -284,6 +297,8 @@ void data_train_model() {
goto CLEAN_UP;
}

freopen("out.txt", "a+", stdout);

status = ml_train_model_run(model, NULL);
if (status != ML_ERROR_NONE) {
LOG_E("run model failed %d", status);
Expand Down
55 changes: 20 additions & 35 deletions Applications/Tizen_native/CustomShortcut/src/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,36 +264,29 @@ static void _on_draw_proceed(void *data, Evas_Object *obj, const char *emission,
int status = APP_ERROR_NONE;

char buf[256];
ad->tries++;

if (ad->mode != INFER) {
LOG_D("labeling proceed");
if (ad->tries >= MAX_TRIES) {
ad->tries = 0;
elm_naviframe_item_pop(ad->naviframe);
data_train_model();
view_routes_to(ad, "train_result");
return;
}

sprintf(buf, "draw your symbol [%d/%d]", ad->tries, MAX_TRIES);
elm_object_part_text_set(obj, "draw/title", buf);
LOG_D("starting extraction");

status = data_extract_feature(ad, TRAIN_SET_PATH, true);
if (status != APP_ERROR_NONE) {
LOG_E("feature extraction failed");
}
} else {
LOG_D("infer proceed");
status = data_extract_feature(ad, "test.dat", true);
if (status != APP_ERROR_NONE) {
LOG_E("feature extraction failed");
}

view_routes_to(ad, "test_result");
LOG_D("labeling proceed");
status = data_extract_feature(
ad, ad->tries < MAX_TRAIN_TRIES ? TRAIN_SET_PATH : VALIDATION_SET_PATH,
true);
if (status != APP_ERROR_NONE) {
LOG_E("feature extraction failed");
}

if (ad->tries == MAX_TRIES - 1) {
ad->tries = 0;
elm_naviframe_item_pop(ad->naviframe);
data_train_model();
view_routes_to(ad, "train_result");
return;
}

sprintf(buf, "draw for %s [%d/%d]", ad->tries % NUM_CLASS ? "😊" : "😢",
ad->tries + 2, MAX_TRIES);
elm_object_part_text_set(obj, "draw/title", buf);
LOG_D("starting extraction");

ad->tries++;
_canvas_erase_all(ad);
}

Expand All @@ -303,14 +296,6 @@ static int _create_canvas(appdata_s *ad, const char *draw_mode) {

Evas_Object *frame = elm_layout_add(ad->layout);

if (!strcmp(draw_mode, "inference")) {
ad->mode = INFER;
} else if (!strcmp(draw_mode, "smile")) {
ad->mode = TRAIN_SMILE;
} else if (!strcmp(draw_mode, "sad")) {
ad->mode = TRAIN_SAD;
}

status = elm_layout_content_set(ad->layout, "draw/canvas", frame);
if (status == EINA_FALSE) {
LOG_E("failed to get canvas object");
Expand Down

0 comments on commit 13ae17b

Please sign in to comment.