Как добавить еще один слой в предварительно загруженную сеть?

Я загружаю нейронную сеть, используя tensorflow и colab notbook от google. И я хочу удалить полностью связанный слой выходного слоя и добавить другой, полностью связанный только с одним нейроном, и я хочу заморозить остальные слои и обучить только этот добавленный выходной слой. Я использую tf.keras.application.MobileNetV2 и мледу-datasets/cats_and_dogs.

Я искал в API-интерфейсе tensorflow и тестировал методы для добавления, и у меня ничего не получилось. Мой код следующий


Original file is located at
    https://colab.research.google.com/drive/16VdqQFBfY_jp5-5kRQvWQ0Y0ytN9W1kN

https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/images/classification.ipynb#scrollTo=3f0Z7NZgVrWQ

This tutorial follows a basic machine learning workflow:

1.   Examine and understand data
2.   Build an input pipeline
3.   Build the model
4.   Train the model
5.   Test the model
6.   Improve the model and repeat the process

## Import packages

Let's start by importing the required packages. The `os` package is used to read files and directory structure, NumPy is used to convert python list to numpy array and to perform required matrix operations and `matplotlib.pyplot` to plot the graph and display images in the training and validation data.
"""

from __future__ import absolute_import, division, print_function, unicode_literals

"""Import Tensorflow and the Keras classes needed to construct our model."""

# try:
#   # %tensorflow_version only exists in Colab.
#   %tensorflow_version 2.x
# except Exception:
#   pass

import tensorflow as tf

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator

import os
import numpy as np
import matplotlib.pyplot as plt

import keras
from keras import backend as K
from keras.layers.core import Dense, Activation
from keras.metrics import categorical_crossentropy
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from keras.models import Model
from keras.applications import imagenet_utils
from keras.layers import Dense,GlobalAveragePooling2D
from keras.applications import MobileNet
from keras.applications.mobilenet import preprocess_input
from IPython.display import Image
from keras.optimizers import Adam

"""## Load data
Begin by downloading the dataset. This tutorial uses a filtered version of Dogs vs Cats dataset from Kaggle. Download the archive version of the dataset and store it in the "/tmp/" directory.
"""

_URL = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'

path_to_zip = tf.keras.utils.get_file('cats_and_dogs.zip', origin=_URL, extract=True)

PATH = os.path.join(os.path.dirname(path_to_zip), 'cats_and_dogs_filtered')

"""The dataset has the following directory structure:

<pre>
<b>cats_and_dogs_filtered</b>
|__ <b>train</b>
    |______ <b>cats</b>: [cat.0.jpg, cat.1.jpg, cat.2.jpg ....]
    |______ <b>dogs</b>: [dog.0.jpg, dog.1.jpg, dog.2.jpg ...]
|__ <b>validation</b>
    |______ <b>cats</b>: [cat.2000.jpg, cat.2001.jpg, cat.2002.jpg ....]
    |______ <b>dogs</b>: [dog.2000.jpg, dog.2001.jpg, dog.2002.jpg ...]
</pre>



After extracting its contents, assign variables with the proper file path for the training and validation set.
"""

train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')

train_cats_dir = os.path.join(train_dir, 'cats')  # directory with our training cat pictures
train_dogs_dir = os.path.join(train_dir, 'dogs')  # directory with our training dog pictures
validation_cats_dir = os.path.join(validation_dir, 'cats')  # directory with our validation cat pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs')  # directory with our validation dog pictures

"""### Understand the data
Let's look at how many cats and dogs images are in the training and validation directory:
"""

num_cats_tr = len(os.listdir(train_cats_dir))
num_dogs_tr = len(os.listdir(train_dogs_dir))

num_cats_val = len(os.listdir(validation_cats_dir))
num_dogs_val = len(os.listdir(validation_dogs_dir))

total_train = num_cats_tr + num_dogs_tr
total_val = num_cats_val + num_dogs_val

print('total training cat images:', num_cats_tr)
print('total training dog images:', num_dogs_tr)

print('total validation cat images:', num_cats_val)
print('total validation dog images:', num_dogs_val)
print("--")
print("Total training images:", total_train)
print("Total validation images:", total_val)

"""For convenience, set up variables to use while pre-processing the dataset and training the network."""

batch_size = 32
epochs = 15
IMG_HEIGHT = 160
IMG_WIDTH = 160

"""### Data preparation

Format the images into appropriately pre-processed floating point tensors before feeding to the network:

1. Read images from the disk.
2. Decode contents of these images and convert it into proper grid format as per their RGB content.
3. Convert them into floating point tensors.
4. Rescale the tensors from values between 0 and 255 to values between 0 and 1, as neural networks prefer to deal with small input values.

