我用TFlearn开发细胞神经网络分类图像基于Tensorflow,我用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]的尺寸为负

最佳答案

这会有所帮助。

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)

关于python - InvalidArgumentError(请参阅上面的回溯):形状[-1,150,150,1]的尺寸为负,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45647888/

10-12 20:01