nekitmm / DLPacker

DLPacker
MIT License
28 stars 20 forks source link

incompatible with tf 2.16.0 #13

Closed YaoYinYing closed 5 months ago

YaoYinYing commented 8 months ago

Hi, I just got an error from my daily test, raised via a new tf version with Keras3.

In brief

Since we have pinned tensorflow as ^2.5.0, newly installed environment will always require the latest version, which does not allow this operation.

Detailed error message

  File "/home/runner/miniconda3/envs/REvoDesign/lib/python3.11/site-packages/joblib/parallel.py", line 589, in <listcomp>
    return [func(*args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/REvoDesign/REvoDesign/REvoDesign/common/MutantVisualizer.py", line 93, in process_mutant
    temp_session_path = self.create_mutagenesis_objects(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/REvoDesign/REvoDesign/REvoDesign/common/MutantVisualizer.py", line 136, in create_mutagenesis_objects
    temp_mutant_pdb_path = self.mutate_runner.run_mutate(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/REvoDesign/REvoDesign/REvoDesign/sidechain_solver/DLPacker.py", line 89, in run_mutate
    self.dlpacker_worker = DLPacker(str_pdb=self.pdb_file)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/miniconda3/envs/REvoDesign/lib/python3.11/site-packages/DLPacker/dlpacker.py", line 124, in __init__
    self.model = DLPModel(width=128, nres=6)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/miniconda3/envs/REvoDesign/lib/python3.11/site-packages/DLPacker/utils.py", line 203, in __init__
    self.model = self.model()
                 ^^^^^^^^^^^^
  File "/home/runner/miniconda3/envs/REvoDesign/lib/python3.11/site-packages/DLPacker/utils.py", line 320, in model
    fc = tf.reshape(
         ^^^^^^^^^^^
  File "/home/runner/miniconda3/envs/REvoDesign/lib/python3.11/site-packages/tensorflow/python/ops/weak_tensor_ops.py", line 88, in wrapper
    return op(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/runner/miniconda3/envs/REvoDesign/lib/python3.11/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/runner/miniconda3/envs/REvoDesign/lib/python3.11/site-packages/keras/src/backend/common/keras_tensor.py", line 92, in __tf_tensor__
    raise ValueError(
ValueError: A KerasTensor cannot be used as input to a TensorFlow function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces `keras.layers` and `keras.operations`). You are likely doing something like:

  \```
  x = Input(...)
  ...
  tf_fn(x)  # Invalid.
  \```

What you should do instead is wrap `tf_fn` in a layer:

  \```
  class MyLayer(Layer):
      def call(self, x):
          return tf_fn(x)

  x = MyLayer()(x)
  \```

Affected code

https://github.com/nekitmm/DLPacker/blob/cb79d2933d2e6822edfefc20313e0beecfe05b46/dlpacker/utils.py#L357

Possible fix

my fix attempt here is use fc = K.ops.reshape(fc, (-1, self.grid_size, self.grid_size, self.grid_size, 1)) instead, and it works from py39-311.

Update:

tf 2.15.0 vs K.ops.reshape(macos with m1 chips):

  File "/Users/yyy/Documents/protein_design/REvoDesign/REvoDesign/common/MutantVisualizer.py", line 136, in create_mutagenesis_objects
    temp_mutant_pdb_path = self.mutate_runner.run_mutate(
  File "/Users/yyy/Documents/protein_design/REvoDesign/REvoDesign/sidechain_solver/DLPacker.py", line 89, in run_mutate
    self.dlpacker_worker = DLPacker(str_pdb=self.pdb_file)
  File "/Users/yyy/miniconda_py39_arm64/lib/python3.10/site-packages/DLPacker/dlpacker.py", line 124, in __init__
    self.model = DLPModel(width=128, nres=6)
  File "/Users/yyy/miniconda_py39_arm64/lib/python3.10/site-packages/DLPacker/utils.py", line 204, in __init__
    self.model = self.model()
  File "/Users/yyy/miniconda_py39_arm64/lib/python3.10/site-packages/DLPacker/utils.py", line 323, in model
    fc = K.ops.reshape(fc, (-1, self.grid_size, self.grid_size, self.grid_size, 1))
AttributeError: module 'tensorflow.keras' has no attribute 'ops'

Update -2

currently tensorflow-macos does not have >=2.16.0 and pip may complain if we install tensorflow-macos after tensorflow >=2.16.0 and keras >=3. bui it is total fine.

so i think we have two options for this:

Useful links

https://keras.io/guides/migrating_to_keras_3/ https://keras.io/api/ops/numpy/#reshape-function

nekitmm commented 5 months ago

Sorry for delay and thanks for looking into it. Fixing the TF version to be <2.16 here #16

YaoYinYing commented 5 months ago

Nice, thanks for your fix!