卷积 神经网

预处理cifar

filename = 'F:\人工-006\深度学习1\\tensorflow补充\cifar-10-batches-bin\data_batch_1.bin'  # cifar10二进制文件路径
num = 10000  # 文件中包含的图片数量
onesize = 1 + 32 * 32 * 3
bytestream = open(filename, "rb")
# print(bytestream)
buf = bytestream.read(num * onesize)
# print(buf)  #二进制
bytestream.close()
data = np.frombuffer(buf, dtype=np.uint8)
# print(data.shape)#(30730000,)
data = data.reshape(num, onesize)
# print(data.shape)#(10000, 3073)
labels_images = np.hsplit(data, [1])
# print(labels_images[0].shape)#(10000, 1)
# print(labels_images[1].shape)#(10000, 3072)
labels = labels_images[0].reshape(num)
images = labels_images[1].reshape(num, 3, 32, 32)  # 1024red 1024green 1024blue
images = images.transpose(0, 2, 3, 1)  # (?, 32, 32, 3)

imgArr = images/255
Y_one_hot = np.eye(10)[labels]
total = imgArr.shape[0]

预处理cifar100

tf.set_random_seed(777) #设置随机种子
# 获取数据集
filename = r'F:\人工-006\深度学习1\\tensorflow补充\cifar-100-binary\\test.bin'  # cifar100二进制文件路径
num = 10000  # 文件中包含的图片数量
onesize = 2 + 32 * 32 * 3
bytestream = open(filename, "rb")
buf = bytestream.read(num * onesize)
bytestream.close()
data = np.frombuffer(buf, dtype=np.uint8)
# print(data.shape)   #(30740000,)
data = data.reshape(num, onesize)
# print(data.shape)   #(10000, 3074)
labels_images = np.hsplit(data, [1, 2])
# print(len(labels_images))  #3
# print(np.array(labels_images[0]).shape) #(10000, 1)
# print(np.array(labels_images[1]).shape) #(10000, 1)
# print(np.array(labels_images[2]).shape) #(10000, 3072)
labels1 = labels_images[0].reshape(num)
# print(labels1)
labels = labels_images[1].reshape(num)
# print(labels)
images = labels_images[2].reshape(num, 3, 32, 32)  # 1024red 1024green 1024blue
# print(images.shape) #(10000, 3, 32, 32)
images = images.transpose(0, 2, 3, 1)  # (?, 32, 32, 3)



imgArr = images/255
Y_one_hot = np.eye(100)[labels]
total = imgArr.shape[0]

预处理cifar10(序列化)

import pickle

tf.set_random_seed(777) #设置随机种子
# 获取数据集
fo = open(r'D:\tmp\cifar-100-python\test', 'rb')
dict = pickle.load(fo, encoding='bytes')
fo.close()
imgArr = dict[b'data'].reshape(-1, 3, 32, 32).transpose(0, 2, 3, 1) / 255
total = imgArr.shape[0]
Y_one_hot = np.eye(100)[dict[b'fine_labels']]

三通道 变 一通道

#彩色转成灰度  img三维的数据(高、宽、通道数)
def convert2gray(img):
    if len(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

自己实现next_batch函数

# 自己实现next_batch函数,每次返回一批数据
def next_batch(size):
    global g_b
    xb = imgArr[g_b:g_b+size]
    yb = Y_one_hot[g_b:g_b+size]
    g_b = g_b + size
    return xb,yb

cat VS dog

#报警信息
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])

#单个图片解码
def img_one_encode(file_path):
    oder=plt.imread(file_path)
    img=misc.imresize(oder,(IMG_SIZE,IMG_SIZE))
    return img/255

#多个图片解码
def img_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)

验证码

import numpy as np
from PIL import Image
import os
import matplotlib.pyplot as plt
import  numpy as np

#定义数值的变量
image_dir = r'F:\人工-006\深度学习1\\tensorflow补充\\vcode\train'
test_dir = r'F:\人工-006\深度学习1\\tensorflow补充\\vcode\test'
train_number=1000
test_number=100
IMG_HEIGHT = 60 #图片的高度
IMG_WIDTH = 160 #图片的宽度
size_lenght=4
lable_num=10


#三通道专一通道
def Three_to_One(img):
    if len(img.shape)>2:
        r,g,b=img[:,:,0],img[:,:,1],img[:,:,2]
        return r*0.333 + g*0.333 +b*0.333
    else:
        return img


def One_hot(item):
    one_hot=np.zeros(shape=[size_lenght*lable_num])
    for i,num in enumerate(item):
        index=i*lable_num + int(num)
        one_hot[index]=1
    return one_hot


#读取图片的函数
def file_path(file_path,train_count):
    i=0
    list_img=[]
    list_Y=[]
    for item in os.listdir(file_path):
        i+=1
        all_path=file_path+'\\'+item
        image_three=plt.imread(all_path)
        image_one=Three_to_One(np.array(image_three))
        image_one=image_one.reshape([IMG_WIDTH,IMG_HEIGHT,1])
        list_img.append(image_one/255.0)

        name=item[:4]
        One_hot_y=One_hot(name)
        list_Y.append(One_hot_y)

        if i==train_count:
            break
    return np.array(list_img),list_Y


x_img,y_one_hot=file_path(image_dir,train_number)
print(x_img.shape)
print(y_one_hot)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值