簡體   English   中英

應用於圖像層的“轉換層”的權重可提供飽和輸出

[英]Weights from Conv Layer when applied to image layer gives saturated output

當應用訓練有素的權重時,我正在使用image_layer可視化我的第一層輸出。 但是,當我嘗試可視化時,會得到如下白色圖像:

體重可視化

忽略最后四個,過濾器的大小為7x7,其中有32個。

該模型基於以下體系結構(附加代碼)構建:

import numpy as np
import tensorflow as tf
import cv2
from matplotlib import pyplot as plt
% matplotlib inline

model_path = "T_set_4/Model/model.ckpt"

# Define the model parameters
# Convolutional Layer 1.
filter_size1 = 7          # Convolution filters are 7 x 7 pixels.
num_filters1 = 32         # There are 32 of these filters.

# Convolutional Layer 2.
filter_size2 = 7          # Convolution filters are 7 x 7 pixels.
num_filters2 = 64         # There are 64 of these filters.

# Fully-connected layer.
fc_size = 512             # Number of neurons in fully-connected layer.

# Define the data dimensions
# We know that MNIST images are 48 pixels in each dimension.
img_size = 48

# Images are stored in one-dimensional arrays of this length.
img_size_flat = img_size * img_size

# Tuple with height and width of images used to reshape arrays.
img_shape = (img_size, img_size)

# Number of colour channels for the images: 1 channel for gray-scale.
num_channels = 1

# Number of classes, one class for each of 10 digits.
num_classes = 2

def new_weights(shape):
    return tf.Variable(tf.truncated_normal(shape, stddev=0.05))

def new_biases(length):
    return tf.Variable(tf.constant(0.05, shape=[length]))

def new_conv_layer(input,              # The previous layer.
               num_input_channels, # Num. channels in prev. layer.
               filter_size,        # Width and height of each filter.
               num_filters,        # Number of filters.
               use_pooling=True):  # Use 2x2 max-pooling.

# Shape of the filter-weights for the convolution.
# This format is determined by the TensorFlow API.
shape = [filter_size, filter_size, num_input_channels, num_filters]

# Create new weights aka. filters with the given shape.
weights = new_weights(shape=shape)

# Create new biases, one for each filter.
biases = new_biases(length=num_filters)

# Create the TensorFlow operation for convolution.
# Note the strides are set to 1 in all dimensions.
# The first and last stride must always be 1,
# because the first is for the image-number and
# the last is for the input-channel.
# But e.g. strides=[1, 2, 2, 1] would mean that the filter
# is moved 2 pixels across the x- and y-axis of the image.
# The padding is set to 'SAME' which means the input image
# is padded with zeroes so the size of the output is the same.
layer = tf.nn.conv2d(input=input,
                     filter=weights,
                     strides=[1, 1, 1, 1],
                     padding='SAME')

# Add the biases to the results of the convolution.
# A bias-value is added to each filter-channel.
layer += biases

# Rectified Linear Unit (ReLU).
# It calculates max(x, 0) for each input pixel x.
# This adds some non-linearity to the formula and allows us
# to learn more complicated functions.
layer = tf.nn.relu(layer)

# Use pooling to down-sample the image resolution?
if use_pooling:
    # This is 2x2 max-pooling, which means that we
    # consider 2x2 windows and select the largest value
    # in each window. Then we move 2 pixels to the next window.
    layer = tf.nn.max_pool(value=layer,
                           ksize=[1, 2, 2, 1],
                           strides=[1, 2, 2, 1],
                           padding='SAME')
# norm1
norm1 = tf.nn.lrn(layer, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75,
                name='norm1')

# Note that ReLU is normally executed before the pooling,
# but since relu(max_pool(x)) == max_pool(relu(x)) we can
# save 75% of the relu-operations by max-pooling first.

# We return both the resulting layer and the filter-weights
# because we will plot the weights later.
return layer, weights

def flatten_layer(layer):
# Get the shape of the input layer.
    layer_shape = layer.get_shape()

# The shape of the input layer is assumed to be:
# layer_shape == [num_images, img_height, img_width, num_channels]

# The number of features is: img_height * img_width * num_channels
# We can use a function from TensorFlow to calculate this.
num_features = layer_shape[1:4].num_elements()

# Reshape the layer to [num_images, num_features].
# Note that we just set the size of the second dimension
# to num_features and the size of the first dimension to -1
# which means the size in that dimension is calculated
# so the total size of the tensor is unchanged from the reshaping.
layer_flat = tf.reshape(layer, [-1, num_features])

# The shape of the flattened layer is now:
# [num_images, img_height * img_width * num_channels]

# Return both the flattened layer and the number of features.
return layer_flat, num_features

def new_fc_layer(input,          # The previous layer.
             num_inputs,     # Num. inputs from prev. layer.
             num_outputs,    # Num. outputs.
             use_relu=True): # Use Rectified Linear Unit (ReLU)?

# Create new weights and biases.
weights = new_weights(shape=[num_inputs, num_outputs])
biases = new_biases(length=num_outputs)

# Calculate the layer as the matrix multiplication of
# the input and weights, and then add the bias-values.
layer = tf.matmul(input, weights) + biases

# Use ReLU?
if use_relu:
    layer = tf.nn.relu(layer)

return layer


# Create the model

tf.reset_default_graph()

x = tf.placeholder(tf.float32, shape=[None, img_size_flat], name='x')
x_image = tf.reshape(x, [-1, img_size, img_size, num_channels])

y_true = tf.placeholder(tf.float32, shape=[None, num_classes], 
name='y_true')
y_true_cls = tf.argmax(y_true, dimension=1)

# Create the model footprint

layer_conv1, weights_conv1 =     new_conv_layer(input=x_image,
           num_input_channels=num_channels,
           filter_size=filter_size1,
           num_filters=num_filters1,
           use_pooling=True)

layer_conv2, weights_conv2 =     new_conv_layer(input=layer_conv1,
           num_input_channels=num_filters1,
           filter_size=filter_size2,
           num_filters=num_filters2,
           use_pooling=True)

layer_flat, num_features = flatten_layer(layer_conv2)

layer_fc1 = new_fc_layer(input=layer_flat,
                 num_inputs=num_features,
                 num_outputs=fc_size,
                 use_relu=True)

layer_fc2 = new_fc_layer(input=layer_fc1,
                 num_inputs=fc_size,
                 num_outputs=num_classes,
                 use_relu=False)

y_pred = tf.nn.softmax(layer_fc2)
y_pred_cls = tf.argmax(y_pred, dimension=1)

# Restore the model

saver = tf.train.Saver()
session = tf.Session()
saver.restore(session, model_path)

我用來創建可視化權重的代碼來自以下代碼源代碼

有人可以告訴我培訓或網絡太淺嗎?

這是訓練初期由第一卷積層生成的特征圖 (不是權重)的完美可視化。

第一層學習提取簡單特征,學習過程有點慢,因此您首先學習了“模糊”輸入圖像,但是一旦網絡開始融合,您將看到第一層將開始提取有意義的底層圖像功能(邊緣等)。

只需監視培訓過程,然后讓網絡進行更多培訓即可。

相反,如果您的性能不佳(始終查看驗證准確性),則特征圖將始終顯得嘈雜,您應該開始調整超參數(降低學習率,進行正則化……),以便提取有意義的特征,從而取得好成績

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM