コード例 #1
0
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()
コード例 #2
0
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])
コード例 #3
0
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])