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

Export ONNX #331

Open
vorodrigues opened this issue Oct 7, 2020 · 5 comments
Open

Export ONNX #331

vorodrigues opened this issue Oct 7, 2020 · 5 comments

Comments

@vorodrigues
Copy link

vorodrigues commented Oct 7, 2020

Hi,

When you try to export a model in ONNX format, the deploy method expects the InputLayer parameters height and n_channels not to be None, even though they are meant to be used only with image data.

It would be nice not to have to specify dummy values for them when training RNNs and DNNs for example.

Best regards,
Victor

@dxq77dxq
Copy link
Collaborator

dxq77dxq commented Oct 7, 2020

Hi Victor,

Thanks for bringing this topic up. width, height and n_channels are only needed for CNN models. If the model is RNN (meaning there is at least one Recurrent layer) then these parameters can be None.

Thanks,
Maggie

@dxq77dxq
Copy link
Collaborator

Hi Victor,

Please let me know if the answer solves the question and we can close the issue.

Thanks,
Maggie

@vorodrigues
Copy link
Author

vorodrigues commented Oct 14, 2020

Hi Maggie,

Thanks a lot for your answer! I raised this issue because I trained a RNN for a sentiment analysis task and then tried to export it in ONNX format:

model1.deploy(path='/models', output_format='onnx')

And got the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-6a07ec717e1a> in <module>
----> 1 model1.deploy(path='/models', output_format='onnx')

~/.local/lib/python3.7/site-packages/dlpy/network.py in deploy(self, path, output_format, model_weights, layers, **kwargs)
   1727             self.save_to_table(path=path)
   1728         elif output_format.lower() == 'onnx':
-> 1729             self.save_to_onnx(path, model_weights=model_weights)
   1730         else:
   1731             raise DLPyError('output_format must be "astore", "castable", "table",'

~/.local/lib/python3.7/site-packages/dlpy/network.py in save_to_onnx(self, path, model_weights)
   1664         onnx_model = sas_to_onnx(layers=self.layers,
   1665                                  model_table=model_table,
-> 1666                                  model_weights=model_weights)
   1667         file_name = self.model_name + '.onnx'
   1668         if path is None:

~/.local/lib/python3.7/site-packages/dlpy/model_conversion/write_onnx_model.py in sas_to_onnx(layers, model_table, model_weights)
     69     for layer in layers:
     70         if layer.type == 'input':
---> 71             H = int(layer.config['height'])
     72             W = int(layer.config['width'])
     73             C = int(layer.config['n_channels'])

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

I managed to export it by adding a dummy value to the height and n_channels parameters in the input layer:

model1.add(InputLayer(std='STD', height=0, n_channels=1))

I think that sas_to_onnx code might need some fix to avoid that.

Thanks,
Victor

@eusdougc
Copy link
Contributor

Hi Victor - we don't officially support exporting an RNN model to ONNX format. With your work-around, aren't you getting an OnnxWriteError exception for your RNN layers? The error message should be something like

<layer_type> is not supported.

Doug

@vorodrigues
Copy link
Author

Hi Doug,

You are right! I'm sorry, I made many tests to find a workaround and I forgot to mention it worked with a simple DNN - NOT with the original RNN.

Here it is the code I ran:

model1 = Sequential(sas, model_table=name)
model1.add(InputLayer(std='STD', height=0, n_channels=1))
model1.add(Dense(20, act='relu'))
model1.add(Dense(20, act='relu'))
model1.add(OutputLayer(act='softmax', n=2, error='entropy'))

model1.fit(data=bank[bank['_PartInd_']==1], 
           valid_table=bank[bank['_PartInd_']==2],
           inputs=["duration","campaign","pdays","previous","age","emp_var_rate",
                   "cons_price_idx","cons_conf_idx","euribor3m","nr_employed"],
           target='y',
           nominals='y',
           optimizer=Optimizer(AdamSolver(learning_rate=0.001),
                              mini_batch_size=64,
                              max_epochs=20,
                              log_level=2),
           train_from_scratch=True,
           save_best_weights=True)

model1.deploy(path='/home/sasdemo/poc', output_format='onnx')

Anyways, it's nice to know RNNs aren't supported yet.

Many thanks,
Victor

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

No branches or pull requests

3 participants