![](/img/trans.png)
[英]InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 768 values, but the requested shape has 3072
[英]InvalidArgumentError (see above for traceback): Shape [-1,150,150,1] has negative dimensions
我正在开发一个细胞神经网络通过对图像分类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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.