Created
October 9, 2017 23:43
-
-
Save DSLituiev/4633acda282f3434265dbe7ca9c959ed to your computer and use it in GitHub Desktop.
Initialization with constant results in error when chaining layers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import numpy as np | |
import keras | |
from keras.models import load_model, Model | |
from keras.layers import Conv2D, MaxPool2D, GlobalAveragePooling2D, Dense, Dropout | |
from keras import backend as K | |
from keras.applications.inception_v3 import InceptionV3 | |
def get_model( | |
dropout=0.5, | |
alpha_dense=0.01, | |
n_dense = 64, | |
n_classes=1, | |
final_activation='sigmoid'): | |
base_model = InceptionV3(include_top=False) | |
x = base_model.layers[-1].output | |
x = GlobalAveragePooling2D()(x) | |
# let's add a fully-connected layer | |
x = Dropout(dropout)(x) | |
x = Dense(n_dense, activation='relu', kernel_regularizer= keras.regularizers.l2(alpha_dense))(x) | |
# and a logistic layer -- let's say we have 200 classes | |
predictions = Dense(n_classes, activation=final_activation)(x) | |
model = Model(inputs=base_model.input, outputs=predictions) | |
return model | |
def dense_to_conv(dense_): | |
n_dense = dense_.output.shape | |
kernel_arr = dense_.get_weights()[0].reshape((1,1) + dense_.get_weights()[0].shape ) | |
bias_arr = dense_.get_weights()[1] | |
print("bias_arr", bias_arr.shape) | |
conv_ = Conv2D(n_dense, 1, use_bias=True, | |
weights = [kernel_arr, bias_arr], | |
input_shape = dense_.input.shape) | |
# conv_ = Conv2D(n_dense, 1, use_bias=True, | |
# kernel_initializer = keras.initializers.Constant(kernel_arr), | |
# bias_initializer = keras.initializers.Constant(bias_arr)) | |
return conv_ | |
##################################### | |
n_dense = 64 | |
n_classes = 3 | |
model = get_model(n_dense=n_dense, n_classes=n_classes) | |
la = MaxPool2D(pool_size=(6,6))(model.layers[-5].output) | |
dense_to_conv1a = dense_to_conv(model.layers[-2]) | |
print(*["{}: {}".format(kk,vv) for kk, vv in dense_to_conv1a.get_config().items()], sep="\n") | |
print('-'*20) | |
dense_to_conv1b = Conv2D(n_dense, 1, use_bias=True, ) | |
print(*["{}: {}".format(kk,vv) for kk, vv in dense_to_conv1b.get_config().items()], sep="\n") | |
##### This option fails: ###### | |
dense_to_conv1 = dense_to_conv1b | |
dense_to_conv2 = dense_to_conv(model.layers[-1]) | |
## Chaining layers: | |
la = dense_to_conv1(la) | |
print(la) | |
la = dense_to_conv2(la) | |
print(la) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment