Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 76800 values, but the requested shape has 768 #315

Closed
mymuli opened this issue Jun 8, 2019 · 3 comments
Assignees

Comments

@mymuli
Copy link

mymuli commented Jun 8, 2019

-- coding: utf-8 --

Copyright 2015 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

==============================================================================

"""Simple transfer learning with an Inception v3 architecture model which
displays summaries in TensorBoard.

This example shows how to take a Inception v3 architecture model trained on
ImageNet images, and train a new top layer that can recognize other classes of
images.

The top layer receives as input a 2048-dimensional vector for each image. We
train a softmax layer on top of this representation. Assuming the softmax layer
contains N labels, this corresponds to learning N + 2048*N model parameters
corresponding to the learned biases and weights.

Here's an example, which assumes you have a folder containing class-named
subfolders, each full of images for each label. The example folder flower_photos
should have a structure like this:

~/flower_photos/daisy/photo1.jpg
~/flower_photos/daisy/photo2.jpg
...
~/flower_photos/rose/anotherphoto77.jpg
...
~/flower_photos/sunflower/somepicture.jpg

The subfolder names are important, since they define what label is applied to
each image, but the filenames themselves don't matter. Once your images are
prepared, you can run the training with a command like this:

bazel build third_party/tensorflow/examples/image_retraining:retrain &&
bazel-bin/third_party/tensorflow/examples/image_retraining/retrain
--image_dir ~/flower_photos

You can replace the image_dir argument with any folder containing subfolders of
images. The label for each image is taken from the name of the subfolder it's
in.

This produces a new model file that can be loaded and run by any TensorFlow
program, for example the label_image sample code.

To use with TensorBoard:

By default, this script will log summaries to /tmp/retrain_logs directory

Visualize the summaries with this command:

tensorboard --logdir /tmp/retrain_logs

"""
from future import absolute_import
from future import division
from future import print_function

from datetime import datetime
import glob
import hashlib
import os.path
import random
import re
import sys
import tarfile

import numpy as np
from six.moves import urllib
import tensorflow as tf
slim=tf.contrib.slim
trunc_normal = lambda stddev: tf.truncated_normal_initializer(0.0, stddev)

from tensorflow.python.framework import graph_util
from tensorflow.python.framework import tensor_shape
from tensorflow.python.platform import gfile
from tensorflow.python.util import compat

import struct

FLAGS = tf.app.flags.FLAGS

batch_size 大小设置

train_batch_size=100

Input and output file flags.

tf.app.flags.DEFINE_string('image_dir', 'images',
"""Path to folders of labeled images.""")
tf.app.flags.DEFINE_string('output_graph', 'bottle-tmp-7/retrained_graph.pb',
"""Where to save the trained graph.""")
tf.app.flags.DEFINE_string('output_labels', 'bottle-tmp-7/retrained_labels.txt',
"""Where to save the trained graph's labels.""")
tf.app.flags.DEFINE_string('summaries_dir', 'bottle-tmp-7/retrain_logs',
"""Where to save summary logs for TensorBoard.""")

Details of the training configuration.

tf.app.flags.DEFINE_integer('how_many_training_steps', 100000,
"""How many training steps to run before ending.""")
tf.app.flags.DEFINE_float('learning_rate', 0.01,
"""How large a learning rate to use when training.""")
tf.app.flags.DEFINE_integer(
'testing_percentage', 10,
"""What percentage of images to use as a test set.""")
tf.app.flags.DEFINE_integer(
'validation_percentage', 10,
"""What percentage of images to use as a validation set.""")
tf.app.flags.DEFINE_integer('eval_step_interval', 10,
"""How often to evaluate the training results.""")
tf.app.flags.DEFINE_integer('train_batch_size', train_batch_size,
"""How many images to train on at a time.""")
tf.app.flags.DEFINE_integer('test_batch_size', 500,
"""How many images to test on at a time. This"""
""" test set is only used infrequently to verify"""
""" the overall accuracy of the model.""")
tf.app.flags.DEFINE_integer(
'validation_batch_size', 100,
"""How many images to use in an evaluation batch. This validation set is"""
""" used much more often than the test set, and is an early indicator of"""
""" how accurate the model is during training.""")

File-system cache locations.

tf.app.flags.DEFINE_string('model_dir', 'model_dir',
"""Path to classify_image_graph_def.pb, """
"""imagenet_synset_to_human_label_map.txt, and """
"""imagenet_2012_challenge_label_map_proto.pbtxt.""")

将瓶颈层值缓存为文件的路径

tf.app.flags.DEFINE_string(
'bottleneck_dir', 'bottle-tmp-7/bottlenecks',
"""Path to cache bottleneck layer values as files.""")

重新训练 图中输出分类层的名称

tf.app.flags.DEFINE_string('final_tensor_name', 'final_result',
"""The name of the output classification layer in"""
""" the retrained graph.""")

Controls the distortions used during training.

控制训练过程中使用的变形

tf.app.flags.DEFINE_boolean(
'flip_left_right', False,
"""Whether to randomly flip half of the training images horizontally.""")
tf.app.flags.DEFINE_integer(
'random_crop', 0,
"""A percentage determining how much of a margin to randomly crop off the"""
""" training images.""")
tf.app.flags.DEFINE_integer(
'random_scale', 0,
"""A percentage determining how much to randomly scale up the size of the"""
""" training images by.""")
tf.app.flags.DEFINE_integer(
'random_brightness', 0,
"""A percentage determining how much to randomly multiply the training"""
""" image input pixels up or down by.""")

These are all parameters that are tied to the particular model architecture

we're using for Inception v3. These include things like tensor names and their

sizes. If you want to adapt this script to work with another model, you will

need to update these to reflect the values in the network you're using.

pylint: disable=line-too-long

DATA_URL = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'

pylint: enable=line-too-long

MODEL_INPUT_WIDTH = 299
MODEL_INPUT_HEIGHT = 299
MODEL_INPUT_DEPTH = 3

图像输入张量所对应的名称

JPEG_DATA_TENSOR_NAME = 'DecodeJpeg/contents:0'

获取瓶颈层--concat层之后的 池化层

BOTTLENECK_TENSOR_NAME = 'mixed_7/join:0'
BOTTLENECK_TENSOR_SIZE = 768
#Auxiliary_TENSOR_NAME='mixed_7/join:0'
#Auxiliary_TENSOR_SIZE=768

将输入resize为一维向量

RESIZED_INPUT_TENSOR_NAME = 'ResizeBilinear:0'

文件夹里面的图片 不能过多

MAX_NUM_IMAGES_PER_CLASS = 2 ** 27 - 1 # ~134M

Directory containing files with correct image labels for each image.

图像标签的文件夹:每个图像对应的标签

IMAGE_LABELS_DIR = 'image_labels_dir'

Contains cached ground_truth vectors to prevent calculating them again and again

包含缓存的真实向量,以防止反复计算它们

CACHED_GROUND_TRUTH_VECTORS = {}

Contains list of all labels, each label is on a separate line, just like in image_label files

包含所有标签的列表,每个标签位于单独的行上,就像在图像标签文件中一样

ALL_LABELS_FILE = "labels.txt"

从文件系统生成训练图像列表

