def show_feature_map(): cat = plt.imread('cat.jpg') #unit8 plt.imshow(cat) cat = tf.cast(cat, tf.float32) #[360, 300, 3] x = tf.reshape(cat, [1, 360, 300, 3]) #[1, 360, 300, 3] out = 25 with tf.variable_scope('conv1'): w = tools.weight([3, 3, 3, out], is_uniform=True) x_w = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') b = tools.bias([out]) x_b = tf.nn.bias_add(x_w, b) x_relu = tf.nn.relu(x_b) n_feature = int(x_w.get_shape()[-1]) sess = tf.Session() sess.run(tf.global_variables_initializer()) feature_map = tf.reshape(x_w, [360, 300, out]) images = tf.image.convert_image_dtype(feature_map, dtype=tf.uint8) images = sess.run(images) plt.figure(figsize=(10, 10)) for i in np.arange(0, n_feature): plt.subplot(5, 5, i + 1) plt.axis('off') plt.imshow(images[:, :, i]) plt.show()
import tensorflow as tf import matplotlib.pyplot as plt import tools cat = plt.imread('cat.jpg') #unit8 plt.imshow(cat) cat = tf.cast(cat, tf.float32) #[360, 300, 3] x = tf.reshape(cat, [1, 360, 300, 3]) #[1, 360, 300, 3] # First conv with tf.variable_scope('conv1'): w = tools.weight([3, 3, 3, 16], is_uniform=True) x_w = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') b = tools.bias([16]) x_b = tf.nn.bias_add(x_w, b) x_relu = tf.nn.relu(x_b) x_pool = tools.pool('test1', x_relu, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) # Second conv with tf.variable_scope('conv2'): w2 = tools.weight([3, 3, 16, 32], is_uniform=True) x_w2 = tf.nn.conv2d(x_pool, w2, strides=[1, 1, 1, 1], padding='SAME') b2 = tools.bias([32])
第一层: """ # 卷积层conv1 with tf.name_scope('conv1'): with tf.name_scope('kernel1'): kernel1 = tools.Weight_with_WeightLoss(shape=[5, 5, 3, 64], stddev=5e-2, lamda=0) tools.variables_summaries(kernel1, 'conv1/kernel1') with tf.name_scope('conv_op'): conv1_result = tf.nn.conv2d(image_holder, kernel1, strides=[1, 1, 1, 1], padding='SAME') with tf.name_scope('bias_op'): b1 = tools.bias(0.1, shape=[64]) tools.variables_summaries(b1, 'conv1/b1') with tf.name_scope('activate'): act1_result = tf.nn.relu(tf.nn.bias_add(conv1_result, b1)) # 池化层与局部响应归一化 with tf.name_scope('max_pool_and_norm1'): with tf.name_scope('max_pool'): pool1_result = tf.nn.max_pool(act1_result, ksize=[1, 2, 2, 1], strides=[1, 3, 3, 1], padding='SAME') with tf.name_scope('lrn1'): norm1_result = tf.nn.lrn(pool1_result, 4, bias=1.0,
import tools #%% cat = plt.imread('cat.jpg') #unit8 plt.imshow(cat) cat = tf.cast(cat, tf.float32) #[360, 300, 3] x = tf.reshape(cat, [1, 360, 300, 3]) #[1, 360, 300, 3] #%% # First conv with tf.variable_scope('conv1'): w = tools.weight([3,3,3,16], is_uniform=True) x_w = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') b = tools.bias([16]) x_b = tf.nn.bias_add(x_w, b) x_relu = tf.nn.relu(x_b) x_pool = tools.pool('test1', x_relu, kernel=[1,2,2,1], stride=[1,2,2,1],is_max_pool=True) # Second conv with tf.variable_scope('conv2'): w2 = tools.weight([3,3,16,32], is_uniform=True) x_w2 = tf.nn.conv2d(x_pool, w2, strides=[1, 1, 1, 1], padding='SAME') b2 = tools.bias([32]) x_b2 = tf.nn.bias_add(x_w2, b2) x_relu2 = tf.nn.relu(x_b2)