Вы можете либо Преобразовать обе модели в Последовательные ИЛИ Преобразовать обе модели в Функциональные и позже объединить.
Преобразовать оба модель для последовательного:
Модель 1 -
import tensorflow as tf
from tensorflow.python.keras import layers, models, applications, Input, Model
from tensorflow.keras.layers import Convolution2D, MaxPooling2D, UpSampling2D
# Create the Sequential Model
model = Sequential()
model.add(Convolution2D(16, (3, 3), input_shape=(424,424,3), activation='relu', padding='same'))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(Convolution2D(8, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(Convolution2D(8, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D((2, 2), padding='same'))
# Model summary
model.summary()
# Save the Model and Architecture
model.save('Encoded.h5')
Выход -
Model: "sequential_8"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_60 (Conv2D) (None, 424, 424, 16) 448
_________________________________________________________________
max_pooling2d_45 (MaxPooling (None, 212, 212, 16) 0
_________________________________________________________________
conv2d_61 (Conv2D) (None, 212, 212, 8) 1160
_________________________________________________________________
max_pooling2d_46 (MaxPooling (None, 106, 106, 8) 0
_________________________________________________________________
conv2d_62 (Conv2D) (None, 106, 106, 8) 584
_________________________________________________________________
max_pooling2d_47 (MaxPooling (None, 53, 53, 8) 0
=================================================================
Total params: 2,192
Trainable params: 2,192
Non-trainable params: 0
_________________________________________________________________
Модель 2 - Это полная полная модель. Слои из Модель 1 и дополнительные слои.
import tensorflow as tf
from tensorflow.python.keras import layers, models, applications, Input, Model, Sequential
from tensorflow.keras.layers import Convolution2D, MaxPooling2D, UpSampling2D, Conv2D, Dense, Dropout, Flatten, BatchNormalization
from tensorflow.keras.models import load_model
# Load the previoulsy saved enocdermodel
model = load_model('Encoded.h5')
# Add the additonal layers
model.add(Conv2D(64,(3,3), activation='relu'))#3x3 is default
model.add(MaxPooling2D(pool_size=(3,3)))
#model.add(Dropout(.1))#test
model.add(Dense(32, activation='relu'))#test
model.add(Conv2D(64,(3,3), activation='relu'))#input_shape=(424,424,3)
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Dense(64, activation='relu'))
model.add(Dropout(.3))#test
model.add(Conv2D(64,(3,3), activation='relu'))#input_shape=(424,424,3)
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Dropout(.3))
model.add(Flatten(input_shape=(424,424,3)))
model.add(BatchNormalization())
model.add(Dense(2, activation='softmax'))
# Model summary
model.summary()
Вывод -
WARNING:tensorflow:No training configuration found in the save file, so the model was *not* compiled. Compile it manually.
Model: "sequential_8"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_60 (Conv2D) (None, 424, 424, 16) 448
_________________________________________________________________
max_pooling2d_45 (MaxPooling (None, 212, 212, 16) 0
_________________________________________________________________
conv2d_61 (Conv2D) (None, 212, 212, 8) 1160
_________________________________________________________________
max_pooling2d_46 (MaxPooling (None, 106, 106, 8) 0
_________________________________________________________________
conv2d_62 (Conv2D) (None, 106, 106, 8) 584
_________________________________________________________________
max_pooling2d_47 (MaxPooling (None, 53, 53, 8) 0
_________________________________________________________________
conv2d_63 (Conv2D) (None, 51, 51, 64) 4672
_________________________________________________________________
max_pooling2d_48 (MaxPooling (None, 17, 17, 64) 0
_________________________________________________________________
dense_24 (Dense) (None, 17, 17, 32) 2080
_________________________________________________________________
conv2d_64 (Conv2D) (None, 15, 15, 64) 18496
_________________________________________________________________
max_pooling2d_49 (MaxPooling (None, 5, 5, 64) 0
_________________________________________________________________
dense_25 (Dense) (None, 5, 5, 64) 4160
_________________________________________________________________
dropout_16 (Dropout) (None, 5, 5, 64) 0
_________________________________________________________________
conv2d_65 (Conv2D) (None, 3, 3, 64) 36928
_________________________________________________________________
max_pooling2d_50 (MaxPooling (None, 1, 1, 64) 0
_________________________________________________________________
dropout_17 (Dropout) (None, 1, 1, 64) 0
_________________________________________________________________
flatten_8 (Flatten) (None, 64) 0
_________________________________________________________________
batch_normalization_8 (Batch (None, 64) 256
_________________________________________________________________
dense_26 (Dense) (None, 2) 130
=================================================================
Total params: 68,914
Trainable params: 68,786
Non-trainable params: 128
_________________________________________________________________
Преобразование обеих моделей Функционально:
Модель 1-
import tensorflow as tf
from tensorflow.python.keras import layers, models, applications, Input, Model
from tensorflow.keras.layers import Convolution2D, MaxPooling2D, UpSampling2D
#load in data using imagedatagenreator
input_img = Input(shape=(424,424,3))
x = Convolution2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
##save weights and and model start conv network with these weights
encoder = Model(input_img, encoded)
# Model Summary
encoder.summary()
encoder.save('Encoded.h5')
Выход -
Model: "model_5"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_8 (InputLayer) [(None, 424, 424, 3)] 0
_________________________________________________________________
conv2d_66 (Conv2D) (None, 424, 424, 16) 448
_________________________________________________________________
max_pooling2d_51 (MaxPooling (None, 212, 212, 16) 0
_________________________________________________________________
conv2d_67 (Conv2D) (None, 212, 212, 8) 1160
_________________________________________________________________
max_pooling2d_52 (MaxPooling (None, 106, 106, 8) 0
_________________________________________________________________
conv2d_68 (Conv2D) (None, 106, 106, 8) 584
_________________________________________________________________
max_pooling2d_53 (MaxPooling (None, 53, 53, 8) 0
=================================================================
Total params: 2,192
Trainable params: 2,192
Non-trainable params: 0
_________________________________________________________________
Модель 2 - Это полная модель. Слои из Модель 1 и дополнительные слои.
import tensorflow as tf
from tensorflow.python.keras import layers, models, applications, Input, Model, Sequential
from tensorflow.keras.layers import Convolution2D, MaxPooling2D, UpSampling2D, Conv2D, Dense, Dropout, Flatten, BatchNormalization
from tensorflow.keras.models import load_model
# Load the previoulsy saved enocdermodel
load_model('Encoded.h5')
# Add the additonal layers
x = Convolution2D(64,(3,3), activation='relu')(encoded)#3x3 is default
x = MaxPooling2D(pool_size=(3,3))(x)
#model.add(Dropout(.1))#test
x = Dense(32, activation='relu')(x)#test
x = Conv2D(64,(3,3), activation='relu')(x)#input_shape=(424,424,3)
x = MaxPooling2D(pool_size=(3,3))(x)
x = Dense(64, activation='relu')(x)
x = Dropout(.3)(x)#test
x = Conv2D(64,(3,3), activation='relu')(x)#input_shape=(424,424,3)
x = MaxPooling2D(pool_size=(3,3))(x)
x = Dropout(.3)(x)
x = Flatten(input_shape=(424,424,3))(x)
x = BatchNormalization()(x)
output = Dense(2, activation='softmax')(x)
##save weights and and model start conv network with these weights
model = Model(input_img, output)
# Model summary
model.summary()
Вывод -
WARNING:tensorflow:No training configuration found in the save file, so the model was *not* compiled. Compile it manually.
Model: "model_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_7 (InputLayer) [(None, 424, 424, 3)] 0
_________________________________________________________________
conv2d_44 (Conv2D) (None, 424, 424, 16) 448
_________________________________________________________________
max_pooling2d_33 (MaxPooling (None, 212, 212, 16) 0
_________________________________________________________________
conv2d_45 (Conv2D) (None, 212, 212, 8) 1160
_________________________________________________________________
max_pooling2d_34 (MaxPooling (None, 106, 106, 8) 0
_________________________________________________________________
conv2d_46 (Conv2D) (None, 106, 106, 8) 584
_________________________________________________________________
max_pooling2d_35 (MaxPooling (None, 53, 53, 8) 0
_________________________________________________________________
conv2d_57 (Conv2D) (None, 51, 51, 64) 4672
_________________________________________________________________
max_pooling2d_42 (MaxPooling (None, 17, 17, 64) 0
_________________________________________________________________
dense_21 (Dense) (None, 17, 17, 32) 2080
_________________________________________________________________
conv2d_58 (Conv2D) (None, 15, 15, 64) 18496
_________________________________________________________________
max_pooling2d_43 (MaxPooling (None, 5, 5, 64) 0
_________________________________________________________________
dense_22 (Dense) (None, 5, 5, 64) 4160
_________________________________________________________________
dropout_14 (Dropout) (None, 5, 5, 64) 0
_________________________________________________________________
conv2d_59 (Conv2D) (None, 3, 3, 64) 36928
_________________________________________________________________
max_pooling2d_44 (MaxPooling (None, 1, 1, 64) 0
_________________________________________________________________
dropout_15 (Dropout) (None, 1, 1, 64) 0
_________________________________________________________________
flatten_7 (Flatten) (None, 64) 0
_________________________________________________________________
batch_normalization_7 (Batch (None, 64) 256
_________________________________________________________________
dense_23 (Dense) (None, 2) 130
=================================================================
Total params: 68,914
Trainable params: 68,786
Non-trainable params: 128
_________________________________________________________________