def create_image_lists(image_dir, testing_percentage, validation_percentage):
"""Builds a list of training images from the file system.

Analyzes the sub folders in the image directory, splits them into stable
training, testing, and validation sets, and returns a data structure
describing the lists of images for each label and their paths.

Args:
image_dir: String path to a folder containing subfolders of images.
testing_percentage: Integer percentage of the images to reserve for tests.
validation_percentage: Integer percentage of images reserved for validation.

Returns:
A dictionary containing an entry for each label subfolder, with images split
into training, testing, and validation sets within each label.
"""
"""
分析图像目录中的子文件夹,将其分割成稳定的训练、测试和验证集,并返回数据结构,描述每个标签及其路径的图像列表。
Args:
image_dir:包含图像子文件夹的文件夹的字符串路径。
testing_percentage:为测试保留的图像的整数百分比。
validation_percentage:保留用于验证的图像的整数百分比。
返回:
一个字典,包含进入每一个标签的子文件夹和分割到每个标签的训练,测试和验证集的图像。
"""
if not gfile.Exists(image_dir):
print("Image directory '" + image_dir + "' not found.")
return None
result = {}

获取当前目录下所有的子目录

sub_dirs = [x[0] for x in os.walk(image_dir)]

The root directory comes first, so skip it.

首先进入根目录,所以先跳过它

is_root_dir = True

读取所有的子目录

for sub_dir in sub_dirs:
if is_root_dir:
is_root_dir = False
continue
# 获取一个子目录所有的图片文件
extensions = ['jpg', 'jpeg', 'JPG', 'JPEG']
file_list = []
# 子目录的名字为pic
dir_name = os.path.basename(sub_dir)
print("子目录的名字: ",dir_name)
if dir_name == image_dir:
continue
print("Looking for images in '" + dir_name + "'")
for extension in extensions:
file_glob = os.path.join(image_dir, dir_name, '.' + extension)
file_list.extend(glob.glob(file_glob))
if not file_list:
print('No files found')
continue
# 每个文件夹内 图像 不能太少
if len(file_list) < 20:
print('WARNING: Folder has less than 20 images, which may cause issues.')
# 每个文件夹内 图像 不能过多
elif len(file_list) > MAX_NUM_IMAGES_PER_CLASS:
print('WARNING: Folder {} has more than {} images. Some images will '
'never be selected.'.format(dir_name, MAX_NUM_IMAGES_PER_CLASS))
label_name = re.sub(r'[^a-z0-9]+', ' ', dir_name.lower())
training_images = []
testing_images = []
validation_images = []
# 处理每一张图片数据
for file_name in file_list:
# 获取对应路径下文件的名字
base_name = os.path.basename(file_name)
# We want to ignore anything after 'nohash' in the file name when
# deciding which set to put an image in, the data set creator has a way of
# grouping photos that are close variations of each other. For example
# this is used in the plant disease data set to group multiple pictures of
# the same leaf.
hash_name = re.sub(r'nohash.
$', '', file_name)
# This looks a bit magical, but we need to decide whether this file should
# go into the training, testing, or validation sets, and we want to keep
# existing files in the same set even if more files are subsequently
# added.
# To do that, we need a stable way of deciding based on just the file name
# itself, so we do a hash of that and then use that to generate a
# probability value that we use to assign it.
hash_name_hashed = hashlib.sha1(compat.as_bytes(hash_name)).hexdigest()
# 产生随机数
percentage_hash = ((int(hash_name_hashed, 16) %
(MAX_NUM_IMAGES_PER_CLASS + 1)) *
(100.0 / MAX_NUM_IMAGES_PER_CLASS))
# 随机 划分数据集--分别划分到 training_images/testing_images/validation_images
if percentage_hash < validation_percentage:
validation_images.append(base_name)
elif percentage_hash < (testing_percentage + validation_percentage):
testing_images.append(base_name)
else:
training_images.append(base_name)
result[label_name] = {
'dir': dir_name,
'training': training_images,
'testing': testing_images,
'validation': validation_images,
}

{'pic': {'dir': 'pic', 'training': 1600, 'validation': 200, 'testing': 200}}

return result

返回包含正确图像标签的文件的路径。

def get_image_labels_path(image_lists, label_name, index, image_labels_dir, category):
""""Returns a path to a file containing correct image labels.

This is just slightly edited get_image_path() method.

Args:
image_lists: Dictionary of training images for each label.
label_name: Label string we want to get an image for.
index: Int offset of the image we want. This will be moduloed by the
available number of images for the label, so it can be arbitrarily large.
image_labels_dir: Root folder string of the subfolders containing the training
images.
category: Name string of set to pull images from - training, testing, or
validation.

Returns:
File system path string to an image that meets the requested parameters.

"""
"""
这只是稍微编辑一下get_image_path()方法。
Args:
image_lists:每个标签的训练图像字典。
label_name:我们要为其获取图像的标签字符串。
index:所需图像的int偏移量。这将被标签的可用图像数模化,因此它可以任意大
image_labels_dir:包含培训的子文件夹的根文件夹字符串图像。
category:从训练、测试或验证中提取图像的集合的名称字符串
返回:
满足请求参数的图像的文件系统路径字符串。
"""

image_lists = {'pic': {'dir': 'pic', 'training': 1600, 'validation': 200, 'testing': 200}}

label_name: pic

if label_name not in image_lists:
tf.logging.fatal('Label does not exist %s.', label_name)
label_lists = image_lists[label_name]
if category not in label_lists:
tf.logging.fatal('Category does not exist %s.', category)
category_list = label_lists[category]
if not category_list:
tf.logging.fatal('Label %s has no images in the category %s.',
label_name, category)
mod_index = index % len(category_list)
base_name = category_list[mod_index]

print("base_name:",base_name)

获取 对应图像名 的 标签

full_path = os.path.join(image_labels_dir, base_name)
full_path += '.txt'
return full_path

定义函数,通过类别名称、所属数据集和图片编码获取一张图片的地址

def get_image_path(image_lists, label_name, index, image_dir, category):
""""Returns a path to an image for a label at the given index.

Args:
image_lists: Dictionary of training images for each label.
label_name: Label string we want to get an image for.
index: Int offset of the image we want. This will be moduloed by the
available number of images for the label, so it can be arbitrarily large.
image_dir: Root folder string of the subfolders containing the training
images.
category: Name string of set to pull images from - training, testing, or
validation.

Returns:
File system path string to an image that meets the requested parameters.

"""
"""
Args:
image_lists:每个标签的训练图像字典。
label_name:我们要为其获取图像的标签字符串。
index:所需图像的int偏移量。这将被标签的可用图像数模化,因此它可以任意大。
image_dir:包含trainin映像的子文件夹的根文件夹字符串。
category:从训练、测试或验证中提取图像的集合的名称字符串。
返回:
满足请求参数的图像的文件系统路径字符串。
"""

label_name: pic

if label_name not in image_lists:
tf.logging.fatal('Label does not exist %s.', label_name)
label_lists = image_lists[label_name]
if category not in label_lists:
tf.logging.fatal('Category does not exist %s.', category)

label_lists = {'dir': 'pic', 'training': 1600, 'validation': 200, 'testing': 200}

获取 category_list 为训练集、测试集或验证集 中的图片

category_list = label_lists[category]
if not category_list:
tf.logging.fatal('Label %s has no images in the category %s.',
label_name, category)
mod_index = index % len(category_list) # mod_index为某个序号
base_name = category_list[mod_index] # base_name为序号对应的图片
sub_dir = label_lists['dir'] # pic

bottleneck_dir/pic/base_name

最终的地址为数据 根目录的地址 + 类别的文件夹 + 图片的名称

full_path = os.path.join(image_dir, sub_dir, base_name)
return full_path

这个函数通过类别名称、所属数据集和图片编号 获取经过Inception-v3模型处理之后的特征向量文件地址

def get_bottleneck_path(image_lists, label_name, index, bottleneck_dir,
category):
""""Returns a path to a bottleneck file for a label at the given index.

Args:
image_lists: Dictionary of training images for each label.
label_name: Label string we want to get an image for.
index: Integer offset of the image we want. This will be moduloed by the
available number of images for the label, so it can be arbitrarily large.
bottleneck_dir: Folder string holding cached files of bottleneck values.
category: Name string of set to pull images from - training, testing, or
validation.

Returns:
File system path string to an image that meets the requested parameters.
"""
"""
Args:
image_lists:每个标签的训练图像字典。
label_name:我们要为其获取图像的标签字符串。
index:我们想要的图像的整数偏移量。这将被标签的可用图像数模化,因此它可以任意大。
bottleneck_dir:存放瓶颈值缓存文件的文件夹字符串。
category:从训练、测试或验证中提取图像的集合的名称字符串。
返回:
满足请求参数的图像的文件系统路径字符串。
"""

index:从0开始,为该划分集的(总数-1)

return get_image_path(image_lists, label_name, index, bottleneck_dir,
category) + '.txt'

从保存的GraphDef文件创建一个图像,并返回一个图像对象

def create_inception_graph():
""""Creates a graph from saved GraphDef file and returns a Graph object.

Returns:
Graph holding the trained Inception network, and various tensors we'll be
manipulating.
返回:
包含训练的起始网络和我们要操纵的各种张量的图。
JPEG_DATA_TENSOR_NAME = 'DecodeJpeg/contents:0'
BOTTLENECK_TENSOR_NAME = 'pool_3/_reshape:0'
RESIZED_INPUT_TENSOR_NAME = 'ResizeBilinear:0'
"""
with tf.Session() as sess:
model_filename = os.path.join(
FLAGS.model_dir, 'classify_image_graph_def.pb')
with gfile.FastGFile(model_filename, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
bottleneck_tensor, jpeg_data_tensor, resized_input_tensor = (
tf.import_graph_def(graph_def, name='', return_elements=[
BOTTLENECK_TENSOR_NAME, JPEG_DATA_TENSOR_NAME,
RESIZED_INPUT_TENSOR_NAME]))
print("---------------------------------------------------------")
print("bottleneck_tensor:",bottleneck_tensor)
print("bottleneck_tensor-type:",type(bottleneck_tensor))
print("bottleneck_tensor:",np.shape(bottleneck_tensor))
print("--------------------------------------------------8888888888888888888888888---")

aux_logits=bottleneck_tensor

aux_logits = tf.identity(end_points['mixed_17x17x768e'])

aux_logits = tf.identity(bottleneck_tensor)
with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],stride=1, padding='SAME'):  
  with tf.variable_scope('AuxLogits'):   # variable   # initializer=tf.random_normal_initializer()
    # 17*17*768  --->  5*5*768
    aux_logits = slim.avg_pool2d(
          aux_logits, [5, 5], stride=3, padding='VALID',
          scope='AvgPool_1a_5x5')
    print("AvgPool_1a_5x5:",np.shape(aux_logits))
    # 以下两个卷积:  5*5*768  --> 1*1*768
    aux_logits = slim.conv2d(aux_logits, 128, [1, 1],
                               scope='Conv2d_1b_1x1')
    print("Conv2d_1b_1x1:",np.shape(aux_logits))
    aux_logits = slim.conv2d(aux_logits, 768, [5,5],
         weights_initializer=trunc_normal(0.01),
          padding='VALID', scope='Conv2d_2a_5x5')
    print("Conv2d_2a_5x5",np.shape(aux_logits))
    # 将 1*1*1*768变为 1*768
    # 中间两个维度变化:1*1*1*768 

