将功能模型转换为顺序Keras [英] Convert Functional Model to Sequential Keras
问题描述
我有一个自动编码器,我想从中保存模型,特别是编码器部分(或权重,不确定我到底需要什么)的模型,然后将其加载到CNN中. 我的目标是使用自动编码器学习要分类的项目的功能,然后使用这些权重启动CNN.
I have an autoencoder from which I want to save the model, specifically of the encoder part (or weights, not exactly sure what I need) and then load that into a CNN. My goal for this is to use the autoencoder to learn features of items I want to classify, and then use those weights to start the CNN.
我尝试仅加载权重,但是由于两个网络的大小不同,它们将无法加载.我虽然只是导入整个网络都可以,但是一个是顺序的,另一个是起作用的.
I have tried just loading the weights, but they won't load since the two networks are different sizes. I though just importing the whole network would work, but one is sequential and the other is functional.
自动编码器
#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
我还将接受任何人的任何批评或建议.
I will also accept any criticism or advice anyone would have.
推荐答案
您可以将两个模型都转换为顺序模型,也可以将两个模型都转换为功能模型,然后进行串联
You can either Convert both the model to Sequential OR Convert both the model to Functional and later concatenate.
将两个模型都转换为顺序模型:
模型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 中的图层以及其他图层.
Model 2 - This has complete full model. Layers from Model 1 and additional layers.
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 中的图层以及其他图层.
Model 2 - This has complete full model. Layers from Model 1 and additional layers.
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
_________________________________________________________________
这篇关于将功能模型转换为顺序Keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!