【问题标题】:Saving extracted features in CNN在 CNN 中保存提取的特征
【发布时间】:2017-11-07 11:40:26
【问题描述】:

我刚刚开始学习机器学习算法。我想为自己的数据集训练 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'

【问题讨论】:

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


    【解决方案1】:

    您应该将网络的 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)
    

    【讨论】:

    • 我运行了以下几行: feed_dict = feed_dict_builder(X, Y, model.inputs, model.targets) output = model.predictor.evaluate(feed_dict, [convnet1]) 但我得到了这个异常:线程 Thread-33 中的异常:文件“/home/anupama/anaconda3/lib/python3.6/site-packages/tflearn/utils.py”,第 180 行,在 返回 [x[start] for x in X ] IndexError: index 2 is out of bounds for axis 1 with size 2 当我打印输出时,它的值为 [0.0]
    • 编辑您的问题并提供您正在使用的代码,最好带有示例数据,以便可以复制
    • @Anupama 对不起,我的错,忘了如何正确保存 numpy。我已经更新了我的答案。
    • 谢谢,我现在可以保存功能了。但是运行代码行引发了问题中提到的异常。而且我只得到一个值作为输出。所以它不是我想要的形状(1000、4096)的numpy数组,而是形状1。我不明白为什么会这样。
    • 我无法复制它。你能打印convnet1.shape吗?
    猜你喜欢
    • 2021-10-23
    • 2018-08-20
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    相关资源
    最近更新 更多