if spatial_squeeze:

aux_logits = tf.squeeze(aux_logits, [1, 2], name='SpatialSqueeze')

aux_logits=tf.squeeze(aux_logits,[1,2],name='SpatialSqueeze')

    aux_logits = tf.reshape(aux_logits,(-1,768),name='SpatialSqueeze')

sess.run(aux_logits.initializer)

    # 返回的向量维度为 1*768
    bottleneck_tensor = aux_logits
print("auxiliary_tensor-shape:",np.shape(bottleneck_tensor))
print("info:",bottleneck_tensor)
print("XXXXXXXXXXXXXx------------XXXXXXXXXXXXXXXXXXXXXXXx")

return sess.graph, bottleneck_tensor, jpeg_data_tensor, resized_input_tensor

在图像上运行推理,以提取“瓶颈”摘要层

这个函数使用加载的训练好的Inception-v3模型处理一张图片,得到这个图片的特征向量。

def run_bottleneck_on_image(sess, image_data, image_data_tensor,
bottleneck_tensor):
"""Runs inference on an image to extract the 'bottleneck' summary layer.

Args:
sess: Current active TensorFlow Session.
image_data: String of raw JPEG data.
image_data_tensor: Input data layer in the graph.
bottleneck_tensor: Layer before the final softmax.

Returns:
Numpy array of bottleneck values.
NumPy数组的瓶颈值。
"""

这个过程实际上就是将当前图片作为输入计算瓶颈张量的值。这个瓶颈张量的值就是这张图片的新的特征向量

bottleneck_values = sess.run(
bottleneck_tensor,
{image_data_tensor: image_data})

squeeze 函数:从数组的形状中删除单维度条目,即把shape中为1的维度去掉

经过卷积神经网络处理的结果是一个四维数组,需要将这个结果压缩成一个特征向量(一维数组)

bottleneck_values = np.squeeze(bottleneck_values)
return bottleneck_values

下载并提取模型tar文件

def maybe_download_and_extract():
"""Download and extract model tar file.

If the pretrained model we're using doesn't already exist, this function
downloads it from the TensorFlow.org website and unpacks it into a directory.

如果我们使用的预培训模型还不存在,那么这个函数将从tensorflow.org网站下载它并将其解包到一个目录中。
"""
dest_directory = FLAGS.model_dir

是否创建目录

if not os.path.exists(dest_directory):
os.makedirs(dest_directory)

DATA_URL = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'

DATA_URL--下载链接

filename = DATA_URL.split('/')[-1]
filepath = os.path.join(dest_directory, filename)
if not os.path.exists(filepath):
def _progress(count, block_size, total_size):
sys.stdout.write('\r>> Downloading %s %.1f%%' %
(filename,
float(count * block_size) / float(total_size) * 100.0))
sys.stdout.flush()

filepath, _ = urllib.request.urlretrieve(DATA_URL,
                                         filepath,
                                         _progress)
print()
statinfo = os.stat(filepath)
print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')

解压

tarfile.open(filepath, 'r:gz').extractall(dest_directory)

确保文件夹存在于磁盘上;没有就创建

def ensure_dir_exists(dir_name):
"""Makes sure the folder exists on disk.

Args:
dir_name: Path string to the folder we want to create.
dir_name: 我们想创建的文件夹路径的字符串。
"""
if not os.path.exists(dir_name):
os.makedirs(dir_name)

将一个已给定的floats列表写入到一个二进制文件

def write_list_of_floats_to_file(list_of_floats , file_path):
"""Writes a given list of floats to a binary file.

Args:
list_of_floats: List of floats we want to write to a file.
file_path: Path to a file where list of floats will be stored.

list_of_floats:我们想写入到一个文件的floats列表。
file_path:floats列表文件将要存储的路径。
"""

s = struct.pack('d' * BOTTLENECK_TENSOR_SIZE, *list_of_floats)
with open(file_path, 'wb') as f:
f.write(s)

从一个给定的文件读取floats列表

def read_list_of_floats_from_file(file_path):
"""Reads list of floats from a given file.

Args:
file_path: Path to a file where list of floats was stored.
floats列表文件存储的的路径
Returns:
Array of bottleneck values (list of floats).
瓶颈值的数组 (floats列表)

"""

with open(file_path, 'rb') as f:
s = struct.unpack('d' * BOTTLENECK_TENSOR_SIZE, f.read())
return list(s)

