Search code examples
pythontensorflowkerasconv-neural-networkautoencoder

Convert Functional Model to Sequential Keras


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.

Autoencoder

#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.


Solution

  • You can either Convert both the model to Sequential OR Convert both the model to Functional and later concatenate.


    Convert both the model to Sequential :

    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 - 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()
    

    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
    _________________________________________________________________
    

    Convert both the model to Functional:

    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 - 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()
    

    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
    _________________________________________________________________