将函数模型转换为顺序 Keras

Convert Functional Model to Sequential Keras

我有一个自动编码器,我想从中保存模型,特别是编码器部分(或权重,不确定我需要什么),然后将其加载到 CNN 中。 我的目标是使用自动编码器学习我要分类的项目的特征,然后使用这些权重启动 CNN。

我试过只加载权重,但它们不会加载,因为两个网络的大小不同。我虽然只导入整个网络就可以工作,但一个是顺序的,另一个是功能性的。

自编码器

#load in data using imagedatagenreator
input_img = Input(shape=(img_width, img_height,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)

# at this point the representation is (8, 4, 4) i.e. 128-dimensional
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(3, (3, 3), activation='sigmoid', padding='same')(x)input_img = Input(shape=(img_width, img_height,3))


#compile and run

##save weights and and model start conv network with these weights
encoder = Model(input_img, encoded)
encoder.save('Encoded.h5')

CNN

#load in data using imagedatagenreator

model = load_model('/home/ryan/Documents/Unsupervised_Jelly/Autoenconding/Encoded.h5')
#model = Sequential(model) #this was the start of the CNN before
model.add(Conv2D(64,(3,3), input_shape=(424,424,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'))

#compile and run

我也会接受任何人的批评或建议。

您可以将两个模型都转换为顺序模型,或者将两个模型都转换为函数模型,然后再连接。


将两个模型都转换为顺序模型:

模型 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
_________________________________________________________________