-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_next.html
512 lines (360 loc) · 15.3 KB
/
train_next.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TensorFlow.js Tutorial</title>
<!-- Import TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0/dist/tf.min.js"></script>
<!-- Import tfjs-vis -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-vis@1.0.2/dist/tfjs-vis.umd.min.js"></script>
<script type="module" id="script_main" src="script_main_ver2.js"></script>
<script type="module" id="data"></script>
<style>
.Btn {
background-color: blue;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
.Btn2 {
background-color: orange;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
</style>
<!-- Import the main script file -->
</head>
<body>
<h1>Machine Learning (ML) Training Tool</h1><br>
The number of images in the data set:
<br><input type="number" STYLE="color: #FFFFFF; background-color: #72A4D2;" id="myText" ><br>
The number of training images in the data set:
<br><input type="number" STYLE="color: #FFFFFF; background-color: #72A4D2;" id="number_of_training_images" ><br>
The number of test images in the data set:
<br><input type="number" STYLE="color: #FFFFFF; background-color: #72A4D2;" id="number_of_test_images" ><br>
The resulting image size in pixels:
<br><input type="number" STYLE="color: #FFFFFF; background-color: #72A4D2;" id="selected_size" >
<br>
The number of classes:
<br><input type="number" STYLE="color: #FFFFFF; background-color: #72A4D2;" id="number_of_classes" >
<br>
number_of_images_in_the_class:
<br><input type="number" id="number_of_images_in_the_class" value=100>
<br>
<p hidden>
Enter the starting number (example: if the first file is "image3.png", enter 3):
<br><input type="number" id="starting_number" value=1>
<br>
Enter the file name (example: enter "img" if your files are named like img1.png, img2.png...):
<br><input type="text" id="file_name" value="combined/image"><br>
Enter the file extension (examples: png, jpg, jpeg):
<br><input type="text" id="file_extension" value="jpg"><br>
Select image files that will be used in training:
<br><input type="file" id="loaded_image_files" onclick="loadImageFiles()" accept=".png, .jpg, .jpeg"
webkitdirectory multiple><br>
<button onclick="display_selected_images()" >Load Selected Image Files</button><br>
<button class="Btn" onclick="myFunction()">Display selected images</button><br>
<button class="Btn" onclick="combine()">Combine images</button><br>
<button class="Btn" onclick="reset()">Reset image selections</button><br>
</p>
Enter the class names seperated with commas (example: classname1,classname2,classname3...):
<br><input type="text" id="class_names" value="class1,class2">
<br>
Select an Image Block File to Load:
<br><input class="Btn" type="file" id="fileToLoad"><br>
<button class="Btn" onclick="loadFileAsText()">Load Selected File</button><br>
Enter <i>batch size</i>:
<br><input type="number" id="batch_size_entered" value="10">
<br>
Enter a value for <i>epochs</i>:
<br><input type="number" id="epochs_value" value="10">
<br>
Provide a name for the model:
<br><input id="model_file_name" value="my-model">
<br>
<button hidden id="finalize_parameters" onclick="finalize_parameters()">finalize parameters</button><br>
<button class="Btn" id="training_started">Train the model</button>
<br>
After completing the steps above, you can try your ML model by clicking below.<br>
<a class="Btn2" href="predict_input_data.html">Try Your Model!</a> <br>
<p hidden>
<br>Last image in the data set:<br>
<canvas class="result" id="output_image" height=60 width=60 ></canvas>
<br>-------------------<br>
<br />
Selected images are displayed below:<br>
<div id="selected_inages_to_be_displayed"></div>
</p>
<br><br>
<i>
Copyright (2021) Erol Ozan <br>
This tool is an open source software licensed under Apache License 2.0.
The code is built on TensorFlow JS platform and modified the code provided by TensorFlow JS. </i>
<script>
const queryString = window.location.search;
//console.log(queryString);
const urlParams = new URLSearchParams(queryString);
//try.htm?number_of_images=180&number_of_classes=3&number_of_training_images=160&selected_size=40
var number_of_images = urlParams.get('number_of_images');
//console.log("number_of_images", number_of_images);
// shirt
var number_of_classes = urlParams.get('number_of_classes');
//console.log("number_of_classes",number_of_classes);
// blue
var number_of_training_images = urlParams.get('number_of_training_images');
//console.log(number_of_training_images);
var number_of_test_images = urlParams.get('number_of_test_images');
var selected_size = urlParams.get('selected_size');
var model_file_name = parseInt(document.getElementById("model_file_name").value);
//console.log(selected_size);
// empty string
//var number_of_images = 180;
document.getElementById("myText").value= number_of_images;
document.getElementById("myText").readOnly=true;
//var number_of_classes = 3;
document.getElementById("number_of_classes").value=number_of_classes;
document.getElementById("number_of_classes").readOnly=true;
//var number_of_training_images = 160;
document.getElementById("number_of_training_images").value=number_of_training_images ;
document.getElementById("number_of_test_images").value=number_of_test_images ;
document.getElementById("number_of_training_images").readOnly=true;
document.getElementById("number_of_test_images").readOnly=true;
//var selected_size = 80;
document.getElementById("selected_size").value=selected_size ;
document.getElementById("selected_size").readOnly=true;
//var starting_number = 1;
starting_number = parseInt(document.getElementById("starting_number").value);
// var epochs_value = 10;
epochs_value = parseInt(document.getElementById("epochs_value").value);
batch_size_entered = parseInt(document.getElementById("batch_size_entered").value);
var file_extension="jpg";
var file_name = "image";
var textFromFileLoaded = "";
var final_array = [];
var image_array = [0,1];
var file_names = ["",""] ;
function finalize_parameters(){
// document.getElementById("script_main").src = "script_main.js";
document.getElementById("data").src = "data_final_ver2.js";
}
//var imageData=[];
//var combined_imageData=[];
//var combined_imageData2=[];
function myFunction(){
number_of_images = parseInt(document.getElementById("myText").value);
selected_size = parseInt(document.getElementById("selected_size").value);
file_name = document.getElementById("file_name").value;
file_extension = document.getElementById("file_extension").value;
starting_number = parseInt(document.getElementById("starting_number").value);
number_of_images_in_the_class= parseInt(document.getElementById("number_of_images_in_the_class").value);
var k;
for (k = starting_number; k < starting_number+number_of_images_in_the_class; k++){
var btn = document.createElement("img");
btn.src = file_name + k +"."+ file_extension;
btn.id=k;
//btn.height = selected_size;
//btn.width = selected_size;
document.body.appendChild(btn);
}
}
function display_selected_images(){
//number_of_images = file_names.length;
selected_size = parseInt(document.getElementById("selected_size").value);
//file_name = document.getElementById("file_name").value;
// file_extension = document.getElementById("file_extension").value;
// starting_number = parseInt(document.getElementById("starting_number").value);
var numm = 0;
var k;
//var upper_limit = number_of_images_in_the_class;
for (k = 0; k < file_names.length; k++){
var btn = document.createElement("img");
var idxDot = file_names[k].lastIndexOf(".") + 1;
var extFile = file_names[k].substr(idxDot, file_names[k].length).toLowerCase();
if (extFile=="jpg" || extFile=="jpeg" || extFile=="png"){
//TO DO
btn.src = file_names[k];
btn.id=k+1;
//btn.height = selected_size;
//btn.width = selected_size;
document.body.appendChild(btn);
numm = numm +1;
}else{
console.log("a non-image file is found in the folder");
//upper_limit = upper_limit+1;
}
console.log ("number of images added =", numm);
}
}
function loadFileAsText()
{
var fileToLoad = document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
//console.log("textFromFileLoaded");
//console.log(textFromFileLoaded);
image_array= textFromFileLoaded.split(",");
console.log(image_array);
};
fileReader.readAsText(fileToLoad, "UTF-8");
}
function loadImageFiles()
{
var filesInput = document.getElementById("loaded_image_files");
//var filesInput = document.getElementById("files");
filesInput.addEventListener("change", function(event) {
var files = event.target.files; //FileList object
//var output = document.getElementById("result");
for (var i = 0; i < files.length; i++) {
var file = files[i];
console.log(file.webkitRelativePath);
file_names[i]=file.webkitRelativePath;
//console.log("file", file_names[i]);
//console.log("file", file);
}
});
/*var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
console.log("textFromFileLoaded");
console.log(textFromFileLoaded);
image_array= textFromFileLoaded.split(",");
console.log(image_array);
};
fileReader.readAsText(fileToLoad, "UTF-8");
*/
}
/*
{
var fileToLoad = document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
// fileReader.readAsText(fileToLoad, "UTF-8");
// fileReader.readAsText(fileToLoad);
textFromFileLoaded = fileLoadedEvent.target.result;
console.log("textFromFileLoaded");
console.log(textFromFileLoaded);
textFromFileLoaded = textFromFileLoaded.replace(/(\r\n|\n|\r)/gm, "");//this eliminates line breaks
//document.getElementById("inputTextToSave").value = textFromFileLoaded;
console.log("textFromFileLoaded");
console.log(textFromFileLoaded);
};
*/
// fileReader.readAsText(fileToLoad);
// image_array= textFromFileLoaded.split(',');
function reset(){
var j;
var limit = document.querySelectorAll("img").length;
console.log("image length)");
console.log(document.querySelectorAll("img").length);
var myobj2 = document.querySelectorAll("img");
for (j = 0; j < limit; j++) {
//document.querySelectorAll("img")[j].remove;
var myobj = myobj2[j];
myobj.remove();
}
}
function combine(){
x = document.getElementById("myText").value;
number_of_images = parseInt(document.getElementById("myText").value);
starting_number = parseInt(document.getElementById("starting_number").value);
number_of_classes = parseInt(document.getElementById("number_of_classes").value);
selected_size = parseInt(document.getElementById("selected_size").value);
number_of_training_images = parseInt(document.getElementById("number_of_training_images").value);
number_of_test_images = parseInt(document.getElementById("number_of_test_images").value);
y = parseInt(document.getElementById("selected_size").value);
selected_size = y;
var i;
//var all_images = document.querySelectorAll("img");
//img.crossOrigin = '';
for (i = 0; i < number_of_images; i++) {
let resEle = document.getElementById("output_image");
var context = resEle.getContext("2d");
//let BtnEle = document.querySelector(".Btn");
//document.getElementById("myText").value;
// resEle.height = selected_size;
// resEle.width = selected_size;
//var image_at_hand = document.querySelectorAll("img")[i];
// context.drawImage(document.querySelectorAll("img")[i],0,0,document.querySelectorAll("img")[i].width,document.querySelectorAll("img")[i].height ,0,0,selected_size,selected_size);
//var num = i+1;
//var id_ = num.toString();
console.log("**********************************");
//var j = parseInt(i +1);
var imogo = document.getElementsByTagName("img")[i];
console.log(imogo.src);
// console.log(document.getElementsByTagName("img")[1].src);
// context.drawImage(document.getElementsByTagName("img")[1],0,0,selected_size,selected_size,0,0,selected_size,selected_size);
// context.drawImage(document.getElementsByTagName("img")[1],0,0,document.getElementsByTagName("img")[1].width,document.getElementsByTagName("img")[1].height,0,0,selected_size,selected_size);
context.drawImage(imogo,0,0,imogo.width,imogo.height,0,0,selected_size,selected_size);
imageData=context.getImageData(0, 0, selected_size, selected_size);
if (i==0){
final_array=Array.from(imageData.data);
}
else{
//console.log(imageData.data);
//console.log(combined_imageData);
//console.log(combined_imageData2.data);
combined_imageData2 = Array.from(imageData.data);
combined_imageData = final_array;
//combined_imageData = combined_imageData.concat(combined_imageData2);
final_array = combined_imageData.concat(combined_imageData2);
//console.log(combined_imageData);
}
console.log("looping");
}
//final_array=combined_imageData;
//image_array = final_array;
image_array = Array.prototype.slice.call(final_array);
//console.log("Array.prototype.slice.call(final_array) success");
console.log(image_array);
//file save function was adapted from https://thiscouldbebetter.neocities.org/texteditor.html
var textToWrite = image_array.toString();
//textToWrite = textToWrite.replace(/(\r\n|\n|\r)/gm, "");//this eliminates line breaks
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
var fileNameToSaveAs = "image_data_file.txt";
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null)
{
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
}
else
{
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
// ctox.drawImage(image_array, 0, 0,100,56);
//console.log(final_array)
}
//console.log(image_array);
//console.log(combined_imageData2.data);
//fruits.push("Kiwi");
</script>
</body>
</html>