I
I
Ilya Neizvestnyj2019-08-28 17:00:54
Python
Ilya Neizvestnyj, 2019-08-28 17:00:54

How to get and freeze a pre-trained convolutional warp?

There is a trained network:

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation="relu", input_shape=(150, 150, 3)))

model.add(layers.MaxPooling2D(2, 2))
model.add(layers.Conv2D(64, (3, 3), activation="relu"))
# model.add(layers.MaxPooling2D(2, 2))
# model.add(layers.Conv2D(128, (3, 3), activation="relu"))
model.add(layers.MaxPooling2D(2, 2))
model.add(layers.Conv2D(128, (3, 3), activation="relu"))
model.add(layers.MaxPooling2D(2, 2))
model.add(layers.Flatten())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(512, activation="relu"))
model.add(layers.Dense(1, activation="sigmoid"))

How to get and freeze the convolution base as in the example below:
conv_base = VGG16(weights="imagenet",
                  include_top=False,
                  input_shape=(150, 150, 3))

model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation="relu"))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(1, activation="sigmoid"))
conv_base.trainable = False

(so freezing)
for layer in model.layers:
    if layer.name == "flatten_1" or layer.name == "dense_1" or layer.name == "dropout_1" or layer.name == "dense_2":
        set_trainable = True
    else:
        layer.trainable = False

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Dugin, 2019-08-28
@Cheloved

Freezing via layer.trainable is correct. What does it mean to "get" the convolutional base?
Here are the layer weights:

layer.weights
layer.get_weights()

And rather than listing layer names, it's easier to do something like:
for layer in model.layers[:4]:
    layer.trainable = False

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question