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

fix duplicate model name #327

Merged
merged 2 commits into from
Apr 6, 2020
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
36 changes: 16 additions & 20 deletions apps/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var csvContent;
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
// id(autoinc), name, location(name+id), classes
var request; var db;
var namearray = [];
var modelName;

// tensorflowjs creates its own IndexedDB on saving a model.
(async function(callback) {
Expand Down Expand Up @@ -158,6 +158,7 @@ async function initUIcomponents() {
// create the message queue
$UI.message = new MessageQueue();
const dropDownList = [];
modelName = [];
Object.keys(await tf.io.listModels()).forEach(function(element) {
const dict = {};
const value = element.split('/').pop();
Expand All @@ -167,6 +168,8 @@ async function initUIcomponents() {
dict.title = title;
dict.value = value;
dict.checked = false;
// Saving to previously defined model names
modelName.push(dict['title']);
dropDownList.push(dict);
}
});
Expand Down Expand Up @@ -600,14 +603,6 @@ function runPredict(key) {
}
}

// Function to check if model name is repeated or not.
function checkNameDuplicate(name) {
if (namearray.indexOf(name)!=-1) {
return 1;
}
return 0;
}


// TO-DO: Allow uploading and using tensorflow graph models. Can't save graph models. Need to use right away.
function uploadModel() {
Expand Down Expand Up @@ -664,6 +659,18 @@ function uploadModel() {
var modelInput = tf.io.browserFiles([topology.files[0], ...weights.files]);
}

// Check if model with same name is previously defined
try {
if (modelName.indexOf(_name.value)!=-1) {
throw new Error('Model name repeated');
}
} catch (e) {
status.innerHTML = 'Model with the same name already exists. Please choose a new name';
status.classList.remove('blink');
console.log(e);
return;
}

try {
// This also ensures that valid model is uploaded.
const model = await tf.loadLayersModel(modelInput);
Expand All @@ -679,17 +686,6 @@ function uploadModel() {
return;
}

try {
if (checkNameDuplicate(_name.value)) {
throw new Error('Model name repeated');
}
} catch (e) {
status.innerHTML = 'Model with the same name already exists. Please choose a new name';
status.classList.remove('blink');
return;
}
namearray.push(_name.value);

await model.save(IDB_URL + name);

// Update the model store db entry to have the classes array
Expand Down
16 changes: 16 additions & 0 deletions apps/segment/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const IDB_URL = 'indexeddb://';
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
// id(autoinc), name, location(name+id), classes
var request; var db;
var modelName;

// tensorflowjs creates its own IndexedDB on saving a model.
async function dbInit() {
Expand Down Expand Up @@ -144,6 +145,7 @@ async function initUIcomponents() {
checked: true,
}];

modelName = [];
Object.keys(await tf.io.listModels()).forEach(function(element) {
const dict = {};
const value = element.split('/').pop();
Expand All @@ -153,6 +155,8 @@ async function initUIcomponents() {
dict.title = title;
dict.value = value;
dict.checked = false;
// Saving to previously defined model names
modelName.push(dict['title']);
dropDownList.push(dict);
}
});
Expand Down Expand Up @@ -584,6 +588,18 @@ function uploadModel() {
var modelInput = tf.io.browserFiles([topology.files[0], ...weights.files]);
}

// Check if model with same name is previously defined
try {
if (modelName.indexOf(_name.value)!=-1) {
throw new Error('Model name repeated');
}
} catch (e) {
status.innerHTML = 'Model with the same name already exists. Please choose a new name';
status.classList.remove('blink');
console.log(e);
return;
}

try {
// This also ensures that valid model is uploaded.
// Adding some extra digits in the end to maintain uniqueness
Expand Down