卷积网络

来源:互联网 发布:深圳入学积分怎么算法 编辑:程序博客网 时间:2024/06/03 21:32

学习内容:

numpy 卷积函数,包括零填充、卷积窗口、前向卷积、反向卷积;池化函数,包括前向池化、mask、分配直,反向池化。

对比 tensorflow

定义函数:

def zero_pad(X, pad):

    X_pad = np.pad(X, ((0, 0), (pad, pad), (pad, pad), (0, 0)), 'constant', constant_values=(0, 0))

    return X_pad

-----------------------------------------------------------

def conv_single_step(a_slice_prev, W, b):

    s = a_slice_prev * W

    Z = np.sum(s)

    Z = Z + b

    return Z

--------------------------------------------------------------

def conv_forward(A_prev, W, b, hparameters):

    (m , n_H_prev, n_W_prev, n_C_prev) = A_prev, shape

    (f, f, n_C_prev, n_C) = W.shape

    stride = hparameters['stride']

    pad = hparameters['pad']

    n_H = int(n_H_prev - f + 2 * pad) / stride +1)

    n_W = int(n_W_prev - f + 2 * pad) / stride +1)

    Z = np.zeros((m, n_H, n_W, n_C))

    A_prev_pad = zero_pad(A_prev, pad)

    for i in range(m):

        a_prev_pad = A_prev_pad[i, :, :, :]

        for h in range(n_H):

            for w in range(n_W):

                for c in range(n_C)

                    vert_start = stride * h

                    vert_end = vert_start + f

                    horiz_start = stride * w

                    horiz_end = horiz_start + f

                    a_slice_prev = a_prev_pad(vert_start : vert_end, horiz_start : horiz_end, :]

                    Z[i, hw, w, c] = conv_single_step(a_slice_prev, W[:,:,:, c], b[:,:,:,c])

    cache = (A_prev, W, b, hparameters)

    return Z, cache

-----------------------------------------------------------------

def pool_forward(A_prev, hparameters, mode = 'max'):

    (m, n_H_prev, n_W_prev, n_C_prev) = A_prev.shape

    f = hparameters['f']

    stride = hparameters['stride']

    n_H = int(1 + (n_H_prev -f) / stride)

    n_W = int(1 + (n_W_prev - f) / stride)

    n_C = n_C_prev

    A = np.zeros(m, n_H, n_W, n_C))

    for i in range(m):

        for h in range(n_H):

            for w in range(n_W):

                for c in range(n_C):

                    vert_start = stride * h

                   vert_end = vert_start + f

                    horiz_start = stride * h

                    horiz_end = horiz_start + f

                   a_prev_slice = A_prev[i, vert_start: vert_end, horiz_start: horiz_end, c]

                   if mode == 'max':

                       A[i, h, w, c] = np.max(a_prev_slice)

                   elif mode == 'average':

                       A[i, h, w, c] = np.mean(a_prev_slice)

    cache = A(prev, hparameters)

    return A, cache

****************************************************

def create_placeholders(n_H0, n_W0, n_C0, n_y):

    X = tf.placeholder(tf.float32, shape=[None, n_H0, n_W0, n_C0])

    Y = tf.placeholder(tf.float32, shape=[None, n_y])

    return X, Y

------------------------------------------------------------------

def initialize_parameters():

    tf.set_random_seed(1)

    W1 = tf.get_variable('W1', [4, 4, 3, 8], initializer=tf.contrib.layers.xavier_initializer(seed=0))

    W2 = tf.get_variable('W2', [2, 2, 8, 16], initializer=tf.contrib.layers.xavier_initializer(seed=0))

    parameters = {'W1': W1, 'W2': W2}

    return parameters

------------------------------------------------------------------

def forward_propagation(X, parameters):

    W1 = parameters['W1']

    W2 = parameters['W2']

    Z1 = tf.nn.conv2d(X, W1, strides=[1, 1, 1, 1], padding='SAME')

    A1 = tf.nn.relu(Z1)

    P1 = tf.nn.max_pool(A1, ksize=[1, 8, 8, 1], strides=[1, 8, 8, 1], padding='SAME')

    Z2 = tf.nn.conv2d(P1, W2, strides=[1, 1, 1, 1], padding='SAME')

    A2 = tf.nn.relu(Z2)

    P2 = tf.nn.max_pool(A2, ksize = [1, 4, 4, 1], strides=[1, 4, 4, 1], padding='SAME')

    P2 = tf.contrib.layers.flatten(P2)

    Z3 = tf.contrib.layers.fully_connected(P2, 6, activation_fn=None)

    return Z3

****************************************************

