簡體   English   中英

將功能 Model 轉換為順序 Keras

[英]Convert Functional Model to Sequential Keras

我有一個自動編碼器,我想從中保存 model,特別是編碼器部分(或權重,不完全確定我需要什么),然后將其加載到 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')

美國有線電視新聞網

#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

我也會接受任何人的任何批評或建議。

您可以將 model 都轉換為順序將 model 都轉換為功能,然后再連接。


將 model 都轉換為順序:

Model 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')

Output -

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
_________________________________________________________________

Model 2 -這有完整的 model。 Model 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()

Output -

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
_________________________________________________________________

將 model 都轉換為功能:

Model 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')

Output -

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
_________________________________________________________________

Model 2 -這有完整的 model。 Model 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()

Output -

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
_________________________________________________________________

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM