Reputation: 61
Needs your help to design a cnn model for text detection. Getting error here when invoking keras.sequential.fit(x_train, y_train,..)? I've .txt files with multiple GT annotations for each image, what will be y_train? How these .txt files will be in y_train?
Code related to ground truth (.txt file) is as below :
# Upload the training dataset gt from disk
source_train_gt = '/content/sample_data/y_train'
directory = os.scandir(source_train_gt)
y_train_data = np.zeros((20,))
for file in directory:
gtfile = open(source_train_gt +'/'+ file.name,'r')
np.append(y_train_data, gtfile)
# My CNN Model
myCNN = Sequential()
myCNN.add(Convolution2D(filters = 32, kernel_size = 3, input_shape = (64,64, 3), activation = 'relu'))
myCNN.add(MaxPooling2D(pool_size = (2, 2), strides = 2))
myCNN.add(Flatten())
myCNN.add(Dense(units = 128, activation = 'relu'))
myCNN.add(Dense(units = 1, activation = 'sigmoid'))
myCNN.compile(optimizer = 'adam',loss = 'binary_crossentropy',metrics = ['accuracy'])
# when following fit method is invoked, error is generated:
myCNN.fit(x_train_data, y_train_data, epochs = 10, validation_data = (x_test_data, y_test_data))
When invoking fit method, it throws following error :
Epoch 1/10WARNING:tensorflow:Model was constructed with shape (None, 64, 64, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 64, 64, 3), dtype=tf.float32, name='conv2d_input'), name='conv2d_input', description="created by layer 'conv2d_input'"), but it was called on an input with incompatible shape (None,).---------------------------------------------------------------------------ValueError Traceback (most recent call last) in ()----> 1 myCNN.fit(x_train_data, y_train_data, epochs = 10, validation_data = (x_test_data, y_test_data)) 1 frames/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs) 1127 except Exception as e: # pylint:disable=broad-except 1128 if hasattr(e, "ag_error_metadata"):-> 1129 raise e.ag_error_metadata.to_exception(e) 1130 else: 1131 raise ValueError: in user code: File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function * return step_function(self, iterator) File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, 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 860, in run_step ** outputs = model.train_step(data) File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 808, in train_step y_pred = self(x, training=True) File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler raise e.with_traceback(filtered_tb) from None File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 227, in assert_input_compatibility raise ValueError(f'Input {input_index} of layer "{layer_name}" ' ValueError: Exception encountered when calling layer "sequential" (type Sequential). Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=1. Full shape received: (None,) Call arguments received: • inputs=tf.Tensor(shape=(None,), dtype=float32) • training=True • mask=None
Upvotes: 0
Views: 1749
Reputation:
Obviously problem is with your input data shape. Conv2D
expects input hape 4D. but you giving 1D.
Working sample code
import tensorflow as tf
import numpy as np
import tensorflow.keras as keras
X_train = np.random.random((2356,64,64,3))
y_train = np.random.random((2356, 1))
myCNN = tf.keras.Sequential()
myCNN.add(keras.layers.Conv2D(filters = 32, kernel_size = 3, input_shape = (64,64, 3), activation = 'relu'))
myCNN.add(keras.layers.MaxPool2D(pool_size = (2, 2), strides = 2))
myCNN.add(keras.layers.Flatten())
myCNN.add(keras.layers.Dense(units = 128, activation = 'relu'))
myCNN.add(keras.layers.Dense(units = 1, activation = 'sigmoid'))
myCNN.compile(optimizer = 'adam',loss = 'binary_crossentropy',metrics = ['accuracy'])
# when following fit method is invoked, error is generated:
myCNN.fit(X_train, y_train, epochs = 15)
Output
poch 1/15
74/74 [==============================] - 14s 9ms/step - loss: 0.8949 - accuracy: 0.0000e+00
Epoch 2/15
74/74 [==============================] - 1s 8ms/step - loss: 0.6775 - accuracy: 0.0000e+00
Epoch 3/15
74/74 [==============================] - 1s 8ms/step - loss: 0.6103 - accuracy: 0.0000e+00
Epoch 4/15
74/74 [==============================] - 1s 10ms/step - loss: 0.5413 - accuracy: 0.0000e+00
Epoch 5/15
74/74 [==============================] - 1s 9ms/step - loss: 0.5157 - accuracy: 0.0000e+00
Epoch 6/15
74/74 [==============================] - 1s 8ms/step - loss: 0.5067 - accuracy: 0.0000e+00
Epoch 7/15
74/74 [==============================] - 1s 9ms/step - loss: 0.5030 - accuracy: 0.0000e+00
Epoch 8/15
74/74 [==============================] - 1s 8ms/step - loss: 0.5029 - accuracy: 0.0000e+00
Epoch 9/15
74/74 [==============================] - 1s 8ms/step - loss: 0.5030 - accuracy: 0.0000e+00
Epoch 10/15
74/74 [==============================] - 1s 8ms/step - loss: 0.5036 - accuracy: 0.0000e+00
Epoch 11/15
74/74 [==============================] - 1s 8ms/step - loss: 0.5065 - accuracy: 0.0000e+00
Epoch 12/15
74/74 [==============================] - 1s 8ms/step - loss: 0.5054 - accuracy: 0.0000e+00
Epoch 13/15
74/74 [==============================] - 1s 7ms/step - loss: 0.5031 - accuracy: 0.0000e+00
Epoch 14/15
74/74 [==============================] - 1s 9ms/step - loss: 0.5025 - accuracy: 0.0000e+00
Epoch 15/15
74/74 [==============================] - 1s 8ms/step - loss: 0.5025 - accuracy: 0.0000e+00
Upvotes: 0