Error: "Labels and preOutput must have equal shapes" in 1D convolutional layer

Dear,

I am working on a 1D convolutional network for fall detection, using the MobiAct dataset.

The data file contains 12,734 arrays of size 400, and the label file contains the corresponding classes (fall and non-fall).

When running the network, I am encountering the following error: “Exception in thread “main” java.lang.IllegalArgumentException: Labels and preOutput must have equal shapes: got shapes [12734] vs [4978994, 2]”

I am a beginner in DL4J and facing difficulties in setting up the network. Below, I am sending the Java code using DL4J and the equivalent code that I managed to run in Python using Keras.

I would appreciate any help.

NETWORK WITH DL4J:

public class Main {
public static void main(String args) {

    int numClasses = 2; // Número de classes de saída
    int inputSize = 400; // Tamanho da entrada unidimensional
    int numFilters = 5; // Número de filtros convolucionais
    int filterSize = 3; // Tamanho do filtro
    int poolingSize = 2; // Tamanho da camada de pooling

    // Configuração da rede
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .seed(12345)
            .updater(new Adam(0.001))
            .weightInit(WeightInit.NORMAL)
            .list()
            .layer(0, new Convolution1DLayer.Builder()
                    .nIn(1)
                    .nOut(numFilters)
                    .kernelSize(filterSize)
                    .activation(Activation.RELU)

                    .dropOut(0.2)
                    .build())
            .layer(1, new Subsampling1DLayer.Builder()
                .poolingType(SubsamplingLayer.PoolingType.MAX)
                .kernelSize(poolingSize)
                .build())
            .layer(2, new Convolution1DLayer.Builder()
                    .nOut(numFilters)
                    .kernelSize(filterSize*2)
                    .activation(Activation.RELU)
                    .dropOut(0.2)
                    .build())
            .layer(3, new Subsampling1DLayer.Builder()
                    .poolingType(SubsamplingLayer.PoolingType.MAX)
                    .kernelSize(poolingSize)
                    .build())
            .layer(4, new DenseLayer.Builder()
                    .nOut(110) // Número de neurônios na camada densa
                    .activation(Activation.RELU)
                    .dropOut(0.2)
                    .build())
            .layer(5, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                    .nOut(numClasses)
                    .activation(Activation.SOFTMAX)
                    .build())
            //.inputPreProcessor(0,  new FeedForwardToCnnPreProcessor(1,inputSize))
            .setInputType(InputType.recurrent(400,1))
            .build();

    

    // Criação do modelo
    MultiLayerNetwork model = new MultiLayerNetwork(conf);
    model.init();

    // carregando os dados

    INDArray data = Nd4j.readNpy("/home/dev/Área de trabalho/pasta sem título/dados_tempo_acc.npy");
    INDArray labels = Nd4j.readNpy("/home/dev/Área de trabalho/pasta sem título/rotulos_tempo_acc.npy");

    data = data.reshape(12734, 1, inputSize, 1);
    

    DataSet dataSet = new DataSet(data, labels);
    
    for (int i = 0; i < 20; i++) {
        // Treina o modelo
        System.out.println("Epoch "+ (i+1));
        model.fit(dataSet.getFeatures(),dataSet.getLabels());
        }
    }

}

NETWORK WITH PYTHON (KERAS):

dados =‘/home/dev/PycharmProjects/FallDetection/conv/mobiact/Dados GIR e ACC atualizados/ACC/dados_freq_acc.npy’
rotulos =‘/home/dev/PycharmProjects/FallDetection/conv/mobiact/Dados GIR e ACC atualizados/ACC/rotulos_freq_acc.npy’

import numpy as np
X = np.load(dados)
y = np.load(rotulos)

def rede_cnn_1d(X,y,i):

from sklearn.model_selection import train_test_split

# Dividindo os dados em conjunto de treinamento e teste
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)

# Dividindo o conjunto de treinamento em conjunto de treinamento e validação
X_test, X_val, y_test, y_val = train_test_split(X_test, y_test, test_size=0.5)

from keras.utils import to_categorical
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
y_val = to_categorical(y_val)

from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout,Conv1D,Flatten,MaxPooling1D

kernel_size = 5

model = keras.Sequential()


model.add(Conv1D(filters=107, kernel_size=kernel_size, activation='relu', input_shape=(400,1)))
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(0.3))

model.add(Conv1D(filters=214, kernel_size=kernel_size, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(0.3))

model.add(Conv1D(filters=428, kernel_size=kernel_size, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(0.3))

model.add(Conv1D(filters=856, kernel_size=kernel_size, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(0.3))

model.add(Flatten())
model.add(Dense(291, activation='relu'))
model.add(Dense(2, activation='softmax'))

#optimizer = keras.optimizers.Adam(learning_rate=0.003)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

historico = model.fit(X_train,y_train,batch_size= 32,epochs = 20, validation_data = (X_val,y_val),verbose =1)

Best regards.

Ignore the Python code above, I pasted the wrong one. The corresponding Python code is below:

def rede_cnn_1d(X,y,i):

from sklearn.model_selection import train_test_split

# Dividindo os dados em conjunto de treinamento e teste
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)

# Dividindo o conjunto de treinamento em conjunto de treinamento e validação
X_test, X_val, y_test, y_val = train_test_split(X_test, y_test, test_size=0.5)

from keras.utils import to_categorical
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
y_val = to_categorical(y_val)

from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout,Conv1D,Flatten,MaxPooling1D

kernel_size = 3

model = keras.Sequential()


model.add(Conv1D(filters=278, kernel_size=kernel_size, activation='relu', input_shape=(200,1)))
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(0.4))

model.add(Conv1D(filters=556, kernel_size=kernel_size, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(0.4))

model.add(Flatten())
model.add(Dense(110, activation='relu'))
model.add(Dense(2, activation='softmax'))

optimizer = keras.optimizers.Adam(learning_rate=0.006)
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])

historico = model.fit(X_train,y_train,batch_size= 32,epochs = 20, validation_data = (X_val,y_val),verbose =1)

@SoaresLMB sorry for the late response. You probably have 2d labels. Make sure they are all the same shape as the error says. You can check the shape that’s coming out from the network with a .output(…) call.