【问题标题】:InvalidArgumentError (see above for traceback): Shape [-1,150,150,1] has negative dimensionsInvalidArgumentError(参见上文的回溯):形状 [-1,150,150,1] 具有负尺寸
【发布时间】:2017-08-12 07:49:20
【问题描述】:

我正在开发一个 CNN,以使用基于 TensorflowTFlearn 对图像进行分类,我使用 scipy.misc.imread 读取图像,现在我向我的模型提供数据,但发生了意外错误。

这是我的代码:

import numpy as np
import os
import tensorflow as tf
from scipy import misc
from PIL import Image
import keras


IMAGE_SIZE = 150
image_path = "dragonfly"


labels = np.zeros((4063, 2))
labels [0:2363] = (1, 0)
labels [2364:4062] = (0, 1)
test_labels = np.zeros((200, 2))
test_labels [0:99] = (1, 0)
test_labels [100:199] = (0, 1)


fset = []
fns=[os.path.join(root,fn) for root,dirs,files in os.walk(image_path) for fn in files]
for f in fns:
    fset.append(f)

def create_train_data():
    train_data = []
    fns=[os.path.join(root,fn) for root,dirs,files in os.walk(image_path) for fn in files]
    for f in fns:
        image = misc.imread(f, mode = 'L')
        image = misc.imresize(image, (IMAGE_SIZE, IMAGE_SIZE))
        train_data.append(np.array(image))
    return train_data


train_data = create_train_data()
print (len(train_data))

training_data = train_data[0:2264] + train_data[2364:3963]
train_labels = np.concatenate((labels[0:2264], labels[2364:3963]))
test_data = train_data[2264:2364] + train_data[3963:4063]


import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression

convnet = input_data(shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1], name='input')

convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=1e-3, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(convnet, tensorboard_dir='log')

X = np.asarray(training_data).reshape(3863, IMAGE_SIZE, IMAGE_SIZE, 1)
Y = [i for i in train_labels]

test_x = np.asarray(test_data).reshape(200, IMAGE_SIZE, IMAGE_SIZE, 1)
test_y = [i for i in test_labels]

model.fit({'input': X}, {'targets': train_labels}, n_epoch=2, validation_set=({'input': test_x}, {'targets': test_labels}), 
    snapshot_step=200, show_metric=True)

错误:

InvalidArgumentError:形状 [-1,150,150,1] 具有负尺寸

【问题讨论】:

  • 能否请您发布完整的回溯?
  • @ml4294 其实traceback很长,我觉得最有价值的部分是: InvalidArgumentError: Shape [-1,150,150,1] hasnegative dimensions [[Node: input_1/X = Placeholder[dtype=DT_FLOAT] , shape=[?,150,150,1], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]

标签: python machine-learning tensorflow deep-learning tflearn


【解决方案1】:

这会有所帮助。

model.fit(feed_dicts={input: X, targets: train_labels}, n_epoch=2, val_feed_dicts={input: test_x, targets: test_labels}, 
snapshot_step=200, show_metric=True)

【讨论】:

  • 是的,我实际上将一个 n-d-array 传递给模型,在我的代码中查看 X 和 test_x
猜你喜欢
  • 2021-08-11
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 2019-01-02
  • 2020-05-24
相关资源
最近更新 更多