簡體   English   中英

tflearn:model.fit中的ValueError

[英]tflearn: ValueError in model.fit

我是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有意義的東西)。

顯然,我對圖像的假設是不正確的:它們不一定是299x299,當我將resize=[299, 299] image_dirs_to_samples resize=[299, 299]傳遞給image_dirs_to_samples它就開始起作用。 但是,我仍然不明白為什么會收到ValueError。

這意味着它不能將X變成一個numpy數組。 其含義是,列表X中的元素並非都具有相同的形狀。 確保將圖像加載到樣本的功能確實使圖像的尺寸正常化,如果沒有,請確保所有圖像的尺寸均相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM