How to fix the error "Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 150528 but received input with shape [224, 672]".
I'd problem to build sequantial model. Hope you all will help me on this time.
def decode_csv(csv_row):
record_defaults = ["path", "flower"]
filename, label_string = tf.io.decode_csv(csv_row, record_defaults)
img = read_and_decode(filename, [IMG_HEIGHT, IMG_WIDTH])
return img, label_string
train_dataset (tf.data.TextLineDataset("/home/pi/Downloads/ml_code/train_set.csv").map(decode_csv)).take(500)
eval_dataset = (tf.data.TextLineDataset("/home/pi/Downloads/ml_code/eval_set.csv").map(decode_csv)).take(50)
model =tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape = (IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS)),
tf.keras.layers.Dense(len(CLASS_NAME), activation = "softmax")
])
model.compile(optimizer="adam",
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),
metrics = ['accuracy'])
model.summary()
tf.keras.utils.plot_model(model, show_shapes = True, show_layer_names = False, to_file = "model.jpg")
history = model.fit(train_dataset, validation_data = eval_dataset, epochs = 10)
model.save("first")
json.dump(history.history, open("First_History", "w"))
Try something like this:
import pandas as pd
# Create dummy data
tf.keras.utils.save_img('image1.png', tf.random.normal((64, 64, 3)))
tf.keras.utils.save_img('image2.png', tf.random.normal((64, 64, 3)))
tf.keras.utils.save_img('image3.png', tf.random.normal((64, 64, 3)))
tf.keras.utils.save_img('image4.png', tf.random.normal((64, 64, 3)))
tf.keras.utils.save_img('image5.png', tf.random.normal((64, 64, 3)))
df = pd.DataFrame(data= {'path': ['/content/image1.png', '/content/image2.png', '/content/image3.png', '/content/image4.png', '/content/image5.png'],
'label': ['0', '1', '2', '3', '2']})
df.to_csv('data.csv', index=False)
Preprocess data and train:
import tensorflow as tf
def decode_csv(csv_row):
record_defaults = ["path", "label"]
filename, label_string = tf.io.decode_csv(csv_row, record_defaults)
img = tf.io.decode_png(tf.io.read_file(filename), channels=3)
return img, tf.strings.to_number(label_string, out_type=tf.int32)
# Skip header row.
train_dataset = tf.data.TextLineDataset("/content/data.csv").skip(1).map(decode_csv).batch(2)
model =tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape = (64, 64, 3)),
tf.keras.layers.Dense(4, activation = "softmax")
])
model.compile(optimizer="adam",
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),
metrics = ['accuracy'])
model.summary()
tf.keras.utils.plot_model(model, show_shapes = True, show_layer_names = False, to_file = "model.jpg")
history = model.fit(train_dataset, epochs = 2)
model.save("first")
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_2 (Flatten) (None, 12288) 0
dense_2 (Dense) (None, 4) 49156
=================================================================
Total params: 49,156
Trainable params: 49,156
Non-trainable params: 0
_________________________________________________________________
Epoch 1/2
3/3 [==============================] - 1s 62ms/step - loss: 623.7551 - accuracy: 0.4000
Epoch 2/2
3/3 [==============================] - 0s 7ms/step - loss: 1710.6586 - accuracy: 0.2000
INFO:tensorflow:Assets written to: first/assets
My Final Fix Code is here:
def read_and_decode(filename, reshape_dims):
img = tf.io.read_file(filename)
# Range [0, 255]
img = tf.image.decode_jpeg(img, channels = 3)
#convert into range[0, 1] for ml flexible
img = tf.image.convert_image_dtype(img, tf.float32)
return img
def decode_csv(csv_row):
record_defaults = ["path", "flower"]
filenames, label_string = tf.io.decode_csv(csv_row, record_defaults, field_delim = ",")
img = read_and_decode(filenames, [IMG_HEIGHT, IMG_WIDTH])
return img, tf.argmax(tf.cast(label_string == CLASS_NAME, tf.int32))
train_dataset = tf.data.TextLineDataset("/home/pi/Downloads/ml_code/train_set.csv").skip(1).map(decode_csv).batch(2)
eval_dataset = tf.data.TextLineDataset("/home/pi/Downloads/ml_code/eval_set.csv").skip(1).map(decode_csv).batch(2)
model =tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape = (IMG_HEIGHT, IMG_WIDTH, 3)),
tf.keras.layers.Dense(len(CLASS_NAME), activation = "softmax")
])
model.compile(optimizer="adam",
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),
metrics = ['accuracy'])
model.summary()
tf.keras.utils.plot_model(model, show_shapes = True, show_layer_names = False, to_file = "model.jpg")
history = model.fit(train_dataset, validation_data = eval_dataset, epochs = 2)
model.save("first")
json.dump(history.history, open("First_History", "w"))
All credits should go to AloneTogether, Thanks for your help ^_^.
Related
I am new to neural networks and I am working on prediction from images. I would like to predict 101 data points from an image. The images are made from data measured by sensors . I have tried the following code, but when I predict, I always get the same prediction. Any idea how to solve this problem? I also tried with more epochs, different layers, different activation functions and got the same problem.
Trainingfolder = 'C:\Users\sozan\Documents\SmartLocomotion\Dataset\Dataset1\Trainingset'
Testfolder = r'C:\Users\sozan\Documents\Smart Locomotion\Dataset\Dataset1\Testset'
image_files = [os.path.join(Trainingfolder, f) for f in os.listdir(Trainingfolder) if f.endswith('.jpeg')]
plt.figure()
for i in range(5):
imgfile = random.choice(image_files)
img = image.imread(imgfile)
outname = imgfile[0:-5] + ".mat"
outfile = scipy.io.loadmat(outname, squeeze_me=True,struct_as_record=False)
output = outfile['output'].AnkleFlexExt
ax=plt.subplot(2,5,i+1)
plt.imshow(img)
ax=plt.subplot(2,5,i+1+5)
plt.plot(output)
dataset : the image is made from sensors data and the curve represent the output data that I would like to predict
def create_dataset(datafolder):
image_files = [os.path.join(datafolder, f) for f in os.listdir(datafolder) if f.endswith('.jpeg')]
img_data_array = []
output_data = []
for imgfile in image_files:
imag = image.imread(imgfile)
imag = np.array(imag)
imag = imag.astype('float64')
imag /= 255
img_data_array.append(imag)
outname = imgfile[0:-5] + ".mat"
outfile = scipy.io.loadmat(outname, squeeze_me=True,struct_as_record=False)
output = outfile['output'].AnkleFlexExt
output_data.append(output)
return img_data_array, output_data
img_Trainingdata, output_Trainingdata = create_dataset(Trainingfolder)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=(101,9,3)))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2), padding='same'))
model.add(tf.keras.layers.Conv2D(128, kernel_size=(3,3), activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(3000, activation = 'relu'))
model.add(tf.keras.layers.Dense(101))
adam = tf.keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False);
model.compile(optimizer=adam, loss='mse', metrics=["mse"])
model.summary()
Layer (type)
Output Shape
Param #
conv2d_2 (Conv2D)
(None, 99, 7, 64)
1792
max_pooling2d (MaxPooling2D)
(None, 50, 4, 64)
0
conv2d_3 (Conv2D)
(None, 48, 2, 128)
73856
max_pooling2d_1 (MaxPooling2D)
(None, 24, 1, 128)
0
flatten_1 (Flatten)
(None, 3072)
0
dense_2 (Dense)
(None, 3000)
9219000
dense_3 (Dense)
(None, 101)
303101
Total params: 9,597,749
Trainable params: 9,597,749
Non-trainable params: 0
history = model.fit(x=np.array(img_Trainingdata, np.float64), y=np.array(output_Trainingdata, np.float64), epochs=1)
36/36 [==============================] - 2s 55ms/step - loss: 0.0639 - mse: 0.0639
def load_Testdata(dataname):
img_data_array = []
imag = image.imread(imgfile)
imag = np.array(imag)
imag = imag.astype('float64')
imag /= 255
img_data_array.append(imag)
return img_data_array
image_test = [os.path.join(Testfolder, f) for f in os.listdir(Testfolder) if f.endswith('.jpeg')]
plt.figure()
for itest in image_test:
outname = itest[0:-5] + ".mat"
outfile = scipy.io.loadmat(outname, squeeze_me=True,struct_as_record=False)
output = outfile['output'].AnkleFlexExt
plt.plot(output, color='blue', label='Measured')
for itest in image_test:
img_test = load_Testdata(itest)
pred = model.predict (x=np.array(img_test, np.float64))
pred = pred[0]
plt.plot(pred, color='red', label='Predicted')
plt.title('Ankle Flex-Ext Moment')
plt.ylabel('Nm.kg$^{-1}$')
plt.xlabel('% cycle')
plt.show()
So I have a problem when train deep learning with BERT with tensorflow which contain text dataset. So i want to fit() the model but got an error when training. I think it happen because the data_train did't have the label. But from my research It also same problem like SO question in here Same problem. Since it didn't have a answer is this a bug? The error is like this
ValueError: Target data is missing. Your model was compiled with loss=<keras.losses.CategoricalCrossentropy object at 0x7fa707d96fd0>, and therefore expects target data to be provided in `fit()`.
My code like this
X_input_ids = np.zeros((len(df), 256))
X_attn_masks = np.zeros((len(df), 256))
def generate_training_data(df, ids, masks, tokenizer):
for i, text in tqdm(enumerate(df['text'])):
tokenized_text = tokenizer.encode_plus(
text,
max_length=256,
truncation=True,
padding='max_length',
add_special_tokens=True,
return_tensors='tf'
)
ids[i, :] = tokenized_text.input_ids
masks[i, :] = tokenized_text.attention_mask
return ids, masks
X_input_ids, X_attn_masks = generate_training_data(df, X_input_ids, X_attn_masks, tokenizer)
labels = np.zeros((len(df), 3))
labels[np.arange(len(df)), df['label'].values] = 1
dataset = tf.data.Dataset.from_tensor_slices((X_input_ids, X_attn_masks, labels))
def SentimentDatasetMapFunction(input_ids, attn_masks, labels):
return {
'input_ids': input_ids,
'attention_mask': attn_masks
},
dataset = dataset.map(SentimentDatasetMapFunction)
dataset = dataset.shuffle(2000).batch(6, drop_remainder=True)
p = 0.8
train_size = int((len(df)//16)*p)
train_dataset = dataset.take(train_size)
val_dataset = dataset.skip(train_size)
model = TFBertModel.from_pretrained('cahya/bert-base-indonesian-522M')
input_ids = tf.keras.layers.Input(shape=(256,), name='input_ids', dtype='int32')
attn_masks = tf.keras.layers.Input(shape=(256,), name='attention_mask', dtype='int32')
bert_embds = model.bert(input_ids, attention_mask=attn_masks)[1]
intermediate_layer = tf.keras.layers.Dense(512, activation='relu', name='intermediate_layer')(bert_embds)
output_layer = tf.keras.layers.Dense(3, activation='softmax', name='output_layer')(intermediate_layer) # softmax -> calcs probs of classes
sentiment_model = tf.keras.Model(inputs=[input_ids, attn_masks], outputs=output_layer)
sentiment_model.summary()
optim = tf.keras.optimizers.Adam(learning_rate=1e-5, decay=1e-6)
loss_func = tf.keras.losses.CategoricalCrossentropy()
acc = tf.keras.metrics.CategoricalAccuracy('accuracy')
optim = tf.keras.optimizers.Adam(learning_rate=1e-5, decay=1e-6)
loss_func = tf.keras.losses.CategoricalCrossentropy()
acc = tf.keras.metrics.CategoricalAccuracy('accuracy')
sentiment_model.compile(optimizer=optim, loss=loss_func, metrics=[acc])
hist = sentiment_model.fit(
train_dataset,
validation_data=val_dataset,
epochs=2
)
I spend a bit of time finding something we can update and I download the model from the websites.
[ Sample ]:
import tensorflow as tf
import tensorflow_text as text # Registers the ops.
import tensorflow_hub as hub
import os
from os.path import exists
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def generate_training_data(train_labels):
input_ids = [ ]
attn_masks = [ ]
labels = [ ]
for item in train_labels:
input_ids.append( str(item) )
attn_masks.append( int(1) )
labels.append( item )
attn_masks = tf.constant(attn_masks, shape=(1, len(attn_masks),1), dtype=tf.float32)
labels = tf.constant(labels, shape=(1, len(labels),1), dtype=tf.int64)
input_ids = tf.constant(input_ids, shape=(1, len(input_ids),1), dtype=tf.string)
return input_ids, attn_masks, labels
def SentimentDatasetMapFunction(input_ids, attn_masks, labels):
return {
'input_ids': input_ids,
'attention_mask': attn_masks,
'labels': labels
},
def build_classifier_model():
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='text')
preprocessing_layer = hub.KerasLayer(tfhub_handle_preprocess, name='preprocessing')
encoder_inputs = preprocessing_layer(text_input)
encoder = hub.KerasLayer(tfhub_handle_encoder, trainable=True, name='BERT_encoder')
outputs = encoder(encoder_inputs)
net = outputs['pooled_output']
net = tf.keras.layers.Dropout(0.1)(net)
net = tf.keras.layers.Dense(1, activation=None, name='classifier')(net)
return tf.keras.Model(text_input, net)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
DataSet
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()
X_input_ids, X_attn_masks, labels = generate_training_data(train_labels)
dataset = tf.data.Dataset.from_tensor_slices((X_input_ids, X_attn_masks))
options = tf.saved_model.LoadOptions(
allow_partial_checkpoint=False,
experimental_io_device="/physical_device:GPU:0",
experimental_skip_checkpoint=True
)
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='sentences')
preprocessor = hub.KerasLayer(export_dir)
encoder_inputs = preprocessor(text_input)
encoder = hub.KerasLayer( export_dir_2, trainable=False, load_options=options)
outputs = encoder(encoder_inputs)
intermediate_layer = tf.keras.layers.Dense(512, activation='relu', name='intermediate_layer')(outputs['default'])
output_layer = tf.keras.layers.Dense(1, activation='softmax', name='output_layer')(intermediate_layer)
sentiment_model = tf.keras.Model(inputs=[text_input], outputs=output_layer)
sentiment_model.summary()
optim = tf.keras.optimizers.Adam(learning_rate=1e-5, decay=1e-6)
loss_func = tf.keras.losses.CategoricalCrossentropy()
acc = tf.keras.metrics.CategoricalAccuracy('accuracy')
optim = tf.keras.optimizers.Adam(learning_rate=1e-5, decay=1e-6)
loss_func = tf.keras.losses.CategoricalCrossentropy()
acc = tf.keras.metrics.CategoricalAccuracy('accuracy')
sentiment_model.compile(optimizer=optim, loss=loss_func, metrics=[acc])
hist = sentiment_model.fit(
dataset,
validation_data=dataset,
epochs=2
)
[ Output ]:
outputs: KerasTensor(type_spec=TensorSpec(shape=(None, 512), dtype=tf.float32, name=None), name='keras_layer_1/StatefulPartitionedCall:0', description="created by layer 'keras_layer_1'")
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
sentences (InputLayer) [(None,)] 0 []
keras_layer (KerasLayer) {'input_mask': (Non 0 ['sentences[0][0]']
e, 128),
'input_word_ids':
(None, 128),
'input_type_ids':
(None, 128)}
keras_layer_1 (KerasLayer) {'default': (None, 28763649 ['keras_layer[0][0]',
512), 'keras_layer[0][1]',
'encoder_outputs': 'keras_layer[0][2]']
[(None, 128, 512),
(None, 128, 512),
(None, 128, 512),
(None, 128, 512)],
'sequence_output':
(None, 128, 512),
'pooled_output': (
None, 512)}
intermediate_layer (Dense) (None, 512) 262656 ['keras_layer_1[0][0]']
output_layer (Dense) (None, 1) 513 ['intermediate_layer[0][0]']
==================================================================================================
Total params: 29,026,818
Trainable params: 263,169
Non-trainable params: 28,763,649
__________________________________________________________________________________________________
Epoch 1/2
I am getting this error when I am trying to train my model:
ValueError: Input 0 of layer dense_encoder is incompatible with the layer: expected axis -1 of input shape to have value 2048 but received input with shape [446, 98, 1024]
My model architecture is:
input1 = Input(shape=(2048), name='Image_1')
dense1 = Dense(256, kernel_initializer=tf.keras.initializers.glorot_uniform(seed = 56), name='dense_encoder')(input1)
input2 = Input(shape=(153), name='Text_Input')
emb_layer = Embedding(input_dim = vocab_size, output_dim = 300, input_length=153, mask_zero=True, trainable=False,
weights=[embedding_matrix], name="Embedding_layer")
emb = emb_layer(input2)
LSTM1 = LSTM(units=256, activation='tanh', recurrent_activation='sigmoid', use_bias=True,
kernel_initializer=tf.keras.initializers.glorot_uniform(seed=23),
recurrent_initializer=tf.keras.initializers.orthogonal(seed=7),
bias_initializer=tf.keras.initializers.zeros(), return_sequences=True, name="LSTM1")(emb)
#LSTM1_output = LSTM1(emb)
LSTM2 = LSTM(units=256, activation='tanh', recurrent_activation='sigmoid', use_bias=True,
kernel_initializer=tf.keras.initializers.glorot_uniform(seed=23),
recurrent_initializer=tf.keras.initializers.orthogonal(seed=7),
bias_initializer=tf.keras.initializers.zeros(), name="LSTM2")
LSTM2_output = LSTM2(LSTM1)
dropout1 = Dropout(0.5, name='dropout1')(LSTM2_output)
dec = tf.keras.layers.Add()([dense1, dropout1])
fc1 = Dense(256, activation='relu', kernel_initializer=tf.keras.initializers.he_normal(seed = 63), name='fc1')
fc1_output = fc1(dec)
dropout2 = Dropout(0.4, name='dropout2')(fc1_output)
output_layer = Dense(vocab_size, activation='softmax', name='Output_layer')
output = output_layer(dropout2)
encoder_decoder = Model(inputs = [input1, input2], outputs = output)
encoder_decoder.summary()
Here's my code for training the model:
for epoch in range(20):
print('EPOCH : ',epoch+1)
start = time.time()
batch_loss_tr = 0
batch_loss_vl = 0
for img, report in train_generator:
r1 = bytes_to_string(report.numpy())
img_input, rep_input, output_word = convert(img.numpy(), r1)
rep_input = pad_sequences(rep_input, maxlen=153, padding='post')
results = encoder_decoder.train_on_batch([img_input, rep_input], output_word)
batch_loss_tr += results
train_loss = batch_loss_tr/(X_train.shape[0]//14)
with train_summary_writer.as_default():
tf.summary.scalar('loss', train_loss, step = epoch)
for img, report in cv_generator:
r1 = bytes_to_string(report.numpy())
img_input, rep_input, output_word = convert(img.numpy(), r1)
rep_input = pad_sequences(rep_input, maxlen=153, padding='post')
results = encoder_decoder.test_on_batch([img_input, rep_input], output_word)
batch_loss_vl += results
The img_input shape is (417, 98, 1024) and I am getting the error for Image_1 layer.
What could be the reasons? Any help would be appreciated.
I was trying to run model.fit. I had a data of 5486 rows with target shape (5486,3). I noticed that while running the model.fit, it printed Running on 4489 training samples and 997 validation samples and it started with
Epoch 1/32
5/4856 [>.............................] - ETA: 180s - loss: 0.3286 - accuracy: 0.5406.............
something
Next day I tried another script and while applying fit method it printed
The first code that showed 5/4489 is
`
input_txt = Input(shape=(100,), dtype='int32')
txt = Masking(mask_value=0)(input_txt)
txt = Embedding(len(word_index) + 1, embedding_dim, weights=[embedding_matrix], input_length=max_seq_len, trainable=False)(txt)
txt = Conv1D(32, 5,trainable = False)(txt)
txt = Conv1D(60, 4,trainable = False)(txt)
txt = Conv1D(100, 3,trainable = False)(txt)
text_lstm = Bidirectional(LSTM(30,return_sequences=True,trainable= False))(txt)
text_lstm = Bidirectional(LSTM(30,return_sequences=True,trainable= False))(text_lstm)
text_lstm = Bidirectional(LSTM(30,return_sequences=False,trainable= False))(text_lstm)
lstm = Dense(512, activation='relu')(text_lstm)
lstm = Dropout(0.8)(lstm)
input_img = Input(shape=(224,224,3))
model = VGG16(weights='imagenet', include_top=False)
model.trainable = False
x = model(input_img)
flatten = Flatten()(x)
flatten = Dense(1024, activation='relu')(flatten)
flatten = Dense(512, activation='relu')(flatten)
flatten = Dropout(0.8)(flatten)
merged = concatenate([lstm,flatten], axis=1)
dense = Dense(1024, activation='relu')(merged)
dense = Dropout(0.6)(dense)
dense = Dense(512, activation='relu')(dense)
dense = Dense(256, activation='relu')(dense)
dense = Dense(128, activation='relu')(dense)
dense = Dense(3, activation='softmax')(dense)
model = Model(inputs=(input_img,input_txt), outputs=dense)
model.compile(loss='categorical_crossentropy',optimizer=keras.optimizers.Adam(lr=2e-5),metrics=["accuracy"])
X_trn = [image_data,text_data]
y_trn = labels
model.fit(X_trn,y_trn,validation_split=0.2,epochs = 32, batch_size = 64)
`
Epoch 1/32
5/151 [>.............................] - ETA: 40s - loss: 0.0286 - accuracy: 0.5406.............
And the code that gave the above verbose was
`
vgg = VGG19(weights='imagenet',include_top=False)
vgg.trainable = False
img_in = Input(shape=(224,224,3),dtype = 'int32')
img = vgg(img_in)
img = Flatten()(img)
img = Dense(1024,activation='sigmoid')(img)
img = Dense(512,activation='sigmoid')(img)
img = Dense(64,activation='relu')(img)
img = Dense(32,activation = 'relu')(img)
out = Dense(3,activation='softmax')(img)
model = Model(inputs = [img_in],outputs=out)
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=2e-5),
# optimizer=optimizers.RMSprop(),
metrics=["accuracy"]
model.fit(x=images_data[4856:],y= data_labels[4856:],validation_data=(images_data[:4856], data_labels[:4856]),batch_size =64, epochs=32)
`
I noticed that data drop from 4489 to 101. I tried checking everything, but didn't got anywhere. Someone please explain what happened.
Thanks in advance
I have Keras model shown below where I'm trying to merge an image input with a feature vector of numerical values, but having the following error:
ValueError: Input 0 is incompatible with layer flatten_2: expected
min_ndim=3, found ndim=2
which occurs on the following statement:
value_model.add(Flatten(input_shape=(12,)))
Any ideas on how I can solve the issue?
image_input = Input((512, 512, 1))
vector_input = Input((12,))
image_model = Sequential()
image_model.add(Convolution2D(32,8,8, subsample=(4,4), input_shape=(512,512,1)))
image_model.add(Activation('relu'))
image_model.add(Convolution2D(64,4,4, subsample=(2,2)))
image_model.add(Activation('relu'))
image_model.add(Convolution2D(64,3,3, subsample=(1,1)))
image_model.add(Activation('relu'))
image_model.add(Flatten())
image_model.add(Dense(512))
image_model.add(Activation('relu'))
value_model = Sequential()
value_model.add(Flatten(input_shape=(12,)))
value_model.add(Dense(16))
value_model.add(Activation('relu'))
value_model.add(Dense(16))
value_model.add(Activation('relu'))
value_model.add(Dense(16))
value_model.add(Activation('relu'))
merged = Concatenate([image_model, value_model])
final_model = Sequential()
final_model.add(merged)
final_model.add(Dense(1, activation='sigmoid'))
model = Model(inputs=[image_input, vector_input], outputs=output)
model.compile(loss='binary_crossentropy', optimizer='adam',metrics=['acc'])
model.fit([images, features], y, epochs=5)
EDIT-1
This is the full script:
from keras.layers import Input, Concatenate, Conv2D, Flatten, Dense, Convolution2D, Activation
from keras.models import Model, Sequential
import pandas as pd
import numpy as np
import cv2
import os
def label_img(img):
word_label = img.split('.')[-3]
if word_label == 'r':
return 1
elif word_label == 'i':
return 0
train_directory = '/train'
images = []
y = []
dataset = pd.read_csv('results.csv')
dataset = dataset[[ 'first_value',
'second_value']]
features = dataset.iloc[:,0:12].values
for root, dirs, files in os.walk(train_directory):
for file in files:
image = cv2.imread(root + '/' + file)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.resize(image,(512,512),interpolation=cv2.INTER_AREA)
image = image/255
images.append(image)
label = label_img(file)
y.append(label)
images = np.asarray(images)
images = images.reshape((-1,512,512,1))
image_input = Input((512, 512, 1))
vector_input = Input((12,))
image_model = Sequential()
image_model.add(Convolution2D(32,8,8, subsample=(4,4), input_shape=(512,512,1)))
image_model.add(Activation('relu'))
image_model.add(Convolution2D(64,4,4, subsample=(2,2)))
image_model.add(Activation('relu'))
image_model.add(Convolution2D(64,3,3, subsample=(1,1)))
image_model.add(Activation('relu'))
image_model.add(Flatten())
image_model.add(Dense(512))
image_model.add(Activation('relu'))
value_model = Sequential()
#value_model.add(Flatten(input_shape=(12,)))
value_model.add(Dense(16))
value_model.add(Activation('relu'))
value_model.add(Dense(16))
value_model.add(Activation('relu'))
value_model.add(Dense(16))
value_model.add(Activation('relu'))
merged = Concatenate([image_model, value_model])
final_model = Sequential()
final_model.add(merged)
final_model.add(Dense(1, activation='sigmoid'))
model = Model(inputs=[image_input, vector_input], outputs=output)
model.compile(loss='binary_crossentropy', optimizer='adam',metrics=['acc'])
model.fit([images, features], y, epochs=5)
EDIT-2
When I did the following:
output = final_model.add(Dense(1, activation='sigmoid'))
I still received the same error.
You can change your code to reflect new Keras 2 API like shown below. In your code you are trying a mixed approach of older keras API and Keras 2 API.
I also recommend to use new Conv2D layer instead of Convolution2D layer along with Keras 2 API. The subsample argument is now called strides in Conv2D
image_input = Input((512, 512, 1))
vector_input = Input((12,))
image_model = Conv2D(32,(8,8), strides=(4,4))(image_input)
image_model = Activation('relu')(image_model)
image_model = Conv2D(64,(4,4), strides=(2,2))(image_model)
image_model = Activation('relu')(image_model)
image_model = Conv2D(64,(3,3), strides=(1,1))(image_model)
image_model = Activation('relu')(image_model)
image_model = Flatten()(image_model)
image_model = Dense(512)(image_model)
image_model = Activation('relu')(image_model)
value_model = Dense(16)(vector_input)
value_model = Activation('relu')(value_model)
value_model = Dense(16)(value_model)
value_model = Activation('relu')(value_model)
value_model = Dense(16)(value_model)
value_model = Activation('relu')(value_model)
merged = concatenate([image_model, value_model])
output = Dense(1, activation='sigmoid')(merged)
model = Model(inputs=[image_input, vector_input], outputs=output)
model.compile(loss='binary_crossentropy', optimizer='adam')
Consider a toy dataset,
I = np.random.rand(100, 512, 512, 1)
V = np.random.rand(100, 12, )
y = np.random.rand(100, 1, )
Training,
model.fit([I, V], y, epochs=10, verbose=1)
Epoch 1/10
100/100 [==============================] - 9s 85ms/step - loss: 3.4615
Epoch 2/10
32/100 [========>.....................] - ETA: 4s - loss: 0.9696