I am trying to train a 1D CNN. My input is a numpy array which has 476 rows and 4 columns. I couldn't figure out how to set input shape. I also tried to reshape input to (476, 4, 1) but still got error. Train_labels's shape is (476, ). O is train data and 0[0].shape is (4,) Here is the code, error and data below.
ValueError: Input 0 of layer "sequential_17" is incompatible with the layer: expected shape=(None, 476, 4), found shape=(1, 4, 1)
Traceback (most recent call last)
<ipython-input-363-440de758fd68> in <module>()
10
11
---> 12 model.fit(o, train_labels, epochs=5, batch_size=1)
13 print(model.evaluate(o, train_labels))
1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, 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 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, 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 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
model=Sequential()
model.add(Conv1D(filters=32, kernel_size=3, activation='relu', input_shape=(476,4)))
model.add(Conv1D(filters=16, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(50, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(o, train_labels, epochs=5, batch_size=1)
print(model.evaluate(o, train_labels))
Here is the model structure below.
Model: "sequential_34"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_56 (Conv1D) (None, 474, 32) 416
conv1d_57 (Conv1D) (None, 472, 16) 1552
dropout_19 (Dropout) (None, 472, 16) 0
max_pooling1d_19 (MaxPoolin (None, 236, 16) 0
g1D)
flatten_19 (Flatten) (None, 3776) 0
dense_40 (Dense) (None, 50) 188850
dense_41 (Dense) (None, 2) 102
Total params: 190,920
Trainable params: 190,920
Non-trainable params: 0
My data is a numpy array which has 476 rows and 4 columns. It's shape is (476,4).
[[0.35603836 0.6439616 0.49762452 0.5023755 ]
[0.12395032 0.87604964 0.49762452 0.5023755 ]
[0.5605615 0.43943852 0.49762452 0.5023755 ]
...
[0.6250699 0.37493005 0.48114303 0.51885694]
[0.6650569 0.33494312 0.48114303 0.51885694]
[0.53505033 0.46494964 0.48114303 0.51885694]]
I solved the problem myself and wanted to provide an answer for anyone having the same issue.
Here is the steps I applied:
I used sparse categorical crossentropy.
I changed input shape to (number of attributes, 1)
I changed CNN kernel size from 3 to 2
model=Sequential()
model.add(Conv1D(filters=8, kernel_size=2, activation='relu', input_shape=(4,1)))
model.add(Conv1D(filters=4, kernel_size=2, activation='relu'))
model.add(Dropout(0.3))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(20, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(o, train_labels, epochs=5, batch_size=1)
print(model.evaluate(o, train_labels))
Related
I am trying to create a LSTM model for time series prediction, at each time step input has 9 elements and the output has 4.
To create a dataset I write this code:
def create_dataset(dataset, look_back):
dataX, dataY = [], []
for i in range(len(dataset)-look_back):
dataX.append(dataset[i:(i+look_back)]) # all 22 columns for X
dataY.append(dataset[i + look_back, 9:14]) # first 8 columns for Y, just as an example
return np.array(dataX), np.array(dataY)
data = np.concatenate((input, output), axis=1)
X, Y = create_dataset(data, 1)
I used this model
model=Sequential()
model.add(Embedding(1, 13, input_length=304413))
model.add(LSTM(12, input_shape=(304413,1,13), kernel_initializer='normal',activation='relu',return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(12, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(4, activation='relu'))
model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),metrics=['accuracy','mse'])
The input shape is (304413,13) and the output shape is (304413, 4)
The output of summary is:
Model: "sequential_20"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_15 (Embedding) (None, 304413, 13) 13
lstm_30 (LSTM) (None, 304413, 12) 1248
dropout_28 (Dropout) (None, 304413, 12) 0
lstm_31 (LSTM) (None, 12) 1200
dropout_29 (Dropout) (None, 12) 0
dense_14 (Dense) (None, 4) 52
=================================================================
Total params: 2,513
Trainable params: 2,513
Non-trainable params: 0
and here where I face the error:
pred=model.fit(x,Y, verbose=0,epochs=150, batch_size=70)
The error is
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, 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 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, 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 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential_20" is incompatible with the layer: expected shape=(None, 304413), found shape=(None, 1, 13)
What is my mistake and how can I solve it?
I'm creating a Bidirectional LSTM but I faced following error
ValueError: Dimensions must be equal, but are 5 and 250 for '{{node Equal}} = Equal[T=DT_INT64, incompatible_shape_error=true](ArgMax, ArgMax_1)' with input shapes: [?,5], [?,250]
I have no idea what is wrong and how to fix it!
I have a text dataset with 59k row for train the model and i would divid them into 15 classes which then I would use for text similarity base on classes for the received new text.
Based on the other post I played with loss but still it doesn't solve the issue.
Here is the model plot:
Also sequential model would be as follow:
model_lstm = Sequential()
model_lstm.add(InputLayer(250,))
model_lstm.add(Embedding(input_dim=max_words+1, output_dim=200, weights=[embedding_matrix],
mask_zero=True, trainable= True, name='corpus_embed'))
enc_lstm = Bidirectional(LSTM(128, activation='sigmoid', return_sequences=True, name='LSTM_Encod'))
model_lstm.add(enc_lstm)
model_lstm.add(Dropout(0.25))
model_lstm.add(Bidirectional(LSTM( 128, activation='sigmoid',dropout=0.25, return_sequences=True, name='LSTM_Decod')))
model_lstm.add(Dropout(0.25))
model_lstm.add(Dense(15, activation='softmax'))
model_lstm.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['Accuracy'])
## Feed the model
history = model_lstm.fit(x=corpus_seq_train,
y=target_seq_train,
batch_size=128,
epochs=50,
validation_data=(corpus_seq_test,target_seq_test),
callbacks=[tensorboard],
sample_weight= sample_wt_mat)
This is the model summary:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
corpus_embed (Embedding) (None, 250, 200) 4000200
bidirectional (Bidirectiona (None, 250, 256) 336896
l)
dropout (Dropout) (None, 250, 256) 0
bidirectional_1 (Bidirectio (None, 250, 256) 394240
nal)
dropout_1 (Dropout) (None, 250, 256) 0
dense (Dense) (None, 250, 15) 3855
=================================================================
Total params: 4,735,191
Trainable params: 4,735,191
Non-trainable params: 0
_________________________________
and dataset shape:
corpus_seq_train.shape, target_seq_train.shape
((59597, 250), (59597, 5, 8205))
Finally, here is the error:
Epoch 1/50
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
C:\Users\AMIRSH~1\AppData\Local\Temp/ipykernel_10004/3838451254.py in <module>
9 ## Feed the model
10
---> 11 history = model_lstm.fit(x=corpus_seq_train,
12 y=target_seq_train,
13 batch_size=128,
C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
C:\ProgramData\Anaconda3\lib\site-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 "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1051, in train_function *
return step_function(self, iterator)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1040, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1030, in run_step **
outputs = model.train_step(data)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 894, in train_step
return self.compute_metrics(x, y, y_pred, sample_weight)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 987, in compute_metrics
self.compiled_metrics.update_state(y, y_pred, sample_weight)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\compile_utils.py", line 501, in update_state
metric_obj.update_state(y_t, y_p, sample_weight=mask)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\metrics_utils.py", line 70, in decorated
update_op = update_state_fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\metrics\base_metric.py", line 140, in update_state_fn
return ag_update_state(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\metrics\base_metric.py", line 646, in update_state **
matches = ag_fn(y_true, y_pred, **self._fn_kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\metrics\metrics.py", line 3295, in categorical_accuracy
return metrics_utils.sparse_categorical_matches(
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\metrics_utils.py", line 893, in sparse_categorical_matches
matches = tf.cast(tf.equal(y_true, y_pred), backend.floatx())
ValueError: Dimensions must be equal, but are 5 and 250 for '{{node Equal}} = Equal[T=DT_INT64, incompatible_shape_error=true](ArgMax, ArgMax_1)' with input shapes: [?,5], [?,250].
the problem is because of the Loss function and y-label shape.
we should not pad y_label and it should fit the model directly without any other process
Code:
Y = Y.to_numpy()
X = X.to_numpy()
X.reshape((1, 478405, 33))
opt = tf.keras.optimizers.Adam(lr=0.001, decay=1e-6)
model = Sequential()
model.add(LSTM(33, return_sequences=True, input_shape=(X.shape[1], X.shape[0]), activation='sigmoid'))
model.add(Dropout(0.2))
model.add(LSTM(33, return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(1, activation = "sigmoid"))
model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
filepath = "RNN_Final-{epoch:02d}-{val_acc:.3f}" # unique file name that will include the epoch and the validation acc for that epoch
checkpoint = ModelCheckpoint("models/{}.model".format(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')) # saves only the best ones
history = model.fit(X, Y, epochs=35, batch_size=1, shuffle=False)
scores = model.evaluate(X, Y)
Error:
WARNING:tensorflow:Model was constructed with shape (None, 33, 478405) for input KerasTensor(type_spec=TensorSpec(shape=(None, 33, 478405), dtype=tf.float32, name='lstm_input'), name='lstm_input', description="created by layer 'lstm_input'"), but it was called on an input with incompatible shape (1, 33).
Traceback (most recent call last):
File "C:\Users\W10\PycharmProjects\TheCryptoBot\cryptobot\app\ai-model -2.py", line 84, in <module>
history = model.fit(X, Y, epochs=35, batch_size=1, shuffle=False)
File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1129, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\engine\training.py", line 878, in train_function *
return step_function(self, iterator)
File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\engine\training.py", line 867, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\engine\training.py", line 860, in run_step **
outputs = model.train_step(data)
File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\engine\training.py", line 808, in train_step
y_pred = self(x, training=True)
File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\engine\input_spec.py", line 213, 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 "lstm" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (1, 33)
Call arguments received:
• inputs=tf.Tensor(shape=(1, 33), dtype=float32)
• training=True
• mask=None
Process finished with exit code 1
Model:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm (LSTM) (None, 478405, 33) 63153948
dropout (Dropout) (None, 478405, 33) 0
lstm_1 (LSTM) (None, 478405, 33) 8844
dropout_1 (Dropout) (None, 478405, 33) 0
dense (Dense) (None, 478405, 1) 34
=================================================================
Total params: 63,162,826
Trainable params: 63,162,826
Non-trainable params: 0
_________________________________________________________________
I think the problem is that you are reshaping the variable X like so X.reshape((1, 478405, 33)), however, this does not change the shape of X on its own. You need to set the result to X, like this X = X.reshape((1, 478405, 33)).
For time series you must use a TimeseriesGenerator
generator = TimeseriesGenerator(X, Y, length=478404, batch_size=100)
# print each sample
#for i in range(len(generator)):
#x, y = generator[i]
#print('%s => %s' % (x, y))
opt = tf.keras.optimizers.Adam(learning_rate=0.001, decay=1e-6)
print("Adding layer 1...")
model = Sequential()
model.add(LSTM(33, return_sequences=True, input_shape=(478404, 33), activation='sigmoid'))
print("Adding layer 2...")
model.add(Dropout(0.2))
print("Adding layer 3...")
model.add(LSTM(33, return_sequences=True))
print("Adding layer 4...")
model.add(Dropout(0.2))
print("Adding layer 5...")
model.add(Dense(1, activation="sigmoid"))
print("Adding layer 6...")
model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
print ('model compiled')
print (model.summary())
# Compile model
filepath = "RNN_Final-{epoch:02d}-{val_acc:.3f}" # unique file name that will include the epoch and the validation acc for that epoch
checkpoint = ModelCheckpoint("models/{}.model".format(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')) # saves only the best ones
history = model.fit(generator, steps_per_epoch=1, epochs=30, verbose=0)
print("Fit DOne")
print(history.history.keys())
# evaluate the model
scores = model.evaluate(generator)
I am trying to implement an image classifier using "The Street View House Numbers (SVHN) Dataset" from this link. I am using format 2 which contains 32x32 RGB centered digit images from 0 to 9. When I try to compile and fit the model I get the following error:
Epoch 1/10
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-37-31870b6986af> in <module>()
3
4 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
----> 5 model.fit(trainX, trainY, validation_data=(validX, validY), batch_size=128, epochs=10)
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
975 except Exception as e: # pylint:disable=broad-except
976 if hasattr(e, "ag_error_metadata"):
--> 977 raise e.ag_error_metadata.to_exception(e)
978 else:
979 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 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:2730 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:3417 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:756 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:203 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:152 __call__
losses = call_fn(y_true, y_pred)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:256 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:1537 categorical_crossentropy
return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py:4833 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py:1134 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, 1) and (None, 10) are incompatible
The code is:
model = Sequential([
Conv2D(filters=64, kernel_size=3, strides=2, activation='relu', input_shape=(32,32,3)),
MaxPooling2D(pool_size=(2, 2), strides=1, padding='same'),
Conv2D(filters=32, kernel_size=3, strides=1, activation='relu'),
MaxPooling2D(pool_size=(2, 2), strides=1, padding='same'),
Flatten(),
Dense(10, activation='softmax')
])
model.summary()
Model: "sequential_10"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_23 (Conv2D) (None, 15, 15, 64) 1792
_________________________________________________________________
max_pooling2d_23 (MaxPooling (None, 15, 15, 64) 0
_________________________________________________________________
conv2d_24 (Conv2D) (None, 13, 13, 32) 18464
_________________________________________________________________
max_pooling2d_24 (MaxPooling (None, 13, 13, 32) 0
_________________________________________________________________
flatten_10 (Flatten) (None, 5408) 0
_________________________________________________________________
dense_13 (Dense) (None, 10) 54090
=================================================================
Total params: 74,346
Trainable params: 74,346
Non-trainable params: 0
_________________________________________________________________
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(trainX, trainY, validation_data=(validX, validY), batch_size=128, epochs=10)
I was unable to solve the error, does anyone have any ideas on how to fix it?
As i could not see your coding for trainY; seems like - your trainY has only one column and your model output have 10 neurons, so Shapes (None, 1) and (None, 10) are incompatible. you can try this on your trainY(i.e one-hot encoding)
from sklearn.preprocessing import LabelBinarizer
label_as_binary = LabelBinarizer()
train__y_labels = label_as_binary.fit_transform(trainY)
and compile will look like as (look for train__y_labels)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_X_input, train__y_labels, batch_size=128, epochs=1)
note: if your valid also throws the error, same would be needed on all y(s).
Change the compile statement so that
loss = 'sparse_categorical_cross_entropy'
The "sparse" indicates that the y values are numeric rather than one-hot
I am developing a image classification model. I have my input shape of image as (128,128,3) but when I am running the model.fit it is giving an error.
My input data is
real_data = [f for f in os.listdir(data_dir+'/test') if f.endswith('.png')]
fake_data = [f for f in os.listdir(data_dir+'/test_f') if f.endswith('.png')]
print(real_data)
X = []
Y = []
for img in real_data:
X.append(img_to_array(load_img(data_dir+'/test/'+img)) / 255.0)
Y.append(1)
for img in fake_data:
X.append(img_to_array(load_img(data_dir+'/test_f/'+img)) / 255.0)
Y.append(0)
Y_val_org = Y
X = np.array(X)
Y = to_categorical(Y, 2)
print(X)
print(Y)
My model is
model = Sequential()
model.add(Conv2D(16, kernel_size=(3,3), activation='relu',input_shape=(128,128,3)))
model.add(Conv2D(16, kernel_size=(3,3), activation='relu'))
model.add(Dense(units=3, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer=optimizers.Adam(lr=1e-5, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False),
metrics=['accuracy'])
#model.build(input_shape=(128,128,3))
model.summary()
And model summary is
Model: "sequential_80"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_892 (Conv2D) (None, 126, 126, 16) 448
_________________________________________________________________
conv2d_893 (Conv2D) (None, 124, 124, 16) 2320
_________________________________________________________________
dense_48 (Dense) (None, 124, 124, 3) 51
=================================================================
Total params: 2,819
Trainable params: 2,819
Non-trainable params: 0
_________________________________________________________________
When I am fitting the model through model.fit()
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=2, mode='auto')
EPOCHS = 20
BATCH_SIZE = 100
history = model.fit(X_train, Y_train, batch_size = BATCH_SIZE, epochs = EPOCHS, validation_data = (X_val, Y_val))
This is the error I am getting
Epoch 1/20
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-168-b3e2ed37ed88> in <module>()
2 EPOCHS = 20
3 BATCH_SIZE = 100
----> 4 history = model.fit(X_train, Y_train, batch_size = BATCH_SIZE, epochs = EPOCHS, validation_data = (X_val, Y_val))
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
975 except Exception as e: # pylint:disable=broad-except
976 if hasattr(e, "ag_error_metadata"):
--> 977 raise e.ag_error_metadata.to_exception(e)
978 else:
979 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 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:2730 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:3417 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:756 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:203 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:152 __call__
losses = call_fn(y_true, y_pred)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:256 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:1608 binary_crossentropy
K.binary_crossentropy(y_true, y_pred, from_logits=from_logits), axis=-1)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py:4979 binary_crossentropy
return nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/nn_impl.py:174 sigmoid_cross_entropy_with_logits
(logits.get_shape(), labels.get_shape()))
ValueError: logits and labels must have the same shape ((None, 124, 124, 3) vs (None, 2))
Change your model into:
model.add(Conv2D(16, kernel_size=(3,3), activation='relu'))
model.add(Flatten()) # added flatten before dense
model.add(Dense(units=2, activation='softmax'))
Last output should be 2 units because you have 2 classes. Also change your loss to:
loss='categorical_crossentropy'
because you applied to_categorical().