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

Tensorflow 如何在预加载的网络上添加另一层?,tensorflow,python-3.7,tensorflow2.0,Tensorflow,Python 3.7,Tensorflow2.0,我正在用谷歌的tensorflow和colab notbook加载一个神经网络。我想移除输出层的完全连接层,添加另一个完全连接的,只有一个神经元的层,我想冻结其他层,只训练这个添加的输出层。 我使用的是tf.keras.application.mobilenetw2,我使用的是mledu-数据集/cats\u和dogs 我对tensorflow API进行了研究,并测试了要添加的方法,但没有成功。我的代码如下 Original file is located at https://co

我正在用谷歌的tensorflow和colab notbook加载一个神经网络。我想移除输出层的完全连接层,添加另一个完全连接的,只有一个神经元的层,我想冻结其他层,只训练这个添加的输出层。 我使用的是
tf.keras.application.mobilenetw2
,我使用的是mledu-
数据集/cats\u和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

原始文件位于
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
本教程遵循基本的机器学习工作流:
1.检查和理解数据
2.构建输入管道
3.建立模型
4.训练模型
5.测试模型
6.改进模型并重复该过程
##导入包
让我们从导入所需的包开始。“os”包用于读取文件和目录结构,NumPy用于将python列表转换为NumPy数组,并执行所需的矩阵操作,“matplotlib.pyplot”用于在训练和验证数据中绘制图形和显示图像。
"""
从未来导入绝对导入、除法、打印函数、unicode文本
“”“导入构建模型所需的Tensorflow和Keras类。”“”
#尝试:
##%tensorflow_版本仅存在于Colab中。
#%tensorflow_版本2.x
#除例外情况外:
#通过
导入tensorflow作为tf
从tensorflow.keras.models导入顺序
从tensorflow.keras.layers导入稠密、Conv2D、展平、衰减、MaxPoolig2D
从tensorflow.keras.preprocessing.image导入ImageDataGenerator
导入操作系统
将numpy作为np导入
将matplotlib.pyplot作为plt导入
进口干酪
从keras导入后端为K
从keras.layers.core导入致密,激活
从keras.metrics导入分类熵
从keras.preprocessing.image导入ImageDataGenerator
从keras.preprocessing导入图像
从keras.models导入模型
从keras.applications导入imagenet_utils
从keras.layers导入稠密、全局平均池2D
从keras.applications导入MobileNet
从keras.applications.mobilenet导入预处理输入
从IPython.display导入图像
从keras.optimizers导入Adam
“”“##加载数据
首先下载数据集。本教程使用Kaggle提供的狗与猫数据集的过滤版本。下载数据集的存档版本并将其存储在“/tmp/”目录中。
"""
_URL='1〕https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip=tf.keras.utils.get_文件('cats_and_dogs.zip',origin=\u URL,extract=True)
PATH=os.PATH.join(os.PATH.dirname(PATH\u-to\u-zip),'cats\u和\u-dogs\u-filtered')
“”“数据集具有以下目录结构:
...
__________________________________________________________________________________________________
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
__________________________________________________________________________________________________
猫和狗 |__训练 |______猫:[猫0.jpg,猫1.jpg,猫2.jpg….] |______狗:[dog.0.jpg,dog.1.jpg,dog.2.jpg…] |__验证 |______猫:[猫2000.jpg,猫2001.jpg,猫2002.jpg….] |______狗:[dog.2000.jpg,dog.2001.jpg,dog.2002.jpg…] 提取其内容后,为培训和验证集指定具有适当文件路径的变量。 """ train_dir=os.path.join(路径“train”) validation\u dir=os.path.join(路径“validation”) train_cats_dir=os.path.join(train_dir,'cats')#目录和我们的训练猫图片 train_dogs_dir=os.path.join(train_dir,'dogs')#目录和我们的训练狗图片 validation_cats_dir=os.path.join(validation_dir,'cats')#目录与我们的验证猫图片 validation_dogs_dir=os.path.join(validation_dir,'dogs')#目录和我们的验证狗图片 “”理解数据 让我们看看培训和验证目录中有多少猫和狗的图像: """ num_cats_tr=len(os.listdir(train_cats_dir)) num_dogs_tr=len(os.listdir(train_dogs_dir)) num_cats_val=len(os.listdir(验证_cats_dir)) num_dogs_val=len(os.listdir(验证_dogs_dir)) 列车总数=猫数+狗数 总数=猫数+狗数 打印('训练猫图像总数:',数量\u猫\u tr) 打印('total training dog images:',num_dogs_tr) 打印('total validation cat images:',num_cats_val) 打印('total validation dog images:',num_dogs_val) 打印(“--”) 打印(“总训练图像:”,总训练) 打印(“总验证图像:”,总值) “”“为方便起见,请设置在预处理数据集和训练网络时使用的变量。”“” 批量大小=32 纪元=15 IMG_高度=160 IMG_宽度=160 “”“####数据准备 将图像格式化为适当的预处理浮点张量,然后再馈送到网络: 1.从磁盘读取图像。 2.解码这些图像的内容,并根据其RGB内容将其转换为适当的网格格式。 3.将它们转换为浮点张量。 4.将张量从0到255之间的值重新缩放为0到1之间的值,因为神经网络更喜欢处理小的输入值。 幸运的是,所有这些任务都可以通过`tf.keras`提供的`ImageDataGenerator`类完成。它可以从磁盘读取图像并将其预处理为适当的张量。它还将设置生成器,将这些图像转换成成批的张量,在训练网络时很有用。 """ train_image_generator=ImageDataGenerator(重缩放=1./255)#用于我们的培训数据的生成器 验证_image_generator=ImageDataGenerator(重缩放=1./255)#验证数据的生成器 “”“定义用于培训和验证映像的生成器后,“flow\u from\u directory”方法从磁盘加载映像,应用重缩放,并将映像大小调整为所需的尺寸。”“” 列车数据\u gen=列车图像\u生成器。来自\u目录的流程(批量大小=批量大小,
...
__________________________________________________________________________________________________
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
__________________________________________________________________________________________________