I have been trying to stack a single LSTM layer on top of Bert embeddings, but whilst my model starts to train it fails on the last batch and throws the following error message:
Node: 'model/tf.reshape/Reshape'
Input to reshape is a tensor with 59136 values, but the requested shape has 98304
[[{{node model/tf.reshape/Reshape}}]] [Op:__inference_train_function_70500]
This is how I build the model and I honestly cannot figure out what is going wrong here:
batch_size = 128
bert_preprocess = hub.load('https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3')
bert_encoder = hub.KerasLayer('https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/4', trainable=True)
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='text')
preprocessed_text = bert_preprocess(text_input)
outputs = bert_encoder(preprocessed_text) #shape=(None, 768)
bert_output = outputs['pooled_output']
l = tf.reshape(bert_output, [batch_size, 1, 768])
l = tf.keras.layers.LSTM(32, activation='relu')(l)
l = tf.keras.layers.Dropout(0.1, name='dropout')(l)
l = tf.keras.layers.Dense(8, activation='softmax', name="output")(l)
model = tf.keras.Model(inputs=[text_input], outputs = [l])
print(model.summary())
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=1, batch_size = batch_size)
this is the full output:
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
text (InputLayer) [(None,)] 0 []
keras_layer (KerasLayer) {'input_mask': (Non 0 ['text[0][0]']
e, 128),
'input_word_ids':
(None, 128),
'input_type_ids':
(None, 128)}
keras_layer_1 (KerasLayer) {'sequence_output': 109482241 ['keras_layer[0][0]',
(None, 128, 768), 'keras_layer[0][1]',
'default': (None, 'keras_layer[0][2]']
768),
'encoder_outputs':
[(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768)],
'pooled_output': (
None, 768)}
tf.reshape (TFOpLambda) (128, 1, 768) 0 ['keras_layer_1[0][13]']
lstm (LSTM) (128, 32) 102528 ['tf.reshape[0][0]']
dropout (Dropout) (128, 32) 0 ['lstm[0][0]']
output (Dense) (128, 8) 264 ['dropout[0][0]']
==================================================================================================
Total params: 109,585,033
Trainable params: 102,792
Non-trainable params: 109,482,241
__________________________________________________________________________________________________
None
WARNING:tensorflow:AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x7fc4ff809440> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: 'arguments' object has no attribute 'posonlyargs'
To silence this warning, decorate the function with #tf.autograph.experimental.do_not_convert
WARNING:tensorflow:AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x7fc4ff809440> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: 'arguments' object has no attribute 'posonlyargs'
To silence this warning, decorate the function with #tf.autograph.experimental.do_not_convert
18/19 [===========================>..] - ETA: 25s - loss: 1.5747 - accuracy: 0.5456Traceback (most recent call last):
File "bert-test-lstm.py", line 62, in <module>
model.fit(x_train, y_train, epochs=1, batch_size = batch_size)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/tensorflow/python/eager/execute.py", line 55, in quick_execute
inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:
Detected at node 'model/tf.reshape/Reshape' defined at (most recent call last):
File "bert-test-lstm.py", line 62, in <module>
model.fit(x_train, y_train, epochs=1, batch_size = batch_size)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 1384, in fit
tmp_logs = self.train_function(iterator)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 1021, in train_function
return step_function(self, iterator)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 1010, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 1000, in run_step
outputs = model.train_step(data)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/base_layer.py", line 1096, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/functional.py", line 452, in call
inputs, training=training, mask=mask)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/functional.py", line 589, in _run_internal_graph
outputs = node.layer(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/base_layer.py", line 1096, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/layers/core/tf_op_layer.py", line 226, in _call_wrapper
return self._call_wrapper(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/layers/core/tf_op_layer.py", line 261, in _call_wrapper
result = self.function(*args, **kwargs)
Node: 'model/tf.reshape/Reshape'
Input to reshape is a tensor with 59136 values, but the requested shape has 98304
The code runs perfectly fine if I just drop the LSTM and reshape layers - any help is appreciated.
You should use tf.keras.layers.Reshape in order to reshape bert_output into a 3D tensor and automatically taking into account the batch dimension.
Simply changing:
l = tf.reshape(bert_output, [batch_size, 1, 768])
into:
l = tf.keras.layers.Reshape((1,768))(bert_output)
should work.
Related
I'm trying to train the model but I am stuck with this error
ValueError: Shapes (None, 1) and (None, 24) are incompatible
My code
model = models.Sequential()
model.add(layers.Conv2D(32, kernel_size=(3, 3), activation='relu',input_shape=(28, 28, 1)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Dropout(0.25))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(25, activation=tf.nn.softmax))
model.summary()
Model Summary
Model: "sequential_6"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_18 (Conv2D) (None, 26, 26, 32) 320
conv2d_19 (Conv2D) (None, 24, 24, 64) 18496
conv2d_20 (Conv2D) (None, 22, 22, 128) 73856
max_pooling2d_8 (MaxPooling (None, 11, 11, 128) 0
2D)
dropout_8 (Dropout) (None, 11, 11, 128) 0
flatten_5 (Flatten) (None, 15488) 0
dense_10 (Dense) (None, 128) 1982592
dropout_9 (Dropout) (None, 128) 0
dense_11 (Dense) (None, 25) 3225
=================================================================
Total params: 2,078,489
Trainable params: 2,078,489
Non-trainable params: 0
Complier
model.compile(loss=tf.keras.losses.categorical_crossentropy,
optimizer=tf.keras.optimizers.Adadelta(), metrics=['accuracy'])
Fit the model
model.fit(train_img, train_y_values, batch_size=64, epochs=5,
verbose=1, validation_data=(test_img, test_y_values))
Error
Epoch 1/5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-61-1ace0d4f685d> in <module>
1 model.fit(train_img, train_y_values, batch_size=64, epochs=5,
----> 2 verbose=1, validation_data=(test_img, test_y_values))
1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1051, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1040, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1030, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 890, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 949, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 139, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 243, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1788, in categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5119, in categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
ValueError: Shapes (None, 1) and (None, 24) are incompatible
train_img shape = (27455, 28, 28)
train_y_value shape = (27455,)
test_img shape = (7172, 28, 28)
test_y_values shape = (7172,)
Somebody help me please
The problem comes from a mismatch of the last layer of your model (None, 25) and the labels (None, 1).
This can be fixed by changing the loss function to "sparse_categorical_crossentropy"
See What is the difference between sparse_categorical_crossentropy and categorical_crossentropy? for more details.
I was making a CNN classifier to identify cat and dog images on my colab notebook, but it is giving me the Graph execution error. Initially, I trained the model with various pictures of them with 500 images each which worked without any problems, but my model was not giving me decent accuracy. So, I increased the pictures to 1500 each which gave me this error.
Epoch 1/10
---------------------------------------------------------------------------
UnknownError Traceback (most recent call last)
<ipython-input-32-086f2dc31200> in <module>
----> 1 model.fit(x = train_batches, validation_data = valid_batches, epochs = 10, verbose = 2)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
53 ctx.ensure_initialized()
54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
UnknownError: Graph execution error:
2 root error(s) found.
(0) UNKNOWN: UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f7814555ef0>
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/script_ops.py", line 271, in __call__
ret = func(*args)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/autograph/impl/api.py", line 642, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 1004, in generator_py_func
values = next(generator_state.get_iterator(iterator_id))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/data_adapter.py", line 830, in wrapped_generator
for data in generator_fn():
File "/usr/local/lib/python3.7/dist-packages/keras/engine/data_adapter.py", line 956, in generator_fn
yield x[I]
File "/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/iterator.py", line 65, in __getitem__
return self._get_batches_of_transformed_samples(index_array)
File "/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/iterator.py", line 230, in _get_batches_of_transformed_samples
interpolation=self.interpolation)
File "/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/utils.py", line 114, in load_img
img = pil_image.open(io.BytesIO(f.read()))
File "/usr/local/lib/python3.7/dist-packages/PIL/Image.py", line 2896, in open
"cannot identify image file %r" % (filename if filename else fp)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f7814555ef0>
[[{{node PyFunc}}]]
[[IteratorGetNext]]
[[IteratorGetNext/_2]]
(1) UNKNOWN: UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f7814555ef0>
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/script_ops.py", line 271, in __call__
ret = func(*args)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/autograph/impl/api.py", line 642, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 1004, in generator_py_func
values = next(generator_state.get_iterator(iterator_id))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/data_adapter.py", line 830, in wrapped_generator
for data in generator_fn():
File "/usr/local/lib/python3.7/dist-packages/keras/engine/data_adapter.py", line 956, in generator_fn
yield x[I]
File "/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/iterator.py", line 65, in __getitem__
return self._get_batches_of_transformed_samples(index_array)
File "/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/iterator.py", line 230, in _get_batches_of_transformed_samples
interpolation=self.interpolation)
File "/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/utils.py", line 114, in load_img
img = pil_image.open(io.BytesIO(f.read()))
File "/usr/local/lib/python3.7/dist-packages/PIL/Image.py", line 2896, in open
"cannot identify image file %r" % (filename if filename else fp)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f7814555ef0>
[[{{node PyFunc}}]]
[[IteratorGetNext]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_2503]
This is my model
model = Sequential([
Conv2D(filters = 32, kernel_size = (3, 3), activation = "relu", padding = "same", input_shape = (224, 224, 3)),
MaxPool2D(pool_size = (2, 2), strides = 3),
Conv2D(filters = 64, kernel_size = (3, 3), activation = "relu", padding = "same"),
MaxPool2D(pool_size = (2, 2), strides = 3),
Flatten(),
Dense(units = 2, activation = "softmax")
])
My Model Summary
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 224, 224, 32) 896
max_pooling2d_2 (MaxPooling 2D) (None, 75, 75, 32) 0
conv2d_3 (Conv2D) (None, 75, 75, 64) 18496
max_pooling2d_3 (MaxPooling 2D) (None, 25, 25, 64) 0
flatten_1 (Flatten) (None, 40000) 0
dense_1 (Dense) (None, 2) 80002
=================================================================
Total params: 99,394
Trainable params: 99,394
Non-trainable params: 0
_________________________________________________________________
My Compile and Training code
model.compile(optimizer = Adam(learning_rate = 0.0001), loss =
"categorical_crossentropy", metrics = ["accuracy"])
model.fit(x = train_batches, validation_data = valid_batches, epochs = 10, verbose = 2)
Any solutions?
You may have a corrupt image in your dataset. Try to find out that image and remove it.
import PIL
from pathlib import Path
from PIL import UnidentifiedImageError
def aggregate_images(
dataset_root,
extensions = ["png", "jpg", "jpeg"],
):
"""
Globs for images in a given data directory and returns them
"""
dataset_root = Path(dataset_root)
image_paths = []
for extension in extensions:
image_paths.extend(list(dataset_root.glob("**/*.{}".format(extension))))
return image_paths
path = aggregate_images(/path/to/your/dataset/root)
for img_p in path:
try:
img = PIL.Image.open(img_p)
except PIL.UnidentifiedImageError:
print(img_p)
I am running into an issue running a CLDNN model in Python 3. I ran model.summary() and I did not see an issue. I am receiving a sizing/shaping error between Keras and Tensorflow.
Below is the code with the output error. Any input would be greatly apricated.
Update: reformatted the code and supplied the output as text.
def cldnn():
model = Sequential()
#input layer
model.add(keras.layers.InputLayer(input_shape=(2, 128, 1)))
#first layer
model.add(Conv2D(256, (1, 3), activation='relu', use_bias=True))
model.add(Dropout(0.2))
#second layer
model.add(Conv2D(256, (2, 3), activation='relu', use_bias=True))
#third layer
model.add(Conv2D(80, (1, 3), activation='relu', use_bias=True))
model.add(Dropout(0.2))
#fourth layer
model.add(Conv2D(80, (1, 3), activation='relu', use_bias=True))
#reshape data
model.add(Reshape((120,80)))
#fifth layer
model.add(LSTM(50, activation='tanh', return_sequences=True))
#sixth layer
model.add(Dense(128,
activation='relu',
use_bias=True,
kernel_initializer='normal'))
#output layer
model.add(Dense(10, activation='softmax', use_bias=True))
model.summary()
return model
#Create instance of CNN model graph
cldnn = cldnn()
#Compile model using an appropriate loss and optimizer algorithm
cldnn.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
cldnn_mdata = cldnn.fit(data_train, labels_train,
validation_data=(data_test, labels_test),
epochs=15,
batch_size=1024,
shuffle=True)
#Print accuracy of model on testing set after training
scores = cldnn.evaluate(data_test, labels_test)
print("Accuracy: %.2f%%" %(scores[1]*100))
#OUTPUT
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param # =================================================================
conv2d_16 (Conv2D) (None, 2, 126, 256) 1024
_________________________________________________________________
dropout_8 (Dropout) (None, 2, 126, 256) 0
_________________________________________________________________
conv2d_17 (Conv2D) (None, 1, 124, 256) 393472
_________________________________________________________________
conv2d_18 (Conv2D) (None, 1, 122, 80) 61520
_________________________________________________________________
dropout_9 (Dropout) (None, 1, 122, 80) 0
_________________________________________________________________
conv2d_19 (Conv2D) (None, 1, 120, 80) 19280
_________________________________________________________________
reshape_3 (Reshape) (None, 120, 80) 0
_________________________________________________________________
lstm_3 (LSTM) (None, 120, 50) 26200
_________________________________________________________________
dense_6 (Dense) (None, 120, 128) 6528
_________________________________________________________________
dense_7 (Dense) (None, 120, 10) 1290
=================================================================
Total params: 509,314
Trainable params: 509,314
Non-trainable params: 0
_________________________________________________________________
Epoch 1/15
WARNING:tensorflow:Model was constructed with shape (None, 2, 128, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 2, 128, 1), dtype=tf.float32, name='input_5'), name='input_5', description="created by layer 'input_5'"), but it was called on an input with incompatible shape (None, 28, 28, 1).
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-0e0ee3a37c65> in <module>()
52 epochs=15,
53 batch_size=1024,
---> 54 shuffle=True)
55
56 #Print accuracy of model on testing set after training
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
992 except Exception as e: # pylint:disable=broad-except
993 if hasattr(e, "ag_error_metadata"):
--> 994 raise e.ag_error_metadata.to_exception(e)
995 else:
996 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:853 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:842 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1286 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2849 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3632 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:835 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:787 train_step
y_pred = self(x, training=True)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:1037 __call__
outputs = call_fn(inputs, *args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/sequential.py:369 call
return super(Sequential, self).call(inputs, training=training, mask=mask)
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py:415 call
inputs, training=training, mask=mask)
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py:550 _run_internal_graph
outputs = node.layer(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:1037 __call__
outputs = call_fn(inputs, *args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/layers/core.py:539 call
result.set_shape(self.compute_output_shape(inputs.shape))
/usr/local/lib/python3.7/dist-packages/keras/layers/core.py:530 compute_output_shape
self.target_shape)
/usr/local/lib/python3.7/dist-packages/keras/layers/core.py:518 _fix_unknown_dimension
raise ValueError(msg)
ValueError: total size of new array must be unchanged, input_shape = [27, 20, 80], output_shape = [120, 80]
I have a problem with my machine learning code
here is the model :
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=input_shape),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(64, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(128, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(512, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='softmax')])
model.summary()
This is my model.summary() result :
Model: "sequential_32"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_128 (Conv2D) (None, 148, 148, 32) 896
_________________________________________________________________
max_pooling2d_128 (MaxPoolin (None, 74, 74, 32) 0
_________________________________________________________________
conv2d_129 (Conv2D) (None, 72, 72, 64) 18496
_________________________________________________________________
max_pooling2d_129 (MaxPoolin (None, 36, 36, 64) 0
_________________________________________________________________
conv2d_130 (Conv2D) (None, 34, 34, 128) 73856
_________________________________________________________________
max_pooling2d_130 (MaxPoolin (None, 17, 17, 128) 0
_________________________________________________________________
conv2d_131 (Conv2D) (None, 15, 15, 512) 590336
_________________________________________________________________
max_pooling2d_131 (MaxPoolin (None, 7, 7, 512) 0
_________________________________________________________________
flatten_32 (Flatten) (None, 25088) 0
_________________________________________________________________
dense_79 (Dense) (None, 128) 3211392
_________________________________________________________________
dense_80 (Dense) (None, 1) 129
=================================================================
Total params: 3,895,105
Trainable params: 3,895,105
Non-trainable params: 0
And this is the compile setting i use :
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
And this is the fit model code :
EPOCH = 100
history = model.fit(train_data,
steps_per_epoch=len(train_generator),
epochs=EPOCH,
validation_data=val_data,
validation_steps=len(val_generator),
shuffle=True,
verbose = 1)
For the train_data i create use tensorflow tf.data cause i think it is more compatible with tf.keras. this is the tf.data generator function code :
def tf_data_generator(generator, input_shape):
num_class = generator.num_classes
tf_generator = tf.data.Dataset.from_generator(
lambda: generator,
output_types=(tf.float32, tf.float32),
output_shapes=([None
, input_shape[0]
, input_shape[1]
, input_shape[2]]
,[None, num_class])
)
return tf_generator
train_data = tf_data_generator(train_generator, input_shape)
val_data = tf_data_generator(val_generator, input_shape)
actually i got that function from medium.com as source. But i got error when try to train my machine learning code, can someone help me solve the error, this is the error message :
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-16-448faadd058c> in <module>()
6 validation_steps=len(val_generator),
7 shuffle=True,
----> 8 verbose = 1)
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
984 except Exception as e: # pylint:disable=broad-except
985 if hasattr(e, "ag_error_metadata"):
--> 986 raise e.ag_error_metadata.to_exception(e)
987 else:
988 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:855 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:845 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1285 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2833 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3608 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:838 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:797 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/compile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:155 __call__
losses = call_fn(y_true, y_pred)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:259 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:1644 categorical_crossentropy
y_true, y_pred, from_logits=from_logits)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py:4862 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py:1161 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, 3) and (None, 1) are incompatible
Sorry if my question is confusing, i still new in machine learning field. Thanks for helping me
I guess you intend to get a multi-class classifier, for 3 classes. If that is the case, you wrongly assigned the last layer to a DENSE of size 1. You can solve the issue by replacing this line:
tf.keras.layers.Dense(1, activation='softmax')])
by this:
tf.keras.layers.Dense(3, activation='softmax')])
I'm experiencing a "ValueError: Shapes (None, None) and (None, 8, 8, 7) are incompatible" anytime I am training my model in Tensorflow. So far:
history = model.fit(train_batches,
steps_per_epoch=train_steps,
class_weight=class_weights,
validation_data=validation_batches,
validation_steps=val_steps,
epochs=30,
verbose=1,
callbacks=callbacks_list
)
gives this stacktrace:
Traceback (most recent call last):
File "/home/brian/Desktop/381-deep-learning/main.py", line 410, in <module>
epochs=30
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1479, in fit_generator
initial_epoch=initial_epoch)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
return method(self, *args, **kwargs)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 848, in fit
tmp_logs = train_function(iterator)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 580, in __call__
result = self._call(*args, **kwds)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 627, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 506, in _initialize
*args, **kwds))
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 2446, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 2777, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 2667, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py", line 981, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 441, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py", line 968, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:571 train_function *
outputs = self.distribute_strategy.run(
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:533 train_step **
y, y_pred, sample_weight, regularization_losses=self.losses)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/compile_utils.py:205 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/losses.py:143 __call__
losses = self.call(y_true, y_pred)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/losses.py:246 call
return self.fn(y_true, y_pred, **self._fn_kwargs)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/losses.py:1527 categorical_crossentropy
return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/backend.py:4561 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py:1117 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, None) and (None, 8, 8, 7) are incompatible
Process finished with exit code 1
after making it to Epoch 1/30.
Here is my model definition in case anyone in wondering:
# Create Inception Res Net model as used in paper
resnet = tf.keras.applications.inception_resnet_v2.InceptionResNetV2()
print("Layers of ResNet: "+str(len(resnet.layers))) //782 layers
x = resnet.layers[-28].output
x = tf.keras.layers.Dropout(0.25)(x)
# Make a prediction layer with 7 nodes for the 7 dir in our train_dir.
predictions_layer = tf.keras.layers.Dense(7, activation='softmax')(x)
# print(resnet.input)
# inputs=resnet.input selects the input layer, outputs=predictions refers to the
# dense layer we created above.
model = tf.keras.Model(inputs=resnet.input, outputs=predictions_layer)
What I believe may be the cause of my issue is my model declaration because when I observe my model.summary() what I see is this (with all in between layers excluded of course):
Output of model.summary()
input_1 (InputLayer) [(None, 299, 299, 3) 0
__________________________________________________________________________________________________
dropout (Dropout) (None, 8, 8, 192) 0 batch_normalization_195[0][0]
__________________________________________________________________________________________________
dense (Dense) (None, 8, 8, 7) 1351 dropout[0][0]
==================================================================================================
Total params: 47,465,959
Trainable params: 47,411,559
Non-trainable params: 54,400
I included a pastebin of my entire file in case I missed anything: https://pastebin.com/raw/E0VQ83JQ
I understand that it is expecting type (None, None) and my output layer is sent to a Dense Layer of shape (None, 8, 8, 7), but how would I do the reshaping?
Any help is appreciated, including documentation which you'd think I'd find useful on the subject.
There should be a flatten layer between output from ResNet and the Dense layer.
# Create Inception Res Net model as used in paper
resnet = tf.keras.applications.inception_resnet_v2.InceptionResNetV2()
print("Layers of ResNet: "+str(len(resnet.layers))) //782 layers
x = resnet.layers[-28].output
x = tf.keras.layers.Dropout(0.25)(x)
### Edit here.
x = tf.keras.layers.Flatten()(x)
# Make a prediction layer with 7 nodes for the 7 dir in our train_dir.
predictions_layer = tf.keras.layers.Dense(7, activation='softmax')(x)
# print(resnet.input)
# inputs=resnet.input selects the input layer, outputs=predictions refers to the
# dense layer we created above.
model = tf.keras.Model(inputs=resnet.input, outputs=predictions_layer)
Also, make sure that train_batches is valid.
First Convert X and Y into numpy array then convert x_train,y_train into the 'Standardscale' which coverts large values in small values
X=X.values
y=y.values
from sklearn.preprocessing import StandardScaler
scaler=StandardScaler()
X_train=scaler.fit_transform(X_train)
X_test=scaler.fit_transform(X_test)
Now apply model.fit