Fortunately, all these tasks can be done with the `ImageDataGenerator` class provided by `tf.keras`. It can read images from disk and preprocess them into proper tensors. It will also set up generators that convert these images into batches of tensors—helpful when training the network.
"""

train_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our training data
validation_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our validation data

"""After defining the generators for training and validation images, the `flow_from_directory` method load images from the disk, applies rescaling, and resizes the images into the required dimensions."""

train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
                                                            directory=train_dir,
                                                            shuffle=True,
                                                            target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                            class_mode='binary')

val_data_gen = validation_image_generator.flow_from_directory(batch_size=batch_size,
                                                                directory=validation_dir,
                                                                target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                                class_mode='binary')

"""### Visualize training images
Visualize the training images by extracting a batch of images from the training generator—which is 32 images in this example—then plot five of them with `matplotlib`.
"""

sample_training_images, _ = next(train_data_gen)

"""The `next` function returns a batch from the dataset. The return value of `next` function is in form of `(x_train, y_train)` where x_train is training features and y_train, its labels. Discard the labels to only visualize the training images."""

# This function will plot images in the form of a grid with 1 row and 5 columns where images are placed in each column.
def plotImages(images_arr):
    fig, axes = plt.subplots(1, 5, figsize=(20,20))
    axes = axes.flatten()
    for img, ax in zip( images_arr, axes):
        ax.imshow(img)
        ax.axis('off')
    plt.tight_layout()
    plt.show()

plotImages(sample_training_images[:5])

"""## Create the model
The model consists of three convolution blocks with a max pool layer in each of them. There's a fully connected layer with 512 units on top of it thatr is activated by a `relu` activation function. The model outputs class probabilities based on binary classification by the `sigmoid` activation function.
"""

# model = Sequential([
#     Conv2D(16, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
#     MaxPooling2D(),
#     Conv2D(32, 3, padding='same', activation='relu'),
#     MaxPooling2D(),
#     Conv2D(64, 3, padding='same', activation='relu'),
#     MaxPooling2D(),
#     Flatten(),
#     Dense(512, activation='relu'),
#     Dense(1, activation='sigmoid')
# ])

"""Carregando o modelo o modelo `keras.applications.MobileNetV2`, com pesos treinados para a base imagenet e sem as camadas totalmente conectadas."""

# from keras.layers import Input
# input_tensor = Input(shape=(IMG_HEIGHT, IMG_WIDTH ,32))
model = tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=(IMG_HEIGHT,
                                                                    IMG_WIDTH,
                                                                    3),
                                                                    alpha=1.0,
                                                                    include_top=False,
                                                                    weights='imagenet',
                                                                    input_tensor=None,
                                                                    pooling='max',
                                                                    classes=2)
model.trainable = False

Я ожидаю добавить полностью подключенный слой в сеть, но он вообще не добавляется.


person user2535338    schedule 01.11.2019    source источник


Ответы (1)


Скажем, вы загружаете предварительно обученный MobileNetV2:

model = tf.keras.applications.mobilenet_v2.MobileNetV2()

Вы можете проверить, как выглядит ваша модель, с помощью model.summary():

...
__________________________________________________________________________________________________
out_relu (ReLU)                 (None, 7, 7, 1280)   0           Conv_1_bn[0][0]
__________________________________________________________________________________________________
global_average_pooling2d (Globa (None, 1280)         0           out_relu[0][0]
__________________________________________________________________________________________________
Logits (Dense)                  (None, 1000)         1281000     global_average_pooling2d[0][0]
==================================================================================================
Total params: 3,538,984
Trainable params: 3,504,872
Non-trainable params: 34,112
__________________________________________________________________________________________________

Теперь, если вы хотите удалить свой последний слой FC и создать еще один только с одним нейроном. Это делается так:

penultimate_layer = model.layers[-2]  # layer that you want to connect your new FC layer to 
new_top_layer = tf.keras.layers.Dense(1)(penultimate_layer.output)  # create new FC layer and connect it to the rest of the model
new_model = tf.keras.models.Model(model.input, new_top_layer)  # define your new model

Теперь, если вы проверите с помощью new_model.summary(), вы увидите, что ваша новая модель была создана правильно.

...
__________________________________________________________________________________________________
out_relu (ReLU)                 (None, 7, 7, 1280)   0           Conv_1_bn[0][0]
__________________________________________________________________________________________________
global_average_pooling2d (Globa (None, 1280)         0           out_relu[0][0]
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 1)            1281        global_average_pooling2d[0][0]
==================================================================================================
Total params: 2,259,265
Trainable params: 2,225,153
Non-trainable params: 34,112
__________________________________________________________________________________________________

Наконец, чтобы заморозить веса всех слоев перед последним, просто выполните:

for layer in new_model.layers[:-2]:
    layer.trainable = False
person Djib2011    schedule 01.11.2019