delvendahl / miniML

A deep learning framework for synaptic event detection
MIT License
7 stars 3 forks source link

TypeError when Initializing Event Detection Object #5

Closed VonEconomo closed 4 months ago

VonEconomo commented 4 months ago

I am currently following your tutorial and trying it out on one of my recordings. I successfully loaded the .abf file, but when I try to initialize the EventDetection object, I get the following error:

TypeError: Could not locate class 'LSTM'. Make sure custom classes are decorated with `@keras.saving.register_keras_serializable()`. Full object config: {'class_name': 'LSTM', 'config': {'name': 'lstm', 'trainable': True, 'dtype': 'float32', 'return_sequences': False, 'return_state': False, 'go_backwards': False, 'stateful': False, 'unroll': False, 'time_major': False, 'units': 96, 'activation': 'tanh', 'recurrent_activation': 'sigmoid', 'use_bias': True, 'kernel_initializer': {'class_name': 'GlorotUniform', 'config': {'seed': None}}, 'recurrent_initializer': {'class_name': 'Orthogonal', 'config': {'gain': 1.0, 'seed': None}}, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'unit_forget_bias': True, 'kernel_regularizer': None, 'recurrent_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'recurrent_constraint': None, 'bias_constraint': None, 'dropout': 0.2, 'recurrent_dropout': 0.0, 'implementation': 2}

Here is the code I am using:

import sys
sys.path.append('./core/')
from miniML import MiniTrace, EventDetection

filepath = '/Users/ss/Desktop/50-59_PhD/53_Data_analyis/peter test/abf_files/24510005.abf'
scaling = 1e12
unit = 'pA'

# Load data from .abf file
trace = MiniTrace.from_axon_file(filepath=filepath, scaling=scaling, unit=unit)

win_size = 600
stride = int(win_size / 30)
direction = 'negative'

detection = EventDetection(
    data=trace,
    model_path='/Users/ss/Desktop/50-59_PhD/53_Data_analyis/miniML/model/GC_lstm_model.h5',
    window_size=win_size,
    model_threshold=0.5,
    batch_size=512,
    event_direction=direction,
    compile_model=True
)

Could you please help me understand what might be causing this error and how to resolve it?

Thank you!

VonEconomo commented 4 months ago

Does the provided model work with abf files?

delvendahl commented 4 months ago

Yes, the models work with data irrespective of the original file type.

The TypeError you encountered is most likely due to different Tensorflow version being used for model training and for inference (on your device). Could you please check and report your Tensorflow version?

import tensorflow as tf
print(tf.__version__)
VonEconomo commented 4 months ago

Thank you for the fast reply!

Python 3.11.3 (main, May 15 2023, 18:01:31) [Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> print(tf.__version__)
2.16.1
delvendahl commented 4 months ago

This seems to be a backward compatibility problem in the Tensorflow package. You can try using an earlier Tensorflow version (2.14.0 or 2.15.0 should work):

pip install tensorflow==2.15.0 --force-reinstall

Please let me know if this solves the issue.

VonEconomo commented 4 months ago

I reinstalled everything on python 3.9 and made sure to install tensorflow 2.14.0 this solved the issue for me. Thanks alot for the great work!