logo头像

猪老大要进步!

TensorFlow 1.x 卷积等操作的高级封装

本文于 1244 天之前发表,文中内容可能已经过时。

前一段时间写了一些程序,自己搭建卷积神经网络的时候需要用到不少tensorflow的自带低级封装还有一些常用的读取操作等,虽然已经有了keras等高级封装可以直接调用,但还是想自己写一写。

tensorflow版本:1.14

1. 卷积函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def weight_variable(shape):  
initial = tf.truncated_normal(shape, mean=0, stddev=0.01)#正态初始化
return tf.Variable(initial)

def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)

def conv2d(x, W):#步长为1
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def conv(x,next_depth):
depth = x.get_shape()[-1].value
w = weight_variable([3, 3, depth, next_depth])
b = bias_variable([next_depth])
r = tf.nn.relu(tf.nn.bias_add(conv2d(x, w), b))
return r

2. 池化函数

1
2
def max_pool(x):   
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')

3. 读取图片顺便加标签

  • 文件夹结构(假设目录下面有17个文件夹,每个文件夹里面有若干对应种类的图片):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    ./

    –./dogs

    –./cats

    –./tigers


  • 读取的图片压缩为32323的尺寸,便于神经网络的快速迭代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 调用skimage的函数
from skimage.io import imread
from skimage.transform import resize

def load_images(path):
contents = os.listdir(path)
classes = [each for each in contents if os.path.isdir(os.path.join(path,each))]
print('目录下有%s' % classes)

# 用labels来存储图片的类别
labels = []
# images数组用来存储图片数据
images = []

# 对每个不同种类读取图片到list并且+标签
for each in classes:
class_path = os.path.join(path,each)
files = os.listdir(class_path)
print("Starting {} images".format(each),'数量为',len(files))
for ii, file in enumerate(files, 1):
# 载入图片并放入batch数组中
img = imread(os.path.join(class_path, file))
img = img / 255.0
# print(img.shape)
img = resize(img, (32, 32))
# print(img.shape)
images.append(img.reshape((32,32,3)))
labels.append(each)

images = np.array(images)
#将labels的list形式转换为one_hot_label
from sklearn.preprocessing import LabelBinarizer
lb = LabelBinarizer()
lb.fit(labels)
labels_vecs = lb.transform(labels)
print('总共读取了%d张图片'%images.shape[0])
# print(labels,labels_vecs)
return images,labels_vecs # 返回图片以及对应的标签

4. 神经网络模型的建立

下面的示例结构为:3层卷积+2层全连接

图片的输入x_images的shape是32323

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def create(x_images,keep_prob):#x_images的shape是32,32,3 
h = 32
w = 32
d = 3
#first layer
conv1 = conv(x_images,16)
pool1 = max_pool(conv1)
h = int(np.ceil(h/2))
w = int(np.ceil(w/2))
d = 16

#second layer
conv2 = conv(pool1,32)
pool2 = max_pool(conv2)
h = int(np.ceil(h/2))
w = int(np.ceil(w/2))
d = d*2

#third layer
conv3 = conv(pool2,64)
pool3 = max_pool(conv3)
h = int(np.ceil(h/2))
w = int(np.ceil(w/2))
d = d*2

#first fully layer
w_fc1 = weight_variable([h*w*d,1024])
b_fc1 = bias_variable([1024])
h_conv_flat = tf.reshape(pool3,[-1,h*w*d])
h_fc1 = tf.nn.relu(tf.nn.bias_add(tf.matmul(h_conv_flat, w_fc1), b_fc1))
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

#second fully layer
w_fc2 = weight_variable([1024,1024])
b_fc2 = bias_variable([1024])
h_fc2 = tf.nn.relu(tf.nn.bias_add(tf.matmul(h_fc1_drop, w_fc2), b_fc2))
h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob)

#third full layer
w_fc3 = weight_variable([1024,17])
b_fc3 = bias_variable([17])
h_fc3 = tf.add(tf.matmul(h_fc2_drop, w_fc3), b_fc3,name='h_fc3')
out = tf.nn.softmax(h_fc3,name='out')
print('模型建立好了!')
return out
支付宝打赏 微信打赏

赞赏是不耍流氓的鼓励