1. tensorflow實現

# 卷積網絡的訓練數據為MNIST(28*28灰度單色圖像) import tensorflow as tf import numpy as np import matplotlib.pyplot as plt from tensorflow.examples.tutorials.mnist import input_data train_epochs = 100 # 訓練輪數 batch_size = 100 # 隨機出去數據大小 display_step = 1 # 顯示訓練結果的間隔 learning_rate= 0.0001 # 學習效率 drop_prob = 0.5 # 正則化,丟棄比例 fch_nodes = 512 # 全連接隱藏層神經元的個數 # 網絡模型需要的一些輔助函數 # 權重初始化(卷積核初始化) # tf.truncated_normal()不同於tf.random_normal(),返回的值中不會偏離均值兩倍的標准差 # 參數shpae為一個列表對象,例如[5, 5, 1, 32]對應 # 5,5 表示卷積核的大小, 1代表通道channel,對彩色圖片做卷積是3,單色灰度為1 # 最后一個數字32,卷積核的個數,(也就是卷基層提取的特征數量) # 顯式聲明數據類型,切記 def weight_init(shape): weights = tf.truncated_normal(shape, stddev=0.1,dtype=tf.float32) return tf.Variable(weights) # 偏置的初始化 def biases_init(shape): biases = tf.random_normal(shape,dtype=tf.float32) return tf.Variable(biases) # 隨機選取mini_batch def get_random_batchdata(n_samples, batchsize): start_index = np.random.randint(0, n_samples - batchsize) return (start_index, start_index + batchsize) # 全連接層權重初始化函數xavier def xavier_init(layer1, layer2, constant = 1): Min = -constant * np.sqrt(6.0 / (layer1 + layer2)) Max = constant * np.sqrt(6.0 / (layer1 + layer2)) return tf.Variable(tf.random_uniform((layer1, layer2), minval = Min, maxval = Max, dtype = tf.float32)) # 卷積 def conv2d(x, w): return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') # 源碼的位置在tensorflow/python/ops下nn_impl.py和nn_ops.py # 這個函數接收兩個參數,x 是圖像的像素, w 是卷積核 # x 張量的維度[batch, height, width, channels] # w 卷積核的維度[height, width, channels, channels_multiplier] # tf.nn.conv2d()是一個二維卷積函數, # stirdes 是卷積核移動的步長,4個1表示,在x張量維度的四個參數上移動步長 # padding 參數'SAME',表示對原始輸入像素進行填充,卷積后映射的2D圖像與原圖大小相等 # 填充,是指在原圖像素值矩陣周圍填充0像素點 # 如果不進行填充,假設 原圖為 32x32 的圖像,卷積和大小為 5x5 ,卷積后映射圖像大小 為 28x28 # 池化 def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # 池化跟卷積的情況有點類似 # x 是卷積后,有經過非線性激活后的圖像, # ksize 是池化滑動張量 # ksize 的維度[batch, height, width, channels],跟 x 張量相同 # strides [1, 2, 2, 1],與上面對應維度的移動步長 # padding與卷積函數相同,padding='VALID',對原圖像不進行0填充 # x 是手寫圖像的像素值,y 是圖像對應的標簽 x = tf.placeholder(tf.float32, [None, 784]) y = tf.placeholder(tf.float32, [None, 10]) # 把灰度圖像一維向量,轉換為28x28二維結構 x_image = tf.reshape(x, [-1, 28, 28, 1]) # -1表示任意數量的樣本數,大小為28x28深度為一的張量 # 可以忽略(其實是用深度為28的,28x1的張量,來表示28x28深度為1的張量) w_conv1 = weight_init([5, 5, 1, 16]) # 5x5,深度為1,16個 b_conv1 = biases_init([16]) h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1) # 輸出張量的尺寸:28x28x16 h_pool1 = max_pool_2x2(h_conv1) # 池化后張量尺寸:14x14x16 # h_pool1 , 14x14的16個特征圖 w_conv2 = weight_init([5, 5, 16, 32]) # 5x5,深度為16,32個 b_conv2 = biases_init([32]) h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2) # 輸出張量的尺寸:14x14x32 h_pool2 = max_pool_2x2(h_conv2) # 池化后張量尺寸:7x7x32 # h_pool2 , 7x7的32個特征圖 # h_pool2是一個7x7x32的tensor,將其轉換為一個一維的向量 h_fpool2 = tf.reshape(h_pool2, [-1, 7*7*32]) # 全連接層,隱藏層節點為512個 # 權重初始化 w_fc1 = xavier_init(7*7*32, fch_nodes) b_fc1 = biases_init([fch_nodes]) h_fc1 = tf.nn.relu(tf.matmul(h_fpool2, w_fc1) + b_fc1) # 全連接隱藏層/輸出層 # 為了防止網絡出現過擬合的情況,對全連接隱藏層進行 Dropout(正則化)處理,在訓練過程中隨機的丟棄部分 # 節點的數據來防止過擬合.Dropout同把節點數據設置為0來丟棄一些特征值,僅在訓練過程中, # 預測的時候,仍使用全數據特征 # 傳入丟棄節點數據的比例 #keep_prob = tf.placeholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob=drop_prob) # 隱藏層與輸出層權重初始化 w_fc2 = xavier_init(fch_nodes, 10) b_fc2 = biases_init([10]) # 未激活的輸出 y_ = tf.add(tf.matmul(h_fc1_drop, w_fc2), b_fc2) # 激活后的輸出 y_out = tf.nn.softmax(y_) # 交叉熵代價函數 cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(y_out), reduction_indices = [1])) # tensorflow自帶一個計算交叉熵的方法 # 輸入沒有進行非線性激活的輸出值 和 對應真實標簽 #cross_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_, y)) # 優化器選擇Adam(有多個選擇) optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy) # 准確率 # 每個樣本的預測結果是一個(1,10)的vector correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_out, 1)) # tf.cast把bool值轉換為浮點數 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 全局變量進行初始化的Operation init = tf.global_variables_initializer() # 加載數據集MNIST mnist = input_data.read_data_sets('MNIST/mnist', one_hot=True) n_samples = int(mnist.train.num_examples) total_batches = int(n_samples / batch_size) # 會話 with tf.Session() as sess: sess.run(init) Cost = [] Accuracy = [] for i in range(train_epochs): for j in range(100): start_index, end_index = get_random_batchdata(n_samples, batch_size) batch_x = mnist.train.images[start_index: end_index] batch_y = mnist.train.labels[start_index: end_index] _, cost, accu = sess.run([ optimizer, cross_entropy,accuracy], feed_dict={x:batch_x, y:batch_y}) Cost.append(cost) Accuracy.append(accu) if i % display_step ==0: print ('Epoch : %d , Cost : %.7f'%(i+1, cost)) print ('training finished') # 代價函數曲線 fig1,ax1 = plt.subplots(figsize=(10,7)) plt.plot(Cost) ax1.set_xlabel('Epochs') ax1.set_ylabel('Cost') plt.title('Cross Loss') plt.grid() plt.show() # 准確率曲線 fig7,ax7 = plt.subplots(figsize=(10,7)) plt.plot(Accuracy) ax7.set_xlabel('Epochs') ax7.set_ylabel('Accuracy Rate') plt.title('Train Accuracy Rate') plt.grid() plt.show() #----------------------------------各個層特征可視化------------------------------- # imput image fig2,ax2 = plt.subplots(figsize=(2,2)) ax2.imshow(np.reshape(mnist.train.images[11], (28, 28))) plt.show() # 第一層的卷積輸出的特征圖 input_image = mnist.train.images[11:12] conv1_16 = sess.run(h_conv1, feed_dict={x:input_image}) # [16, 28, 28 ,1] conv1_reshape = sess.run(tf.reshape(conv1_16, [16, 1, 28, 28])) fig3,ax3 = plt.subplots(nrows=1, ncols=16, figsize = (16,1)) for i in range(16): ax3[i].imshow(conv1_reshape[i][0]) # tensor的切片[batch, channels, row, column] plt.title('Conv1 16x28x28') plt.show() # 第一層池化后的特征圖 pool1_16 = sess.run(h_pool1, feed_dict={x:input_image}) # [16, 14, 14, 1] pool1_reshape = sess.run(tf.reshape(pool1_16, [16, 1, 14, 14])) fig4,ax4 = plt.subplots(nrows=1, ncols=16, figsize=(16,1)) for i in range(16): ax4[i].imshow(pool1_reshape[i][0]) plt.title('Pool1 16x14x14') plt.show() # 第二層卷積輸出特征圖 conv2_32 = sess.run(h_conv2, feed_dict={x:input_image}) # [32, 14, 14, 1] conv2_reshape = sess.run(tf.reshape(conv2_32, [32, 1, 14, 14])) fig5,ax5 = plt.subplots(nrows=1, ncols=32, figsize = (32, 1)) for i in range(32): ax5[i].imshow(conv2_reshape[i][0]) plt.title('Conv2 32x14x14') plt.show() # 第二層池化后的特征圖 pool2_32 = sess.run(h_pool2, feed_dict={x:input_image}) #[32, 7, 7, 1] pool2_reshape = sess.run(tf.reshape(pool2_32, [32, 1, 7, 7])) fig6,ax6 = plt.subplots(nrows=1, ncols=32, figsize = (32, 1)) plt.title('Pool2 32x7x7') for i in range(32): ax6[i].imshow(pool2_reshape[i][0]) plt.show()
2.keras實現
- Essentials of Deep Learning: Visualizing Convolutional Neural Networks in Python
- https://github.com/keras-team/keras/blob/master/examples/conv_filter_visualization.py
- https://blog.keras.io/how-convolutional-neural-networks-see-the-world.html
- Neural network visualization toolkit for keras https://raghakot.github.io/keras-vis

'''Visualization of the filters of VGG16, via gradient ascent in input space. This script can run on CPU in a few minutes. Results example: http://i.imgur.com/4nj4KjN.jpg ''' from __future__ import print_function from scipy.misc import imsave import numpy as np import time from keras.applications import vgg16 from keras import backend as K # dimensions of the generated pictures for each filter. img_width = 128 img_height = 128 # the name of the layer we want to visualize # (see model definition at keras/applications/vgg16.py) layer_name = 'block5_conv1' # util function to convert a tensor into a valid image def deprocess_image(x): # normalize tensor: center on 0., ensure std is 0.1 x -= x.mean() x /= (x.std() + K.epsilon()) x *= 0.1 # clip to [0, 1] x += 0.5 x = np.clip(x, 0, 1) # convert to RGB array x *= 255 if K.image_data_format() == 'channels_first': x = x.transpose((1, 2, 0)) x = np.clip(x, 0, 255).astype('uint8') return x # build the VGG16 network with ImageNet weights model = vgg16.VGG16(weights='imagenet', include_top=False) print('Model loaded.') model.summary() # this is the placeholder for the input images input_img = model.input # get the symbolic outputs of each "key" layer (we gave them unique names). layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]]) def normalize(x): # utility function to normalize a tensor by its L2 norm return x / (K.sqrt(K.mean(K.square(x))) + K.epsilon()) kept_filters = [] for filter_index in range(200): # we only scan through the first 200 filters, # but there are actually 512 of them print('Processing filter %d' % filter_index) start_time = time.time() # we build a loss function that maximizes the activation # of the nth filter of the layer considered layer_output = layer_dict[layer_name].output if K.image_data_format() == 'channels_first': loss = K.mean(layer_output[:, filter_index, :, :]) else: loss = K.mean(layer_output[:, :, :, filter_index]) # we compute the gradient of the input picture wrt this loss grads = K.gradients(loss, input_img)[0] # normalization trick: we normalize the gradient grads = normalize(grads) # this function returns the loss and grads given the input picture iterate = K.function([input_img], [loss, grads]) # step size for gradient ascent step = 1. # we start from a gray image with some random noise if K.image_data_format() == 'channels_first': input_img_data = np.random.random((1, 3, img_width, img_height)) else: input_img_data = np.random.random((1, img_width, img_height, 3)) input_img_data = (input_img_data - 0.5) * 20 + 128 # we run gradient ascent for 20 steps for i in range(20): loss_value, grads_value = iterate([input_img_data]) input_img_data += grads_value * step print('Current loss value:', loss_value) if loss_value <= 0.: # some filters get stuck to 0, we can skip them break # decode the resulting input image if loss_value > 0: img = deprocess_image(input_img_data[0]) kept_filters.append((img, loss_value)) end_time = time.time() print('Filter %d processed in %ds' % (filter_index, end_time - start_time)) # we will stich the best 64 filters on a 8 x 8 grid. n = 8 # the filters that have the highest loss are assumed to be better-looking. # we will only keep the top 64 filters. kept_filters.sort(key=lambda x: x[1], reverse=True) kept_filters = kept_filters[:n * n] # build a black picture with enough space for # our 8 x 8 filters of size 128 x 128, with a 5px margin in between margin = 5 width = n * img_width + (n - 1) * margin height = n * img_height + (n - 1) * margin stitched_filters = np.zeros((width, height, 3)) # fill the picture with our saved filters for i in range(n): for j in range(n): img, loss = kept_filters[i * n + j] stitched_filters[(img_width + margin) * i: (img_width + margin) * i + img_width, (img_height + margin) * j: (img_height + margin) * j + img_height, :] = img # save the result to disk imsave('stitched_filters_%dx%d.png' % (n, n), stitched_filters)
3.tflearn

""" An example showing how to save/restore models and retrieve weights. """ from __future__ import absolute_import, division, print_function import tflearn import tflearn.datasets.mnist as mnist # MNIST Data X, Y, testX, testY = mnist.load_data(one_hot=True) # Model input_layer = tflearn.input_data(shape=[None, 784], name='input') dense1 = tflearn.fully_connected(input_layer, 128, name='dense1') dense2 = tflearn.fully_connected(dense1, 256, name='dense2') softmax = tflearn.fully_connected(dense2, 10, activation='softmax') regression = tflearn.regression(softmax, optimizer='adam', learning_rate=0.001, loss='categorical_crossentropy') # Define classifier, with model checkpoint (autosave) model = tflearn.DNN(regression, checkpoint_path='model.tfl.ckpt') # Train model, with model checkpoint every epoch and every 200 training steps. model.fit(X, Y, n_epoch=1, validation_set=(testX, testY), show_metric=True, snapshot_epoch=True, # Snapshot (save & evaluate) model every epoch. snapshot_step=500, # Snapshot (save & evalaute) model every 500 steps. run_id='model_and_weights') # --------------------- # Save and load a model # --------------------- # Manually save model model.save("model.tfl") # Load a model model.load("model.tfl") # Or Load a model from auto-generated checkpoint # >> model.load("model.tfl.ckpt-500") # Resume training model.fit(X, Y, n_epoch=1, validation_set=(testX, testY), show_metric=True, snapshot_epoch=True, run_id='model_and_weights') # ------------------ # Retrieving weights # ------------------ # Retrieve a layer weights, by layer name: dense1_vars = tflearn.variables.get_layer_variables_by_name('dense1') # Get a variable's value, using model `get_weights` method: print("Dense1 layer weights:") print(model.get_weights(dense1_vars[0])) # Or using generic tflearn function: print("Dense1 layer biases:") with model.session.as_default(): print(tflearn.variables.get_value(dense1_vars[1])) # It is also possible to retrieve a layer weights through its attributes `W` # and `b` (if available). # Get variable's value, using model `get_weights` method: print("Dense2 layer weights:") print(model.get_weights(dense2.W)) # Or using generic tflearn function: print("Dense2 layer biases:") with model.session.as_default(): print(tflearn.variables.get_value(dense2.b))