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

Plugin import keras #120

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open

Plugin import keras #120

wants to merge 28 commits into from

Conversation

umesh-timalsina
Copy link
Contributor

@umesh-timalsina umesh-timalsina commented Oct 22, 2019

To address #12,
A new plugin called ImportKeras is added that takes in a keras model as a JSON file and tires to convert it to a deepforge resource.

Files Changed:

Copy link
Contributor

@brollb brollb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! It looks much better. I left a couple minor comments to be addressed.

.gitattributes Outdated Show resolved Hide resolved
src/plugins/ImportKeras/ImportKeras.js Outdated Show resolved Hide resolved
src/plugins/ImportKeras/ImportKeras.js Outdated Show resolved Hide resolved
src/plugins/ImportKeras/ImportKeras.js Outdated Show resolved Hide resolved
test/plugins/ImportKeras/ImportKeras.spec.js Outdated Show resolved Hide resolved
@brollb
Copy link
Contributor

brollb commented Oct 23, 2019

Actually, upon testing it a bit further, I found a couple bugs around parsing and setting attributes. Essentially, I imported the redshiftModel.json file and then generated code to ensure that it was valid keras and encountered the following error:

NameError: name 'false' not defined

Comment on lines 195 to 206
ImportKeras.prototype._toPythonIterable = function (obj) {
if (obj == null) {
return 'None';
}
if (obj instanceof Array) {
return '[' + obj.map((val) => {
return this._toPythonIterable(val);
}).join(', ') + ']';
} else {
return obj;
}
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, upon testing it a bit further, I found a couple bugs around parsing and setting attributes. Essentially, I imported the redshiftModel.json file and then generated code to ensure that it was valid keras and encountered the following error:

NameError: name 'false' not defined

I think I know the cause of the problem you alluded to, booleans start with capital case in python which I had not considered here. Refactoring this to the following should do the job.

// This method is used to convert javascript arrays/booleans to a
    // list(python)/boolean in string Representation. Needed for
    // Code generation.
    ImportKeras.prototype._toPythonType = function (obj) {
        if (obj == null) {
            return 'None';
        }
        if (obj instanceof Array) {
            return '[' + obj.map((val) => {
                return this._toPythonType(val);
            }).join(', ') + ']';
        } else if (typeof obj === 'boolean') {
            return obj ? 'True' : 'False';
        } else {
            return obj;
        }
    };

Copy link
Contributor Author

@umesh-timalsina umesh-timalsina Oct 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a minor side note, it might make sense to test GenerateKeras on this plugin as a test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I think it is also expecting tuples ((1,2,3)) rather than arrays which also caused an error after fixing the booleans. However, this is something that might be better to fix in GenerateKeras (https://github.com/deepforge-dev/deepforge-keras/blob/master/src/plugins/GenerateKeras/GenerateKeras.js#L416)

@umesh-timalsina
Copy link
Contributor Author

Up until now, the plugin is ready. However as #130 points out, additional tests needs to be written inorder merge this PR. For this, #128, #126 and their respective PRs (#129, #131) are merged to this branch inorder to resolve #130 .

@umesh-timalsina
Copy link
Contributor Author

image
Running GenerateKeras while implementing tests for #130 gave rise to the following error. Should be fixed in ImportKeras. Some required attributes for code generation are not being set.

@umesh-timalsina
Copy link
Contributor Author

umesh-timalsina commented Nov 7, 2019

The problem with #120 (comment) was that the layer pointers were not being correctly set. Also, some metanodes in deepforge-keras are not case consistent with their peer classes in keras. This should be investigated and resolved by having a class map that maps the case from keras class_name case to correct case for deepforge-keras metanode name. For details see this.

@umesh-timalsina
Copy link
Contributor Author

umesh-timalsina commented Nov 7, 2019

This adds the test to run code generated from the imported architectures. However, the Dropout layers have problem in their input and training connections not being assigned correctly. So, the current test only takes a model without Dropout layers.

@brollb
Copy link
Contributor

brollb commented Nov 13, 2019

I am just seeing the comment about case-consistency now. I actually wouldn't consider it an inconsistency with the metamodel; layers are often aliased in keras and deepforge-keras simply creates one and maps all the aliases to the created one (the aliases are recorded in the json schemas). Rather than hard coding the aliases again, it would be better to resolve them using the schemas.

That said, there is an issue around this as it appears that the aliases are being picked up as the actual regularizers and the actual names are being ignored (schema vs source).

@brollb
Copy link
Contributor

brollb commented Nov 14, 2019

So the bug with the dropout layers is due to not checking what input/output to use when connecting the layers. Currently, the first port is selected (from an unordered set). I added a helper method which returns the ordered set of inputs/outputs (the ports can be ordered using the index member attribute).

However, are there any cases where we will want to connect the layers using some other input? It is possible to connect layers to other inputs but it doesn't seem like this is addressed in the code.

That said, it is coming along! Nice work adding the integration testing capabilities - I am happy they have helped us catch these sorts of issues early!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants