中国大学MOOC-人工智能实践:Tensorflow笔记-课程笔记 Chapter8 卷积网络实践

本篇博客为学习中国大学MOOC-人工智能实践:Tensorflow笔记课程时的个人笔记记录。具体课程情况可以点击链接查看。(这里推一波中国大学MOOC,很好的学习平台,质量高,种类全,想要学习的话很有用的)

Chapter 8 卷积网络实践

8.1 复现已有的卷积神经网络

常用的一些函数

x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMAGE_PIXELS])
# example
x = tf.placeholder(tf.float32, shape=[1,224,224,3])
# [1,224,224,3] 一次喂入一张图,224*224的分辨率,3通道

tf.placeholder 用于传入真实训练样本/测试/真实特征/待处理特征,仅占位,不必给初值,用sess.run 的feed_dict参数以字典形式喂入数据

sess.run(求分类评估值的节点,feed_dict{x:})

np.save() # 将数组以二进制格式写入磁盘, 拓展名为.npy
np.load() # 将数组以二进制格式读出磁盘,

np.save("名称.npy", 数组)
变量名 = np.load("名称.npy", encoding="").item()
# encoding 可以不写,有'latin1','ASCII','bytes',默认为"ASCII"
# 也可以直接写为
B = np.load('A.npy')

.item() # 遍历(键值对)
如: data_dict = np.load(vgg16.npy, encoding = ‘latin1’).item() # 读vgg16.npy文件,遍历其内键值对,导出模型参数赋给data_dict.

# tf.shape(a) # 返回a的纬度
x = tf.constant([[1,2,3],[4,5,6]]) #tensor
y = [[1,2,3],[4,5,6]] # list
z = np.arange(24).reshape([2,3,4])  # array
sess.run(tf.shape(x)) #[2,3]
sess.run(tf.shape(y)) #[2,3]
sess.run(tf.shape(z)) #[2,3,4]

a.get_shape()

x_shape=x.get_shape() # 返回的是 TensorShape([Dimension(2),
Dimension(3)]),不能使用 sess.run(), 因为返回的不是 tensor 或 string,而是元组
x_shape=x.get_shape().as_list() # 可以使用 as_list()得到具体的尺寸, x_shape=[2 3]
y_shape=y.get_shape() # AttributeError: 'list' object hasno attribute 'get_shape'
z_shape=z.get_shape() # AttributeError: 'numpy.ndarray'object has no attribute 'get_shape'

tf.nn.bias_add(乘加和, bias):把 bias 加到乘加和上。
tf.reshape(tensor, shape):

# 改变 tensor 的形状
tensor ‘t’ is [1, 2, 3, 4, 5, 6, 7, 8, 9]
tensor ‘t’ has shape [9]
reshape(t, [3, 3]) ==>
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
#如果 shape 有元素[-1],表示在该维度打平至一维
# -1 将自动推导得为 9:
reshape(t, [2, -1]) ==>
[[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]

np.argsort(列表):对列表从小到大排序,返回索引值
OS 模块
os.getcwd():返回当前工作目录。
os.path.join(path1[,path2[,……]]):返回值:将多个路径组合后返回。注意:第一个绝对路径之前的参数将被忽略

import os
vgg16_path = os.path.join(os.getcwd(),"vgg16.npy")

tf.split(dimension, num_split, input):
dimension: 输入张量的哪一个维度,如果是 0 就表示对第 0 维度进行切割。
num_split: 切割的数量,如果是 2 就表示输入张量被切成 2 份,每一份是一个列表。

import tensorflow as tf;
import numpy as np;
A = [[1,2,3],[4,5,6]]
x = tf.split(1, 3, A)
with tf.Session() as sess:
    c = sess.run(x)
    for ele in c:
        print ele
##########################
输出:
[[1]
[4]]
[[2]
[5]]
[[3]
[6]]

tf.concat(concat_dim, values): 沿着某一维度连结 tensor:

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat(0, [t1, t2]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11,12]]
tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
# 如果想沿着 tensor 一新轴连结打包,那么可以:
tf.concat(axis, [tf.expand_dims(t, axis) for t in tensors])
# 等同于 tf.pack(tensors, axis=axis)

fig = plt.figure(“图名字”):实例化图对象。
ax = fig.add_subplot(m n k): 将画布分割成 m 行 n 列,图像画在从左到右从上到下的第 k 块。

#引入对应的库函数
import matplotlib.pyplot as plt
from numpy import *
#绘图
fig = plt.figure()
ax = fig.add_subplot(3 4 9)
ax.plot(x,y)
plt.show()

ax.bar(bar 的个数, bar 的值,每个 bar 的名字, bar 的宽, bar 的颜色):绘制直方图。给出 bar 的个数, bar 的值,每个 bar 的名字, bar 的宽, bar 的颜色。

ax.set_ylabel(“”):给出 y 轴的名字。

ax.set_title(“”):给出子图的名字。

ax.text(x,y,string,fontsize=15,verticalalignment=”top”,horizontalalignment=”right”):
x,y: 表示坐标轴上的值。
string: 表示说明文字。
fontsize: 表示字体大小。
verticalalignment: 垂直对齐方式,参数: [ ‘ center’ | ‘ top’ | ‘ bottom’| ‘ baseline’ ]
horizontalalignment: 水平对齐方式,参数: [‘ center’ |‘ right’ |‘ left’ ]

plt.show():画出来。

axo = imshow(图):画子图。

本节课所使用的代码文件总共有5个
aap.py:应用程序,实现图像识别
vgg16.py: 读模型参数,搭建
utils.py: 读入图片,概率显示
Nclasses.py: 包含lables字典

app.py

#coding:utf-8
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import vgg16
import utils
from Nclasses import labels

img_path = raw_input('Input the path and image name:')
img_ready = utils.load_image(img_path) 

fig=plt.figure(u"Top-5 预测结果") 

with tf.Session() as sess:
    images = tf.placeholder(tf.float32, [1, 224, 224, 3])
    vgg = vgg16.Vgg16() 
    vgg.forward(images) 
    probability = sess.run(vgg.prob, feed_dict={images:img_ready})
    top5 = np.argsort(probability[0])[-1:-6:-1]
    print "top5:",top5
    values = []
    bar_label = []
    for n, i in enumerate(top5): 
        print "n:",n
        print "i:",i
        values.append(probability[0][i]) 
        bar_label.append(labels[i]) 
        print i, ":", labels[i], "----", utils.percent(probability[0][i]) 

    ax = fig.add_subplot(111) 
    ax.bar(range(len(values)), values, tick_label=bar_label, width=0.5, fc='g')
    ax.set_ylabel(u'probabilityit') 
    ax.set_title(u'Top-5') 
    for a,b in zip(range(len(values)), values):
        ax.text(a, b+0.0005, utils.percent(b), ha='center', va = 'bottom', fontsize=7)   
    plt.show() 

vgg16.py

#!/usr/bin/python
#coding:utf-8

import inspect
import os
import numpy as np
import tensorflow as tf
import time
import matplotlib.pyplot as plt

VGG_MEAN = [103.939, 116.779, 123.68] 

