在CNN中保存提取的功能

时间:2017-11-07 11:40:27

标签: machine-learning tensorflow deep-learning conv-neural-network tflearn

我刚刚开始学习机器学习算法。我想为我自己的数据集训练VGG-16网络。我正在使用tflearn.DNN来模拟VGG网络。 我想保存完全连接层的输出(这是一个张量),它将4096个特征提取到一个文件中。我想知道如何保存这些功能。

当我跑出以下几行时:

feed_dict = feed_dict_builder(X, Y, model.inputs, model.targets)
output = model.predictor.evaluate(feed_dict, convnet1)
print(output)
output.save('features.npy')

我收到以下异常和错误:

Exception in thread Thread-48:
Traceback (most recent call last):
  File "/home/anupama/anaconda3/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/home/anupama/anaconda3/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/home/anupama/anaconda3/lib/python3.6/site-packages/tflearn/data_flow.py", line 187, in fill_feed_dict_queue
    data = self.retrieve_data(batch_ids)
  File "/home/anupama/anaconda3/lib/python3.6/site-packages/tflearn/data_flow.py", line 222, in retrieve_data
    utils.slice_array(self.feed_dict[key], batch_ids)
  File "/home/anupama/anaconda3/lib/python3.6/site-packages/tflearn/utils.py", line 180, in slice_array
    return [x[start] for x in X]
  File "/home/anupama/anaconda3/lib/python3.6/site-packages/tflearn/utils.py", line 180, in <listcomp>
    return [x[start] for x in X]
IndexError: index 2 is out of bounds for axis 1 with size 2

[0.0]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-23-f2d62c020964> in <module>()
      4 output = model.predictor.evaluate(feed_dict, convnet1)
      5 print(output)
----> 6 output.save('/home/anupama/Internship/feats')

AttributeError: 'list' object has no attribute 'save'

1 个答案:

答案 0 :(得分:0)

您应该将网络的FC层保存为单独的张量,并使用DNN.predictor对其进行评估。示例代码:

import tflearn
from tflearn.utils import feed_dict_builder

# VGG model definition
...
previous_layer = ...
fc_layer1 = tflearn.fully_connected(previous_layer, 4096, activation='relu', name='fc1')
fc_layer2 = tflearn.fully_connected(fc_layer1, 4096, activation='relu', name='fc2')
network = ...

# Training
model = tflearn.DNN(network)
model.fit(x, y)

# Evaluation
feed_dict = feed_dict_builder(x, y, model.inputs, model.targets)
output = model.predictor.evaluate(feed_dict, [fc_layer2])
np.save('features.npy', output)