您好,学习tensoflow和numpy。我正在尝试为图像分类创建CNN模型。
我的图片大小为28X28
这是我的模特:
MODEL_NAME = 'ComputerVision-{}-{}.model'.format(LR, '6conv-basic') # 6conv layer
import tensorflow as tf
tf.reset_default_graph()
convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')
# layer
convnet = conv_2d(convnet, 28, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
# layer
convnet = conv_2d(convnet, (56, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
#layer
convnet = conv_2d(convnet, 112, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
# layer
convnet = conv_2d(convnet, 56, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
#layer
convnet = conv_2d(convnet, 28, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
#dense layer
convnet = fully_connected(convnet, 784, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')
#my neural network model
model = tflearn.DNN(convnet, tensorboard_dir='log')
# train and test set
train = train_data[:-500]#my train data
test = train_data[-500:]
X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
Y = [i[1] for i in train]
test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
test_y = [i[1] for i in test]
model.fit({'input': X}, {'targets': Y}, n_epoch=10,
validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=200,show_metric=True,run_id=MODEL_NAME)
当我尝试训练模型时,出现此错误:
ValueError Traceback (most recent call last)
<ipython-input-9-d74ffe227367> in <module>()
4 snapshot_step=200,
5 show_metric=True,
----> 6 run_id=MODEL_NAME)
#here is the last error it seems like I have a numpy array problem
/anaconda3/lib/python3.6/site-packages/numpy/core/numeric.py in asarray(a, dtype, order)
490
491 """
--> 492 return array(a, dtype, copy=False, order=order)
493
494
ValueError: setting an array element with a sequence.
答案 0 :(得分:0)
使用model.fit()
调用tflearn
方法时,属性validation_set
中的条目必须是元组。尝试这样做:
model.fit(X_inputs=X, Y_targets=Y, n_epoch=10, validation_set=(test_x, test_y),\
snapshot_step=200, show_metric=True, run_id=MODEL_NAME)
我希望它能起作用!
另外,请查看Deep Neural Network Model TFLearn docs here。