develop-a-deep-learning-caption-generation-model-in-python.md 72.4 KB
Newer Older
W
dl nlp  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 如何从头开发深度学习图片标题生成器

> 原文: [https://machinelearningmastery.com/develop-a-deep-learning-caption-generation-model-in-python/](https://machinelearningmastery.com/develop-a-deep-learning-caption-generation-model-in-python/)

#### 开发一个深度学习模型自动
用 Keras 逐步描述 Python 中的照片。

字幕生成是一个具有挑战性的人工智能问题,必须为给定的照片生成文本描述。

它既需要计算机视觉的方法来理解图像的内容,也需要来自自然语言处理领域的语言模型,以便将图像的理解转化为正确的单词。最近,深度学习方法已经在这个问题的例子上取得了最新的成果。

深度学习方法已经证明了关于字幕生成问题的最新结果。这些方法最令人印象深刻的是,可以定义单个端到端模型来预测标题,给定照片,而不是需要复杂的数据准备或专门设计模型的管道。

在本教程中,您将了解如何从头开发照片字幕深度学习模型。

完成本教程后,您将了解:

*   如何准备用于训练深度学习模型的照片和文本数据。
*   如何设计和训练深度学习字幕生成模型。
W
wizardforcel 已提交
20
*   如何评估训练标题生成模型并使用它来标注全新的照片。
W
dl nlp  
wizardforcel 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

**注**:摘录自:“[深度学习自然语言处理](https://machinelearningmastery.com/deep-learning-for-nlp/)”。
看一下,如果你想要更多的分步教程,在使用文本数据时充分利用深度学习方法。

让我们开始吧。

*   **2017 年 11 月更新**:添加了关于 Keras 2.1.0 和 2.1.1 中引入的影响本教程中代码的错误的说明。
*   **2017 年 12 月更新**:在解释如何将描述保存到文件时更新了函数名称中的拼写错误,感谢 Minel。
*   **Update Apr / 2018** :增加了一个新的部分,展示了如何使用渐进式加载为具有最小 RAM 的工作站训练模型。
*   **2002 年 2 月更新**:提供了 Flickr8k_Dataset 数据集的直接链接,因为官方网站已被删除。

![How to Develop a Deep Learning Caption Generation Model in Python from Scratch](img/7c3093e713bfc0f44e9aa591c5ae3415.jpg)

如何从头开始在 Python 中开发深度学习字幕生成模型
照片由[生活在蒙罗维亚](https://www.flickr.com/photos/livinginmonrovia/8069637650/),保留一些权利。

## 教程概述

本教程分为 6 个部分;他们是:

1.  照片和标题数据集
2.  准备照片数据
3.  准备文本数据
4.  开发深度学习模型
5.  逐步加载训练( **NEW**
6.  评估模型
7.  生成新标题

### Python 环境

本教程假设您安装了 Python SciPy 环境,理想情况下使用 Python 3。

您必须安装带有 TensorFlow 或 Theano 后端的 Keras(2.2 或更高版本)。

本教程还假设您安装了 scikit-learn,Pandas,NumPy 和 Matplotlib。

如果您需要有关环境的帮助,请参阅本教程:

*   [如何使用 Anaconda 设置用于机器学习和深度学习的 Python 环境](https://machinelearningmastery.com/setup-python-environment-machine-learning-deep-learning-anaconda/)

我建议在带 GPU 的系统上运行代码。您可以在 Amazon Web Services 上以低成本方式访问 GPU。在本教程中学习如何:

W
wizardforcel 已提交
63
*   [如何设置 Amazon AWS EC2 GPU 以训练 Keras 深度学习模型(循序渐进)](https://machinelearningmastery.com/develop-evaluate-large-deep-learning-models-keras-amazon-web-services/)
W
dl nlp  
wizardforcel 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

让我们潜入。

## 照片和标题数据集

Flickr8K 数据集是开始使用图像字幕时使用的一个很好的数据集。

原因是因为它是现实的并且相对较小,因此您可以使用 CPU 在工作站上下载它并构建模型。

数据集的确切描述在论文“[框架图像描述作为排名任务:数据,模型和评估指标](https://www.jair.org/media/3994/live-3994-7274-jair.pdf)”从 2013 年开始。

作者将数据集描述如下:

> 我们为基于句子的图像描述和搜索引入了一个新的基准集合,包括 8,000 个图像,每个图像与五个不同的标题配对,提供对显着实体和事件的清晰描述。
> 
> ...
> 
> 图像是从六个不同的 Flickr 组中选择的,并且往往不包含任何知名人物或位置,而是手动选择以描绘各种场景和情况。

- [框架图像描述作为排名任务:数据,模型和评估指标](https://www.jair.org/media/3994/live-3994-7274-jair.pdf),2013。

数据集可免费获得。您必须填写申请表,并通过电子邮件将链接发送给您。我很乐意为您链接,但电子邮件地址明确要求:“_ 请不要重新分发数据集 _”。

您可以使用以下链接来请求数据集:

*   [数据集申请表](https://illinois.edu/fb/sec/1713398)

在短时间内,您将收到一封电子邮件,其中包含指向两个文件的链接:

*   **Flickr8k_Dataset.zip** (1 千兆字节)所有照片的存档。
*   **Flickr8k_text.zip** (2.2 兆字节)照片所有文字说明的档案。

**UPDATE(2019 年 2 月)**:官方网站似乎已被删除(虽然表格仍然有效)。以下是我的[数据集 GitHub 存储库](https://github.com/jbrownlee/Datasets)的一些直接下载链接:

*   [Flickr8k_Dataset.zip](https://github.com/jbrownlee/Datasets/releases/download/Flickr8k/Flickr8k_Dataset.zip)
*   [Flickr8k_text.zip](https://github.com/jbrownlee/Datasets/releases/download/Flickr8k/Flickr8k_text.zip)

下载数据集并将其解压缩到当前工作目录中。您将有两个目录:

*   **Flicker8k_Dataset** :包含 8092 张 JPEG 格式的照片。
*   **Flickr8k_text** :包含许多包含不同照片描述来源的文件。

数据集具有预定义的训练数据集(6,000 个图像),开发数据集(1,000 个图像)和测试数据集(1,000 个图像)。

可用于评估模型技能的一个衡量标准是 BLEU 分数。作为参考,下面是在测试数据集上评估时对于熟练模型的一些球场 BLEU 分数(取自 2017 年论文“[将图像放入图像标题生成器](https://arxiv.org/abs/1703.09137)”中):

*   BLEU-1:0.401 至 0.578。
*   BLEU-2:0.176 至 0.390。
*   BLEU-3:0.099 至 0.260。
*   BLEU-4:0.059 至 0.170。

我们在评估模型时会更晚地描述 BLEU 指标。

接下来,我们来看看如何加载图像。

## 准备照片数据

我们将使用预先训练的模型来解释照片的内容。

有很多型号可供选择。在这种情况下,我们将使用 2014 年赢得 ImageNet 竞赛的 Oxford Visual Geometry Group 或 VGG 模型。在此处了解有关该模型的更多信息:

*   [用于大规模视觉识别的超深卷积网络](http://www.robots.ox.ac.uk/~vgg/research/very_deep/)

Keras 直接提供这种预先训练的模型。请注意,第一次使用此模型时,Keras 将从 Internet 下载模型权重,大约为 500 兆字节。这可能需要几分钟,具体取决于您的互联网连接。

我们可以将此模型用作更广泛的图像标题模型的一部分。问题是,它是一个大型模型,每次我们想要测试一个新的语言模型配置(下游)是多余的时,通过网络运行每张照片。

相反,我们可以使用预先训练的模型预先计算“照片功能”并将其保存到文件中。然后,我们可以稍后加载这些功能,并将它们作为数据集中给定照片的解释提供给我们的模型。通过完整的 VGG 模型运行照片也没有什么不同;我们只是提前做过一次。

这是一种优化,可以更快地训练我们的模型并消耗更少的内存。

我们可以使用 VGG 类在 Keras 中加载 VGG 模型。我们将从加载的模型中删除最后一层,因为这是用于预测照片分类的模型。我们对图像分类不感兴趣,但我们对分类前的照片内部表示感兴趣。这些是模型从照片中提取的“特征”。

Keras 还提供了用于将加载的照片整形为模型的优选尺寸的工具(例如,3 通道 224×224 像素图像)。

下面是一个名为 _extract_features()_ 的函数,给定目录名称,将加载每张照片,为 VGG 准备,并从 VGG 模型中收集预测的特征。图像特征是 1 维 4,096 元素向量。

该函数返回图像标识符的字典到图像特征。

W
wizardforcel 已提交
143
```py
W
dl nlp  
wizardforcel 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
# extract features from each photo in the directory
def extract_features(directory):
	# load the model
	model = VGG16()
	# re-structure the model
	model.layers.pop()
	model = Model(inputs=model.inputs, outputs=model.layers[-1].output)
	# summarize
	print(model.summary())
	# extract features from each photo
	features = dict()
	for name in listdir(directory):
		# load an image from file
		filename = directory + '/' + name
		image = load_img(filename, target_size=(224, 224))
		# convert the image pixels to a numpy array
		image = img_to_array(image)
		# reshape data for the model
		image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
		# prepare the image for the VGG model
		image = preprocess_input(image)
		# get features
		feature = model.predict(image, verbose=0)
		# get image id
		image_id = name.split('.')[0]
		# store feature
		features[image_id] = feature
		print('>%s' % name)
	return features
```

我们可以调用此函数来准备用于测试模型的照片数据,然后将生成的字典保存到名为“ _features.pkl_ ”的文件中。

下面列出了完整的示例。

W
wizardforcel 已提交
179
```py
W
dl nlp  
wizardforcel 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
from os import listdir
from pickle import dump
from keras.applications.vgg16 import VGG16
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.models import Model

# extract features from each photo in the directory
def extract_features(directory):
	# load the model
	model = VGG16()
	# re-structure the model
	model.layers.pop()
	model = Model(inputs=model.inputs, outputs=model.layers[-1].output)
	# summarize
	print(model.summary())
	# extract features from each photo
	features = dict()
	for name in listdir(directory):
		# load an image from file
		filename = directory + '/' + name
		image = load_img(filename, target_size=(224, 224))
		# convert the image pixels to a numpy array
		image = img_to_array(image)
		# reshape data for the model
		image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
		# prepare the image for the VGG model
		image = preprocess_input(image)
		# get features
		feature = model.predict(image, verbose=0)
		# get image id
		image_id = name.split('.')[0]
		# store feature
		features[image_id] = feature
		print('>%s' % name)
	return features

# extract features from all images
directory = 'Flicker8k_Dataset'
features = extract_features(directory)
print('Extracted Features: %d' % len(features))
# save to file
dump(features, open('features.pkl', 'wb'))
```

运行此数据准备步骤可能需要一段时间,具体取决于您的硬件,可能需要一个小时的 CPU 与现代工作站。

在运行结束时,您将提取的特征存储在' _features.pkl_ '中供以后使用。该文件大小约为 127 兆字节。

## 准备文本数据

数据集包含每张照片的多个描述,描述文本需要一些最小的清洁。

如果您不熟悉清理文本数据,请参阅此帖子:

*   [如何使用 Python 清理机器学习文本](https://machinelearningmastery.com/clean-text-machine-learning-python/)

首先,我们将加载包含所有描述的文件。

W
wizardforcel 已提交
240
```py
W
dl nlp  
wizardforcel 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
# load doc into memory
def load_doc(filename):
	# open the file as read only
	file = open(filename, 'r')
	# read all text
	text = file.read()
	# close the file
	file.close()
	return text

filename = 'Flickr8k_text/Flickr8k.token.txt'
# load descriptions
doc = load_doc(filename)
```

每张照片都有唯一的标识符。此标识符用于照片文件名和描述的文本文件中。

接下来,我们将逐步浏览照片说明列表。下面定义了一个函数 _load_descriptions()_,给定加载的文档文本,它将返回描述的照片标识符字典。每个照片标识符映射到一个或多个文本描述的列表。

W
wizardforcel 已提交
260
```py
W
dl nlp  
wizardforcel 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
# extract descriptions for images
def load_descriptions(doc):
	mapping = dict()
	# process lines
	for line in doc.split('\n'):
		# split line by white space
		tokens = line.split()
		if len(line) < 2:
			continue
		# take the first token as the image id, the rest as the description
		image_id, image_desc = tokens[0], tokens[1:]
		# remove filename from image id
		image_id = image_id.split('.')[0]
		# convert description tokens back to string
		image_desc = ' '.join(image_desc)
		# create the list if needed
		if image_id not in mapping:
			mapping[image_id] = list()
		# store description
		mapping[image_id].append(image_desc)
	return mapping

# parse descriptions
descriptions = load_descriptions(doc)
print('Loaded: %d ' % len(descriptions))
```

W
wizardforcel 已提交
288
接下来,我们需要清理描述文本。描述已经被分词并且易于使用。
W
dl nlp  
wizardforcel 已提交
289 290 291 292 293 294 295 296 297 298

我们将通过以下方式清理文本,以减少我们需要使用的单词词汇量:

*   将所有单词转换为小写。
*   删除所有标点符号。
*   删除所有长度不超过一个字符的单词(例如“a”)。
*   删除包含数字的所有单词。

下面定义 _clean_descriptions()_ 函数,给定描述图像标识符的字典,逐步执行每个描述并清理文本。

W
wizardforcel 已提交
299
```py
W
dl nlp  
wizardforcel 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
import string

def clean_descriptions(descriptions):
	# prepare translation table for removing punctuation
	table = str.maketrans('', '', string.punctuation)
	for key, desc_list in descriptions.items():
		for i in range(len(desc_list)):
			desc = desc_list[i]
			# tokenize
			desc = desc.split()
			# convert to lower case
			desc = [word.lower() for word in desc]
			# remove punctuation from each token
			desc = [w.translate(table) for w in desc]
			# remove hanging 's' and 'a'
			desc = [word for word in desc if len(word)>1]
			# remove tokens with numbers in them
			desc = [word for word in desc if word.isalpha()]
			# store as string
			desc_list[i] =  ' '.join(desc)

# clean descriptions
clean_descriptions(descriptions)
```

清理完毕后,我们可以总结一下词汇量的大小。

理想情况下,我们想要一个既富有表现力又尽可能小的词汇。较小的词汇量将导致较小的模型将更快地训练。

作为参考,我们可以将干净的描述转换为一个集合并打印其大小,以了解我们的数据集词汇表的大小。

W
wizardforcel 已提交
331
```py
W
dl nlp  
wizardforcel 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
# convert the loaded descriptions into a vocabulary of words
def to_vocabulary(descriptions):
	# build a list of all description strings
	all_desc = set()
	for key in descriptions.keys():
		[all_desc.update(d.split()) for d in descriptions[key]]
	return all_desc

# summarize vocabulary
vocabulary = to_vocabulary(descriptions)
print('Vocabulary Size: %d' % len(vocabulary))
```

最后,我们可以将图像标识符和描述字典保存到名为 _descriptionss.txt_ 的新文件中,每行一个图像标识符和描述。

下面定义 _save_descriptions()_ 函数,给定包含标识符到描述和文件名的映射的字典,将映射保存到文件。

W
wizardforcel 已提交
349
```py
W
dl nlp  
wizardforcel 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
# save descriptions to file, one per line
def save_descriptions(descriptions, filename):
	lines = list()
	for key, desc_list in descriptions.items():
		for desc in desc_list:
			lines.append(key + ' ' + desc)
	data = '\n'.join(lines)
	file = open(filename, 'w')
	file.write(data)
	file.close()

# save descriptions
save_descriptions(descriptions, 'descriptions.txt')
```

综合这些,下面提供了完整的列表。

W
wizardforcel 已提交
367
```py
W
dl nlp  
wizardforcel 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
import string

# load doc into memory
def load_doc(filename):
	# open the file as read only
	file = open(filename, 'r')
	# read all text
	text = file.read()
	# close the file
	file.close()
	return text

# extract descriptions for images
def load_descriptions(doc):
	mapping = dict()
	# process lines
	for line in doc.split('\n'):
		# split line by white space
		tokens = line.split()
		if len(line) < 2:
			continue
		# take the first token as the image id, the rest as the description
		image_id, image_desc = tokens[0], tokens[1:]
		# remove filename from image id
		image_id = image_id.split('.')[0]
		# convert description tokens back to string
		image_desc = ' '.join(image_desc)
		# create the list if needed
		if image_id not in mapping:
			mapping[image_id] = list()
		# store description
		mapping[image_id].append(image_desc)
	return mapping

def clean_descriptions(descriptions):
	# prepare translation table for removing punctuation
	table = str.maketrans('', '', string.punctuation)
	for key, desc_list in descriptions.items():
		for i in range(len(desc_list)):
			desc = desc_list[i]
			# tokenize
			desc = desc.split()
			# convert to lower case
			desc = [word.lower() for word in desc]
			# remove punctuation from each token
			desc = [w.translate(table) for w in desc]
			# remove hanging 's' and 'a'
			desc = [word for word in desc if len(word)>1]
			# remove tokens with numbers in them
			desc = [word for word in desc if word.isalpha()]
			# store as string
			desc_list[i] =  ' '.join(desc)

# convert the loaded descriptions into a vocabulary of words
def to_vocabulary(descriptions):
	# build a list of all description strings
	all_desc = set()
	for key in descriptions.keys():
		[all_desc.update(d.split()) for d in descriptions[key]]
	return all_desc

# save descriptions to file, one per line
def save_descriptions(descriptions, filename):
	lines = list()
	for key, desc_list in descriptions.items():
		for desc in desc_list:
			lines.append(key + ' ' + desc)
	data = '\n'.join(lines)
	file = open(filename, 'w')
	file.write(data)
	file.close()

filename = 'Flickr8k_text/Flickr8k.token.txt'
# load descriptions
doc = load_doc(filename)
# parse descriptions
descriptions = load_descriptions(doc)
print('Loaded: %d ' % len(descriptions))
# clean descriptions
clean_descriptions(descriptions)
# summarize vocabulary
vocabulary = to_vocabulary(descriptions)
print('Vocabulary Size: %d' % len(vocabulary))
# save to file
save_descriptions(descriptions, 'descriptions.txt')
```

首先运行该示例打印加载的照片描述的数量(8,092)和清晰词汇的大小(8,763 个单词)。

W
wizardforcel 已提交
457
```py
W
dl nlp  
wizardforcel 已提交
458 459 460 461 462 463 464 465
Loaded: 8,092
Vocabulary Size: 8,763
```

最后,干净的描述写入' _descriptionss.txt_ '。

看一下这个文件,我们可以看到这些描述已经准备好进行建模了。文件中的描述顺序可能有所不同。

W
wizardforcel 已提交
466
```py
W
dl nlp  
wizardforcel 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
2252123185_487f21e336 bunch on people are seated in stadium
2252123185_487f21e336 crowded stadium is full of people watching an event
2252123185_487f21e336 crowd of people fill up packed stadium
2252123185_487f21e336 crowd sitting in an indoor stadium
2252123185_487f21e336 stadium full of people watch game
...
```

## 开发深度学习模型

在本节中,我们将定义深度学习模型并将其拟合到训练数据集上。

本节分为以下几部分:

1.  加载数据中。
2.  定义模型。
3.  适合模型。
4.  完整的例子。

### 加载数据中

首先,我们必须加载准备好的照片和文本数据,以便我们可以使用它来适应模型。

W
wizardforcel 已提交
490
我们将训练训练数据集中所有照片和标题的数据。在训练期间,我们将监控模型在开发数据集上的表现,并使用该表现来决定何时将模型保存到文件。
W
dl nlp  
wizardforcel 已提交
491

W
wizardforcel 已提交
492
训练和开发数据集已分别在 _Flickr_8k.trainImages.txt_ 和 _Flickr_8k.devImages.txt_ 文件中预定义,两者都包含照片文件名列表。从这些文件名中,我们可以提取照片标识符并使​​用这些标识符来过滤每组的照片和说明。
W
dl nlp  
wizardforcel 已提交
493

W
wizardforcel 已提交
494
下面的函数 _load_set()_ 将在给定训练或开发集文件名的情况下加载一组预定义的标识符。
W
dl nlp  
wizardforcel 已提交
495

W
wizardforcel 已提交
496
```py
W
dl nlp  
wizardforcel 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
# load doc into memory
def load_doc(filename):
	# open the file as read only
	file = open(filename, 'r')
	# read all text
	text = file.read()
	# close the file
	file.close()
	return text

# load a pre-defined list of photo identifiers
def load_set(filename):
	doc = load_doc(filename)
	dataset = list()
	# process line by line
	for line in doc.split('\n'):
		# skip empty lines
		if len(line) < 1:
			continue
		# get the image identifier
		identifier = line.split('.')[0]
		dataset.append(identifier)
	return set(dataset)
```

W
wizardforcel 已提交
522
现在,我们可以使用预定义的一系列训练或开发标识符来加载照片和描述。
W
dl nlp  
wizardforcel 已提交
523 524 525 526 527 528 529

下面是函数 _load_clean_descriptions()_,它为来自' _descriptionss.txt_ '的已清除文本描述加载给定的一组标识符,并将标识符字典返回给文本描述列表。

我们将开发的模型将生成给定照片的标题,并且标题将一次生成一个单词。将提供先前生成的单词的序列作为输入。因此,我们需要一个'_ 第一个字 _'来启动生成过程,'_ 最后一个字 _'来表示标题的结尾。

为此,我们将使用字符串' _startseq_ '和' _endseq_ '。这些令牌在加载时会添加到已加载的描述中。在我们对文本进行编码之前,现在执行此操作非常重要,这样才能正确编码令牌。

W
wizardforcel 已提交
530
```py
W
dl nlp  
wizardforcel 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
# load clean descriptions into memory
def load_clean_descriptions(filename, dataset):
	# load document
	doc = load_doc(filename)
	descriptions = dict()
	for line in doc.split('\n'):
		# split line by white space
		tokens = line.split()
		# split id from description
		image_id, image_desc = tokens[0], tokens[1:]
		# skip images not in the set
		if image_id in dataset:
			# create list
			if image_id not in descriptions:
				descriptions[image_id] = list()
			# wrap description in tokens
			desc = 'startseq ' + ' '.join(image_desc) + ' endseq'
			# store
			descriptions[image_id].append(desc)
	return descriptions
```

接下来,我们可以加载给定数据集的照片功能。

下面定义了一个名为 _load_photo_features()_ 的函数,它加载了整套照片描述,然后返回给定照片标识符集的感兴趣子集。

这不是很有效;尽管如此,这将使我们快速起步。

W
wizardforcel 已提交
559
```py
W
dl nlp  
wizardforcel 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572
# load photo features
def load_photo_features(filename, dataset):
	# load all features
	all_features = load(open(filename, 'rb'))
	# filter features
	features = {k: all_features[k] for k in dataset}
	return features
```

我们可以暂停一下,测试迄今为止开发的所有内容

完整的代码示例如下所示。

W
wizardforcel 已提交
573
```py
W
dl nlp  
wizardforcel 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
from pickle import load

# load doc into memory
def load_doc(filename):
	# open the file as read only
	file = open(filename, 'r')
	# read all text
	text = file.read()
	# close the file
	file.close()
	return text

# load a pre-defined list of photo identifiers
def load_set(filename):
	doc = load_doc(filename)
	dataset = list()
	# process line by line
	for line in doc.split('\n'):
		# skip empty lines
		if len(line) < 1:
			continue
		# get the image identifier
		identifier = line.split('.')[0]
		dataset.append(identifier)
	return set(dataset)

# load clean descriptions into memory
def load_clean_descriptions(filename, dataset):
	# load document
	doc = load_doc(filename)
	descriptions = dict()
	for line in doc.split('\n'):
		# split line by white space
		tokens = line.split()
		# split id from description
		image_id, image_desc = tokens[0], tokens[1:]
		# skip images not in the set
		if image_id in dataset:
			# create list
			if image_id not in descriptions:
				descriptions[image_id] = list()
			# wrap description in tokens
			desc = 'startseq ' + ' '.join(image_desc) + ' endseq'
			# store
			descriptions[image_id].append(desc)
	return descriptions

# load photo features
def load_photo_features(filename, dataset):
	# load all features
	all_features = load(open(filename, 'rb'))
	# filter features
	features = {k: all_features[k] for k in dataset}
	return features

# load training dataset (6K)
filename = 'Flickr8k_text/Flickr_8k.trainImages.txt'
train = load_set(filename)
print('Dataset: %d' % len(train))
# descriptions
train_descriptions = load_clean_descriptions('descriptions.txt', train)
print('Descriptions: train=%d' % len(train_descriptions))
# photo features
train_features = load_photo_features('features.pkl', train)
print('Photos: train=%d' % len(train_features))
```

运行此示例首先在测试数据集中加载 6,000 个照片标识符。然后,这些功能用于过滤和加载已清理的描述文本和预先计算的照片功能。

我们快到了。

W
wizardforcel 已提交
645
```py
W
dl nlp  
wizardforcel 已提交
646 647 648 649 650 651 652 653 654 655 656
Dataset: 6,000
Descriptions: train=6,000
Photos: train=6,000
```

描述文本需要先编码为数字,然后才能像输入中那样呈现给模型,或者与模型的预测进行比较。

编码数据的第一步是创建从单词到唯一整数值​​的一致映射。 Keras 提供 _Tokenizer_ 类,可以从加载的描述数据中学习这种映射。

下面定义 _to_lines()_ 将描述字典转换为字符串列表和 _create_tokenizer()_ 函数,在给定加载的照片描述文本的情况下,它将适合 Tokenizer。

W
wizardforcel 已提交
657
```py
W
dl nlp  
wizardforcel 已提交
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
# convert a dictionary of clean descriptions to a list of descriptions
def to_lines(descriptions):
	all_desc = list()
	for key in descriptions.keys():
		[all_desc.append(d) for d in descriptions[key]]
	return all_desc

# fit a tokenizer given caption descriptions
def create_tokenizer(descriptions):
	lines = to_lines(descriptions)
	tokenizer = Tokenizer()
	tokenizer.fit_on_texts(lines)
	return tokenizer

# prepare tokenizer
tokenizer = create_tokenizer(train_descriptions)
vocab_size = len(tokenizer.word_index) + 1
print('Vocabulary Size: %d' % vocab_size)
```

我们现在可以对文本进行编码。

W
wizardforcel 已提交
680
每个描述将分为单词。该模型将提供一个单词和照片,并生成下一个单词。然后,将描述的前两个单词作为输入提供给模型,以生成下一个单词。这就是模型的训练方式。
W
dl nlp  
wizardforcel 已提交
681 682 683

例如,输入序列“_ 在场 _ 中运行的小女孩”将被分成 6 个输入 - 输出对来训练模型:

W
wizardforcel 已提交
684
```py
W
dl nlp  
wizardforcel 已提交
685 686 687 688 689 690 691 692 693 694 695
X1,		X2 (text sequence), 						y (word)
photo	startseq, 									little
photo	startseq, little,							girl
photo	startseq, little, girl, 					running
photo	startseq, little, girl, running, 			in
photo	startseq, little, girl, running, in, 		field
photo	startseq, little, girl, running, in, field, endseq
```

稍后,当模型用于生成描述时,生成的单词将被连接并递归地提供作为输入以生成图像的标题。

W
wizardforcel 已提交
696
以下函数命名为 _create_sequences()_,给定分词器,最大序列长度以及所有描述和照片的字典,将数据转换为输入 - 输出数据对以训练模型。模型有两个输入数组:一个用于照片功能,另一个用于编码文本。模型有一个输出,它是文本序列中编码的下一个单词。
W
dl nlp  
wizardforcel 已提交
697 698 699 700 701

输入文本被编码为整数,其将被馈送到字嵌入层。照片功能将直接送到模型的另一部分。该模型将输出预测,该预测将是词汇表中所有单词的概率分布。

因此,输出数据将是每个单词的单热编码版本,表示在除了实际单词位置之外的所有单词位置具有 0 值的理想化概率分布,其具有值 1。

W
wizardforcel 已提交
702
```py
W
dl nlp  
wizardforcel 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
# create sequences of images, input sequences and output words for an image
def create_sequences(tokenizer, max_length, descriptions, photos):
	X1, X2, y = list(), list(), list()
	# walk through each image identifier
	for key, desc_list in descriptions.items():
		# walk through each description for the image
		for desc in desc_list:
			# encode the sequence
			seq = tokenizer.texts_to_sequences([desc])[0]
			# split one sequence into multiple X,y pairs
			for i in range(1, len(seq)):
				# split into input and output pair
				in_seq, out_seq = seq[:i], seq[i]
				# pad input sequence
				in_seq = pad_sequences([in_seq], maxlen=max_length)[0]
				# encode output sequence
				out_seq = to_categorical([out_seq], num_classes=vocab_size)[0]
				# store
				X1.append(photos[key][0])
				X2.append(in_seq)
				y.append(out_seq)
	return array(X1), array(X2), array(y)
```

我们需要计算最长描述中的最大字数。名为 _max_length()_ 的短辅助函数定义如下。

W
wizardforcel 已提交
729
```py
W
dl nlp  
wizardforcel 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742
# calculate the length of the description with the most words
def max_length(descriptions):
	lines = to_lines(descriptions)
	return max(len(d.split()) for d in lines)
```

我们现在已经足够加载训练和开发数据集的数据,并将加载的数据转换为输入 - 输出对,以适应深度学习模型。

### 定义模型

我们将基于 Marc Tanti 等人描述的“_ 合并模型 _”来定义深度学习。在 2017 年的论文中:

*   [将图像放在图像标题生成器](https://arxiv.org/abs/1703.09137)中的位置,2017。
W
wizardforcel 已提交
743
*   [循环神经网络(RNN)在图像标题生成器中的作用是什么?](https://arxiv.org/abs/1708.02043) ,2017。
W
dl nlp  
wizardforcel 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756

有关此架构的温和介绍,请参阅帖子:

*   [使用编码器 - 解码器模型的注入和合并架构生成字幕](https://machinelearningmastery.com/caption-generation-inject-merge-architectures-encoder-decoder-model/)

作者提供了一个很好的模型示意图,如下所示。

![Schematic of the Merge Model For Image Captioning](img/a5a04b56f81f1075fd690ba33b5bc864.jpg)

图像标题合并模型的示意图

我们将分三个部分描述该模型:

W
wizardforcel 已提交
757 758
*   **照片功能提取器**。这是在 ImageNet 数据集上预训练的 16 层 VGG 模型。我们已经使用 VGG 模型预处理了照片(没有输出层),并将使用此模型预测的提取特征作为输入。
*   **序列处理器**。这是用于处理文本输入的单词嵌入层,后面是长短期记忆(LSTM)循环神经网络层。
W
wizardforcel 已提交
759
*   **解码器**(缺少一个更好的名字)。特征提取器和序列处理器都输出固定长度的向量。这些被合并在一起并由 Dense 层处理以进行最终预测。
W
dl nlp  
wizardforcel 已提交
760

W
wizardforcel 已提交
761
Photo Feature Extractor 模型要求输入照片要素是 4,096 个元素的向量。这些由 Dense 层处理以产生照片的 256 个元素表示。
W
dl nlp  
wizardforcel 已提交
762 763 764 765 766 767 768 769 770

序列处理器模型期望具有预定义长度(34 个字)的输入序列被馈送到嵌入层,该嵌入层使用掩码来忽略填充值。接下来是具有 256 个存储器单元的 LSTM 层。

两个输入模型都产生 256 个元素向量。此外,两个输入模型都以 50%的丢失形式使用正则化。这是为了减少过度拟合训练数据集,因为这种模型配置学得非常快。

解码器模型使用加法运算合并来自两个输入模型的向量。然后将其馈送到密集 256 神经元层,然后馈送到最终输出密集层,该密集层对序列中的下一个字的整个输出词汇表进行 softmax 预测。

下面名为 _ 的函数 define_model()_ 定义并返回准备好的模型。

W
wizardforcel 已提交
771
```py
W
dl nlp  
wizardforcel 已提交
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
# define the captioning model
def define_model(vocab_size, max_length):
	# feature extractor model
	inputs1 = Input(shape=(4096,))
	fe1 = Dropout(0.5)(inputs1)
	fe2 = Dense(256, activation='relu')(fe1)
	# sequence model
	inputs2 = Input(shape=(max_length,))
	se1 = Embedding(vocab_size, 256, mask_zero=True)(inputs2)
	se2 = Dropout(0.5)(se1)
	se3 = LSTM(256)(se2)
	# decoder model
	decoder1 = add([fe2, se3])
	decoder2 = Dense(256, activation='relu')(decoder1)
	outputs = Dense(vocab_size, activation='softmax')(decoder2)
	# tie it together [image, seq] [word]
	model = Model(inputs=[inputs1, inputs2], outputs=outputs)
	model.compile(loss='categorical_crossentropy', optimizer='adam')
	# summarize model
	print(model.summary())
	plot_model(model, to_file='model.png', show_shapes=True)
	return model
```

W
wizardforcel 已提交
796
要了解模型的结构,特别是层的形状,请参阅下面列出的摘要。
W
dl nlp  
wizardforcel 已提交
797

W
wizardforcel 已提交
798
```py
W
dl nlp  
wizardforcel 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to
====================================================================================================
input_2 (InputLayer)             (None, 34)            0
____________________________________________________________________________________________________
input_1 (InputLayer)             (None, 4096)          0
____________________________________________________________________________________________________
embedding_1 (Embedding)          (None, 34, 256)       1940224     input_2[0][0]
____________________________________________________________________________________________________
dropout_1 (Dropout)              (None, 4096)          0           input_1[0][0]
____________________________________________________________________________________________________
dropout_2 (Dropout)              (None, 34, 256)       0           embedding_1[0][0]
____________________________________________________________________________________________________
dense_1 (Dense)                  (None, 256)           1048832     dropout_1[0][0]
____________________________________________________________________________________________________
lstm_1 (LSTM)                    (None, 256)           525312      dropout_2[0][0]
____________________________________________________________________________________________________
add_1 (Add)                      (None, 256)           0           dense_1[0][0]
                                                                   lstm_1[0][0]
____________________________________________________________________________________________________
dense_2 (Dense)                  (None, 256)           65792       add_1[0][0]
____________________________________________________________________________________________________
dense_3 (Dense)                  (None, 7579)          1947803     dense_2[0][0]
====================================================================================================
Total params: 5,527,963
Trainable params: 5,527,963
Non-trainable params: 0
____________________________________________________________________________________________________
```

我们还创建了一个图表来可视化网络结构,更好地帮助理解两个输入流。

![Plot of the Caption Generation Deep Learning Model](img/3a9ec93ec57895a672f3fd9adac0be96.jpg)

标题生成深度学习模型的情节

### 适合模型

现在我们知道如何定义模型,我们可以将它放在训练数据集上。

该模型学习快速且快速地适应训练数据集。因此,我们将监控训练模型在保持开发数据集上的技能。当开发数据集上的模型技能在时代结束时得到改善时,我们将整个模型保存到文件中。

在运行结束时,我们可以使用训练数据集中具有最佳技能的已保存模型作为我们的最终模型。

我们可以通过在 Keras 中定义 _ModelCheckpoint_ 并指定它来监控验证数据集上的最小损失并将模型保存到文件名中具有训练和验证损失的文件来实现。

W
wizardforcel 已提交
845
```py
W
dl nlp  
wizardforcel 已提交
846 847 848 849 850 851 852 853 854
# define checkpoint callback
filepath = 'model-ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5'
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
```

然后我们可以通过 _ 回调 _ 参数在 _fit()_ 的调用中指定检查点。我们还必须通过 _validation_data_ 参数在 _fit()_ 中指定开发数据集。

我们只适用于 20 个时代的模型,但考虑到训练数据的数量,每个时代在现代硬件上可能需要 30 分钟。

W
wizardforcel 已提交
855
```py
W
dl nlp  
wizardforcel 已提交
856 857 858 859 860 861 862 863
# fit model
model.fit([X1train, X2train], ytrain, epochs=20, verbose=2, callbacks=[checkpoint], validation_data=([X1test, X2test], ytest))
```

### 完整的例子

下面列出了在训练数据上拟合模型的完整示例。

W
wizardforcel 已提交
864
```py
W
dl nlp  
wizardforcel 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
from numpy import array
from pickle import load
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
from keras.utils import plot_model
from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Embedding
from keras.layers import Dropout
from keras.layers.merge import add
from keras.callbacks import ModelCheckpoint

# load doc into memory
def load_doc(filename):
	# open the file as read only
	file = open(filename, 'r')
	# read all text
	text = file.read()
	# close the file
	file.close()
	return text

# load a pre-defined list of photo identifiers
def load_set(filename):
	doc = load_doc(filename)
	dataset = list()
	# process line by line
	for line in doc.split('\n'):
		# skip empty lines
		if len(line) < 1:
			continue
		# get the image identifier
		identifier = line.split('.')[0]
		dataset.append(identifier)
	return set(dataset)

# load clean descriptions into memory
def load_clean_descriptions(filename, dataset):
	# load document
	doc = load_doc(filename)
	descriptions = dict()
	for line in doc.split('\n'):
		# split line by white space
		tokens = line.split()
		# split id from description
		image_id, image_desc = tokens[0], tokens[1:]
		# skip images not in the set
		if image_id in dataset:
			# create list
			if image_id not in descriptions:
				descriptions[image_id] = list()
			# wrap description in tokens
			desc = 'startseq ' + ' '.join(image_desc) + ' endseq'
			# store
			descriptions[image_id].append(desc)
	return descriptions

# load photo features
def load_photo_features(filename, dataset):
	# load all features
	all_features = load(open(filename, 'rb'))
	# filter features
	features = {k: all_features[k] for k in dataset}
	return features

# covert a dictionary of clean descriptions to a list of descriptions
def to_lines(descriptions):
	all_desc = list()
	for key in descriptions.keys():
		[all_desc.append(d) for d in descriptions[key]]
	return all_desc

# fit a tokenizer given caption descriptions
def create_tokenizer(descriptions):
	lines = to_lines(descriptions)
	tokenizer = Tokenizer()
	tokenizer.fit_on_texts(lines)
	return tokenizer

# calculate the length of the description with the most words
def max_length(descriptions):
	lines = to_lines(descriptions)
	return max(len(d.split()) for d in lines)

# create sequences of images, input sequences and output words for an image
def create_sequences(tokenizer, max_length, descriptions, photos):
	X1, X2, y = list(), list(), list()
	# walk through each image identifier
	for key, desc_list in descriptions.items():
		# walk through each description for the image
		for desc in desc_list:
			# encode the sequence
			seq = tokenizer.texts_to_sequences([desc])[0]
			# split one sequence into multiple X,y pairs
			for i in range(1, len(seq)):
				# split into input and output pair
				in_seq, out_seq = seq[:i], seq[i]
				# pad input sequence
				in_seq = pad_sequences([in_seq], maxlen=max_length)[0]
				# encode output sequence
				out_seq = to_categorical([out_seq], num_classes=vocab_size)[0]
				# store
				X1.append(photos[key][0])
				X2.append(in_seq)
				y.append(out_seq)
	return array(X1), array(X2), array(y)

# define the captioning model
def define_model(vocab_size, max_length):
	# feature extractor model
	inputs1 = Input(shape=(4096,))
	fe1 = Dropout(0.5)(inputs1)
	fe2 = Dense(256, activation='relu')(fe1)
	# sequence model
	inputs2 = Input(shape=(max_length,))
	se1 = Embedding(vocab_size, 256, mask_zero=True)(inputs2)
	se2 = Dropout(0.5)(se1)
	se3 = LSTM(256)(se2)
	# decoder model
	decoder1 = add([fe2, se3])
	decoder2 = Dense(256, activation='relu')(decoder1)
	outputs = Dense(vocab_size, activation='softmax')(decoder2)
	# tie it together [image, seq] [word]
	model = Model(inputs=[inputs1, inputs2], outputs=outputs)
	model.compile(loss='categorical_crossentropy', optimizer='adam')
	# summarize model
	print(model.summary())
	plot_model(model, to_file='model.png', show_shapes=True)
	return model

# train dataset

# load training dataset (6K)
filename = 'Flickr8k_text/Flickr_8k.trainImages.txt'
train = load_set(filename)
print('Dataset: %d' % len(train))
# descriptions
train_descriptions = load_clean_descriptions('descriptions.txt', train)
print('Descriptions: train=%d' % len(train_descriptions))
# photo features
train_features = load_photo_features('features.pkl', train)
print('Photos: train=%d' % len(train_features))
# prepare tokenizer
tokenizer = create_tokenizer(train_descriptions)
vocab_size = len(tokenizer.word_index) + 1
print('Vocabulary Size: %d' % vocab_size)
# determine the maximum sequence length
max_length = max_length(train_descriptions)
print('Description Length: %d' % max_length)
# prepare sequences
X1train, X2train, ytrain = create_sequences(tokenizer, max_length, train_descriptions, train_features)

# dev dataset

# load test set
filename = 'Flickr8k_text/Flickr_8k.devImages.txt'
test = load_set(filename)
print('Dataset: %d' % len(test))
# descriptions
test_descriptions = load_clean_descriptions('descriptions.txt', test)
print('Descriptions: test=%d' % len(test_descriptions))
# photo features
test_features = load_photo_features('features.pkl', test)
print('Photos: test=%d' % len(test_features))
# prepare sequences
X1test, X2test, ytest = create_sequences(tokenizer, max_length, test_descriptions, test_features)

# fit model

# define the model
model = define_model(vocab_size, max_length)
# define checkpoint callback
filepath = 'model-ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5'
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
# fit model
model.fit([X1train, X2train], ytrain, epochs=20, verbose=2, callbacks=[checkpoint], validation_data=([X1test, X2test], ytest))
```

W
wizardforcel 已提交
1046
首先运行该示例将打印已加载的训练和开发数据集的摘要。
W
dl nlp  
wizardforcel 已提交
1047

W
wizardforcel 已提交
1048
```py
W
dl nlp  
wizardforcel 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
Dataset: 6,000
Descriptions: train=6,000
Photos: train=6,000
Vocabulary Size: 7,579
Description Length: 34
Dataset: 1,000
Descriptions: test=1,000
Photos: test=1,000
```

W
wizardforcel 已提交
1059
在对模型进行总结之后,我们可以了解训练和验证(开发)输入 - 输出对的总数。
W
dl nlp  
wizardforcel 已提交
1060

W
wizardforcel 已提交
1061
```py
W
dl nlp  
wizardforcel 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070
Train on 306,404 samples, validate on 50,903 samples
```

然后运行该模型,将最佳模型保存到.h5 文件中。

在我的运行中,最佳验证结果已保存到文件中:

*   _model-ep002-loss3.245-val_loss3.612.h5_

W
wizardforcel 已提交
1071
该模型在第 2 迭代结束时保存,训练数据集损失 3.245,开发数据集损失 3.612
W
dl nlp  
wizardforcel 已提交
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

您的具体结果会有所不同。

让我知道你在下面的评论中得到了什么。

如果您在 AWS 上运行该示例,请将模型文件复制回当前工作目录。如果您需要 AWS 上的命令帮助,请参阅帖子:

*   [10 个亚马逊网络服务深度学习命令行方案](https://machinelearningmastery.com/command-line-recipes-deep-learning-amazon-web-services/)

你得到的错误如下:

W
wizardforcel 已提交
1083
```py
W
dl nlp  
wizardforcel 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
Memory Error
```

如果是这样,请参阅下一节。

## 训练与渐进式装载

**注意**:如果您在上一节中没有任何问题,请跳过本节。本节适用于那些没有足够内存来训练模型的人,如上一节所述(例如,出于任何原因不能使用 AWS EC2)。

标题模型的训练确实假设你有很多 RAM。

上一节中的代码不具有内存效率,并假设您在具有 32GB 或 64GB RAM 的大型 EC2 实例上运行。如果您在 8GB RAM 的工作站上运行代码,则无法训练模型。

解决方法是使用渐进式加载。这篇文章在帖子中标题为“ _Progressive Loading_ ”的倒数第二节中详细讨论过:

*   [如何准备用于训练深度学习模型的照片标题数据集](https://machinelearningmastery.com/prepare-photo-caption-dataset-training-deep-learning-model/)

我建议您继续阅读该部分。

如果您想使用渐进式加载来训练此模型,本节将向您展示如何。

第一步是我们必须定义一个可以用作数据生成器的函数。

我们将保持简单,并使数据生成器每批产生一张照片的数据。这将是为照片及其描述生成的所有序列。

_data_generator()_ 下面的函数将是数据生成器,将采用加载的文本描述,照片功能,标记器和最大长度。在这里,我假设您可以将这些训练数据放入内存中,我相信 8GB 的 RAM 应该更有能力。

这是如何运作的?阅读上面刚才提到的引入数据生成器的帖子。

W
wizardforcel 已提交
1113
```py
W
dl nlp  
wizardforcel 已提交
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
# data generator, intended to be used in a call to model.fit_generator()
def data_generator(descriptions, photos, tokenizer, max_length):
	# loop for ever over images
	while 1:
		for key, desc_list in descriptions.items():
			# retrieve the photo feature
			photo = photos[key][0]
			in_img, in_seq, out_word = create_sequences(tokenizer, max_length, desc_list, photo)
			yield [[in_img, in_seq], out_word]
```

您可以看到我们正在调用 _create_sequence()_ 函数来为单个照片而不是整个数据集创建一批数据。这意味着我们必须更新 _create_sequences()_ 函数以删除 for 循环的“迭代所有描述”。

更新的功能如下:

W
wizardforcel 已提交
1129
```py
W
dl nlp  
wizardforcel 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
# create sequences of images, input sequences and output words for an image
def create_sequences(tokenizer, max_length, desc_list, photo):
	X1, X2, y = list(), list(), list()
	# walk through each description for the image
	for desc in desc_list:
		# encode the sequence
		seq = tokenizer.texts_to_sequences([desc])[0]
		# split one sequence into multiple X,y pairs
		for i in range(1, len(seq)):
			# split into input and output pair
			in_seq, out_seq = seq[:i], seq[i]
			# pad input sequence
			in_seq = pad_sequences([in_seq], maxlen=max_length)[0]
			# encode output sequence
			out_seq = to_categorical([out_seq], num_classes=vocab_size)[0]
			# store
			X1.append(photo)
			X2.append(in_seq)
			y.append(out_seq)
	return array(X1), array(X2), array(y)
```

我们现在拥有我们所需要的一切。

W
wizardforcel 已提交
1154
注意,这是一个非常基本的数据生成器。它提供的大内存节省是在拟合模型之前不在存储器中具有训练和测试数据的展开序列,这些样本(例如来自 _create_sequences()_ 的结果)是根据每张照片的需要创建的。
W
dl nlp  
wizardforcel 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

一些关于进一步改进这种数据生成器的袖口想法包括:

*   随机化每个时代的照片顺序。
*   使用照片 ID 列表并根据需要加载文本和照片数据,以进一步缩短内存。
*   每批产生不止一张照片的样品。

我过去经历过这些变化。如果您这样做以及如何参与评论,请告诉我们。

您可以通过直接调用数据生成器来检查数据生成器,如下所示:

W
wizardforcel 已提交
1166
```py
W
dl nlp  
wizardforcel 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
# test the data generator
generator = data_generator(train_descriptions, train_features, tokenizer, max_length)
inputs, outputs = next(generator)
print(inputs[0].shape)
print(inputs[1].shape)
print(outputs.shape)
```

运行此完整性检查将显示一批批量序列的样子,在这种情况下,47 个样本将为第一张照片进行训练。

W
wizardforcel 已提交
1177
```py
W
dl nlp  
wizardforcel 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
(47, 4096)
(47, 34)
(47, 7579)
```

最后,我们可以在模型上使用 _fit_generator()_ 函数来使用此数据生成器训练模型。

在这个简单的例子中,我们将丢弃开发数据集和模型检查点的加载,并在每个训练时期之后简单地保存模型。然后,您可以在训练后返回并加载/评估每个已保存的模型,以找到我们可以在下一部分中使用的最低损失的模型。

使用数据生成器训练模型的代码如下:

W
wizardforcel 已提交
1189
```py
W
dl nlp  
wizardforcel 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
# train the model, run epochs manually and save after each epoch
epochs = 20
steps = len(train_descriptions)
for i in range(epochs):
	# create the data generator
	generator = data_generator(train_descriptions, train_features, tokenizer, max_length)
	# fit for one epoch
	model.fit_generator(generator, epochs=1, steps_per_epoch=steps, verbose=1)
	# save model
	model.save('model_' + str(i) + '.h5')
```

而已。您现在可以使用渐进式加载来训练模型并节省大量 RAM。这也可能慢得多。

下面列出了用于训练字幕生成模型的渐进式加载(使用数据生成器)的完整更新示例。

W
wizardforcel 已提交
1206
```py
W
dl nlp  
wizardforcel 已提交
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
from numpy import array
from pickle import load
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
from keras.utils import plot_model
from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Embedding
from keras.layers import Dropout
from keras.layers.merge import add
from keras.callbacks import ModelCheckpoint

# load doc into memory
def load_doc(filename):
	# open the file as read only
	file = open(filename, 'r')
	# read all text
	text = file.read()
	# close the file
	file.close()
	return text

# load a pre-defined list of photo identifiers
def load_set(filename):
	doc = load_doc(filename)
	dataset = list()
	# process line by line
	for line in doc.split('\n'):
		# skip empty lines
		if len(line) < 1:
			continue
		# get the image identifier
		identifier = line.split('.')[0]
		dataset.append(identifier)
	return set(dataset)

# load clean descriptions into memory
def load_clean_descriptions(filename, dataset):
	# load document
	doc = load_doc(filename)
	descriptions = dict()
	for line in doc.split('\n'):
		# split line by white space
		tokens = line.split()
		# split id from description
		image_id, image_desc = tokens[0], tokens[1:]
		# skip images not in the set
		if image_id in dataset:
			# create list
			if image_id not in descriptions:
				descriptions[image_id] = list()
			# wrap description in tokens
			desc = 'startseq ' + ' '.join(image_desc) + ' endseq'
			# store
			descriptions[image_id].append(desc)
	return descriptions

# load photo features
def load_photo_features(filename, dataset):
	# load all features
	all_features = load(open(filename, 'rb'))
	# filter features
	features = {k: all_features[k] for k in dataset}
	return features

# covert a dictionary of clean descriptions to a list of descriptions
def to_lines(descriptions):
	all_desc = list()
	for key in descriptions.keys():
		[all_desc.append(d) for d in descriptions[key]]
	return all_desc

# fit a tokenizer given caption descriptions
def create_tokenizer(descriptions):
	lines = to_lines(descriptions)
	tokenizer = Tokenizer()
	tokenizer.fit_on_texts(lines)
	return tokenizer

# calculate the length of the description with the most words
def max_length(descriptions):
	lines = to_lines(descriptions)
	return max(len(d.split()) for d in lines)

# create sequences of images, input sequences and output words for an image
def create_sequences(tokenizer, max_length, desc_list, photo):
	X1, X2, y = list(), list(), list()
	# walk through each description for the image
	for desc in desc_list:
		# encode the sequence
		seq = tokenizer.texts_to_sequences([desc])[0]
		# split one sequence into multiple X,y pairs
		for i in range(1, len(seq)):
			# split into input and output pair
			in_seq, out_seq = seq[:i], seq[i]
			# pad input sequence
			in_seq = pad_sequences([in_seq], maxlen=max_length)[0]
			# encode output sequence
			out_seq = to_categorical([out_seq], num_classes=vocab_size)[0]
			# store
			X1.append(photo)
			X2.append(in_seq)
			y.append(out_seq)
	return array(X1), array(X2), array(y)

# define the captioning model
def define_model(vocab_size, max_length):
	# feature extractor model
	inputs1 = Input(shape=(4096,))
	fe1 = Dropout(0.5)(inputs1)
	fe2 = Dense(256, activation='relu')(fe1)
	# sequence model
	inputs2 = Input(shape=(max_length,))
	se1 = Embedding(vocab_size, 256, mask_zero=True)(inputs2)
	se2 = Dropout(0.5)(se1)
	se3 = LSTM(256)(se2)
	# decoder model
	decoder1 = add([fe2, se3])
	decoder2 = Dense(256, activation='relu')(decoder1)
	outputs = Dense(vocab_size, activation='softmax')(decoder2)
	# tie it together [image, seq] [word]
	model = Model(inputs=[inputs1, inputs2], outputs=outputs)
	# compile model
	model.compile(loss='categorical_crossentropy', optimizer='adam')
	# summarize model
	model.summary()
	plot_model(model, to_file='model.png', show_shapes=True)
	return model

# data generator, intended to be used in a call to model.fit_generator()
def data_generator(descriptions, photos, tokenizer, max_length):
	# loop for ever over images
	while 1:
		for key, desc_list in descriptions.items():
			# retrieve the photo feature
			photo = photos[key][0]
			in_img, in_seq, out_word = create_sequences(tokenizer, max_length, desc_list, photo)
			yield [[in_img, in_seq], out_word]

# load training dataset (6K)
filename = 'Flickr8k_text/Flickr_8k.trainImages.txt'
train = load_set(filename)
print('Dataset: %d' % len(train))
# descriptions
train_descriptions = load_clean_descriptions('descriptions.txt', train)
print('Descriptions: train=%d' % len(train_descriptions))
# photo features
train_features = load_photo_features('features.pkl', train)
print('Photos: train=%d' % len(train_features))
# prepare tokenizer
tokenizer = create_tokenizer(train_descriptions)
vocab_size = len(tokenizer.word_index) + 1
print('Vocabulary Size: %d' % vocab_size)
# determine the maximum sequence length
max_length = max_length(train_descriptions)
print('Description Length: %d' % max_length)

# define the model
model = define_model(vocab_size, max_length)
# train the model, run epochs manually and save after each epoch
epochs = 20
steps = len(train_descriptions)
for i in range(epochs):
	# create the data generator
	generator = data_generator(train_descriptions, train_features, tokenizer, max_length)
	# fit for one epoch
	model.fit_generator(generator, epochs=1, steps_per_epoch=steps, verbose=1)
	# save model
	model.save('model_' + str(i) + '.h5')
```

也许评估每个保存的模型,并选择保持数据集中损失最小的最终模型。下一节可能有助于此。

您是否在教程中使用了这个新增功能?
你是怎么去的?

## 评估模型

一旦模型适合,我们就可以评估其预测测试数据集的预测技巧。

我们将通过生成测试数据集中所有照片的描述并使用标准成本函数评估这些预测来评估模型。

首先,我们需要能够使用训练有素的模型生成照片的描述。

这包括传入开始描述标记' _startseq_ ',生成一个单词,然后以生成的单词作为输入递归调用模型,直到到达序列标记结尾' _endseq_ '或达到最大描述长度。

以下名为 _generate_desc()_ 的函数实现此行为,并在给定训练模型和给定准备照片作为输入的情况下生成文本描述。它调用函数 _word_for_id()_ 以将整数预测映射回一个字。

W
wizardforcel 已提交
1398
```py
W
dl nlp  
wizardforcel 已提交
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
# map an integer to a word
def word_for_id(integer, tokenizer):
	for word, index in tokenizer.word_index.items():
		if index == integer:
			return word
	return None

# generate a description for an image
def generate_desc(model, tokenizer, photo, max_length):
	# seed the generation process
	in_text = 'startseq'
	# iterate over the whole length of the sequence
	for i in range(max_length):
		# integer encode input sequence
		sequence = tokenizer.texts_to_sequences([in_text])[0]
		# pad input
		sequence = pad_sequences([sequence], maxlen=max_length)
		# predict next word
		yhat = model.predict([photo,sequence], verbose=0)
		# convert probability to integer
		yhat = argmax(yhat)
		# map integer to word
		word = word_for_id(yhat, tokenizer)
		# stop if we cannot map the word
		if word is None:
			break
		# append as input for generating the next word
		in_text += ' ' + word
		# stop if we predict the end of the sequence
		if word == 'endseq':
			break
	return in_text
```

我们将为测试数据集和训练数据集中的所有照片生成预测。

以下名为 _evaluate_model()_ 的函数将针对给定的照片描述和照片特征数据集评估训练模型。使用语料库 BLEU 分数收集和评估实际和预测的描述,该分数总结了生成的文本与预期文本的接近程度。

W
wizardforcel 已提交
1437
```py
W
dl nlp  
wizardforcel 已提交
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
# evaluate the skill of the model
def evaluate_model(model, descriptions, photos, tokenizer, max_length):
	actual, predicted = list(), list()
	# step over the whole set
	for key, desc_list in descriptions.items():
		# generate description
		yhat = generate_desc(model, tokenizer, photos[key], max_length)
		# store actual and predicted
		references = [d.split() for d in desc_list]
		actual.append(references)
		predicted.append(yhat.split())
	# calculate BLEU score
	print('BLEU-1: %f' % corpus_bleu(actual, predicted, weights=(1.0, 0, 0, 0)))
	print('BLEU-2: %f' % corpus_bleu(actual, predicted, weights=(0.5, 0.5, 0, 0)))
	print('BLEU-3: %f' % corpus_bleu(actual, predicted, weights=(0.3, 0.3, 0.3, 0)))
	print('BLEU-4: %f' % corpus_bleu(actual, predicted, weights=(0.25, 0.25, 0.25, 0.25)))
```

BLEU 分数用于文本翻译,用于针对一个或多个参考翻译评估翻译文本。

在这里,我们将每个生成的描述与照片的所有参考描述进行比较。然后,我们计算 1,2,3 和 4 累积 n-gram 的 BLEU 分数。

您可以在此处了解有关 BLEU 分数的更多信息:

*   [计算 Python 中文本的 BLEU 分数的温和介绍](https://machinelearningmastery.com/calculate-bleu-score-for-text-python/)

[NLTK Python 库在 _corpus_bleu()_ 函数中实现 BLEU 得分](http://www.nltk.org/api/nltk.translate.html)计算。接近 1.0 的较高分数更好,接近零的分数更差。

我们可以将所有这些与上一节中的函数一起用于加载数据。我们首先需要加载训练数据集以准备 Tokenizer,以便我们可以将生成的单词编码为模型的输入序列。使用与训练模型时使用的完全相同的编码方案对生成的单词进行编码至关重要。

然后,我们使用这些函数来加载测试数据集。

下面列出了完整的示例。

W
wizardforcel 已提交
1472
```py
W
dl nlp  
wizardforcel 已提交
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
from numpy import argmax
from pickle import load
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import load_model
from nltk.translate.bleu_score import corpus_bleu

# load doc into memory
def load_doc(filename):
	# open the file as read only
	file = open(filename, 'r')
	# read all text
	text = file.read()
	# close the file
	file.close()
	return text

# load a pre-defined list of photo identifiers
def load_set(filename):
	doc = load_doc(filename)
	dataset = list()
	# process line by line
	for line in doc.split('\n'):
		# skip empty lines
		if len(line) < 1:
			continue
		# get the image identifier
		identifier = line.split('.')[0]
		dataset.append(identifier)
	return set(dataset)

# load clean descriptions into memory
def load_clean_descriptions(filename, dataset):
	# load document
	doc = load_doc(filename)
	descriptions = dict()
	for line in doc.split('\n'):
		# split line by white space
		tokens = line.split()
		# split id from description
		image_id, image_desc = tokens[0], tokens[1:]
		# skip images not in the set
		if image_id in dataset:
			# create list
			if image_id not in descriptions:
				descriptions[image_id] = list()
			# wrap description in tokens
			desc = 'startseq ' + ' '.join(image_desc) + ' endseq'
			# store
			descriptions[image_id].append(desc)
	return descriptions

# load photo features
def load_photo_features(filename, dataset):
	# load all features
	all_features = load(open(filename, 'rb'))
	# filter features
	features = {k: all_features[k] for k in dataset}
	return features

# covert a dictionary of clean descriptions to a list of descriptions
def to_lines(descriptions):
	all_desc = list()
	for key in descriptions.keys():
		[all_desc.append(d) for d in descriptions[key]]
	return all_desc

# fit a tokenizer given caption descriptions
def create_tokenizer(descriptions):
	lines = to_lines(descriptions)
	tokenizer = Tokenizer()
	tokenizer.fit_on_texts(lines)
	return tokenizer

# calculate the length of the description with the most words
def max_length(descriptions):
	lines = to_lines(descriptions)
	return max(len(d.split()) for d in lines)

# map an integer to a word
def word_for_id(integer, tokenizer):
	for word, index in tokenizer.word_index.items():
		if index == integer:
			return word
	return None

# generate a description for an image
def generate_desc(model, tokenizer, photo, max_length):
	# seed the generation process
	in_text = 'startseq'
	# iterate over the whole length of the sequence
	for i in range(max_length):
		# integer encode input sequence
		sequence = tokenizer.texts_to_sequences([in_text])[0]
		# pad input
		sequence = pad_sequences([sequence], maxlen=max_length)
		# predict next word
		yhat = model.predict([photo,sequence], verbose=0)
		# convert probability to integer
		yhat = argmax(yhat)
		# map integer to word
		word = word_for_id(yhat, tokenizer)
		# stop if we cannot map the word
		if word is None:
			break
		# append as input for generating the next word
		in_text += ' ' + word
		# stop if we predict the end of the sequence
		if word == 'endseq':
			break
	return in_text

# evaluate the skill of the model
def evaluate_model(model, descriptions, photos, tokenizer, max_length):
	actual, predicted = list(), list()
	# step over the whole set
	for key, desc_list in descriptions.items():
		# generate description
		yhat = generate_desc(model, tokenizer, photos[key], max_length)
		# store actual and predicted
		references = [d.split() for d in desc_list]
		actual.append(references)
		predicted.append(yhat.split())
	# calculate BLEU score
	print('BLEU-1: %f' % corpus_bleu(actual, predicted, weights=(1.0, 0, 0, 0)))
	print('BLEU-2: %f' % corpus_bleu(actual, predicted, weights=(0.5, 0.5, 0, 0)))
	print('BLEU-3: %f' % corpus_bleu(actual, predicted, weights=(0.3, 0.3, 0.3, 0)))
	print('BLEU-4: %f' % corpus_bleu(actual, predicted, weights=(0.25, 0.25, 0.25, 0.25)))

# prepare tokenizer on train set

# load training dataset (6K)
filename = 'Flickr8k_text/Flickr_8k.trainImages.txt'
train = load_set(filename)
print('Dataset: %d' % len(train))
# descriptions
train_descriptions = load_clean_descriptions('descriptions.txt', train)
print('Descriptions: train=%d' % len(train_descriptions))
# prepare tokenizer
tokenizer = create_tokenizer(train_descriptions)
vocab_size = len(tokenizer.word_index) + 1
print('Vocabulary Size: %d' % vocab_size)
# determine the maximum sequence length
max_length = max_length(train_descriptions)
print('Description Length: %d' % max_length)

# prepare test set

# load test set
filename = 'Flickr8k_text/Flickr_8k.testImages.txt'
test = load_set(filename)
print('Dataset: %d' % len(test))
# descriptions
test_descriptions = load_clean_descriptions('descriptions.txt', test)
print('Descriptions: test=%d' % len(test_descriptions))
# photo features
test_features = load_photo_features('features.pkl', test)
print('Photos: test=%d' % len(test_features))

# load the model
filename = 'model-ep002-loss3.245-val_loss3.612.h5'
model = load_model(filename)
# evaluate model
evaluate_model(model, test_descriptions, test_features, tokenizer, max_length)
```

运行该示例将打印 BLEU 分数。

我们可以看到分数在问题的熟练模型的预期范围的顶部和接近顶部。所选的模型配置决不会优化。

W
wizardforcel 已提交
1643
```py
W
dl nlp  
wizardforcel 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
BLEU-1: 0.579114
BLEU-2: 0.344856
BLEU-3: 0.252154
BLEU-4: 0.131446
```

## 生成新标题

现在我们知道如何开发和评估字幕生成模型,我们如何使用它?

几乎我们为全新照片生成字幕所需的一切都在模型文件中。

我们还需要 Tokenizer 在生成序列时为模型编码生成的单词,以及在我们定义模型时使用的输入序列的最大长度(例如 34)。

我们可以硬编码最大序列长度。通过文本编码,我们可以创建标记生成器并将其保存到文件中,以便我们可以在需要时快速加载它而无需整个 Flickr8K 数据集。另一种方法是使用我们自己的词汇表文件并在训练期间映射到整数函数。

我们可以像以前一样创建 Tokenizer 并将其保存为 pickle 文件 _tokenizer.pkl_ 。下面列出了完整的示例。

W
wizardforcel 已提交
1662
```py
W
dl nlp  
wizardforcel 已提交
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
from keras.preprocessing.text import Tokenizer
from pickle import dump

# load doc into memory
def load_doc(filename):
	# open the file as read only
	file = open(filename, 'r')
	# read all text
	text = file.read()
	# close the file
	file.close()
	return text

# load a pre-defined list of photo identifiers
def load_set(filename):
	doc = load_doc(filename)
	dataset = list()
	# process line by line
	for line in doc.split('\n'):
		# skip empty lines
		if len(line) < 1:
			continue
		# get the image identifier
		identifier = line.split('.')[0]
		dataset.append(identifier)
	return set(dataset)

# load clean descriptions into memory
def load_clean_descriptions(filename, dataset):
	# load document
	doc = load_doc(filename)
	descriptions = dict()
	for line in doc.split('\n'):
		# split line by white space
		tokens = line.split()
		# split id from description
		image_id, image_desc = tokens[0], tokens[1:]
		# skip images not in the set
		if image_id in dataset:
			# create list
			if image_id not in descriptions:
				descriptions[image_id] = list()
			# wrap description in tokens
			desc = 'startseq ' + ' '.join(image_desc) + ' endseq'
			# store
			descriptions[image_id].append(desc)
	return descriptions

# covert a dictionary of clean descriptions to a list of descriptions
def to_lines(descriptions):
	all_desc = list()
	for key in descriptions.keys():
		[all_desc.append(d) for d in descriptions[key]]
	return all_desc

# fit a tokenizer given caption descriptions
def create_tokenizer(descriptions):
	lines = to_lines(descriptions)
	tokenizer = Tokenizer()
	tokenizer.fit_on_texts(lines)
	return tokenizer

# load training dataset (6K)
filename = 'Flickr8k_text/Flickr_8k.trainImages.txt'
train = load_set(filename)
print('Dataset: %d' % len(train))
# descriptions
train_descriptions = load_clean_descriptions('descriptions.txt', train)
print('Descriptions: train=%d' % len(train_descriptions))
# prepare tokenizer
tokenizer = create_tokenizer(train_descriptions)
# save the tokenizer
dump(tokenizer, open('tokenizer.pkl', 'wb'))
```

我们现在可以在需要时加载 tokenizer 而无需加载整个注释的训练数据集。

现在,让我们为新照片生成描述。

下面是我在 Flickr 上随机选择的新照片(可在许可许可下获得)。

![Photo of a dog at the beach.](img/1036583bcaf100d850a94df4e70324d4.jpg)

一条狗的照片在海滩的。
照片由 [bambe1964](https://www.flickr.com/photos/bambe1964/7837618434/) 拍摄,部分版权所有。

我们将使用我们的模型为它生成描述。

下载照片并将其保存到本地目录,文件名为“ _example.jpg_ ”。

首先,我们必须从 _tokenizer.pkl_ 加载 Tokenizer,并定义填充输入所需的生成序列的最大长度。

W
wizardforcel 已提交
1755
```py
W
dl nlp  
wizardforcel 已提交
1756 1757 1758 1759 1760 1761 1762 1763
# load the tokenizer
tokenizer = load(open('tokenizer.pkl', 'rb'))
# pre-define the max sequence length (from training)
max_length = 34
```

然后我们必须像以前一样加载模型。

W
wizardforcel 已提交
1764
```py
W
dl nlp  
wizardforcel 已提交
1765 1766 1767 1768 1769 1770 1771 1772
# load the model
model = load_model('model-ep002-loss3.245-val_loss3.612.h5')
```

接下来,我们必须加载要描述的照片并提取特征。

我们可以通过重新定义模型并向其添加 VGG-16 模型来实现这一目标,或者我们可以使用 VGG 模型预测特征并将其用作现有模型的输入。我们将使用后者并使用在数据准备期间使用的 _extract_features()_ 函数的修改版本,但适用于处理单张照片。

W
wizardforcel 已提交
1773
```py
W
dl nlp  
wizardforcel 已提交
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
# extract features from each photo in the directory
def extract_features(filename):
	# load the model
	model = VGG16()
	# re-structure the model
	model.layers.pop()
	model = Model(inputs=model.inputs, outputs=model.layers[-1].output)
	# load the photo
	image = load_img(filename, target_size=(224, 224))
	# convert the image pixels to a numpy array
	image = img_to_array(image)
	# reshape data for the model
	image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
	# prepare the image for the VGG model
	image = preprocess_input(image)
	# get features
	feature = model.predict(image, verbose=0)
	return feature

# load and prepare the photograph
photo = extract_features('example.jpg')
```

然后,我们可以使用在评估模型时定义的 _generate_desc()_ 函数生成描述。

下面列出了为全新独立照片生成描述的完整示例。

W
wizardforcel 已提交
1801
```py
W
dl nlp  
wizardforcel 已提交
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
from pickle import load
from numpy import argmax
from keras.preprocessing.sequence import pad_sequences
from keras.applications.vgg16 import VGG16
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.models import Model
from keras.models import load_model

# extract features from each photo in the directory
def extract_features(filename):
	# load the model
	model = VGG16()
	# re-structure the model
	model.layers.pop()
	model = Model(inputs=model.inputs, outputs=model.layers[-1].output)
	# load the photo
	image = load_img(filename, target_size=(224, 224))
	# convert the image pixels to a numpy array
	image = img_to_array(image)
	# reshape data for the model
	image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
	# prepare the image for the VGG model
	image = preprocess_input(image)
	# get features
	feature = model.predict(image, verbose=0)
	return feature

# map an integer to a word
def word_for_id(integer, tokenizer):
	for word, index in tokenizer.word_index.items():
		if index == integer:
			return word
	return None

# generate a description for an image
def generate_desc(model, tokenizer, photo, max_length):
	# seed the generation process
	in_text = 'startseq'
	# iterate over the whole length of the sequence
	for i in range(max_length):
		# integer encode input sequence
		sequence = tokenizer.texts_to_sequences([in_text])[0]
		# pad input
		sequence = pad_sequences([sequence], maxlen=max_length)
		# predict next word
		yhat = model.predict([photo,sequence], verbose=0)
		# convert probability to integer
		yhat = argmax(yhat)
		# map integer to word
		word = word_for_id(yhat, tokenizer)
		# stop if we cannot map the word
		if word is None:
			break
		# append as input for generating the next word
		in_text += ' ' + word
		# stop if we predict the end of the sequence
		if word == 'endseq':
			break
	return in_text

# load the tokenizer
tokenizer = load(open('tokenizer.pkl', 'rb'))
# pre-define the max sequence length (from training)
max_length = 34
# load the model
model = load_model('model-ep002-loss3.245-val_loss3.612.h5')
# load and prepare the photograph
photo = extract_features('example.jpg')
# generate description
description = generate_desc(model, tokenizer, photo, max_length)
print(description)
```

在这种情况下,生成的描述如下:

W
wizardforcel 已提交
1879
```py
W
dl nlp  
wizardforcel 已提交
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
startseq dog is running across the beach endseq
```

您可以删除开始和结束标记,您将拥有一个漂亮的自动照片字幕模型的基础。

这就像生活在未来的家伙!

它仍然完全让我感到震惊,我们可以做到这一点。哇。

## 扩展

本节列出了一些扩展您可能希望探索的教程的想法。

W
wizardforcel 已提交
1893
*   **替代预训练照片模型**。使用小的 16 层 VGG 模型进行特征提取。考虑探索在 ImageNet 数据集上提供更好表现的更大模型,例如 Inception。
W
dl nlp  
wizardforcel 已提交
1894
*   **较小的词汇**。在模型的开发中使用了大约八千字的更大词汇。支持的许多单词可能是拼写错误或仅在整个数据集中使用过一次。优化词汇量并缩小尺寸,可能减半。
W
wizardforcel 已提交
1895 1896
*   **预先训练过的单词向量**。该模型学习了单词向量作为拟合模型的一部分。通过使用在训练数据集上预训练或在更大的文本语料库(例如新闻文章或维基百科)上训练的单词向量,可以实现更好的表现。
*   **调谐模型**。该模型的配置没有针对该问题进行调整。探索备用配置,看看是否可以获得更好的表现。
W
dl nlp  
wizardforcel 已提交
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908

你尝试过这些扩展吗?在下面的评论中分享您的结果。

## 进一步阅读

如果您要深入了解,本节将提供有关该主题的更多资源。

### 标题生成论文

*   [Show and Tell:神经图像标题生成器](https://arxiv.org/abs/1411.4555),2015。
*   [显示,参与和讲述:视觉注意的神经图像标题生成](https://arxiv.org/abs/1502.03044),2015。
*   [将图像放在图像标题生成器](https://arxiv.org/abs/1703.09137)中的位置,2017。
W
wizardforcel 已提交
1909
*   [循环神经网络(RNN)在图像标题生成器中的作用是什么?](https://arxiv.org/abs/1708.02043) ,2017。
W
dl nlp  
wizardforcel 已提交
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
*   [图像自动生成描述:模型,数据集和评估措施的调查](https://arxiv.org/abs/1601.03896),2016。

### Flickr8K 数据集

*   [将图像描述框架化为排名任务:数据,模型和评估指标](http://nlp.cs.illinois.edu/HockenmaierGroup/Framing_Image_Description/KCCA.html)(主页)
*   [框架图像描述作为排名任务:数据,模型和评估指标](https://www.jair.org/media/3994/live-3994-7274-jair.pdf),(PDF)2013。
*   [数据集申请表](https://illinois.edu/fb/sec/1713398)
*   [Old Flicrk8K 主页](http://nlp.cs.illinois.edu/HockenmaierGroup/8k-pictures.html)

### API

*   [Keras Model API](https://keras.io/models/model/)
*   [Keras pad_sequences()API](https://keras.io/preprocessing/sequence/#pad_sequences)
*   [Keras Tokenizer API](https://keras.io/preprocessing/text/#tokenizer)
*   [Keras VGG16 API](https://keras.io/applications/#vgg16)
*   [Gensim word2vec API](https://radimrehurek.com/gensim/models/word2vec.html)
*   [nltk.translate 包 API 文档](http://www.nltk.org/api/nltk.translate.html)

## 摘要

在本教程中,您了解了如何从头开发照片字幕深度学习模型。

具体来说,你学到了:

W
wizardforcel 已提交
1934
*   如何准备照片和文本数据,为深度学习模型的训练做好准备。
W
dl nlp  
wizardforcel 已提交
1935
*   如何设计和训练深度学习字幕生成模型。
W
wizardforcel 已提交
1936
*   如何评估训练标题生成模型并使用它来标注全新的照片。
W
dl nlp  
wizardforcel 已提交
1937 1938 1939 1940 1941 1942

你有任何问题吗?
在下面的评论中提出您的问题,我会尽力回答。

**注**:这是以下摘录的章节:“[深度学习自然语言处理](https://machinelearningmastery.com/deep-learning-for-nlp/)”。
看一下,如果你想要更多的分步教程,在使用文本数据时充分利用深度学习方法。