bottleneck_path_2_bottleneck_values = {}

检索或计算图像的瓶颈值。

如果磁盘上存在瓶颈数据的缓存版本,则返回; 否则计算数据并将其保存到磁盘 以备将来使用。

这个函数获取一张图片经过Inception-v3模型处理之后的特征向量。

这个函数会先试图寻找已经计算且保存下来的特征向量,如果找不到则先计算这个特征向量,然后保存到文件

def get_or_create_bottleneck(sess, image_lists, label_name, index, image_dir,
category, bottleneck_dir, jpeg_data_tensor,
bottleneck_tensor):
"""Retrieves or calculates bottleneck values for an image.

If a cached version of the bottleneck data exists on-disk, return that,
otherwise calculate the data and save it to disk for future use.

Args:
sess: The current active TensorFlow Session.
image_lists: Dictionary of training images for each label.
label_name: Label string we want to get an image for.
index: Integer offset of the image we want. This will be modulo-ed by the
available number of images for the label, so it can be arbitrarily large.
image_dir: Root folder string of the subfolders containing the training
images.
category: Name string of which set to pull images from - training, testing,
or validation.
bottleneck_dir: Folder string holding cached files of bottleneck values.
jpeg_data_tensor: The tensor to feed loaded jpeg data into.
bottleneck_tensor: The output tensor for the bottleneck values.

Returns:
Numpy array of values produced by the bottleneck layer for the image.
通过图像的瓶颈层产生的NumPy数组值
"""

label_name:子文件夹名字pic

index:从0开始,为该划分集的(总数-1)

bottleneck_dir: /tmp/bottleneck_dir

label_lists = {'dir': 'pic', 'training': 1600, 'validation': 200, 'testing': 200}

label_lists = image_lists[label_name]
sub_dir = label_lists['dir'] # pic
sub_dir_path = os.path.join(bottleneck_dir, sub_dir) # /tmp/bottleneck_dir/pic
ensure_dir_exists(sub_dir_path)

返回给定索引中的标签的瓶颈文件的路径

index:从0开始,为该划分集的(总数-1)

bottleneck_dir/pic/base_name+ '.txt'之类

bottleneck_path = get_bottleneck_path(image_lists, label_name, index,
bottleneck_dir, category)

print("bottleneck_path:",bottleneck_path) # /tmp/bottleneck/pic/207.jpg.txt

如果这个特征向量文件不存在,则通过Inception-v3模型来计算特征向量,并将计算的结果存入文件

if not os.path.exists(bottleneck_path):

print('Creating bottleneck at ' + bottleneck_path)

# 定义函数,通过类别名称、所属数据集和图片编码获取一张图片的地址
# 获取原始的图片路径  
image_path = get_image_path(image_lists, label_name, index, image_dir,
                            category)
if not gfile.Exists(image_path):
  tf.logging.fatal('File does not exist %s', image_path)
# 读取相应的图片

print("读取相应的图片")

print("--------------------------------------------")

# 获取图片内容
image_data = gfile.FastGFile(image_path, 'rb').read()
# 由于输入的图片大小不一致,此处得到的image_data大小也不一致(已验证),但却都能通过加载的inception-v3模型生成一个2048的特征向量。具体原理不详。  
# 通过Inception-v3模型计算特征向量  
bottleneck_values = run_bottleneck_on_image(sess, image_data,
                                            jpeg_data_tensor,
                                            bottleneck_tensor)
# 将计算得到的特征向量存入文件  
bottleneck_string = ','.join(str(x) for x in bottleneck_values)
with open(bottleneck_path, 'w') as bottleneck_file:
  bottleneck_file.write(bottleneck_string)

bottleneck_dir/pic/base_name+ '.txt'之类

直接从文件中获取图片相应的特征向量

with open(bottleneck_path, 'r') as bottleneck_file:
bottleneck_string = bottleneck_file.read()

print("bottleneck_string:",bottleneck_string )

print("bottleneck_string的维度为:",np.shape(bottleneck_string))

print("--------------------------------------------")

bottleneck_values = [float(x) for x in bottleneck_string.split(',')]

print("bottleneck_values:",np.shape(bottleneck_values))

print("bottleneck_values:",bottleneck_values)

print("--------------------------------------------")

返回得到的特征向量

return bottleneck_values

确保所有的 训练、测试和验证 瓶颈被缓存

def cache_bottlenecks(sess, image_lists, image_dir, bottleneck_dir,
jpeg_data_tensor, bottleneck_tensor):
"""Ensures all the training, testing, and validation bottlenecks are cached.

Because we're likely to read the same image multiple times (if there are no
distortions applied during training) it can speed things up a lot if we
calculate the bottleneck layer values once for each image during
preprocessing, and then just read those cached values repeatedly during
training. Here we go through all the images we've found, calculate those
values, and save them off.
因为我们可能会多次读取同一个图像(如果在训练中没有应用扭曲)。如果我们每个图像预处理期间的瓶颈层值只计算一次,
在训练时只需反复读取这些缓存值,能大幅的加快速度。在这里,我们检测所有发现的图像,计算那些值,并保存。

Args:
sess: The current active TensorFlow Session.
image_lists: Dictionary of training images for each label.
image_dir: Root folder string of the subfolders containing the training images.
bottleneck_dir: Folder string holding cached files of bottleneck values. /tmp/bottleneck_dir
jpeg_data_tensor: Input tensor for jpeg data from file.
bottleneck_tensor: The penultimate output layer of the graph.

Returns:
Nothing.
"""
how_many_bottlenecks = 0

确保 bottleneck_dir路径名 存在

ensure_dir_exists(bottleneck_dir)

label_name:子文件夹名字pic

image_lists = {'pic': {'dir': 'pic', 'training': 1600, 'validation': 200, 'testing': 200}}

for label_name, label_lists in image_lists.items():
for category in ['training', 'testing', 'validation']:
# 得到每个划分集里面的图片
category_list = label_lists[category]
# index:从0开始,为该划分集的(总数-1)
# label_name: pic
# bottleneck_dir: /tmp/bottleneck_dir
for index, unused_base_name in enumerate(category_list):
get_or_create_bottleneck(sess, image_lists, label_name, index,
image_dir, category, bottleneck_dir,
jpeg_data_tensor, bottleneck_tensor)
how_many_bottlenecks += 1
if how_many_bottlenecks % 100 == 0:
print(str(how_many_bottlenecks) + ' bottleneck files created.')

labels: ['desert', 'mountains', 'sea', 'sunset', 'trees']

返回 image_labels_dir/xxx.jpg.txt 里面的真实标签

def get_ground_truth(labels_file, labels, class_count):
# CACHED_GROUND_TRUTH_VECTORS={} :包含缓存的真实向量,以防止反复计算它们
if labels_file in CACHED_GROUND_TRUTH_VECTORS.keys():
ground_truth = CACHED_GROUND_TRUTH_VECTORS[labels_file]
else:
with open(labels_file) as f:
true_labels = f.read().splitlines()
ground_truth = np.zeros(class_count, dtype=np.float32)

print("读取标签:",true_labels)

    idx = 0
    for label in labels:
        if label in true_labels:
            ground_truth[idx] = 1.0
        idx += 1
    CACHED_GROUND_TRUTH_VECTORS[labels_file] = ground_truth

return ground_truth

#检索缓存图像的 瓶颈值

这个函数随机获取一个batch的图片作为训练数据。

