convolution_neural_network

# -*- coding:utf-8 -*-
import argparse
import datetime
import sys
import tensorflow as tf

import input_data

IMAGE_WIDTH = 60
IMAGE_HEIGHT = 100
IMAGE_SIZE = IMAGE_WIDTH * IMAGE_HEIGHT
LABEL_SIZE = 10  # range(0, 10)

MAX_STEPS = 20000
BATCH_SIZE = 50

LOG_DIR = 'log/cnn1-run-%s' % datetime.datetime.now().strftime('%Y%m%d_%H%M%S')

FLAGS = None


def variable_summaries(var):
    """Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
    with tf.name_scope('summaries'):
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean', mean)
        # with tf.name_scope('stddev'):
        #    stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
        # tf.summary.scalar('stddev', stddev)
        # tf.summary.scalar('max', tf.reduce_max(var))
        # tf.summary.scalar('min', tf.reduce_min(var))
        # tf.summary.histogram('histogram', var)


def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)


def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)


def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')


def max_pool_2x2(x):
    return tf.nn.avg_pool(x, ksize=[1, 8, 8, 1],
                          strides=[1, 2, 2, 1], padding='SAME')


def main(_):
    # load data
    train_data, test_data = input_data.load_data_1char(FLAGS.data_dir)
    print 'data loaded. train images: %s. test images: %s' % (train_data.images.shape[0], test_data.images.shape[0])

    # variable in the graph for input data
    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32, [None, IMAGE_SIZE])
        y_ = tf.placeholder(tf.float32, [None, LABEL_SIZE])

        # must be 4-D with shape `[batch_size, height, width, channels]`
        x_image = tf.reshape(x, [-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1])
        tf.summary.image('input', x_image, max_outputs=LABEL_SIZE)

    # define the model
    with tf.name_scope('convolution-layer-1'):
        W_conv1 = weight_variable([7, 7, 1, 32])
        b_conv1 = bias_variable([32])

        h_conv1 = tf.nn.tanh(conv2d(x_image, W_conv1) + b_conv1)
        h_pool1 = max_pool_2x2(h_conv1)

    with tf.name_scope('convolution-layer-2'):
        W_conv2 = weight_variable([7, 7, 32, 64])
        b_conv2 = bias_variable([64])

        h_conv2 = tf.nn.tanh(conv2d(h_pool1, W_conv2) + b_conv2)
        h_pool2 = max_pool_2x2(h_conv2)

    with tf.name_scope('densely-connected'):
        W_fc1 = weight_variable([IMAGE_WIDTH * IMAGE_HEIGHT * 4, 1024])
        b_fc1 = bias_variable([1024])

        h_pool2_flat = tf.reshape(h_pool2, [-1, IMAGE_WIDTH*IMAGE_HEIGHT*4])
        h_fc1 = tf.nn.tanh(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

    with tf.name_scope('dropout'):
        # To reduce overfitting, we will apply dropout before the readout layer
        keep_prob = tf.placeholder(tf.float32)
        h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    with tf.name_scope('readout'):
        W_fc2 = weight_variable([1024, LABEL_SIZE])
        b_fc2 = bias_variable([LABEL_SIZE])

        y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

    # Define loss and optimizer
    # Returns:
    # A 1-D `Tensor` of length `batch_size`
    # of the same type as `logits` with the softmax cross entropy loss.
    with tf.name_scope('loss'):
        cross_entropy = tf.reduce_mean(
            # -tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
            tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
        train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
        variable_summaries(cross_entropy)

    # forword prop
    predict = tf.argmax(y_conv, axis=1)
    expect = tf.argmax(y_, axis=1)

    # evaluate accuracy
    with tf.name_scope('evaluate_accuracy'):
        correct_prediction = tf.equal(predict, expect)
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        variable_summaries(accuracy)

    with tf.Session() as sess:

        merged = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter(LOG_DIR + '/train', sess.graph)
        test_writer = tf.summary.FileWriter(LOG_DIR + '/test', sess.graph)

        tf.global_variables_initializer().run()

        # Train
        for i in range(MAX_STEPS):
            batch_xs, batch_ys = train_data.next_batch(BATCH_SIZE)

            step_summary, _ = sess.run([merged, train_step], feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 1.0})
            train_writer.add_summary(step_summary, i)

            if i % 100 == 0:
                # Test trained model
                valid_summary, train_accuracy = sess.run([merged, accuracy], feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 1.0})
                train_writer.add_summary(valid_summary, i)

                # final check after looping
                test_x, test_y = test_data.next_batch(2000)
                test_summary, test_accuracy = sess.run([merged, accuracy], feed_dict={x: test_x, y_: test_y, keep_prob: 1.0})
                train_writer.add_summary(test_summary, i)

                print 'step %s, training accuracy = %.2f%%, testing accuracy = %.2f%%' % (i, train_accuracy * 100, test_accuracy * 100)

        train_writer.close()
        test_writer.close()

        # final check after looping
        test_x, test_y = test_data.next_batch(2000)
        test_accuracy = accuracy.eval(feed_dict={x: test_x, y_: test_y, keep_prob: 1.0})
        print 'testing accuracy = %.2f%%' % (test_accuracy * 100, )


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--data_dir', type=str, default='images/one-char',
                        help='Directory for storing input data')
    FLAGS, unparsed = parser.parse_known_args()
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
As an AI language model, I am not able to write and save code files. However, I can explain the general steps and process of creating a style transfer program based on convolution neural network with Python. Style transfer is a technique that enables the transfer of style and texture of one image to another image, while preserving the content of the original image. This technique is based on convolution neural network models. The general steps to create a style transfer program are as follows: 1. Load and preprocess the content and style images. 2. Define the convolution neural network model architecture for the style transfer. 3. Define the loss functions to optimize the style transfer. 4. Train the model using the defined loss functions and optimizer. 5. Save the trained model weights in a separate file for future use. Here is a sample code for creating a style transfer program based on convolution neural network with Python: ``` # Import necessary libraries import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # Load and preprocess the content and style images content_image = preprocess_image(content_path) style_image = preprocess_image(style_path) # Define the convolution neural network model architecture for the style transfer def style_transfer_model(content_image, style_image): # Define the input layers for content and style images content_input = tf.keras.layers.Input(shape=content_image.shape, name='content_image') style_input = tf.keras.layers.Input(shape=style_image.shape, name='style_image') # Build the convolution neural network model architecture # Add convolutional and pooling layers to extract features from content and style images # Add upsampling and convolutional layers to apply the learned style to the content image # Define the output layer as the stylized image output_image = ... # Define the model object model = tf.keras.models.Model(inputs=[content_input, style_input], outputs=output_image) return model # Define the loss functions to optimize the style transfer # Use mean squared error for content loss and Gram matrix for style loss def content_loss(content_features, generated_features): mse_loss = tf.reduce_mean(tf.square(content_features - generated_features)) return mse_loss def gram_matrix(input_tensor): channels = int(input_tensor.shape[-1]) a = tf.reshape(input_tensor, [-1, channels]) n = tf.shape(a)[0] gram = tf.matmul(a, a, transpose_a=True) return gram / tf.cast(n, tf.float32) def style_loss(style_features, generated_features): style_gram = gram_matrix(style_features) generated_gram = gram_matrix(generated_features) mse_loss = tf.reduce_mean(tf.square(style_gram - generated_gram)) return mse_loss # Train the model using the defined loss functions and optimizer model = style_transfer_model(content_image, style_image) content_features = ... style_features = ... generated_features = model([content_image, style_image]) content_loss_val = content_loss(content_features, generated_features) style_loss_val = style_loss(style_features, generated_features) total_loss = content_loss_val + style_loss_val optimizer = tf.keras.optimizers.Adam(learning_rate=0.001) model.compile(optimizer, loss=total_loss) model.fit(x=[content_image, style_image], y=target_image, epochs=10) # Save the trained model weights in a separate file for future use model.save_weights('style_transfer_weights.h5') ``` In this code, we first load and preprocess the content and style images. We then define the convolution neural network model architecture for style transfer and the loss functions for optimizing the style transfer. We train the model using the defined loss functions and optimizer, and finally save the trained model weights in a separate file for future use.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值