0

我是 TFLearn 的新手,我正在尝试编写一个简单的 CNN。这是我的代码:

import tensorflow as tf
import tflearn
from tflearn.layers.core import input_data, fully_connected, dropout
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.data_utils import image_dirs_to_samples, to_categorical
from tflearn.layers.estimator import regression


if __name__ == '__main__':

  NUM_CATEGORIES = 5

  X, Y = image_dirs_to_samples('./flower_photos_100')
  Y = to_categorical(Y, NUM_CATEGORIES)

  net = input_data(shape=[None, 299, 299, 3])

  net = conv_2d(net, 32, 3, activation='relu', name='conv_0')
  net = max_pool_2d(net, 2, name='max_pool_0')
  net = dropout(net, 0.75, name='dropout_0')

  for i in range(4):
      net = conv_2d(net, 64, 3, activation='relu', name='conv_{}'.format(i))
      net = max_pool_2d(net, 2, name='max_pool_{}'.format(i))
      net = dropout(net, 0.5, name='dropout_{}'.format(i))

  net = fully_connected(net, 512, activation='relu')
  net = dropout(net, 0.5, name='dropout_fc')
  softmax = fully_connected(net, NUM_CATEGORIES, activation='softmax')

  rgrs = regression(softmax, optimizer='adam',
                          loss='categorical_crossentropy',
                          learning_rate=0.001)

  model = tflearn.DNN(rgrs,
                      checkpoint_path='rs_ckpt',
                      max_checkpoints=3)

  model.fit(X, Y,
            n_epoch=10,
            validation_set=0.1,
            shuffle=True,
            snapshot_step=100,
            show_metric=True,
            batch_size=64,
            run_id='rs')

我收到以下错误:

Traceback (most recent call last):
  File "rs.py", line 46, in <module>
    run_id='rs')
  File "/usr/local/lib/python2.7/site-packages/tflearn/models/dnn.py", line 188, in fit
    run_id=run_id)
  File "/usr/local/lib/python2.7/site-packages/tflearn/helpers/trainer.py", line 277, in fit
    show_metric)
  File "/usr/local/lib/python2.7/site-packages/tflearn/helpers/trainer.py", line 684, in _train
    feed_batch)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 888, in _run
    np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
  File "/usr/local/lib/python2.7/site-packages/numpy/core/numeric.py", line 482, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

我有一种预感,它与 的形状有关X,但我不知道如何修复它(另外,我希望这image_dirs_to_samples会返回对 tflearn 有意义的东西)。

4

2 回答 2

0

显然我对图像的假设不正确:它们不一定是 299x299,当我传递resize=[299, 299]给它时,image_dirs_to_samples它就开始工作了。但是,我仍然不明白为什么会收到 ValueError。

于 2016-11-07T23:03:51.347 回答
0

这意味着它不能将 X 变成一个 numpy 数组。它的含义是,并非列表 X 中的所有元素都具有相同的形状。确保将图像加载到样本的函数确实使图像的大小正常化,如果不是,请确保所有图像的大小相同。

于 2017-12-20T10:00:16.260 回答