错误:
ValueError: setting an array element with a sequence.
import tensorflow as tf
import scipy.io as sio
import numpy as np
import matplotlib.image as mpimg
import pickle as cp
import matplotlib.pyplot as plt
def get_batch(image, label, batch_size, now_batch, total_batch):
if now_batch < total_batch-1:
image_batch = image[now_batch*batch_size:(now_batch+1)*batch_size]
label_batch = label[now_batch*batch_size:(now_batch+1)*batch_size]
else:
image_batch = image[now_batch*batch_size:]
label_batch = label[now_batch*batch_size:]
return image_batch,label_batch
label_tain = list(sio.loadmat('dataset/corel5k_train_annot.mat')['annot1']) #加载文件
label_test = list(sio.loadmat('dataset/corel5k_test_annot.mat')['annot2'])
test_img = []
with open('dataset/corel5k_test_list.txt') as f:
for i in f.readlines():
test_img += [mpimg.imread('dataset/%s.jpeg'%i.strip())]
cp.dump(test_img,open("test.pkl","wb"))#一种保存列表的方式
train_img = []
with open('dataset/corel5k_train_list.txt') as f:
for i in f.readlines():
train_img += [mpimg.imread('dataset/%s.jpeg'%i.strip())]
cp.dump(train_img,open("train.pkl","wb"))
length=len(train_img)
for e in range(1,length+1):
train_img[e-1] = tf.image.resize_images(train_img[e-1], [128,192], method=0)
train_img[e-1] = tf.image.per_image_standardization(train_img[e-1])
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
########卷积层、池化层接下来重复使用的,分别定义创建函数########
tf.nn.conv2d是TensorFlow中的2维卷积函数
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
使用2*2的最大池化
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
x = tf.placeholder(tf.float32,[None,128,192,3])
y_ = tf.placeholder(tf.float32,[None,260])
W_conv1 = weight_variable([3, 3, 3, 32])
用conv2d函数进行卷积操作,加上偏置
b_conv1 = bias_variable([32])
把x_image和权值向量进行卷积,加上偏置项,然后应用ReLU激活函数,
h_conv1 = tf.nn.relu(conv2d(x, W_conv1) + b_conv1)
#tf.global_variables_initializer().run()
对卷积的输出结果进行池化操作
h_pool1 = max_pool_2x2(h_conv1)
image_batch,label_batch = get_batch(train_img, label_train,5,0,9)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(h_pool1,feed_dict={x:image_batch,y_:label_batch})