2016-12-04 44 views
0

我试图通过tflearn中的示例mnist代码来训练自定义灰度图像数据集(仅使用2张图像)时出现此错误。图像大小各不相同,大约在(3000,3000)的高度和宽度范围内。这是我的错误:当用tflearn训练模型时,'int'对象没有属性__get__item错误?

Run id: convnet_images 
Log directory: /tmp/tflearn_logs/ 
Exception in thread Thread-14: 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner 
    self.run() 
    File "/usr/lib/python2.7/threading.py", line 754, in run 
    self.__target(*self.__args, **self.__kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/tflearn/data_flow.py", line 186, in fill_feed_dict_queue 
    data = self.retrieve_data(batch_ids) 
    File "/usr/local/lib/python2.7/dist-packages/tflearn/data_flow.py", line 221, in retrieve_data 
    utils.slice_array(self.feed_dict[key], batch_ids) 
    File "/usr/local/lib/python2.7/dist-packages/tflearn/utils.py", line 187, in slice_array 
    return X[start] 
TypeError: 'int' object has no attribute '__getitem__' 

,这是我的代码:

import tensorflow as tf 
import tflearn 
from scipy.misc import imread 
import numpy as np 
np.set_printoptions(threshold=np.nan) 

image = imread('image.jpg') 
image2 = imread('image2.jpg') 

image3 = imread('image3.jpg') 
image4 = imread('image4.jpg') 

image = np.resize(image, (256, 256, 1)) 
image2 = np.resize(image2, (256, 256, 1)) 
image3 = np.resize(image3, (256, 256, 1)) 
image4 = np.resize(image4, (256, 256,1)) 

image_train = np.stack((image, image2), axis = 0) 
image_test = np.stack((image3, image4), axis = 0) 
# # build the neural net 
from tflearn.layers.core import input_data, dropout, fully_connected 
from tflearn.layers.conv import conv_2d, max_pool_2d 
from tflearn.layers.normalization import local_response_normalization 
from tflearn.layers.estimator import regression 

network = input_data(shape = [None, 256, 256, 1], name = 'input') 
network = conv_2d(network, 32, 3, activation = 'relu', regularizer = 'L2') 
network = max_pool_2d(network, 2) 
network = local_response_normalization(network) 
# network = conv_2d(network, 64, 3, activation = 'relu', regularizer = 'L2') 
# network = max_pool_2d(network, 2) 
# network = local_response_normalization(network) 
network = fully_connected(network, 128, activation = 'tanh') 
network = dropout(network, 0.8) 
network = fully_connected(network, 1, activation = 'softmax') 
network = regression(network, optimizer = 'adam', learning_rate = '0.001', name = 'target') 

#Training 
model = tflearn.DNN(network, tensorboard_verbose = 3) 
model.fit({'input': image_train}, {'target': 0}, n_epoch = 20, batch_size = 1, 
      validation_set = ({'input': image_test}, {'target': 0}), 
      snapshot_step = 100, show_metric = True, run_id = 'convnet_images') 

我高度怀疑的误差来自于事实,我的灰阶像素亮度值是np.uint8而不是NP阵列。这意味着,当我给出像素值时,它是一个np.uint8,这可能意味着tflearn中的代码无法使用索引选择器__getitem__(从我目前阅读的内容)访问值。但我怎样才能获得像素值成为一个NP阵列? np.resize处理灰度图像的正确方法是什么?因为理想情况下,这应该适用于彩色图像,这意味着第四维(RGB的通道像素值)必须是一个np数组,其中包含3个像素值(并且可以理解的是,可能需要使用tflearn代码来访问像素值。__getitem__不过这只是我的猜测,我仍然不确定如何去这

+0

,你可以把你的灰度图像作为一个RGB图像。每个频道有相同的值? – theorifice

+0

嗯,我想要但不会导致数据大小的2倍增加处理?我认为如果我有很多图像处理是很难扩大规模的。解决方案o正确调整图像大小? – kwotsin

+0

我在代码中发现了一个错误,那就是我的Y值没有被格式化为2乘1的np数组,而是仅仅是一个浮点数。修复这个错误后,索引错误现在消失了,但是现在我又遇到了另一个问题: http://stackoverflow.com/questions/40955168/tensorflow-cast-string-to-float-is- not-supported-error-when-using-tflearn-despi 谢谢你的帮助。 – kwotsin

回答

0

OP自我回答评论:

I have discovered an error in my code, that is my Y value isn't formatted as a 2 by 1 np array and is instead just a float. Upon fixing this error, the index error is now gone.

相关问题