def get_random_cached_bottlenecks(sess, image_lists, how_many, category,
bottleneck_dir, image_dir, jpeg_data_tensor,
bottleneck_tensor, labels):
"""Retrieves bottleneck values for cached images.

If no distortions are being applied, this function can retrieve the cached
bottleneck values directly from disk for images. It picks a random set of
images from the specified category.
如果没有应用扭曲,这个函数可以直接从磁盘检索图像缓存的瓶颈值。它从指定类别的图像挑选了一套随机的数据集。

Args:
sess: Current TensorFlow Session.
image_lists: Dictionary of training images for each label.
how_many: The number of bottleneck values to return.
category: Name string of which set to pull from - training, testing, or
validation.
bottleneck_dir: Folder string holding cached files of bottleneck values.
image_dir: Root folder string of the subfolders containing the training
images.
jpeg_data_tensor: The layer to feed jpeg image data into.
bottleneck_tensor: The bottleneck output layer of the CNN graph.
labels: All possible labels loaded from file labels.txt.

Returns:
List of bottleneck arrays and their corresponding ground truths.
瓶颈数组的列表,它们对应于ground truths和相关的文件名。
"""

class_count = len(image_lists.keys())

labels: ['desert', 'mountains', 'sea', 'sunset', 'trees']

class_count = len(labels)
bottlenecks = []
ground_truths = []
for unused_i in range(how_many):
# 随机一个类别和图片的编号加入当前的训练数据
# label_index = random.randrange(class_count)
label_index = 0 # there is only one folder with images = 'multi-label'
# image_lists = {'pic': {'dir': 'pic', 'training': 1600, 'validation': 200, 'testing': 200}}
label_name = list(image_lists.keys())[label_index] # label_name:pic
# image_index可能取很大的一个数值:100321384
image_index = random.randrange(MAX_NUM_IMAGES_PER_CLASS + 1)
# 通过Inception-v3模型计算图片对应的特征向量,并将其加入最终数据的列表。
bottleneck = get_or_create_bottleneck(sess, image_lists, label_name,
image_index, image_dir, category,
bottleneck_dir, jpeg_data_tensor,
bottleneck_tensor)

print("bottleneck:",np.shape(bottleneck)) # bottleneck: (2048,)

# IMAGE_LABELS_DIR = 'image_labels_dir'
labels_file = get_image_labels_path(image_lists, label_name, image_index, IMAGE_LABELS_DIR, category)
# labels: ['desert', 'mountains', 'sea', 'sunset', 'trees']
ground_truth = get_ground_truth(labels_file, labels, class_count)

print("ground_truth:",ground_truth) # ground_truth: [0. 0. 0. 1. 0.]

print("bottleneck-len:",len(bottleneck))

bottlenecks.append(bottleneck)
ground_truths.append(ground_truth)

print("bottlenecks:",bottlenecks)

print("==================================================================")

return bottlenecks, ground_truths

检索训练图像扭曲后的瓶颈值。

def get_random_distorted_bottlenecks(
sess, image_lists, how_many, category, image_dir, input_jpeg_tensor,
distorted_image, resized_input_tensor, bottleneck_tensor, labels):
"""Retrieves bottleneck values for training images, after distortions.

If we're training with distortions like crops, scales, or flips, we have to
recalculate the full model for every image, and so we can't use cached
bottleneck values. Instead we find random images for the requested category,
run them through the distortion graph, and then the full graph to get the
bottleneck results for each.

如果我们训练使用扭曲变换,如裁剪,缩放,或翻转,我们必须重新计算每个图像的完整模型,所以我们不能使用缓存的瓶颈值。
相反,我们找出所要求类别的随机图像,通过扭曲图运行它们,然后得到每个瓶颈结果完整的图。

Args:
sess: Current TensorFlow Session.
image_lists: Dictionary of training images for each label.
how_many: The integer number of bottleneck values to return.
category: Name string of which set of images to fetch - training, testing,
or validation.
image_dir: Root folder string of the subfolders containing the training
images.
input_jpeg_tensor: The input layer we feed the image data to.
distorted_image: The output node of the distortion graph.
resized_input_tensor: The input node of the recognition graph.
bottleneck_tensor: The bottleneck output layer of the CNN graph.
labels: All possible labels loaded from file labels.txt.

Returns:
List of bottleneck arrays and their corresponding ground truths.
瓶颈阵列及其对应的ground truths列表。
"""
class_count = len(labels)
bottlenecks = []
ground_truths = []
for unused_i in range(how_many):
label_index = 0 # there is only one folder with images = 'multi-label'
label_name = list(image_lists.keys())[label_index]
image_index = random.randrange(MAX_NUM_IMAGES_PER_CLASS + 1)
image_path = get_image_path(image_lists, label_name, image_index, image_dir,
category)
if not gfile.Exists(image_path):
tf.logging.fatal('File does not exist %s', image_path)
jpeg_data = gfile.FastGFile(image_path, 'rb').read()
# Note that we materialize the distorted_image_data as a numpy array before
# sending running inference on the image. This involves 2 memory copies and
# might be optimized in other implementations.
distorted_image_data = sess.run(distorted_image,
{input_jpeg_tensor: jpeg_data})
bottleneck = run_bottleneck_on_image(sess, distorted_image_data,
resized_input_tensor,
bottleneck_tensor)
# IMAGE_LABELS_DIR = 'image_labels_dir'
labels_file = get_image_labels_path(image_lists, label_name, image_index, IMAGE_LABELS_DIR, category)
ground_truth = get_ground_truth(labels_file, labels, class_count)

bottlenecks.append(bottleneck)
ground_truths.append(ground_truth)

return bottlenecks, ground_truths

从输入标志是否已启用任何扭曲。

def should_distort_images(flip_left_right, random_crop, random_scale,
random_brightness):
"""Whether any distortions are enabled, from the input flags.

Args:
flip_left_right: Boolean whether to randomly mirror images horizontally.
random_crop: Integer percentage setting the total margin used around the
crop box.
random_scale: Integer percentage of how much to vary the scale by.
random_brightness: Integer range to randomly multiply the pixel values by.

Returns:
Boolean value indicating whether any distortions should be applied.
布尔值,指示是否应用任何扭曲。
"""
return (flip_left_right or (random_crop != 0) or (random_scale != 0) or
(random_brightness != 0))

创建用于应用指定扭曲的操作。

def add_input_distortions(flip_left_right, random_crop, random_scale,
random_brightness):
"""Creates the operations to apply the specified distortions.
在训练过程中,如果我们运行的图像通过简单的扭曲,如裁剪,缩放和翻转,可以帮助改进结果。
这些反映我们期望在现实世界中的变化,因此可以帮助训练模型,以更有效地应对自然数据。
在这里,我们采取的供应参数并构造一个操作网络以将它们应用到图像中。
During training it can help to improve the results if we run the images
through simple distortions like crops, scales, and flips. These reflect the
kind of variations we expect in the real world, and so can help train the
model to cope with natural data more effectively. Here we take the supplied
parameters and construct a network of operations to apply them to an image.

Cropping


Cropping is done by placing a bounding box at a random position in the full
image. The cropping parameter controls the size of that box relative to the
input image. If it's zero, then the box is the same size as the input and no
cropping is performed. If the value is 50%, then the crop box will be half the
width and height of the input. In a diagram it looks like this:
裁剪是通过在完整的图像上一个随机的位置放置一个边界框。裁剪参数控制该框相对于输入图像的尺寸大小。
如果它是零,那么该框以输入图像相同的大小作为输入不进行裁剪。
如果值是50%,则裁剪框将是输入的宽度和高度的一半。

<       width         >
+---------------------+
|                     |
|   width - crop%     |
|    <      >         |
|    +------+         |
|    |      |         |
|    |      |         |
|    |      |         |
|    +------+         |
|                     |
|                     |
+---------------------+

Scaling
~~~~~~~

Scaling is a lot like cropping, except that the bounding box is always
centered and its size varies randomly within the given range. For example if
the scale percentage is zero, then the bounding box is the same size as the
input and no scaling is applied. If it's 50%, then the bounding box will be in
a random range between half the width and height and full size.

 缩放是非常像裁剪,除了边界框总是在中心和它的大小在给定的范围内随机变化。
 例如,如果缩放比例百分比为零,则边界框与输入尺寸大小相同,没有缩放应用。
 如果它是50%,那么边界框将是宽度和高度的一半和全尺寸之间的随机范围。
Args:
Args:
  flip_left_right: Boolean whether to randomly mirror images horizontally.
  random_crop: Integer percentage setting the total margin used around the
  crop box.
  random_scale: Integer percentage of how much to vary the scale by.
  random_brightness: Integer range to randomly multiply the pixel values by.
  graph.

Returns:
  The jpeg input layer and the distorted result tensor.
    JPEG输入层和扭曲结果的张量。
"""
print("正在对图像进行 增强 处理...")
jpeg_data = tf.placeholder(tf.string, name='DistortJPGInput')
decoded_image = tf.image.decode_jpeg(jpeg_data, channels=MODEL_INPUT_DEPTH)
decoded_image_as_float = tf.cast(decoded_image, dtype=tf.float32)
decoded_image_4d = tf.expand_dims(decoded_image_as_float, 0)
margin_scale = 1.0 + (random_crop / 100.0)
resize_scale = 1.0 + (random_scale / 100.0)
margin_scale_value = tf.constant(margin_scale)
resize_scale_value = tf.random_uniform(tensor_shape.scalar(),
                                       minval=1.0,
                                       maxval=resize_scale)