def conv_backward(dZ, cache):

    (A_prev, W, b, hparameters) = cache

    (m, n_H_prev, n_W_prev, n_C_prev) = A_prev.shape

    (f, f, n_C_prev, n_C) = W.shape

    stride = hparameters['stride']

    pad = hparameters['pad']

    (m, n_H, n_W, n_C) = dZ.shape

    dA_prev = np.zeros((m, n_H_prev, n_W_prev, n_C_prev))

    dW = np.zeros((f, f, n_C_prev, n_C))

    db = np.zeros((1, 1, 1, n_C))

    A_prev_pad = zero_pad(A_prev, pad)

    dA_prev_pad = zero_pad(dZ_prev, pad)

        for i in range(m):

        a_prev_pad = A_prev_pad[i, :, :, :]

        da_prev_pad = dA_prev_pad[i, :, :, :]

            for h in range(n_H):

                for w in range(n_W):

                    for c in range(n_C):

                        vert_start = h* stride

                        vert_end = vert_start + f

                        horiz_start = w * stride

                        horiz_end = horiz_start + f

                       a_slice = a_prev_pad[vert_start :  vert_end, horiz_start, horiz_end, :]

                       da_prev_pad[vert_start: vert_end, horiz_start: horiz_end, :]

                       da_prev_pad[vert_start: vert_end, horiz_start: horiz_end, :] += W[:,:,:,c] * dZ[i, h, w, c]

                       dW[:,:,:,c] += a_slice * dZ[i, h, w, c]

                       db[:,:,:,c] += dZ[i, h, w, c]

         dA_prev[i, :,:,:] = da_prev_pad[pad: -pad, pad:-pad, :] #upaded

    return dA_prev, dW, db

------------------------------------------------------------------------------

def create_mask_from_window(x):

    mask = (x == np.max(x))

    return mask

----------------------------------------------------------------------------

def distribute_value(dz, shape):

    (n_H, n_W) = shape

    average = dz / (n_H * n_W)

    a = average * np.ones(shape)

    return a

----------------------------------------------------------------------------

def pool_backward(dA, cache, mode = 'max'):

    (A_prev, hparameters) = cache

    stride = hparameters['stride']

    f = hparameters['f']

    m, n_H_prev, n_W_prev, n_C_prev = A_prev.shape

    m, n_H, n_W, n_C = dA.shape

    dA_prev = np.zeros((m, n_H, n_W, n_C))

    for i in range(m):

        a_prev = A_prev[i, :, :, :]

        for h in range(n_H):

            for w in range(n_W):

                for c in range(n_C):

                    vert_start = stride * h

                    vert_end = vert_start + f

                    horiz_start = stride * h

                    horiz_end = horiz_start + f

                    if mode == 'max':

                        a_prev_slice = a_prev[vert_start: vert_end, horiz_start: horiz_end, c]

                        mask = create_mask_from_window(a_prev_slice)

                        dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += mask * dA[i, h, w, c]

                    elif mode == 'average':

                        da = dA[i, h, w, c]

                        shape = f * f

                        dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += da

    return dA_prev

********************************************************

tensorflow不需要自己定义反向传播

********************************************************

tensorflow模型

def compute_cost(Z3, Y):

    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Z3, labels=Y)

    return cost

---------------------------------------------------------------------

def model(X_train, Y_train, X_test, Y_test, learning_rate = 0.009, num_epochs = 100, minibatch_size = 64, print_cost = True):

    ops.reset_default_graph()

    tf.set_random_seed(1)

    seed = 3

    (m, n_H0, n_W0, n_C0) = X_train.shape

    n_y = Y_train.shape[1]

    costs = []

    X, Y = create_placeholders(n_H0, n_W0, n_C0, n_y)

    parameters = initialize_parameters()

    Z3 = forward_propagation(X, parameters)

    cost = compute_cost(Z3, Y)

    optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

    init = tf.global_varibales_initializer()

    with tf.Session() as sess:

        sess.run(init)

        for epoch in range(num_epochs):

            minibatch_cost = 0

            num_minibatches = int(m / minibatch_size)

            seed = seed + 1

            minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)

           for minibathc in minibatches:

                (minibatch_X, minibatch_Y) = minibatch

                _, temp_cost = sess.run([optimizer, cost], feed_dict={X:minibatch_X, Y:minibatch_Y})

                minibatch_cost += temp_cost / num_minibatches

        if print_cost == True and epoch % 5 == 0:

            print('cost after epoch %i: %f' %(epoch, minibatch_cost))

        if print_cost == True and epoch % 1 == 0:

            costs.append(minibatch_cost)

    plt.plot(np.squeeze(costs))

    plt.ylabel('cost')

    plt.xlabel('iterations per tens')

    plt.title('learning rate = ' + str(learning_rate))

    plt.show()

    predict_op t tf.argmax(Z3, 1)

    correct_prediction = tf.equal(predict_op, tf.argmax(Y, 1))

    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))

    print(accuracy)

    train_accuracy = accuracy.eval({X: X_train, Y: Y_train})

    test_accuracy = accuracy.eval({X: X_test, Y: Y_test})

    print('train accuracy:', train_accuracy)

    print('test accuracy:', test_accuracy)

    return train_accuracy, test_accuracy, parameters

图片检视

image = np.array(ndimage.imread(fname, flatten=False) #scipy

image = scipy.misc.imresize(image, size=(64, 64))

plt.imshow(image)

原创粉丝点击