I’ve literally been pulling my hair out for the last 2 days trying to fix this. I have made a file test.py
to test my model by predicting a single sample:
import os
import keras
import numpy as np
import tensorflow as tf
from main_copy import path_to_fft
model = keras.models.load_model(os.path.join(os.getcwd(), "model.keras"))
model.summary()
model.summary(expand_nested=True)
first_sample = path_to_fft(os.path.join(os.getcwd(), 'Sounds', 'Thomas', 'thomas_original.wav'))
second_sample = path_to_fft(os.path.join(os.getcwd(), 'Sounds', 'Thomas', 'thomas_original.wav'))
print("First: ", first_sample.shape)
print("Second: ", second_sample.shape)
prediction = model.predict([first_sample, second_sample])
print(prediction)
The path_to_fft
function decodes a .wav file and applies a Fast Fourier Transform, then returns the first half of the positive frequencies of that transform, transforming the 5 second 16kHz audio from shape (80000, 1) to (40000, 1), which is the correct size for my model. However, when I try to predict the distance between two samples using model.predict
, I get this error message:
Model: "functional"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃ Connected to ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ InputA (InputLayer) │ (None, 40000, 1) │ 0 │ - │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ InputB (InputLayer) │ (None, 40000, 1) │ 0 │ - │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ SiameseBranch (Functional) │ (None, 32) │ 14,003,824 │ InputA[0][0], InputB[0][0] │
├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤
│ lambda (Lambda) │ (None, 1) │ 0 │ SiameseBranch[0][0], │
│ │ │ │ SiameseBranch[1][0] │
└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘
Input 0 of layer "SiameseBranch" is incompatible with the layer: expected shape=(None, 40000, 1), found shape=(32, 1)
Arguments received by Functional.call():
• inputs=('tf.Tensor(shape=(32, 1), dtype=float32)', 'tf.Tensor(shape=(32, 1), dtype=float32)')
• training=False
• mask=('None', 'None')
When I scroll up, I see that three (?????) arguments are generated by Tensorflow?:
Level 1:tensorflow:Creating new FuncGraph for Python function <function StructuredFunctionWrapper.__init__.<locals>.trace_tf_function.<locals>.wrapped_fn at 0x0000027A795AE480> (key: FunctionContext(context=EagerContext(parent_graph=None, device_functions=(), colocation_stack=(), in_cross_replica_context=False, variable_policy=None, xla_context_id=0), scope_type=<ScopeType.VARIABLE_CREATION: 2>), Input Parameters:
args_0 (POSITIONAL_ONLY): TensorSpec(shape=(32,), dtype=tf.int64, name=None)
args_1 (POSITIONAL_ONLY): TensorSpec(shape=(40000, 1), dtype=tf.float32, name=None)
args_2 (POSITIONAL_ONLY): TensorSpec(shape=(40000, 1), dtype=tf.float32, name=None)
Output Type:
None
Captures:
None)
I have absolutely zero idea what to do or make of these error logs. This is my first time using models that have been saved as files and it’s an absolute nightmare.
For reference, here is link to the GitHub repository (containing the path_to_fft
function and code that was used to fit the model and the model itself): https://github.com/brainage04/WestpacHackathon
Here is the complete error log from running the test.py
function, from start to finish: https://pastebin.com/iVZ7dUWn