scale_value = tf.multiply(margin_scale_value, resize_scale_value)
precrop_width = tf.multiply(scale_value, MODEL_INPUT_WIDTH)
precrop_height = tf.multiply(scale_value, MODEL_INPUT_HEIGHT)
precrop_shape = tf.stack([precrop_height, precrop_width])
precrop_shape_as_int = tf.cast(precrop_shape, dtype=tf.int32)
precropped_image = tf.image.resize_bilinear(decoded_image_4d,
                                            precrop_shape_as_int)
precropped_image_3d = tf.squeeze(precropped_image, squeeze_dims=[0])
cropped_image = tf.random_crop(precropped_image_3d,
                               [MODEL_INPUT_HEIGHT, MODEL_INPUT_WIDTH,
                                MODEL_INPUT_DEPTH])
if flip_left_right:
  flipped_image = tf.image.random_flip_left_right(cropped_image)
else:
  flipped_image = cropped_image
brightness_min = 1.0 - (random_brightness / 100.0)
brightness_max = 1.0 + (random_brightness / 100.0)
brightness_value = tf.random_uniform(tensor_shape.scalar(),
                                     minval=brightness_min,
                                     maxval=brightness_max)
brightened_image = tf.multiply(flipped_image, brightness_value)
distort_result = tf.expand_dims(brightened_image, 0, name='DistortResult')
return jpeg_data, distort_result


# 附加一个张量的很多总结(为tensorboard可视化)
def variable_summaries(var, name):
"""Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
with tf.name_scope('summaries'):
  mean = tf.reduce_mean(var)
  tf.summary.scalar('mean/' + name, mean)
  with tf.name_scope('stddev'):
    stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
  tf.summary.scalar('stddev/' + name, stddev)
  tf.summary.scalar('max/' + name, tf.reduce_max(var))
  tf.summary.scalar('min/' + name, tf.reduce_min(var))
  tf.summary.histogram(name, var)


# 为训练增加了一个新的softmax和全连接层
def add_final_training_ops(class_count, final_tensor_name, bottleneck_tensor):
"""Adds a new softmax and fully-connected layer for training.

We need to retrain the top layer to identify our new classes, so this function
adds the right operations to the graph, along with some variables to hold the
weights, and then sets up all the gradients for the backward pass.
 我们需要重新训练顶层识别我们新的类,所以这个函数向图表添加正确的操作,
 以及一些变量来保持权重,然后设置所有的梯度向后传递。

The set up for the softmax and fully-connected layers is based on:
https://tensorflow.org/versions/master/tutorials/mnist/beginners/index.html

Args:
  class_count: Integer of how many categories of things we're trying to
  recognize.
  final_tensor_name: Name string for the new final node that produces results.
  bottleneck_tensor: The output of the main CNN graph.

Returns:
  The tensors for the training and cross entropy results, and tensors for the
  bottleneck input and ground truth input.
  训练的张量和交叉熵的结果,瓶颈输入和groud truth输入的张量。
"""
# final_tensor_name=final_result
# BOTTLENECK_TENSOR_SIZE = 2048
# 定义新的神经网络输入,这个输入就是新的图片经过Inception-v3模型前向传播到达瓶颈层时的结点取值。
with tf.name_scope('input'):
  # 可以将这个过程类似的理解为一种特征提取。  
  bottleneck_input = tf.placeholder_with_default(
      bottleneck_tensor, shape=[None, BOTTLENECK_TENSOR_SIZE],
      name='BottleneckInputPlaceholder')
#    bottleneck_input = tf.placeholder(
#        tf.float32, shape=[None, BOTTLENECK_TENSOR_SIZE],
#        name='BottleneckInputPlaceholder')
  # 定义新的标准答案输入  
  ground_truth_input = tf.placeholder(tf.float32,
                                      [None, class_count],
                                      name='GroundTruthInput')
#    x=tf.constant(0.01,shape=[1,768])
#    ground_truth_input = tf.placeholder_with_default(x,
#                                        [None, class_count],
#                                        name='GroundTruthInput')

# Organizing the following ops as `final_training_ops` so they're easier
# to see in TensorBoard
# 定义一层全连接层来解决新的图片分类问题。  
# 因为训练好的Inception-v3模型已经将原始的图片抽象为了更加容易分类的特征向量了,
# 所以不需要再训练那么复杂的神经网络来完成这个新的分类任务。 
layer_name = 'final_training_ops'
with tf.name_scope(layer_name):
  with tf.name_scope('weights'):
    # 权重
    layer_weights = tf.Variable(tf.truncated_normal([BOTTLENECK_TENSOR_SIZE, class_count], stddev=0.001), name='final_weights')
    variable_summaries(layer_weights, layer_name + '/weights')
  with tf.name_scope('biases'):
    # 偏置
    layer_biases = tf.Variable(tf.zeros([class_count]), name='final_biases')
    variable_summaries(layer_biases, layer_name + '/biases')
  with tf.name_scope('Wx_plus_b'):
    logits = tf.matmul(bottleneck_input, layer_weights) + layer_biases
    tf.summary.histogram(layer_name + '/pre_activations', logits)
# sigmoid函数
final_tensor = tf.nn.sigmoid(logits, name=final_tensor_name)
tf.summary.histogram(final_tensor_name + '/activations', final_tensor)

with tf.name_scope('cross_entropy'):
  # 对于给定的logits计算sigmoid的交叉熵。
  # 衡量的是分类任务中的概率误差,它也是试用于每一个类别都是相不排斥的(代码中可以看出)
  # 例如,有的可以划到多个类别中,给你一张照片,,同时包含大象和狗
  cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(
    logits=logits, labels=ground_truth_input)
  with tf.name_scope('total'):
     # 取 均值
    cross_entropy_mean = tf.reduce_mean(cross_entropy)
  tf.summary.scalar('cross entropy', cross_entropy_mean)

with tf.name_scope('train'): 
  # 梯度下降算法
  train_step = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(
      cross_entropy_mean)
print("可以返回train_step....................")
print("train_step可以传参....")
return (train_step, cross_entropy_mean, bottleneck_input, ground_truth_input,
        final_tensor)


# 插入我们需要的操作,以评估我们结果的准确性。
def add_evaluation_step(result_tensor, ground_truth_tensor):
"""Inserts the operations we need to evaluate the accuracy of our results.

