Skip to content

Commit 13ae17b

Browse files
zhoonitjijoongmoon
authored andcommitted
[Example] Change user scenario
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]>
1 parent 418dc57 commit 13ae17b

File tree

4 files changed

+54
-79
lines changed

4 files changed

+54
-79
lines changed

Applications/Tizen_native/CustomShortcut/inc/data.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@
3030

3131
#define EDJ_PATH "edje/main.edj"
3232
#define TRAIN_SET_PATH "trainingSet.dat"
33+
#define VALIDATION_SET_PATH "validationSet.dat"
3334

34-
#define MAX_TRIES 5
35+
#define MAX_TRAIN_TRIES 5
36+
#define MAX_TRIES 10
3537

36-
typedef enum _DRAW_MODE {
37-
INFER = 0,
38-
TRAIN_SMILE,
39-
TRAIN_SAD,
40-
} DRAW_MODE;
38+
#define FEATURE_SIZE 62720
39+
#define NUM_CLASS 2
4140
typedef struct appdata {
4241
Evas_Object *win;
4342
Evas_Object *conform;
@@ -60,7 +59,6 @@ typedef struct appdata {
6059

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

6664
/**< ML related */

Applications/Tizen_native/CustomShortcut/res/edje/main.edc

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,17 @@ collections {
9494
group {
9595
name: "home";
9696
parts {
97-
PART_TITLE("home/title", "Select actions...")
98-
PART_BUTTON("home/to_train", "train", 0.55, 0.3, 0.9, 0.7)
99-
PART_BUTTON("home/to_test", "test", 0.1, 0.3, 0.45, 0.7)
97+
PART_TITLE("home/title", "NNTrainer Demo")
98+
PART_BUTTON("home/proceed", "start draw", 0.1, 0.3, 0.9, 0.7)
10099
}
101100
programs {
102-
PROGRAM_BUTTON("home/to_train", "routes/to", "select")
103-
PROGRAM_BUTTON("home/to_test", "routes/to", "draw:inference")
101+
PROGRAM_BUTTON("home/proceed", "routes/to", "draw")
104102
}
105103
}
106-
group {
107-
name: "select";
108-
parts {
109-
PART_TITLE("select/title", "Select target emoji...")
110-
PART_BUTTON("select/sad", "😢", 0.1, 0.3, 0.45, 0.7)
111-
PART_BUTTON("select/smile", "😊", 0.55, 0.3, 0.9, 0.7)
112-
}
113-
programs {
114-
PROGRAM_BUTTON("select/smile", "routes/to", "draw:smile")
115-
PROGRAM_BUTTON("select/sad", "routes/to", "draw:sad")
116-
}
117-
}
118-
// this group is meant to be used for train / test, if it is not applicable, this group is reserved for training part
119104
group {
120105
name: "draw";
121106
parts {
122-
PART_TITLE("draw/title", "draw your symbol")
107+
PART_TITLE("draw/title", "draw for 😊")
123108
part {
124109
name: "draw/canvas";
125110
type: SWALLOW;
@@ -143,12 +128,4 @@ collections {
143128
PART_BUTTON("train_result/go_back", "go back", 0.1, 0.3, 0.9, 0.7)
144129
}
145130
}
146-
group {
147-
name: "test_result";
148-
parts {
149-
PART_TITLE("test_result/title", "test is successfully done")
150-
PART_BUTTON("test_result/go_back", "test result: 😊", 0.1, 0.3, 0.9, 0.7)
151-
// reserve a text area to show the guess from the model
152-
}
153-
}
154131
}

Applications/Tizen_native/CustomShortcut/src/data.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,28 +111,40 @@ static void _on_data_receive(ml_tensors_data_h data,
111111
goto CLEAN;
112112
}
113113

114-
file = fopen(ad->pipe_dst, ad->tries == 1 ? "wb" : "wb+");
114+
LOG_I("current tries %d", ad->tries);
115+
116+
if (ad->tries == MAX_TRAIN_TRIES || ad->tries == 0)
117+
file = fopen(ad->pipe_dst, "wb+");
118+
else
119+
file = fopen(ad->pipe_dst, "ab+");
115120

116121
if (file == NULL) {
117122
LOG_E("cannot open file");
118123
goto CLEAN;
119124
}
120125

121-
if (write_result = fwrite(raw_data, data_size, 1, file) < 0) {
126+
if ((write_result = fwrite(raw_data, 1, data_size, file)) < 0) {
122127
LOG_E("write error happend");
123128
}
124129

125130
if (write_result < data_size) {
126-
LOG_E("data was not fully written to file");
131+
LOG_E("data was not fully written to file, result = %d, data_size = %d",
132+
write_result, data_size);
127133
}
128134

129-
if (ad->mode != INFER) {
130-
LOG_D("writing label");
131-
label = ad->mode == TRAIN_SMILE ? 1 : 0;
132-
if (fwrite(&label, sizeof(float), 1, file) < 0) {
133-
LOG_E("write error happend");
134-
};
135-
}
135+
bool target_label = ad->tries % 2;
136+
LOG_D("writing one-hot encoded label");
137+
label = target_label;
138+
if (fwrite(&label, sizeof(float), 1, file) < 0) {
139+
LOG_E("write error happend");
140+
};
141+
142+
label = !target_label;
143+
if (fwrite(&label, sizeof(float), 1, file) < 0) {
144+
LOG_E("write error happend");
145+
};
146+
147+
LOG_I("file dst: %s size: %ld", ad->pipe_dst, ftell(file));
136148

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

246258
void data_train_model() {
247259
ml_train_model_h model;
260+
248261
char model_conf_path[PATH_MAX];
249262
char label_path[PATH_MAX];
250263
int status = ML_ERROR_NONE;
@@ -284,6 +297,8 @@ void data_train_model() {
284297
goto CLEAN_UP;
285298
}
286299

300+
freopen("out.txt", "a+", stdout);
301+
287302
status = ml_train_model_run(model, NULL);
288303
if (status != ML_ERROR_NONE) {
289304
LOG_E("run model failed %d", status);

Applications/Tizen_native/CustomShortcut/src/view.c

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -264,36 +264,29 @@ static void _on_draw_proceed(void *data, Evas_Object *obj, const char *emission,
264264
int status = APP_ERROR_NONE;
265265

266266
char buf[256];
267-
ad->tries++;
268267

269-
if (ad->mode != INFER) {
270-
LOG_D("labeling proceed");
271-
if (ad->tries >= MAX_TRIES) {
272-
ad->tries = 0;
273-
elm_naviframe_item_pop(ad->naviframe);
274-
data_train_model();
275-
view_routes_to(ad, "train_result");
276-
return;
277-
}
278-
279-
sprintf(buf, "draw your symbol [%d/%d]", ad->tries, MAX_TRIES);
280-
elm_object_part_text_set(obj, "draw/title", buf);
281-
LOG_D("starting extraction");
282-
283-
status = data_extract_feature(ad, TRAIN_SET_PATH, true);
284-
if (status != APP_ERROR_NONE) {
285-
LOG_E("feature extraction failed");
286-
}
287-
} else {
288-
LOG_D("infer proceed");
289-
status = data_extract_feature(ad, "test.dat", true);
290-
if (status != APP_ERROR_NONE) {
291-
LOG_E("feature extraction failed");
292-
}
293-
294-
view_routes_to(ad, "test_result");
268+
LOG_D("labeling proceed");
269+
status = data_extract_feature(
270+
ad, ad->tries < MAX_TRAIN_TRIES ? TRAIN_SET_PATH : VALIDATION_SET_PATH,
271+
true);
272+
if (status != APP_ERROR_NONE) {
273+
LOG_E("feature extraction failed");
274+
}
275+
276+
if (ad->tries == MAX_TRIES - 1) {
277+
ad->tries = 0;
278+
elm_naviframe_item_pop(ad->naviframe);
279+
data_train_model();
280+
view_routes_to(ad, "train_result");
281+
return;
295282
}
296283

284+
sprintf(buf, "draw for %s [%d/%d]", ad->tries % NUM_CLASS ? "😊" : "😢",
285+
ad->tries + 2, MAX_TRIES);
286+
elm_object_part_text_set(obj, "draw/title", buf);
287+
LOG_D("starting extraction");
288+
289+
ad->tries++;
297290
_canvas_erase_all(ad);
298291
}
299292

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

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

306-
if (!strcmp(draw_mode, "inference")) {
307-
ad->mode = INFER;
308-
} else if (!strcmp(draw_mode, "smile")) {
309-
ad->mode = TRAIN_SMILE;
310-
} else if (!strcmp(draw_mode, "sad")) {
311-
ad->mode = TRAIN_SAD;
312-
}
313-
314299
status = elm_layout_content_set(ad->layout, "draw/canvas", frame);
315300
if (status == EINA_FALSE) {
316301
LOG_E("failed to get canvas object");

0 commit comments

Comments
 (0)