如何在预加载的网络上添加另一层?

用户名

我正在使用tensorflow和来自谷歌的colab notbook加载神经网络。我想删除输出层的完全连接层,并添加仅与一个神经元完全连接的另一层,并且我想冻结其他层并仅训练此添加的输出层。我正在使用,tf.keras.application.MobileNetV2并且正在使用mledu- datasets/cats_and_dogs

我已经在tensorflow API中进行了研究,并测试了添加方法,但没有成功。我的代码如下


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

我希望在网络中添加完全连接的层,但根本没有添加。

吉布2011

假设您加载了预先训练的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层,并仅用一个神经元创建另一个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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android如何在ImageView的另一层上绘制几条线?

来自分类Dev

在UIImage上添加一层颜色

来自分类Dev

我怎么知道在我的神经网络上添加一层?

来自分类Dev

从外部在卷积输出中添加另一层

来自分类Dev

如何编写Visio宏以将形状从一层移动到另一层?

来自分类Dev

如何在JLayeredPane的任何一层上使用图形(Piant组件)

来自分类Dev

我如何使图像位于另一层后面

来自分类Dev

如何在Matlab中创建一个具有一层感知器神经元隐藏层和一层径向基础神经元隐藏层的神经网络?

来自分类Dev

如何创建具有相同权重的两层,其中一层是另一层的转置?

来自分类Dev

我如何在下面压入一层?

来自分类Dev

如何在keras的最后一层输入单个变量?

来自分类Dev

接入点在哪一层网络堆栈上运行?

来自分类Dev

如何在同一层次结构上具有页面和自定义帖子类型

来自分类常见问题

让形状在同一层上

来自分类Dev

让形状在同一层上

来自分类Dev

向实体框架linq查询添加另一层分组

来自分类Dev

如何在TensorFlow中同时将一层并行传递到两层中

来自分类Dev

如何获取SLIM vgg16网络中某一层的输出?

来自分类Dev

cocos2d:将CCSprite从一层绘制到另一层

来自分类Dev

QGIS将粘贴点从一层复制到另一层

来自分类Dev

如何使用预训练模型的第一层来提取Keras模型中的特征(功能性API)

来自分类Dev

什么是冻结/解冻神经网络中的一层?

来自分类Dev

BatchNormalization是否算作网络中的一层?

来自分类Dev

如何将分页系统(需求分页)视为另一层缓存?

来自分类Dev

如何在.NET Core的那一层绑定服务dll的依赖项

来自分类Dev

如何在Sails.js中嵌入和编写mongo对象(深度超过一层)?

来自分类Dev

在tensorflow中,如何在渐变传播到下一层之前重新缩放渐变?

来自分类Dev

滤镜如何在CNN的第一层中跨RGB图像运行?

来自分类Dev

如何在Sails.js中嵌入和编写mongo对象(深度超过一层)?

Related 相关文章

  1. 1

    Android如何在ImageView的另一层上绘制几条线?

  2. 2

    在UIImage上添加一层颜色

  3. 3

    我怎么知道在我的神经网络上添加一层?

  4. 4

    从外部在卷积输出中添加另一层

  5. 5

    如何编写Visio宏以将形状从一层移动到另一层?

  6. 6

    如何在JLayeredPane的任何一层上使用图形(Piant组件)

  7. 7

    我如何使图像位于另一层后面

  8. 8

    如何在Matlab中创建一个具有一层感知器神经元隐藏层和一层径向基础神经元隐藏层的神经网络?

  9. 9

    如何创建具有相同权重的两层,其中一层是另一层的转置?

  10. 10

    我如何在下面压入一层?

  11. 11

    如何在keras的最后一层输入单个变量?

  12. 12

    接入点在哪一层网络堆栈上运行?

  13. 13

    如何在同一层次结构上具有页面和自定义帖子类型

  14. 14

    让形状在同一层上

  15. 15

    让形状在同一层上

  16. 16

    向实体框架linq查询添加另一层分组

  17. 17

    如何在TensorFlow中同时将一层并行传递到两层中

  18. 18

    如何获取SLIM vgg16网络中某一层的输出?

  19. 19

    cocos2d:将CCSprite从一层绘制到另一层

  20. 20

    QGIS将粘贴点从一层复制到另一层

  21. 21

    如何使用预训练模型的第一层来提取Keras模型中的特征(功能性API)

  22. 22

    什么是冻结/解冻神经网络中的一层?

  23. 23

    BatchNormalization是否算作网络中的一层?

  24. 24

    如何将分页系统(需求分页)视为另一层缓存?

  25. 25

    如何在.NET Core的那一层绑定服务dll的依赖项

  26. 26

    如何在Sails.js中嵌入和编写mongo对象(深度超过一层)?

  27. 27

    在tensorflow中,如何在渐变传播到下一层之前重新缩放渐变?

  28. 28

    滤镜如何在CNN的第一层中跨RGB图像运行?

  29. 29

    如何在Sails.js中嵌入和编写mongo对象(深度超过一层)?

热门标签

归档