olivettigroup / materials-synthesis-generative-models

Public release of data and code for materials synthesis generation
MIT License
70 stars 20 forks source link

Failure to load paragraph classifier model #5

Open lekah opened 3 years ago

lekah commented 3 years ago

Hi, many thanks for open-sourcing code and models, which we want to use to classify a large corpus of papers on synthesis. Could someone could help me out real quick with the following issue? I run

p = paragraph_classifier.ParagraphClassifier() p.load("/home/ubuntu/scratch/models/paragraph_classifier.model")

where the path points to the pre-trained model in https://figshare.com/s/1a07d18ad20008ddd562.

I get the following error message (below). Could the issue be different code version or am I doing something wrong?

Many thanks,

Libraries

Keras==2.4.3 Keras-Preprocessing==1.1.2 tensorflow==2.3.1 tensorflow-cpu==2.3.1 tensorflow-estimator==2.3.0 Python 3.8.6

Traceback

ValueError Traceback (most recent call last)

in ----> 1 p.load("/home/ubuntu/scratch/models/paragraph_classifier.model") ~/git/materials-synthesis-generative-models/models/paragraph_classifier.py in load(self, filename) 105 def load(self, filename='bin/paragraph_classifier.model'): 106 self.model = load_model(filename) --> 107 self.fast_predict = K.function( 108 [self.model.inputs] + [K.learning_phase()], 109 [self.model.layers[-1].output] ~/.virtualenvs/deepmd/lib/python3.8/site-packages/tensorflow/python/keras/backend.py in function(inputs, outputs, updates, name, **kwargs) 3934 from tensorflow.python.keras import models # pylint: disable=g-import-not-at-top 3935 from tensorflow.python.keras.utils import tf_utils # pylint: disable=g-import-not-at-top -> 3936 model = models.Model(inputs=inputs, outputs=outputs) 3937 3938 wrap_outputs = isinstance(outputs, list) and len(outputs) == 1 ~/.virtualenvs/deepmd/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py in __new__(cls, *args, **kwargs) 240 # Functional model 241 from tensorflow.python.keras.engine import functional # pylint: disable=g-import-not-at-top --> 242 return functional.Functional(*args, **kwargs) 243 else: 244 return super(Model, cls).__new__(cls, *args, **kwargs) ~/.virtualenvs/deepmd/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False # pylint: disable=protected-access 456 try: --> 457 result = method(self, *args, **kwargs) 458 finally: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access ~/.virtualenvs/deepmd/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py in __init__(self, inputs, outputs, name, trainable) 113 # 'arguments during initialization. Got an unexpected argument:') 114 super(Functional, self).__init__(name=name, trainable=trainable) --> 115 self._init_graph_network(inputs, outputs) 116 117 @trackable.no_automatic_dependency_tracking ~/.virtualenvs/deepmd/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False # pylint: disable=protected-access 456 try: --> 457 result = method(self, *args, **kwargs) 458 finally: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access ~/.virtualenvs/deepmd/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py in _init_graph_network(self, inputs, outputs) 142 base_layer_utils.create_keras_history(self._nested_outputs) 143 --> 144 self._validate_graph_inputs_and_outputs() 145 146 # A Network does not create weights of its own, thus it is already ~/.virtualenvs/deepmd/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py in _validate_graph_inputs_and_outputs(self) 634 if not hasattr(x, '_keras_history'): 635 cls_name = self.__class__.__name__ --> 636 raise ValueError('Input tensors to a ' + cls_name + ' ' + 637 'must come from `tf.keras.Input`. ' 638 'Received: ' + str(x) + ValueError: Input tensors to a Functional must come from `tf.keras.Input`. Received: 0 (missing previous layer metadata).
eddotman commented 3 years ago

Oh, I think this is a version problem. This code was written before Tensorflow 2 IIRC. You could try rolling back (as there may be other underlying issues even if you fix this one).

If you want to try fixing this -- you can remove the self.fast_predict() stuff. It's a bit of a hack to speed up inference, but I think that in TF2 and Keras 2 you don't actually need it. If you remove that line from the model loading and from when you call predict(), you should be ok (probably -- I haven't tested it).

You may also be able to get more insight from current group members (@zjensen262?) but IDK if anyone is actively maintaining/updating this code.

oriondollar commented 2 years ago

Any follow ups on this? I'm getting a different error when I try to load paragraph_classifier.model with the ParagraphClassifier object. Do y'all know which version of tensorflow/keras this code was written on?

zjensen262 commented 2 years ago

I just added a yml file (https://github.com/olivettigroup/materials-synthesis-generative-models/blob/master/environment.yml) from the conda environment we use. That should give you an idea of what versions of the packages are needed. I tested the paragraph classifier loading with these so let me know if it still doesn't work