class Vgg16():
    def __init__(self, vgg16_path=None):
        if vgg16_path is None:
            vgg16_path = os.path.join(os.getcwd(), "vgg16.npy") 
            self.data_dict = np.load(vgg16_path, encoding='latin1').item() 

    def forward(self, images):

        print("build model started")
        start_time = time.time() 
        rgb_scaled = images * 255.0 
        red, green, blue = tf.split(rgb_scaled,3,3) 
        bgr = tf.concat([     
            blue - VGG_MEAN[0],
            green - VGG_MEAN[1],
            red - VGG_MEAN[2]],3)

        self.conv1_1 = self.conv_layer(bgr, "conv1_1") 
        self.conv1_2 = self.conv_layer(self.conv1_1, "conv1_2")
        self.pool1 = self.max_pool_2x2(self.conv1_2, "pool1")

        self.conv2_1 = self.conv_layer(self.pool1, "conv2_1")
        self.conv2_2 = self.conv_layer(self.conv2_1, "conv2_2")
        self.pool2 = self.max_pool_2x2(self.conv2_2, "pool2")

        self.conv3_1 = self.conv_layer(self.pool2, "conv3_1")
        self.conv3_2 = self.conv_layer(self.conv3_1, "conv3_2")
        self.conv3_3 = self.conv_layer(self.conv3_2, "conv3_3")
        self.pool3 = self.max_pool_2x2(self.conv3_3, "pool3")

        self.conv4_1 = self.conv_layer(self.pool3, "conv4_1")
        self.conv4_2 = self.conv_layer(self.conv4_1, "conv4_2")
        self.conv4_3 = self.conv_layer(self.conv4_2, "conv4_3")
        self.pool4 = self.max_pool_2x2(self.conv4_3, "pool4")

        self.conv5_1 = self.conv_layer(self.pool4, "conv5_1")
        self.conv5_2 = self.conv_layer(self.conv5_1, "conv5_2")
        self.conv5_3 = self.conv_layer(self.conv5_2, "conv5_3")
        self.pool5 = self.max_pool_2x2(self.conv5_3, "pool5")

        self.fc6 = self.fc_layer(self.pool5, "fc6") 
        self.relu6 = tf.nn.relu(self.fc6) 

        self.fc7 = self.fc_layer(self.relu6, "fc7")
        self.relu7 = tf.nn.relu(self.fc7)

        self.fc8 = self.fc_layer(self.relu7, "fc8")
        self.prob = tf.nn.softmax(self.fc8, name="prob")

        end_time = time.time() 
        print(("time consuming: %f" % (end_time-start_time)))

        self.data_dict = None 

    def conv_layer(self, x, name):
        with tf.variable_scope(name): 
            w = self.get_conv_filter(name) 
            conv = tf.nn.conv2d(x, w, [1, 1, 1, 1], padding='SAME') 
            conv_biases = self.get_bias(name) 
            result = tf.nn.relu(tf.nn.bias_add(conv, conv_biases)) 
            return result

    def get_conv_filter(self, name):
        return tf.constant(self.data_dict[name][0], name="filter") 

    def get_bias(self, name):
        return tf.constant(self.data_dict[name][1], name="biases")

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

    def fc_layer(self, x, name):
        with tf.variable_scope(name): 
            shape = x.get_shape().as_list() 
            dim = 1
            for i in shape[1:]:
                dim *= i 
            x = tf.reshape(x, [-1, dim])
            w = self.get_fc_weight(name) 
            b = self.get_bias(name) 

            result = tf.nn.bias_add(tf.matmul(x, w), b) 
            return result

    def get_fc_weight(self, name):  
        return tf.constant(self.data_dict[name][0], name="weights")

utils.py

#!/usr/bin/python
#coding:utf-8
from skimage import io, transform
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from pylab import mpl

mpl.rcParams['font.sans-serif']=['SimHei'] # 正常显示中文标签
mpl.rcParams['axes.unicode_minus']=False # 正常显示正负号

def load_image(path):
    fig = plt.figure("Centre and Resize")
    img = io.imread(path) 
    img = img / 255.0 

    ax0 = fig.add_subplot(131)  
    ax0.set_xlabel(u'Original Picture') 
    ax0.imshow(img) 

    short_edge = min(img.shape[:2]) 
    y = (img.shape[0] - short_edge) / 2  
    x = (img.shape[1] - short_edge) / 2 
    crop_img = img[y:y+short_edge, x:x+short_edge] 

    ax1 = fig.add_subplot(132) 
    ax1.set_xlabel(u"Centre Picture") 
    ax1.imshow(crop_img)

    re_img = transform.resize(crop_img, (224, 224)) 

    ax2 = fig.add_subplot(133) 
    ax2.set_xlabel(u"Resize Picture") 
    ax2.imshow(re_img)

    img_ready = re_img.reshape((1, 224, 224, 3))

    return img_ready

def percent(value):
    return '%.2f%%' % (value * 100)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值