Args:
  result_tensor: The new final node that produces results.
  ground_truth_tensor: The node we feed ground truth data
  into.
 
result_tensor:产生结果的新的最终节点。
ground_truth_tensor:我们将真实数据输入的节点。
Returns:
  Nothing.
"""
with tf.name_scope('accuracy'):
  with tf.name_scope('correct_prediction'):
    # tf.argmax(result_tensor, 1) = return index of maximal value (= 1 in a 1-of-N encoding vector) in each row (axis = 1)
    # But we have more ones (indicating multiple labels) in one row of result_tensor due to the multi-label classification
    # correct_prediction = tf.equal(tf.argmax(result_tensor, 1), \
    #   tf.argmax(ground_truth_tensor, 1))

    # ground_truth is not a binary tensor, it contains the probabilities of each label = we need to tf.round() it
    # to acquire a binary tensor allowing comparison by tf.equal()
    # See: http://stackoverflow.com/questions/39219414/in-tensorflow-how-can-i-get-nonzero-values-and-their-indices-from-a-tensor-with
    # x = tf.constant([0.9, 2.5, 2.3, 1.5, -4.5])   
    # tf.round(x)  # [ 1.0, 2.0, 2.0, 2.0, -4.0 ]
    # TensorFlow四舍五入:tf.round函数
    correct_prediction = tf.equal(tf.round(result_tensor), ground_truth_tensor)
  with tf.name_scope('accuracy'):
    # Mean accuracy over all labels:
    # http://stackoverflow.com/questions/37746670/tensorflow-multi-label-accuracy-calculation
    evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  tf.summary.scalar('accuracy', evaluation_step)
return evaluation_step


def main(_):
# Setup the directory we'll write summaries to for TensorBoard
# 为TensorBoard设置我们要写摘要的目录
if tf.gfile.Exists(FLAGS.summaries_dir):
  tf.gfile.DeleteRecursively(FLAGS.summaries_dir)
tf.gfile.MakeDirs(FLAGS.summaries_dir)

# Set up the pre-trained graph.
# 是否下载模型
maybe_download_and_extract()
# 创建 图、瓶颈层、图片输入、图片重调
# 加载读取的Inception-v3模型,并返回数据输入所对应的张量以及计算瓶颈层结果所对应的张量。
graph, bottleneck_tensor, jpeg_data_tensor, resized_image_tensor = (
    create_inception_graph())
print("bottleneck_tensor1:",bottleneck_tensor)
sess = tf.Session()
# 获取未初始化的变量集合
#  [b'Conv2d_1b_1x1/weights' b'Conv2d_1b_1x1/biases' b'Conv2d_2a_5x5/weights' b'Conv2d_2a_5x5/biases']
print(sess.run(tf.report_uninitialized_variables()))
lac_var=[]
[lac_var.append(i) for i in tf.global_variables()]
print(lac_var[0:])
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
sess.run(tf.variables_initializer(lac_var[0:]))
print("bottleneck_tensor:",bottleneck_tensor)
# 全局变量初始化
#  sess.run(tf.global_variables_initializer())
#  session.run(my_variable.initializer)
#  sess.run('Conv2d_1b_1x1/weights'.initializer)
#  sess.run(tf.variables_initializer(bottleneck_tensor))

print("***************************^^^^^^^^^^^^^^^^^^^^^^^")
# Look at the folder structure, and create lists of all the images.
# 对图片进行处理,获得训练集、测试集、验证集
# image_lists = {'pic': {'dir': 'pic', 'training': 1600, 'validation': 200, 'testing': 200}}
image_lists = create_image_lists(FLAGS.image_dir, FLAGS.testing_percentage,
                                 FLAGS.validation_percentage)
#  print(image_lists)

if len(image_lists.keys()) == 0:
    print('Folder containing training images has not been found inside {} directory. \n'
          'Put all the training images into '
          'one folder inside {} directory and delete everything else inside the {} directory.'
          .format(FLAGS.image_dir, FLAGS.image_dir, FLAGS.image_dir))
    return -1

if len(image_lists.keys()) > 1:
    print('More than one folder found inside {} directory. \n'
          'In order to prevent validation issues, put all the training images into '
          'one folder inside {} directory and delete everything else inside the {} directory.'
          .format(FLAGS.image_dir, FLAGS.image_dir, FLAGS.image_dir))
    return -1

if not os.path.isfile(ALL_LABELS_FILE):
    print('File {} containing all possible labels (= classes) does not exist.\n'
          'Create it in project root and put each possible label on new line, '
          'it is exactly the same as creating an image_label file for image '
          'that is in all the possible classes.'.format(ALL_LABELS_FILE))
    return -1

# 获取标签的类别
with open(ALL_LABELS_FILE) as f:
    labels = f.read().splitlines()
# 得到要标注图像标签的总数
print("labels:",labels)
# labels: ['desert', 'mountains', 'sea', 'sunset', 'trees']
class_count = len(labels)

if class_count == 0:
  print('No valid labels inside file {} that should contain all possible labels (= classes).'.format(ALL_LABELS_FILE))
  return -1
if class_count == 1:
  print('Only one valid label found inside {} - multiple classes are needed for classification.'.format(ALL_LABELS_FILE))
  return -1

# See if the command-line flags mean we're applying any distortions.
# 看命令行标记是否意味着我们应用任何扭曲操作。
# should_distort_images()默认情况下,返回值为False
do_distort_images = should_distort_images(
    FLAGS.flip_left_right, FLAGS.random_crop, FLAGS.random_scale,
    FLAGS.random_brightness)
# 是否 对图像进行 增强处理
print("do_distort_images:",do_distort_images)


if do_distort_images:
  # We will be applying distortions, so setup the operations we'll need.
  # 我们将应用扭曲,因此设置我们需要的操作
  distorted_jpeg_data_tensor, distorted_image_tensor = add_input_distortions(
      FLAGS.flip_left_right, FLAGS.random_crop, FLAGS.random_scale,
      FLAGS.random_brightness)
else:
  # We'll make sure we've calculated the 'bottleneck' image summaries and cached them on disk.
  # 我们确定计算bottleneck图像总结并缓存在磁盘上。
  cache_bottlenecks(sess, image_lists, FLAGS.image_dir, FLAGS.bottleneck_dir,
                    jpeg_data_tensor, bottleneck_tensor)

# Add the new layer that we'll be training.
# 添加我们将要训练的新层
# final_tensor = tf.nn.sigmoid(logits, name=final_tensor_name)  # sigmoid函数
print("bottleneck_tensor2:",bottleneck_tensor)   # shape=(1, 2048)
print("*******************************************")
(train_step, cross_entropy, bottleneck_input, ground_truth_input,
 final_tensor) = add_final_training_ops(class_count,
                                        FLAGS.final_tensor_name,
                                        bottleneck_tensor)
# Set up all our weights to their initial default values.
# 设置所有的权重到初始的默认值。
var=[]
#  print([str(i.name) for i in tf.global_variables()])
[var.append(i) for i in tf.global_variables()]
print(var[4:])
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
sess.run(tf.variables_initializer(var[4:]))


# Create the operations we need to evaluate the accuracy of our new layer.
# 创建操作,我们需要评估新层的准确性。
print("final_tensor:",np.shape(final_tensor))  #  (?, 5)
print("ground_truth_input:",np.shape(ground_truth_input))  # (?, 5)
print("****************************")
evaluation_step = add_evaluation_step(final_tensor, ground_truth_input)

# Merge all the summaries and write them out to /tmp/retrain_logs (by default)
# 合并所有的摘要,写到/tmp/retrain_logs(默认)。
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/train',
                                      sess.graph)
validation_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/validation')


#  init=tf.global_variables_initializer()
#  sess.run(init)

# Run the training for as many cycles as requested on the command line.
# 按照命令行的要求运行多个周期的训练。
# how_many_training_steps: 总的训练步骤
for i in range(FLAGS.how_many_training_steps):
  # Get a batch of input bottleneck values, either calculated fresh every time
  # with distortions applied, or from the cache stored on disk.
  #获得一批输入瓶颈值,或是用应用的扭曲每一次计算新的值,或是缓存并存储在磁盘上的值。
  if do_distort_images:
    train_bottlenecks, train_ground_truth = get_random_distorted_bottlenecks(
        sess, image_lists, FLAGS.train_batch_size, 'training',
        FLAGS.image_dir, distorted_jpeg_data_tensor,
        distorted_image_tensor, resized_image_tensor, bottleneck_tensor, labels)
  else:
    train_bottlenecks, train_ground_truth = get_random_cached_bottlenecks(
        sess, image_lists, FLAGS.train_batch_size, 'training',
        FLAGS.bottleneck_dir, FLAGS.image_dir, jpeg_data_tensor,
        bottleneck_tensor, labels)
  # labels: ['desert', 'mountains', 'sea', 'sunset', 'trees']
  # Feed the bottlenecks and ground truth into the graph, and run a training
  # step. Capture training summaries for TensorBoard with the `merged` op.
  # 给图像提供瓶颈和groud truth,运行一个训练阶。用‘合并’op计算训练的TensorBoard摘要。
#    print("train_ground_truth:",np.shape(train_ground_truth))  # (100, 5)  # train_batch_size=100
#    print("----------------------------------------------")
#    print("ground_truth_input:",ground_truth_input)
#    print("train_bottlenecks-shape:",np.shape(train_bottlenecks))   # (100, 2048)
#    print("bottleneck_input:",bottleneck_input)
#    print("bottleneck_input-type:",type(bottleneck_input))
#    print("train_bottlenecks-type:",type(train_bottlenecks))
#    print("----------------------------------------------")
  train_summary, _ = sess.run([merged, train_step],
           feed_dict={bottleneck_input: train_bottlenecks,
                      ground_truth_input: train_ground_truth})
  train_writer.add_summary(train_summary, i)

  # Every so often, print out how well the graph is training.
  #每隔一段时间,打印出来图像是如何训练的。
  is_last_step = (i + 1 == FLAGS.how_many_training_steps)
  if (i % FLAGS.eval_step_interval) == 0 or is_last_step:
    train_accuracy, cross_entropy_value = sess.run(
        [evaluation_step, cross_entropy],
        feed_dict={bottleneck_input: train_bottlenecks,
                   ground_truth_input: train_ground_truth})
    print('%s: Step %d: Train accuracy = %.2f%%' % (datetime.now(), i,
                                                    train_accuracy * 100))
    print('%s: Step %d: Cross entropy = %f' % (datetime.now(), i,
                                               cross_entropy_value))
    # 验证集
    validation_bottlenecks, validation_ground_truth = (
        get_random_cached_bottlenecks(
            sess, image_lists, FLAGS.validation_batch_size, 'validation',
            FLAGS.bottleneck_dir, FLAGS.image_dir, jpeg_data_tensor,
            bottleneck_tensor, labels))
    # Run a validation step and capture training summaries for TensorBoard
    # with the `merged` op.
    # 运行一个验证阶。用‘合并’op计算训练的TensorBoard摘要。
    validation_summary, validation_accuracy = sess.run(
        [merged, evaluation_step],
        feed_dict={bottleneck_input: validation_bottlenecks,
                   ground_truth_input: validation_ground_truth})
    validation_writer.add_summary(validation_summary, i)
    print('%s: Step %d: Validation accuracy = %.2f%%' %
          (datetime.now(), i, validation_accuracy * 100))

print("完成了所有的训练,在一些我们从未用过的新的图像上(测试集),运行一个最后的测试评估...")
# We've completed all our training, so run a final test evaluation on
# some new images we haven't used before.
#我们已完成了所有的训练,在一些我们从未用过的新的图像上,运行一个最后的测试评估。
test_bottlenecks, test_ground_truth = get_random_cached_bottlenecks(
    sess, image_lists, FLAGS.test_batch_size, 'testing',
    FLAGS.bottleneck_dir, FLAGS.image_dir, jpeg_data_tensor,
    bottleneck_tensor, labels)
test_accuracy = sess.run(
    evaluation_step,
    feed_dict={bottleneck_input: test_bottlenecks,
               ground_truth_input: test_ground_truth})
print('Final test accuracy = %.2f%%' % (test_accuracy * 100))

# Write out the trained graph and labels with the weights stored as constants.
# 写出训练的图像和以常数形式存储的权重标签。
output_graph_def = graph_util.convert_variables_to_constants(
    sess, graph.as_graph_def(), [FLAGS.final_tensor_name])
# 保存为:out-tmp/retrained_graph.pb
with gfile.FastGFile(FLAGS.output_graph, 'wb') as f:
  f.write(output_graph_def.SerializeToString())

# 保存为:out-tmp/retrained_labels.txt
with gfile.FastGFile(FLAGS.output_labels, 'w') as f:
  f.write('\n'.join(image_lists.keys()) + '\n')


if __name__ == '__main__':
tf.app.run()

`
I just want to use “ mixed_7/join:0” as an auxiliary classifier, but the program has been reporting errors:tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 76800 values, but the requested shape has 768, how can I solve it?

@gowthamkpr gowthamkpr self-assigned this Jun 10, 2019
@gowthamkpr
Copy link

gowthamkpr commented Jun 10, 2019

@RMuli The error was from the shape mismatch. Please try printing the shapes of the intermediate tensors.

@mymuli
Copy link
Author

mymuli commented Jun 11, 2019

@Gowtham-kp :
The shape of the intermediate tensor is as follows:
lad_tensor: (1, 17, 17, 768)
AvgPool_1a_5x5: (1, 5, 5, 768)
Conv2d_1b_1x1: (1, 5, 5, 128)
Conv2d_2a_5x5 (1, 1, 1, 768)
for example: aux_logits --> Tensor("××××", shape=(1, 1, 1, 768), dtype=float32)
and I want to convert a four-dimensional tensor into a two-dimensional tensor, I use the following method:
bottleneck_tensor=tf.squeeze(aux_logits,[1,2],name='SpatialSqueeze')
or:
bottleneck_tensor=tf.reshape(aux_logits,(-1,768),name='SpatialSqueeze')
So the result is:
bottleneck_tensor: Tensor("×××××", shape=(1, 768), dtype=float32)

But I use the following statement :
bottleneck_input = tf.placeholder_with_default(
bottleneck_tensor, shape=[None, 768],
name='BottleneckInputPlaceholder')

The program reported the following error:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 76800 values, but the requested shape has 768
[[Node: train/gradients/AuxLogits/SpatialSqueeze_grad/Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](train/gradients/final_training_ops/Wx_plus_b/MatMul_grad/tuple/control_dependency, train/gradients/AuxLogits/SpatialSqueeze_grad/Shape)]]
and 76800=768×100,train_batch_size=100

But if I use the following statement, the program does not report errors:
bottleneck_input = tf.placeholder_with_default(
s, shape=[None, 768],
name='BottleneckInputPlaceholder')
and s is the type of array

but I find in TensorBoard, the nodes in the graph are disconnected

I'm very confused about it !

@gowthamkpr
Copy link

@RMuli please go through the following issue and also please post this question on tensorflow stackoverflow form where there are more users to help. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants