AssertionError: Could not compute output Tensor("dense/Sigmoid:0", shape=(None, 1), dtype=float32) - tensorflow

I am trying to create a multi branch CNN-LSTM model. I have created 4 models and concatenate it. I am getting the assertion error. Here is my code.
top_words= 50
(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words)
# Pad all sequences
padded_inputs = pad_sequences(X_train, maxlen=top_words, value = 0.0)
padded_inputs_test = pad_sequences(X_test, maxlen=top_words, value = 0.0)
# create the model
embedding_vecor_length = 32
model1_input = keras.Input(shape= 250)
model2_input = keras.Input(shape= 250)
model3_input = keras.Input(shape= 250)
model4_input = keras.Input(shape= 250)
embedding_layer = Embedding(top_words, embedding_vecor_length)
encoded_model1_input = embedding_layer(model1_input)
encoded_model2_input = embedding_layer(model2_input)
encoded_model3_input = embedding_layer(model3_input)
encoded_model4_input = embedding_layer(model4_input)
model1 = Conv1D(filters=32, kernel_size=3, padding='same', activation='relu',
kernel_regularizer=regularizers.l2(0.01) )(encoded_model1_input)
model1 = MaxPooling1D(pool_size=2)(model1)
model1 = keras.layers.Dropout(0.4)(model1)
model1 = keras.layers.BatchNormalization()(model1)
model1 = Bidirectional(LSTM(128))(model1)
model2 = Conv1D(filters=32, kernel_size=5, padding='same', activation='relu',
kernel_regularizer=regularizers.l2(0.01) )(encoded_model2_input)
model2 = MaxPooling1D(pool_size=2)(model2)
model2 = keras.layers.Dropout(0.4)(model2)
model2 = keras.layers.BatchNormalization()(model2)
model2 = Bidirectional(LSTM(128))(model2)
model3 = Conv1D(filters=32, kernel_size=7, padding='same', activation='relu',
kernel_regularizer=regularizers.l2(0.01) )(encoded_model3_input)
model3 = MaxPooling1D(pool_size=2)(model3)
model3 = keras.layers.Dropout(0.4)(model3)
model3 = keras.layers.BatchNormalization()(model3)
model3 = Bidirectional(LSTM(128))(model3)
model4 = Conv1D(filters=32, kernel_size=9, padding='same', activation='relu',
kernel_regularizer=regularizers.l2(0.01) )(encoded_model3_input)
model4 = MaxPooling1D(pool_size=2)(model4)
model4 = keras.layers.Dropout(0.4)(model4)
model4 = keras.layers.BatchNormalization()(model4)
model4 = Bidirectional(LSTM(128))(model4)
abc = keras.layers.concatenate([model1, model2, model3, model4])
out = Dense(1, activation='sigmoid')(abc)
model = keras.Model(inputs=[model1_input, model2_input, model3_input,model4_input], outputs=[out])
rmsprop= optimizers.RMSprop(lr=0.01, decay=0.1)
model.compile(loss='binary_crossentropy', optimizer= rmsprop, metrics=['accuracy'])
model.summary()
model.fit(padded_inputs, y_train, epochs=15, verbose=True, validation_split= 0.2)
Model Architecture
What's wrong with this code. I am not getting exactly what I am doing wrong. Am I doing something wrong with model.fit() Please help

Related

Keras model.fit does not complain incompatible shape?

I defined my model starting with inputs = tf.keras.Input(shape=(512, 512, 3), batch_size=BATCH_SIZE). Then I use model.fit with data of shape (1, 720, 1280, 3). The model still trains normally and the loss decreases. Why is that?
Thanks.
I tried to train with same shape. But the result did not turns out to be good.
So here is the code:
inputs = tf.keras.Input(shape=(512, 512, 3), batch_size=BATCH_SIZE) # REVISE,
x = tf.keras.layers.Cropping2D(cropping=((256, 0), (0, 0)))(inputs)
x = tf.keras.layers.Resizing(66,200)(x)
# x = x / 255
x = tf.keras.layers.Conv2D(24, (5,5), (2,2), activation="elu")(x)
x = tf.keras.layers.Conv2D(36, (5,5), (2,2), activation="elu")(x)
x = tf.keras.layers.Conv2D(48, (5,5), (2,2), activation="elu")(x)
x = tf.keras.layers.Conv2D(64, (3,3), activation="elu")(x)
x = tf.keras.layers.Dropout(0.2)(x)
x = tf.keras.layers.Conv2D(64, (3,3), activation="elu")(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dropout(0.2)(x)
x = tf.keras.layers.Dense(100, activation='elu')(x)
x = tf.keras.layers.Dense(50, activation='elu')(x)
x = tf.keras.layers.Dense(10, activation='elu')(x)
outputs = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
print(model.summary())
optimizer = Adam(learning_rate=1e-3)
model.compile(loss='mse', optimizer=optimizer)
img_gen = image_data_generator(X_train, y_train, batch_size=1)
X = next(img_gen)[0]
print(X.shape)
print(model(X).shape)
# saves the model weights after each epoch if the validation loss decreased
checkpoint_callback = keras.callbacks.ModelCheckpoint(filepath=os.path.join(model_output_dir,'lane_navigation_new_nvmodel_big2'), verbose=1, save_best_only=True) # revise
with tf.device('/device:GPU:0'):
history = model.fit(image_data_generator( X_train, y_train, batch_size=BATCH_SIZE), # 重要!batch_size大了好训练,1个batch_size训不出东西!
steps_per_epoch=10, # 300
epochs=300,
validation_data = image_data_generator( X_valid, y_valid, batch_size=BATCH_SIZE),
validation_steps=10, # 200
verbose=1,
shuffle=1,
callbacks=[checkpoint_callback])

All predictions returning same value after update to tensorflow-gpu

everything was going well with these cnn to classify MarkovTransitionField sequences, when I was using tensorflow, but then I changed to tensorflow-gpu and all predictions return same value, so model isn't learning (but is fast)
#model 1
def model_1_signal():
model = Sequential()
model.add(Conv2D(73, (5,5), strides = (2,2), activation = 'relu',
padding = 'same', input_shape = (145,5,5),
kernel_initializer = 'he_normal',
bias_initializer = 'zeros'))
model.add(Conv2D(73, (5,5), strides = (2,2), activation = 'relu',
padding = 'same', kernel_initializer = 'he_normal',
bias_initializer = 'zeros'))
model.add(Flatten())
model.add(Dense(2, activation = 'sigmoid',
kernel_initializer = 'glorot_uniform',
bias_initializer = 'zeros'))
model.compile(loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'])
return model
def model_2_signal():
model = Sequential()
model.add(Conv2D(73, (5,5), activation = 'relu',
padding = 'same', input_shape = (145,5,5)))
model.add(Dropout(0.2))
model.add(Conv2D(73, (5,5), strides = (2,2), activation = 'relu',
padding = 'same'))
model.add(Dropout(0.2))
model.add(Conv2D(73, (5,5), strides = (2,2), activation = 'relu',
padding = 'same'))
model.add(Dropout(0.2))
model.add(Conv2D(73, (5,5), strides = (2,2), activation = 'relu',
padding = 'same'))
model.add(MaxPooling2D(pool_size = (1,1),strides = 3))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(64, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(64, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(2, activation = 'sigmoid'))
model.compile(loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'])
return model
EPOCHS = 300
BATCH_SIZE = 15
train_X, val_X, train_y, val_y, train_date, val_date = train_test_split(bullish_episodes_img,y,date,test_size = 0.13, shuffle = False)
val_X, test_X, val_y, test_y, val_date, test_date = train_test_split(val_X, val_y, val_date, test_size = 0.38, shuffle = False)
model_1 = model_1_signal()
model_1.fit(train_X,train_y, validation_data=(val_X,val_y),
epochs = EPOCHS, batch_size = BATCH_SIZE, verbose = 2,
shuffle = True)
yhat_model1 = model_1.predict(test_X)
yhat_model1 = np.where(yhat_model1 >= 0.5, 1, 0)
df1 = pd.DataFrame({'signal':yhat_model1[:,1],'test':test_y[:,1],'time':test_date})
df1 = df1.sort_values(by='time')
#model_1.save('/home/f320x/Documents/AT/Py (1)/final_project/new_standard/spyder/spyder/EURCAD/signal_model/signal_model1')
model_2 = model_2_signal()
model_2.fit(train_X,train_y, validation_data=(val_X,val_y),
epochs = EPOCHS, batch_size = BATCH_SIZE, verbose = 2,
shuffle = True)
yhat_model2 = model_2.predict(test_X)
yhat_model2 = np.where(yhat_model2 >= 0.5, 1, 0)
df2 = pd.DataFrame({'signal':yhat_model2[:,1],'test':test_y[:,1],'time':test_date})
df2 = df2.sort_values(by='time')
before transform data to MarkovTransitionField, i scaled all values between 0 and 1 and there's no nan in dataset.
does somebody has a hint?

Tensorflow ModelCheckpoint not saving model, no loss after reloading

The callback is saving checkpoint files, but not the SavedModel model.pb file. Additionally, when I load the model from the checkpoints it does not reload 'val_loss' which I'm conditioning "save_best_model" on.
I tried using a model.save() only on the best iteration but was having trouble with getting that to work correctly and it would be more convenient to use the ModelCheckpoint callback.
Here is the relevant code
LOSS = tf.keras.losses.MeanSquaredError(),
#multi output 3 categories from 0 to 1
model = ImgToClassSimpleContinuous(img_height, img_width)
checkpoint_filename = "../chkpts/ImgToClassSimpleContinuous/checkpoint_dir"
model.load_weights(checkpoint_filename)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_filename,
verbose=1,mode='min', monitor="val_loss", save_best_only=True, save_weights_only=False)
model.compile(
optimizer='adam',
loss = [LOSS, LOSS, LOSS],
metrics=['mse'])
model.fit(
dataset_to_use,
validation_data = dataset_validation_batched,
# validation_steps=50,
epochs=MAX_EPOCHS,
batch_size=BATCH_SIZE,
callbacks=[cp_callback]
)
class ImgToClassSimpleContinuous(Model):
'''
pair with loss = categorical_crossentropy
'''
in_types = [DataType.d]
out_types = [DataType.tlc, DataType.tls, DataType.tll]
def __init__(self, img_height, img_width, *args, **kwargs):
super().__init__(ImgToClassSimple, *args, **kwargs)
initializer = 'he_normal'
input_shape = (img_height, img_width, 1)
inputs = tf.keras.Input(shape=input_shape)
flat_pix = layers.Flatten()(inputs)
x = layers.Conv2D(8, 3, padding='same', kernel_initializer=initializer)(inputs)
x = layers.PReLU()(x)
x = layers.Conv2D(8, 3, padding='same', kernel_initializer=initializer)(x)
x = layers.PReLU()(x)
x = layers.MaxPooling2D(pool_size=(2, 2))(x)
x = layers.BatchNormalization()(x)
x = layers.Conv2D(16, 3, padding='same', kernel_initializer=initializer)(x)
x = layers.PReLU()(x)
x = layers.Conv2D(16, 3, padding='same', kernel_initializer=initializer)(x)
x = layers.PReLU()(x)
x = layers.MaxPooling2D(pool_size=(2, 2))(x)
x = layers.BatchNormalization()(x)
t = layers.Conv2D(32, 3, padding='same', kernel_initializer=initializer)(x)
t = layers.PReLU()(t)
t = layers.Conv2D(32, 3, padding='same', kernel_initializer=initializer)(t)
t = layers.PReLU()(t)
t = layers.MaxPooling2D(pool_size=(2, 2))(t)
t = layers.BatchNormalization()(t)
t = tf.keras.layers.GlobalAveragePooling2D()(t)
t = layers.Flatten()(t)
s = layers.Conv2D(32, 3, padding='same', kernel_initializer=initializer)(x)
s = layers.PReLU()(s)
s = layers.Conv2D(32, 3, padding='same', kernel_initializer=initializer)(s)
s = layers.PReLU()(s)
s = layers.MaxPooling2D(pool_size=(2, 2))(s)
s = layers.BatchNormalization()(s)
s = tf.keras.layers.GlobalAveragePooling2D()(s)
s = layers.Flatten()(s)
l = layers.Conv2D(32, 3, padding='same', kernel_initializer=initializer)(x)
l = layers.PReLU()(l)
l = layers.Conv2D(32, 3, padding='same', kernel_initializer=initializer)(l)
l = layers.PReLU()(l)
l = layers.MaxPooling2D(pool_size=(2, 2))(l)
l = layers.BatchNormalization()(l)
l = tf.keras.layers.GlobalAveragePooling2D()(l)
l = layers.Flatten()(l)
t = layers.Dense(1, activation='sigmoid')(t)
s = layers.Dense(1, activation='sigmoid')(s)
l = layers.Dense(1, activation='sigmoid')(l)
# A Dense classifier with a single unit (binary classification)
self.model = tf.keras.Model(inputs, [t, s, l])
tf.keras.utils.plot_model(self.model, to_file="...", show_shapes=True)
def call(self, x):
return self.model(x)

ValueError: Shape (None, 17) must have rank 1

I am working on a hand character recognition model. I created a CNN+BiLSTM+CTC Loss model. But getting error when I run model.fit(). Please help me fix this error.
My Model
# input with shape of height=32 and width=128
inputs = Input(shape=(32,128,1))
# convolution layer with kernel size (3,3)
conv_1 = Conv2D(64, (3,3), activation = 'relu', padding='same')(inputs)
# poolig layer with kernel size (2,2)
pool_1 = MaxPooling2D(pool_size=(2, 2), strides=2)(conv_1)
conv_2 = Conv2D(128, (3,3), activation = 'relu', padding='same')(pool_1)
pool_2 = MaxPooling2D(pool_size=(2, 2), strides=2)(conv_2)
conv_3 = Conv2D(256, (3,3), activation = 'relu', padding='same')(pool_2)
conv_4 = Conv2D(256, (3,3), activation = 'relu', padding='same')(conv_3)
# poolig layer with kernel size (2,1)
pool_4 = MaxPooling2D(pool_size=(2, 1))(conv_4)
conv_5 = Conv2D(512, (3,3), activation = 'relu', padding='same')(pool_4)
# Batch normalization layer
batch_norm_5 = BatchNormalization()(conv_5)
conv_6 = Conv2D(512, (3,3), activation = 'relu', padding='same')(batch_norm_5)
batch_norm_6 = BatchNormalization()(conv_6)
pool_6 = MaxPooling2D(pool_size=(2, 1))(batch_norm_6)
conv_7 = Conv2D(512, (2,2), activation = 'relu')(pool_6)
squeezed = Lambda(lambda x: K.squeeze(x, 1))(conv_7)
# bidirectional LSTM layers with units=128
blstm_1 = Bidirectional(LSTM(128, return_sequences=True, dropout = 0.2))(squeezed)
blstm_2 = Bidirectional(LSTM(128, return_sequences=True, dropout = 0.2))(blstm_1)
outputs = Dense(len(char_dict)+1, activation = 'softmax')(blstm_2)
act_model = Model(inputs, outputs)
Define a CTC loss model that takes the outputs of previous model as inputs
labels = Input(name='the_labels', shape=[max_length], dtype='float32')
input_length = Input(name='input_length', shape=[1], dtype='int64')
label_length = Input(name='label_length', shape=[1], dtype='int64')
def ctc_lambda_func(args):
y_pred, labels, input_length, label_length = args
return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([outputs, labels, input_length,
label_length])
model = Model(inputs=[inputs, labels, input_length, label_length], outputs=loss_out)
model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer = 'adam')
model.fit(x=[input_array,
output_array,
train_input_length,
train_label_length],
y=np.zeros(input_array.shape[0]),
batch_size=256,
epochs = 100,
validation_data = ([test_input_array, test_output_array, valid_input_length,
valid_label_length], [np.zeros(test_input_array.shape[0])]),
verbose = 1,
callbacks = callbacks_list)
The error I am getting is
ValueError: Shape (None, 17) must have rank 1

Conditional variable error when trying to open serialized Keras model

I have the following model:
embedding_matrix = np.zeros((vocab_size, 100))
for word, i in text_tokenizer.word_index.items():
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
embedding_layer = Embedding(vocab_size,
100,
weights=[embedding_matrix],
input_length=50,
trainable=False)
sequence_input = Input(shape=(50,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
text_cnn = Conv1D(filters=64, kernel_size=5, strides=1, padding='same', activation='relu')(embedded_sequences)
text_cnn = Conv1D(filters=32, kernel_size=5, strides=1, padding='same', activation='relu')(text_cnn)
text_cnn = Conv1D(filters=16, kernel_size=5, strides=1, padding='same', activation='relu')(text_cnn)
text_lstm = Bidirectional(LSTM(256, return_sequences=True))(text_cnn)
text_lstm = Dense(128, activation='relu')(text_lstm)
text_lstm = Dropout(0.3)(text_lstm)
char_in = Input(shape=(50, 18, ))
char_cnn = Conv1D(filters=64, kernel_size=5, strides=1, padding='same', activation='relu')(char_in)
char_cnn = Conv1D(filters=32, kernel_size=5, strides=1, padding='same', activation='relu')(char_cnn)
char_cnn = Conv1D(filters=16, kernel_size=5, strides=1, padding='same', activation='relu')(char_cnn)
char_lstm = Bidirectional(LSTM(256, return_sequences=True))(char_cnn)
char_lstm = GaussianNoise(0.50)(char_lstm)
char_lstm = Dense(128, activation='relu')(char_lstm)
char_lstm = Dropout(0.3)(char_lstm)
merged = concatenate([char_lstm, text_lstm])
merged_d1 = Dense(512, activation='relu')(merged)
merged_d1 = Dropout(0.3)(merged_d1)
merged_d1 = Dense(256, activation='relu')(merged_d1)
merged_d1 = GaussianNoise(0.30)(merged_d1)
text_class = Dense(len(y_unique), activation='softmax')(merged_d1)
model = Model([sequence_input,char_in], text_class)
Trained with a GPU like such:
parallel_model = multi_gpu_model(model, gpus=4)
parallel_model.compile(loss='binary_crossentropy',
optimizer='adam', metrics=['accuracy'])
parallel_model.fit([x_train, x_train_char], y_train, validation_data=([x_test, x_test_char], y_test), epochs=1 ,batch_size=512)
I serialize it with the model.save() method.
When I try to load it on the same machine in a different kernel, I get the following error:
ValueError: Initializer for variable conv1d_4_1/kernel/ is from inside a control-flow construct, such as a loop or conditional. When creating a variable inside a loop or conditional, use a lambda as the initializer.
I'm not sure what loop or conditional it can be referring to, maybe something to do with the Bidirectional wrapper? I've serialized other similar models without any issues.
Any help would be greatly appreciated!