Related
I am currently replicating a machine learning architecture found in this paper:
https://downloads.hindawi.com/journals/acisc/2018/1439312.pdf
Specifically on page 4 of the paper:
Any ideas on how to implement this on TensorFlow? My current model looks like this:
BATCH_SIZE = 32
INPUT_SIZE1 = (15, 30, 3)
INPUT_SIZE2 = (30, 60, 3)
INPUT_SIZE3 = (40 ,80, 3)
LEARNING_RATE = 0.001
EPOCHS = 10
CNN_CR1_INPUT = keras.Input(shape = INPUT_SIZE1)
CNN_CR1 = Conv2D(64, (5, 5), strides=2, padding='same', activation='relu')(CNN_CR1_INPUT)
CNN_CR1 = MaxPooling2D(3,3)(CNN_CR1)
CNN_CR1 = Conv2D(64, (3,3), strides=1, padding='same', activation='relu')(CNN_CR1)
CNN_CR1 = Conv2D(64, (3,3), strides=2, padding='same', activation='relu')(CNN_CR1)
CNN_CR1 = Flatten()(CNN_CR1)
CNN_CR1_OUTPUT = Dense(1)(CNN_CR1)
CNN_CR2_INPUT = keras.Input(shape = INPUT_SIZE2)
CNN_CR2 = Conv2D(64, (5,5), strides=2, padding='same', activation='relu')(CNN_CR2_INPUT)
CNN_CR2 = MaxPooling2D(3, 3)(CNN_CR2)
CNN_CR2 = Conv2D(64, (3,3), strides=1, padding='same', activation='relu')(CNN_CR2)
CNN_CR2 = Conv2D(64, (3,3), strides=2, padding='same', activation='relu')(CNN_CR2)
CNN_CR2 = Flatten()(CNN_CR2)
CNN_CR2_OUTPUT = Dense(1)(CNN_CR2)
CNN_CR3_INPUT = keras.Input(shape = INPUT_SIZE3)
CNN_CR3 = Conv2D(64, (5,5), strides=2, padding='same', activation='relu')(CNN_CR3_INPUT)
CNN_CR3 = MaxPooling2D(3, 3)(CNN_CR3)
CNN_CR3 = Conv2D(64, (3,3), strides=1, padding='same', activation='relu')(CNN_CR3)
CNN_CR3 = Conv2D(64, (3,3), strides=2, padding='same', activation='relu')(CNN_CR3)
CNN_CR3 = Flatten()(CNN_CR3)
CNN_CR3_OUTPUT = Dense(1)(CNN_CR3)
# SUGGESTION: This kinda weird. If this works, we only need 1 valitadation datagen? Not sure how it all connect together.
CNN_MAX = Maximum()([CNN_CR1_OUTPUT, CNN_CR2_OUTPUT, CNN_CR3_OUTPUT])
CNN_MODEL = keras.Model(inputs=[CNN_CR1_INPUT, CNN_CR2_INPUT, CNN_CR3_INPUT], outputs=[CNN_MAX])
I am not sure if the model I make is correct or not and I need some assistance. Also, how do you create the input pipeline for a cascading neural network like this one? I already tried this:
TRAIN_DATAGEN1 = ImageDataGenerator(
# SUGGESTION: Not sure if this is needed??
rescale = 1/255.0
)
TRAIN_GENERATOR1 = TRAIN_DATAGEN1.flow_from_directory(
os.path.join(WORKING_DATASETS['GI4E']['train']['images'], '0'),
target_size = (15, 30),
class_mode ='binary',
batch_size = BATCH_SIZE
)
TEST_DATAGEN1 = ImageDataGenerator(
rescale = 1/255.0,
)
TEST_GENERATOR1 = TEST_DATAGEN1.flow_from_directory(
os.path.join(WORKING_DATASETS['GI4E']['test']['images'], '0'),
target_size = (15, 30),
class_mode ='binary',
batch_size = BATCH_SIZE
)
# CNN 2
TRAIN_DATAGEN2 = ImageDataGenerator(
rescale = 1/255.0
)
TRAIN_GENERATOR2 = TRAIN_DATAGEN2.flow_from_directory(
os.path.join(WORKING_DATASETS['GI4E']['train']['images'], '1'),
target_size = (30, 60),
class_mode ='binary',
batch_size = BATCH_SIZE
)
TEST_DATAGEN2 = ImageDataGenerator(
rescale = 1/255.0,
)
TEST_GENERATOR2 = TEST_DATAGEN2.flow_from_directory(
os.path.join(WORKING_DATASETS['GI4E']['test']['images'], '1'),
target_size = (30, 60),
class_mode ='binary',
batch_size = BATCH_SIZE
)
# CNN 3
TRAIN_DATAGEN3 = ImageDataGenerator(
rescale = 1/255.0
)
TRAIN_GENERATOR3 = TRAIN_DATAGEN3.flow_from_directory(
os.path.join(WORKING_DATASETS['GI4E']['train']['images'], '2'),
target_size = (40 ,80),
class_mode = 'binary',
batch_size = BATCH_SIZE
)
TEST_DATAGEN3 = ImageDataGenerator(
rescale = 1/255.0,
)
TEST_GENERATOR3 = TEST_DATAGEN3.flow_from_directory(
os.path.join(WORKING_DATASETS['GI4E']['test']['images'], '2'),
target_size = (40 ,80),
class_mode = 'binary',
batch_size = BATCH_SIZE
)
But it spits out an error when I try to fit it.
ValueError: Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'keras.preprocessing.image.DirectoryIterator'>"}), <class 'NoneType'>
After digging through the documentation, I am aware that TensorFlow cannot handle a bunch of ImageDataGenerator together in a list. But I'm not sure how to feed images to the model without ImageDataGenerator.
So, to summarize:
How to recreate said model in TensorFlow? Is the model that I create correct already?
How to create the input pipeline for the model? Any alternative beside ImageDataGenerator?
You could try building the model with tf.data - load the datas with keras.utils , for data augmentation you can add layers on to model architecture (tf.keras.layers.Rescaling,tf.keras.layers.RandomRotation etc). There are other augmentation methods that are given in the docs below which are more efficient in utilizing the GPU that ImageDataGenerator
https://www.tensorflow.org/tutorials/images/data_augmentation#data_augmentation_2
https://www.tensorflow.org/api_docs/python/tf/keras/utils
https://www.tensorflow.org/guide/keras/preprocessing_layers
mmm = Input(shape=(input_shape))
conv1 = Conv2D(filters=32, kernel_size=3, activation='relu',padding="same")(mmm)
batch_norm1 = BatchNormalization()(conv1)
act1 = ReLU()(batch_norm1)
#pool1 = MaxPooling2D(pool_size=(2, 2))(act1)
conv2 = Conv2D(filters=32, kernel_size=3, activation='relu',padding="same", strides=(2,2))(act1)
batch_norm2 = BatchNormalization()(conv2)
act2 = ReLU()(batch_norm2)
#pool1 = MaxPooling2D(pool_size=(2, 2))(act1)
conv3 = Conv2D(filters=32, kernel_size=3, activation='relu',padding="same")(act2)
batch_norm3 = BatchNormalization()(conv3)
#pool1 = MaxPooling2D(pool_size=(2, 2))(act1)
conv4 = Conv2D(filters=32, kernel_size=3, activation='relu',padding="same", strides=(2,2))(act1)
batch_norm4 = BatchNormalization()(conv4)
act4 = ReLU()(batch_norm4)
#pool1 = MaxPooling2D(pool_size=(2, 2))(act1)
conv5 = Conv2D(filters=32, kernel_size=3, activation='relu',padding="same")(act4)
batch_norm5 = BatchNormalization()(conv5)
#pool1 = MaxPooling2D(pool_size=(2, 2))(act1)
add1 = concatenate([batch_norm3, batch_norm5])
phrase1 = ReLU()(add1)
conv6 = Conv2D(filters=64, kernel_size=3, activation='relu',padding="same", strides=(2,2))(phrase1)
batch_norm6 = BatchNormalization()(conv6)
act6 = ReLU()(batch_norm6)
conv7 = Conv2D(filters=64, kernel_size=3, activation='relu',padding="same")(act6)
batch_norm7 = BatchNormalization()(conv7)
conv8 = Conv2D(filters=64, kernel_size=3, activation='relu',padding="same", strides=(2,2))(phrase1)
batch_norm8 = BatchNormalization()(conv8)
act8 = ReLU()(batch_norm8)
# ninth conv layer
conv9 = Conv2D(filters=64, kernel_size=3, activation='relu',padding="same")(act8)
batch_norm9 = BatchNormalization()(conv9)
add2 = concatenate([batch_norm7, batch_norm9])
phrase2 = ReLU()(add2)
conv10 = Conv2D(filters=128, kernel_size=3, activation='relu',padding="same", strides=(2,2))(phrase2)
batch_norm10 = BatchNormalization()(conv10)
act10 = ReLU()(batch_norm10)
conv11 = Conv2D(filters=128, kernel_size=3, activation='relu',padding="same")(act10)
batch_norm11 = BatchNormalization()(conv11)
conv12 = Conv2D(filters=128, kernel_size=3, activation='relu',padding="same", strides=(2,2))(phrase2)
batch_norm12 = BatchNormalization()(conv12)
act12 = ReLU()(batch_norm12)
conv13 = Conv2D(filters=128, kernel_size=3, activation='relu',padding="same")(act12)
batch_norm13 = BatchNormalization()(conv13)
add3 = concatenate([batch_norm11, batch_norm13])
phrase3 = ReLU()(add3)
conv14 = Conv2D(filters=256, kernel_size=3, activation='relu',padding="same", strides=(2,2))(phrase3)
batch_norm14 = BatchNormalization()(conv14)
act14 = ReLU()(batch_norm14)
conv15 = Conv2D(filters=256, kernel_size=3, activation='relu',padding="same")(act14)
batch_norm15 = BatchNormalization()(conv15)
conv16 = Conv2D(filters=256, kernel_size=3, activation='relu',padding="same", strides=(2,2))(phrase3)
batch_norm16 = BatchNormalization()(conv16)
act16 = ReLU()(batch_norm16)
conv17 = Conv2D(filters=256, kernel_size=3, activation='relu',padding="same")(act16)
batch_norm17 = BatchNormalization()(conv17)
add4 = concatenate([batch_norm15, batch_norm17])
phrase4 = ReLU()(add4)
avgpool = AveragePooling2D(pool_size=8,strides=1)(phrase4)
flat= Flatten()(avgpool)
fc1 = Dense(100, activation='relu')(flat )
Drop1 = Dropout(0.5)(fc1)
fc2 = Dense(1, activation='sigmoid')(Drop1)
model = Model(inputs= mmm, outputs=fc2)
# summarize layers
print(model.summary())
from tensorflow.keras.optimizers import Adam
opt =tf.keras.optimizers.SGD(learning_rate=0.1)
model.compile(loss = 'binary_crossentropy', optimizer= opt, metrics=['accuracy'])
early = EarlyStopping(monitor='val_accuracy', patience=6, verbose=1, mode='max')
history = model.fit(train_generator,epochs = 5, validation_data = test_generator,callbacks = [early])
the code above is my model when i trained it on my MRI images the valaccuracy and val_loss still the same even i changed the epoch
i changed the optimizers and learning rate but still the val_accuracy is the same i hope to see my model if i have errors in the architecture of the model i take the model from DFU_NET and then want to apdate it for my research
please can anyone help me to solve it
I'm running a CRNN model in Keras to perfrom some handwriting recognition but Im getting an error while computing CTC loss.
The problem only occurs when I'm trying to load a pre-trained network for my CNN. It works fine if I make my own CNN network from scratch.
This is the model where I'm getting error:
def get_DensenetLSTM(training):
input_shape = (img_w, img_h, 3)
inputs = Input(name='the_input', shape=input_shape, dtype='float32')
# out = Conv2D(3, (3, 3), padding='same', name='conv1', kernel_initializer='he_normal')(inputs)
# out = Reshape(target_shape=((250, 80, 3)), name='reshape_inp')(inner)
densenet = DenseNet121(include_top=False, weights='imagenet', input_tensor= inputs)
inner = densenet.output
# CNN to RNN
inner = Reshape(target_shape=((32, 1536)), name='reshape')(inner) # (None, 32, 2048)
# print(inner.shape)
inner = Dense(64, activation='relu', kernel_initializer='he_normal', name='dense1')(inner) # (None, 32, 64)
# print(inner.shape)
# RNN layer
lstm_1 = LSTM(256, return_sequences=True, kernel_initializer='he_normal', name='lstm1')(inner) # (None, 32, 512)
lstm_1b = LSTM(256, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='lstm1_b')(inner)
lstm1_merged = add([lstm_1, lstm_1b]) # (None, 32, 512)
lstm1_merged = BatchNormalization()(lstm1_merged)
lstm_2 = LSTM(256, return_sequences=True, kernel_initializer='he_normal', name='lstm2')(lstm1_merged)
lstm_2b = LSTM(256, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='lstm2_b')(lstm1_merged)
lstm2_merged = concatenate([lstm_2, lstm_2b]) # (None, 32, 1024)
lstm_merged = BatchNormalization()(lstm2_merged)
# transforms RNN output to character activations:
inner = Dense(num_classes, kernel_initializer='he_normal',name='dense2')(lstm2_merged) #(None, 32, 63)
y_pred = Activation('softmax', name='softmax')(inner)
labels = Input(name='the_labels', shape=[max_text_len], dtype='float32') # (None ,8)
input_length = Input(name='input_length', shape=[1], dtype='int64') # (None, 1)
label_length = Input(name='label_length', shape=[1], dtype='int64') # (None, 1)
# Keras doesn't currently support loss funcs with extra parameters
# so CTC loss is implemented in a lambda layer
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length]) #(None, 1)
if training:
return Model(inputs=[inputs, labels, input_length, label_length], outputs=loss_out)
else:
return Model(inputs=[inputs], outputs=y_pred)
This works fine:
def get_Model(training):
input_shape = (img_w, img_h, 1)
# Make Network
inputs = Input(name='the_input', shape=input_shape, dtype='float32')
inner = Conv2D(64, (3, 3), padding='same', name='conv1', kernel_initializer='he_normal')(inputs) # (None, 128, 64, 64)
inner = BatchNormalization()(inner)
inner = Activation('relu')(inner)
inner = MaxPooling2D(pool_size=(2, 2), name='max1')(inner)
inner = Conv2D(128, (3, 3), padding='same', name='conv2', kernel_initializer='he_normal')(inner)
inner = BatchNormalization()(inner)
inner = Activation('relu')(inner)
inner = MaxPooling2D(pool_size=(2, 2), name='max2')(inner)
inner = Conv2D(256, (3, 3), padding='same', name='conv3', kernel_initializer='he_normal')(inner)
inner = BatchNormalization()(inner)
inner = Activation('relu')(inner)
inner = Conv2D(256, (3, 3), padding='same', name='conv4', kernel_initializer='he_normal')(inner)
inner = BatchNormalization()(inner)
inner = Activation('relu')(inner)
inner = MaxPooling2D(pool_size=(1, 2), name='max3')(inner)
inner = Conv2D(512, (3, 3), padding='same', name='conv5', kernel_initializer='he_normal')(inner)
inner = BatchNormalization()(inner)
inner = Activation('relu')(inner)
inner = Conv2D(512, (3, 3), padding='same', name='conv6')(inner)
inner = BatchNormalization()(inner)
inner = Activation('relu')(inner)
inner = MaxPooling2D(pool_size=(1, 2), name='max4')(inner)
inner = Conv2D(512, (2, 2), padding='same', kernel_initializer='he_normal', name='con7')(inner)
inner = BatchNormalization()(inner)
inner = Activation('relu')(inner)
# CNN to RNN
inner = Reshape(target_shape=((62, 2560)), name='reshape')(inner)
inner = Dense(64, activation='relu', kernel_initializer='he_normal', name='dense1')(inner)
# RNN layer
lstm_1 = LSTM(256, return_sequences=True, kernel_initializer='he_normal', name='lstm1')(inner)
lstm_1b = LSTM(256, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='lstm1_b')(inner)
lstm1_merged = add([lstm_1, lstm_1b])
lstm1_merged = BatchNormalization()(lstm1_merged)
lstm_2 = LSTM(256, return_sequences=True, kernel_initializer='he_normal', name='lstm2')(lstm1_merged)
lstm_2b = LSTM(256, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='lstm2_b')(lstm1_merged)
lstm2_merged = concatenate([lstm_2, lstm_2b])
lstm_merged = BatchNormalization()(lstm2_merged)
# transforms RNN output to character activations:
inner = Dense(num_classes, kernel_initializer='he_normal',name='dense2')(lstm2_merged)
y_pred = Activation('softmax', name='softmax')(inner)
labels = Input(name='the_labels', shape=[max_text_len], dtype='float32')
input_length = Input(name='input_length', shape=[1], dtype='int64')
label_length = Input(name='label_length', shape=[1], dtype='int64')
# Keras doesn't currently support loss funcs with extra parameters
# so CTC loss is implemented in a lambda layer
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length]) #(None, 1)
if training:
return Model(inputs=[inputs, labels, input_length, label_length], outputs=loss_out)
else:
return Model(inputs=[inputs], outputs=y_pred)
This is the ctc_loss function:
def ctc_lambda_func(args):
y_pred, labels, input_length, label_length = args
# the 2 is critical here since the first couple outputs of the RNN
# tend to be garbage:
y_pred = y_pred[:, 2:, :]
print(y_pred.shape)
print(input_length)
print(labels.shape)
return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
When I run the Densenet_LSTM model, I get the following error:
tensorflow.python.framework.errors_impl.InvalidArgumentError: sequence_length(0) <= 30
[[{{node ctc/CTCLoss}} = CTCLoss[_class=["loc:#training/Adam/gradients/ctc/CTCLoss_grad/mul"], ctc_merge_repeated=true, ignore_longer_outputs_than_inputs=false, preprocess_collapse_repeated=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](ctc/Log/_7309, ctc/ToInt64/_7311, ctc/ToInt32_2/_7313, ctc/ToInt32_1/_7315)]]
Please help.
I'm just getting started with Keras and with Deep learning, so the answer to my question could be obvious to some, but for me it isn't.
I made a model to colorize some black and white photos following the article on Floydhub (where I'm training it) and it works just fine when I train it with similar pictures (such as human faces) but as soon as I use a larger dataset as an input with different pictures, the loss just remains stable and doesn't get better.
I've tried different learning rates and optimizers but just cannot get a good result.
What could I change to get a better result?
This is the code (thanks to Emil Wallner for the article on Floydhub)
# Get images
X = []
for filename in os.listdir('/data/images/Train/'):
X.append(img_to_array(load_img('/data/images/Train/'+filename)))
X = np.array(X, dtype=float)
Xtrain = 1.0/255*X
#Load weights
inception = InceptionResNetV2(weights=None, include_top=True)
inception.load_weights('/data/inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5')
inception.graph = tf.get_default_graph()
embed_input = Input(shape=(1000,))
#Encoder
encoder_input = Input(shape=(256, 256, 1,))
encoder_output = Conv2D(64, (3,3), activation='relu', padding='same', strides=2)(encoder_input)
encoder_output = Conv2D(128, (3,3), activation='relu', padding='same')(encoder_output)
encoder_output = Conv2D(128, (3,3), activation='relu', padding='same', strides=2)(encoder_output)
encoder_output = Conv2D(256, (3,3), activation='relu', padding='same')(encoder_output)
encoder_output = Conv2D(256, (3,3), activation='relu', padding='same', strides=2)(encoder_output)
encoder_output = Conv2D(512, (3,3), activation='relu', padding='same')(encoder_output)
encoder_output = Conv2D(512, (3,3), activation='relu', padding='same')(encoder_output)
encoder_output = Conv2D(256, (3,3), activation='relu', padding='same')(encoder_output)
#Fusion
fusion_output = RepeatVector(32 * 32)(embed_input)
fusion_output = Reshape(([32, 32, 1000]))(fusion_output)
fusion_output = concatenate([encoder_output, fusion_output], axis=3)
fusion_output = Conv2D(256, (1, 1), activation='relu', padding='same')(fusion_output)
#Decoder
decoder_output = Conv2D(128, (3,3), activation='relu', padding='same')(fusion_output)
decoder_output = UpSampling2D((2, 2))(decoder_output)
decoder_output = Conv2D(64, (3,3), activation='relu', padding='same')(decoder_output)
decoder_output = UpSampling2D((2, 2))(decoder_output)
decoder_output = Conv2D(32, (3,3), activation='relu', padding='same')(decoder_output)
decoder_output = Conv2D(16, (3,3), activation='relu', padding='same')(decoder_output)
decoder_output = Conv2D(2, (3, 3), activation='tanh', padding='same')(decoder_output)
decoder_output = UpSampling2D((2, 2))(decoder_output)
model = Model(inputs=[encoder_input, embed_input], outputs=decoder_output)
#Create embedding
def create_inception_embedding(grayscaled_rgb):
grayscaled_rgb_resized = []
for i in grayscaled_rgb:
i = resize(i, (299, 299, 3), mode='constant')
grayscaled_rgb_resized.append(i)
grayscaled_rgb_resized = np.array(grayscaled_rgb_resized)
grayscaled_rgb_resized = preprocess_input(grayscaled_rgb_resized)
with inception.graph.as_default():
embed = inception.predict(grayscaled_rgb_resized)
return embed
# Image transformer
datagen = ImageDataGenerator(
shear_range=0.4,
zoom_range=0.4,
rotation_range=40,
horizontal_flip=True)
#Generate training data
batch_size = 20
def image_a_b_gen(batch_size):
for batch in datagen.flow(Xtrain, batch_size=batch_size):
grayscaled_rgb = gray2rgb(rgb2gray(batch))
embed = create_inception_embedding(grayscaled_rgb)
lab_batch = rgb2lab(batch)
X_batch = lab_batch[:,:,:,0]
X_batch = X_batch.reshape(X_batch.shape+(1,))
Y_batch = lab_batch[:,:,:,1:] / 128
yield ([X_batch, create_inception_embedding(grayscaled_rgb)], Y_batch)
#Train model
tensorboard = TensorBoard(log_dir="/output")
model.compile(optimizer='adam', loss='mse')
model.fit_generator(image_a_b_gen(batch_size), callbacks=[tensorboard], epochs=1000, steps_per_epoch=20)
#Make a prediction on the unseen images
color_me = []
for filename in os.listdir('../Test/'):
color_me.append(img_to_array(load_img('../Test/'+filename)))
color_me = np.array(color_me, dtype=float)
color_me = 1.0/255*color_me
color_me = gray2rgb(rgb2gray(color_me))
color_me_embed = create_inception_embedding(color_me)
color_me = rgb2lab(color_me)[:,:,:,0]
color_me = color_me.reshape(color_me.shape+(1,))
# Test model
output = model.predict([color_me, color_me_embed])
output = output * 128
# Output colorizations
for i in range(len(output)):
cur = np.zeros((256, 256, 3))
cur[:,:,0] = color_me[i][:,:,0]
cur[:,:,1:] = output[i]
imsave("result/img_"+str(i)+".png", lab2rgb(cur))
You can check this question for problem description. I create 7 models by a for loop then train them (didn't mention training process in the code):
for i in range(1, 8):
j=str(i)
img_input = Input(shape=(img_width,img_height,1),name='input')
x = Conv2D(32, (3,3), activation='relu', padding='same', name='conv1-'+j)(img_input)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1-'+j)(x)
x = Conv2D(64, (3,3), activation='relu', padding='same', name='conv2-'+j)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2-'+j)(x)
x = Conv2D(128, (3,3), activation='relu', padding='same', name='conv3-'+j)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool3-'+j)(x)
x = Flatten()(x)
x = Dense(512, name='dense1-'+j)(x)
x = Dense(512, name='dense2-'+j)(x)
predictions = Dense(6, activation='softmax', name='predictions-'+j)(x)
model = Model(inputs=img_input, outputs=predictions)
model.compile(optimizer='Adam', loss='binary_crossentropy',
metrics=['accuracy'])
Popping the last softmax layer of each model and saving the rest of the models into a list named as models[]:
inputTensor = Input(shape=(img_width,img_height,1),name='inputs')
for i in range (1,8): #save models into a models[] list
j = str(i)
models[i] = load_model(path+j+"/weights.best.hdf5") #load models from disk
models[i].layers.pop() #Pop last layer
models[i].outputs= [models[i].layers[-1].output] #Fix layer pop bug
models[i].layers[-1].outbound_nodes = [] #Fix layer pop bug
for layer in models[i].layers: #Make intermediate
layer.trainable=False #layers untrainable
Implementing a concat to all of these models and then trying to save the finalModel:
outputTensors= [models[m](inputTensor) for m in models]
output = Concatenate()(outputTensors)
predictions = Dense(6, activation='softmax', name='predictionss')(output)
model=Model(inputTensor, predictions)
model.compile(optimizer='Adam', loss='binary_crossentropy', metrics=['accuracy'])
model.save('model')
But when saving occurs it throws an error:
Traceback (most recent call last):
File "<ipython-input-43-6221fb2664c1>", line 10, in <module>
model.save('model')
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 2553, in save
save_model(self, filepath, overwrite, include_optimizer)
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\models.py", line 107, in save_model
'config': model.get_config()
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 2326, in get_config
layer_config = layer.get_config()
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 2390, in get_config
new_node_index = node_conversion_map[node_key]
KeyError: 'predictions-1_ib-0
And I can't overcome this error.
Appreciate your helps.