ValueError: total size of new array must be unchanged

Hi, I’m trying to train a image classification model generated by EON tuner. The model input image size is 96x96x3.
When viewed the model in keras expert mode:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, InputLayer, Dropout, Conv1D, Conv2D, Flatten, Reshape, MaxPooling1D, MaxPooling2D, BatchNormalization, TimeDistributed
from tensorflow.keras.optimizers import Adam

# model architecture
model = Sequential()
channels = 3
columns = 96
rows = int(input_length / (columns * channels))
model.add(Reshape((rows, columns, channels), input_shape=(input_length, )))
model.add(Conv2D(16, kernel_size=3, activation='relu', kernel_constraint=tf.keras.constraints.MaxNorm(1), padding='same'))
model.add(MaxPooling2D(pool_size=2, strides=2, padding='same'))
model.add(Conv2D(32, kernel_size=3, activation='relu', kernel_constraint=tf.keras.constraints.MaxNorm(1), padding='same'))
model.add(MaxPooling2D(pool_size=2, strides=2, padding='same'))
model.add(Conv2D(64, kernel_size=3, activation='relu', kernel_constraint=tf.keras.constraints.MaxNorm(1), padding='same'))
model.add(MaxPooling2D(pool_size=2, strides=2, padding='same'))
model.add(Flatten())
model.add(Dropout(0.25))
model.add(Dense(classes, activation='softmax', name='y_pred'))

# this controls the learning rate
opt = Adam(lr=0.0005, beta_1=0.9, beta_2=0.999)
# this controls the batch size, or you can manipulate the tf.data.Dataset objects yourself
BATCH_SIZE = 32
train_dataset = train_dataset.batch(BATCH_SIZE, drop_remainder=False)
validation_dataset = validation_dataset.batch(BATCH_SIZE, drop_remainder=False)
callbacks.append(BatchLoggerCallback(BATCH_SIZE, train_sample_count))

# train the neural network
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
model.fit(train_dataset, epochs=10, validation_data=validation_dataset, verbose=2, callbacks=callbacks)

With this model, im facing the following error:

Traceback (most recent call last):
File “/home/train.py”, line 283, in
main_function()
File “/home/train.py”, line 152, in main_function
model, override_mode = train_model(train_dataset, validation_dataset, MODEL_INPUT_LENGTH, callbacks,
File “/home/train.py”, line 72, in train_model
model.add(Reshape((rows, columns, channels), input_shape=(input_length, )))
File “/usr/local/lib/python3.8/dist-packages/tensorflow/python/training/tracking/base.py”, line 517, in _method_wrapper
result = method(self, *args, **kwargs)
File “/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/engine/sequential.py”, line 208, in add
layer(x)
File “/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/engine/base_layer.py”, line 951, in call
return self._functional_construction_call(inputs, args, kwargs,
File “/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/engine/base_layer.py”, line 1090, in _functional_construction_call
outputs = self._keras_tensor_symbolic_call(
File “/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/engine/base_layer.py”, line 822, in _keras_tensor_symbolic_call
return self._infer_output_signature(inputs, args, kwargs, input_masks)
File “/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/engine/base_layer.py”, line 863, in _infer_output_signature
outputs = call_fn(inputs, *args, **kwargs)
File “/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/layers/core.py”, line 557, in call
result.set_shape(self.compute_output_shape(inputs.shape))
File “/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/layers/core.py”, line 547, in compute_output_shape
output_shape += self._fix_unknown_dimension(input_shape[1:],
File “/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/layers/core.py”, line 536, in _fix_unknown_dimension
raise ValueError(msg)
ValueError: total size of new array must be unchanged, input_shape = [96], output_shape = [0, 96, 3]

Application exited with code 1 (Error)

Job failed (see above)

Can anyone please help me solve this issue?

Thanks and Regards,
Ramson Jehu K

Hey Ramson,

I was able to replicate the error in my project, it looks like that first reshape layer is incorrect. The input_length is already set to the correct number of rows.

We’ll look at this error further but in the meantime it appears you can just delete the first reshape layer in the Eon Tuner generated model, and then the model will train without errors. Let me know if this works for your project.

Best,
David

2 Likes

Hi Daschwar,

Thanks for your solution. I deleted the reshape layer and added input layer, it worked.

Thanks and Regards,
Ramson Jehu K

1 Like

Hi, could you fix all problems ? how did you solve this problem on code ? I have a similar error on my project