#彩色转成灰度 img三维的数据(高、宽、通道数)defconvert2gray(img):iflen(img.shape)>2:
r, g, b = img[:,:,0], img[:,:,1], img[:,:,2]
gray =0.2989* r +0.5870* g +0.1140* b
return gray
else:return img
#报警信息
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'#乱序
np.random.seed(seed)
oder_index=np.random.permutation(num_all)#切分数据集
num_split=int(num_all*0.7)
train_path,test_path=np.hsplit(data_path,[num_split])
train_label,test_label=np.vsplit(data_lebal,[num_split])#单个图片解码defimg_one_encode(file_path):
oder=plt.imread(file_path)
img=misc.imresize(oder,(IMG_SIZE,IMG_SIZE))return img/255#多个图片解码defimg_all_encode(file_path):return[img_one_encode(i)for i in file_path]
文本(情感分类)
positive_data_file ='../rt-polarity.pos'#正面评价文本
negative_data_file ='../rt-polarity.neg'#负面评价文本
list_true=list(open(positive_data_file,'r',encoding='utf-8').readlines())
list_true=[i.strip()for i in list_true]
list_false=list(open(negative_data_file,'r',encoding='utf-8').readlines())
list_false=[i.strip()for i in list_true]#拼接
x_data=list_true+list_false
label_true=[[0,1]for i in list_true]
label_false=[[1,0]for i in list_true]
label_data=np.concatenate([label_true,label_false],0)#找到最大单词个数
max_word_lenght=max([len(i.split(' '))for i in x_data])print(max_word_lenght)#重新建立数据
new_data=learn.preprocessing.VocabularyProcessor(max_document_length=max_word_lenght)
x=np.array(list(new_data.fit_transform(x_data)))print('单词个数:',len(new_data.vocabulary_))print(x.shape)#乱序
np.random.seed(33)
count_data=label_data.shape[0]
order=np.random.permutation(count_data)
x=x[order]
label_data=label_data[order]#切分
x_train,x_test=np.split(x,[int(-0.8*count_data)])
label_train,label_test=np.split(label_data,[int(-0.8*count_data)])# print(x_train.shape) #(2133, 59)# print(label_train.shape) #(2133, 2)
X=tf.placeholder(tf.int32,shape=[None,max_word_lenght])
Y=tf.placeholder(tf.int32,shape=[None,2])
emdding_size=8
W=tf.Variable(tf.random_normal([len(new_data.vocabulary_),8],stddev=0.01))
x_img=tf.nn.embedding_lookup(W,X)
x_img=tf.expand_dims(x_img,-1)print(x_img.shape)