keunwoochoi/kapre

How to print/get the output of kapre layer?

Closed this issue · 1 comments

I want to view the output of the Kapre layer.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv1D, MaxPool1D, BatchNormalization,Dropout, ReLU, GlobalAveragePooling1D, Dense, Softmax
import keras
from kapre.composed import get_melspectrogram_layer, get_log_frequency_spectrogram_layer
from tensorflow.keras.layers import Reshape
import numpy as np

def get_model():
    numclass = 12
    clip_duration = 2
    sr = 16000
    input_shape = (sr*clip_duration,1)

    model = Sequential()
    logfreq_stft_mag = get_log_frequency_spectrogram_layer(input_shape=input_shape, n_fft=512, \
                                                           win_length=int(sr * 0.025),\
                                                           hop_length=int(sr * 0.01), pad_end=True,\
                                                           log_n_bins=26)
    for layer in logfreq_stft_mag.layers:
        model.add(layer)
    model.add(Reshape((199, 26)))
    model.add(Dropout(0.1))
    model.add(Conv1D(filters=32, kernel_size=3, strides=1, activation='relu', padding='same'))
    model.add(BatchNormalization())
    model.add(MaxPool1D(strides=2))

    model.add(Dense(numclass, activation='softmax'))
    model.summary()
    return model

model = get_model()
dat = np.random.random((32000,1))[np.newaxis,...] #Dummy input

layer_name = 'stft'
intermediate_layer_model = keras.Model(inputs=model.input,
                                       outputs=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model(data)

When I run this code, I get

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("stft_input:0", shape=(None, 32000, 1), dtype=float32) at layer "stft_input". The following previous layers were accessed without issue: []

Hi, I'm not sure exactly but I'd try with removing the for-loop and using model.add(logfreq_stft_mag) first.