janclemenslab / das

Deep Audio Segmenter
http://janclemenslab.org/das/
27 stars 10 forks source link

Preciting error when using Colab model #51

Closed chitayata closed 1 year ago

chitayata commented 2 years ago

Dear Jan,

I am training in Colab using the below code

from numpy import False_
path_to_data = '/content/panthoot.npy'

das.train.train(model_name='tcn_stft',
                data_dir=path_to_data,
                save_dir='/content/drive/MyDrive/panthoot.res',
                save_prefix= '4096_bat32_lr002_PP',
                pre_nb_conv= 16,
                pre_nb_dft= 64,
                nb_hist=4096,
                kernel_size=16,
                nb_filters=32,
                ignore_boundaries=False,
                verbose=2,
                nb_conv=3,
                batch_size= 16,
                learning_rate=0.001,
                reduce_lr=True,
                reduce_lr_patience= 4,
                nb_epoch= 10)

Everything seems to work fine, but then when I try to predict I get the below errors and the function fails. This only happens with colab-trained models, it does not happen with models I have trained in the GUI. Why is this?

Also, the WARNING -"input with incompatible shape (32, 8192, 2)" - always pops up when I am predicting on any model. What is this and is it a problem? Could it be because I trained with mono channel audio but prediction on stereo?

INFO:root:   Loading data from C:/Users/chita/OneDrive/MMNP_Back_UP_AP/Acoustic Sensors_B-J/Site B - Kasiha/test\SITEB-CORE3_20180405_200000.wav.
INFO:root:   Resampling. Audio rate is 24000Hz but model was trained on data with 16000Hz.
INFO:root:   Annotating using model at C:/Users/chita/OneDrive/Desktop/Chimp_vocalizations/Dataset_ChimpNetwork/Colab/8192_bat32_lr002_PP_20221014_183516.
  0%|                                                                                           | 0/41 [00:00<?, ?it/s]WARNING:tensorflow:Model was constructed with shape (None, 8192, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 8192, 1), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (32, 8192, 2).
WARNING:tensorflow:Model was constructed with shape (None, 8192, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 8192, 1), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (32, 8192, 2).
  0%|                                                                                           | 0/41 [00:00<?, ?it/s]
ERROR:root:Error processing file C:/Users/chita/OneDrive/MMNP_Back_UP_AP/Acoustic Sensors_B-J/Site B - Kasiha/test\SITEB-CORE3_20180405_200000.wav.
Traceback (most recent call last):
  File "C:\Users\chita\miniconda3\envs\das\lib\site-packages\das\predict.py", line 618, in cli_predict
    events, segments, class_probabilities, class_names = predict(x,
  File "C:\Users\chita\miniconda3\envs\das\lib\site-packages\das\predict.py", line 492, in predict
    class_probabilities = predict_probabilities(x, model, params, verbose, prepend_data_padding)
  File "C:\Users\chita\miniconda3\envs\das\lib\site-packages\das\predict.py", line 52, in predict_probabilities
    y_pred_batch = model.predict_on_batch(batch_data)  # run the network
  File "C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1945, in predict_on_batch
    outputs = self.predict_function(iterator)
  File "C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\eager\def_function.py", line 885, in __call__
    result = self._call(*args, **kwds)
  File "C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\eager\def_function.py", line 924, in _call
    results = self._stateful_fn(*args, **kwds)
  File "C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\eager\function.py", line 3038, in __call__
    filtered_flat_args) = self._maybe_define_function(args, kwargs)
  File "C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\eager\function.py", line 3459, in _maybe_define_function
    return self._define_function_with_shape_relaxation(
  File "C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\eager\function.py", line 3381, in _define_function_with_shape_relaxation
    graph_function = self._create_graph_function(
  File "C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\eager\function.py", line 3298, in _create_graph_function
    func_graph_module.func_graph_from_py_func(
  File "C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1007, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\eager\def_function.py", line 668, in wrapped_fn
    out = weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\framework\func_graph.py", line 994, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\keras\engine\training.py:1584 predict_function  *
        return step_function(self, iterator)
    C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\keras\engine\training.py:1574 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1286 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2849 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3632 _call_for_each_replica
        return fn(*args, **kwargs)
    C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\keras\engine\training.py:1567 run_step  **
        outputs = model.predict_step(data)
    C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\keras\engine\training.py:1540 predict_step
        return self(x, training=False)
    C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1057 __call__
        outputs = call_fn(inputs, *args, **kwargs)
    C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\keras\engine\functional.py:420 call
        return self._run_internal_graph(
    C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\keras\engine\functional.py:556 _run_internal_graph
        outputs = node.layer(*args, **kwargs)
    C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1040 __call__
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    C:\Users\chita\miniconda3\envs\das\lib\site-packages\tensorflow\python\keras\engine\input_spec.py:251 assert_input_compatibility
        raise ValueError(

    ValueError: Input 0 of layer conv1d is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape (32, 8192, 2)
postpop commented 2 years ago

Hi,

yes, this fails because the model was trained with stereo inputs. Not sure why this happens only on colab - maybe the GUI version is older that the one used on colab.

I'll think about how to fix this automatically - but for now you'll have to convert your files to stereo. You can do this with ffmpeg ( [https://trac.ffmpeg.org/wiki/AudioChannelManipulation#monostereo]()) - this will simplicate duplicate the mono signal on the left and the right stereo channels: ffmpeg -i input.wav -ac 2 output.wav.

chitayata commented 2 years ago

Great, thank you Jan. The opposite was true, I trained with mono but was trying to predict on the stereo, but I was able to convert the files and everything is working now! Versions of das were the same in colab and GUI, so I still don't know it only failed with Colab.