64

TensorEditor :一个小白都能快速玩转的神经网络搭建工具

 5 years ago
source link: https://www.jiqizhixin.com/articles/TensorEditor-introduction?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

工具地址:https://www.tensoreditor.com/

TensorEditor 是一个强大的机器学习工具,甚至小白都能以可视化的方式快速生成整个模型的代码。通过TensorEditor,小白可以连接卷积层、全连接层和池化层等可视化结点创建整个模型,且我们可以将它们转化为TensorFlow 和 Python 代码,并进一步在自己的环境中运行。

基本上,TensorEditor 的步骤即定义我们的数据集、图像或特征,然后创建深度神经网络并下载 Python 2.7 的代码,最后就需要在我们自己的TensorFLow 环境下运行就好了。

通过TensorEditor,我们不仅可以创建深度网络并避免一些常见的代码问题,同时还能生成基于TensorFlow Estimator 的高效代码。如下所示,机器之心尝试构建了一个简单的卷积网络,我们使用了两个卷积层、两个池化层和一个全连接层,并在最后的 Estimator 使用了交叉熵损失函数和 Adagrad 最优化方法。

bENF7bQ.png!web

上述简单搭建的卷积网络同样可以生成完全可执行的代码,这样可以避免大量的一般代码问题与重复性工作。

  1. import tensorflow as tf

  2. import pandas as pd

  3. tf.logging.set_verbosity(tf.logging.INFO)

  4. project_name="CNN"

  5. train_csv_file=""

  6. test_csv_file=""

  7. image_resize=[28,28]

  8. def model_fn(features, labels, mode, params):

  9.    convolutional_2d_1 = tf.layers.conv2d(

  10.            inputs=features,

  11.            filters=32,

  12.            kernel_size=[3,3],

  13.            strides=(1,1),

  14.            padding="same",

  15.            data_format="channels_last",

  16.            dilation_rate=(1,1),

  17.            activation=tf.nn.relu,

  18.            use_bias=True)

  19.    max_pool_2d_1 = tf.layers.max_pooling2d(

  20.        inputs=convolutional_2d_1,

  21.        pool_size=[2,2],

  22.        strides=[2,2],

  23.        padding='same',

  24.        data_format='channels_last')

  25.    convolutional_2d_2 = tf.layers.conv2d(

  26.            inputs=max_pool_2d_1,

  27.            filters=64,

  28.            kernel_size=[3,3],

  29.            strides=(1,1),

  30.            padding="same",

  31.            data_format="channels_last",

  32.            dilation_rate=(1,1),

  33.            activation=tf.nn.relu,

  34.            use_bias=True)

  35.    max_pool_2d_2 = tf.layers.max_pooling2d(

  36.        inputs=max_pool_2d_1,

  37.        pool_size=[2,2],

  38.        strides=[2,2],

  39.        padding='same',

  40.        data_format='channels_last')

  41.    convolutional_2d_3 = tf.layers.conv2d(

  42.            inputs=max_pool_2d_2,

  43.            filters=128,

  44.            kernel_size=[3,3],

  45.            strides=(1,1),

  46.            padding="same",

  47.            data_format="channels_last",

  48.            dilation_rate=(1,1),

  49.            activation=tf.nn.relu,

  50.            use_bias=True)

  51.    max_pool_2d_3 = tf.layers.max_pooling2d(

  52.        inputs=convolutional_2d_3,

  53.        pool_size=[2,2],

  54.        strides=[2,2],

  55.        padding='same',

  56.        data_format='channels_last')

  57.    flatten_1 = tf.reshape(max_pool_2d_3, [-1, 2048])

  58.    dense_1 = tf.layers.dense(inputs=flatten_1, units=1024, activation=tf.nn.relu)

  59.    dropout_1= tf.layers.dropout(inputs=dense_1, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)

  60.    dense_2 = tf.layers.dense(inputs=dropout_1, units=256, activation=tf.nn.relu)

  61.    logits=dense_2

  62.    predictions = {

  63.        "classes": tf.argmax(input=logits, axis=1),

  64.        "probabilities": tf.nn.softmax(logits, name="softmax_tensor")

  65.    }

  66.    #Prediction and training

  67.    if mode == tf.estimator.ModeKeys.PREDICT:

  68.        return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

  69.    # Calculate Loss (for both TRAIN and EVAL modes)

  70.    onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=256)

  71.    loss = tf.losses.softmax_cross_entropy(

  72.        onehot_labels=onehot_labels, logits=logits)

  73.    # Compute evaluation metrics.

  74.    accuracy = tf.metrics.accuracy(labels=labels,

  75.                                   predictions=predictions["classes"],

  76.                                   name='acc_op')

  77.    metrics = {'accuracy': accuracy}

  78.    tf.summary.scalar('accuracy', accuracy[1])

  79.    # Configure the Training Op (for TRAIN mode)

  80.    if mode == tf.estimator.ModeKeys.TRAIN:

  81.        optimizer = tf.train.AdagradOptimizer(learning_rate=0.001)

  82.        train_op = optimizer.minimize(

  83.            loss=loss,

  84.            global_step=tf.train.get_global_step())

  85.        return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)

  86.    # Add evaluation metrics (for EVAL mode)

  87.    eval_metric_ops = {

  88.        "accuracy": tf.metrics.accuracy(

  89.            labels=labels, predictions=predictions["classes"])}

  90.    return tf.estimator.EstimatorSpec(

  91.        mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)

  92. # Parse CSV input file and resize image

  93. def _parse_csv(line):

  94.    parsed_line= tf.decode_csv(line, [[""], []])

  95.    filename = parsed_line[0]

  96.    label = parsed_line[1]

  97.    image_string = tf.read_file(filename)

  98.    image_decoded = tf.image.decode_jpeg(image_string, channels=3)

  99.    image_resized = tf.image.resize_images(image_decoded, image_resize)

  100.    image_gray = tf.image.rgb_to_grayscale(image_resized)

  101.    return image_gray, label

  102. def data_train_estimator():

  103.    dataset = tf.data.TextLineDataset(train_csv_file).map(_parse_csv)  # Map each line to convert the data

  104.    dataset = dataset.batch(100)

  105.    dataset = dataset.shuffle(1000)

  106.    dataset = dataset.repeat()

  107.    iterator = dataset.make_one_shot_iterator()  # create one shot iterator

  108.    feature, label = iterator.get_next()

  109.    return feature, label

  110. def data_test_estimator():

  111.    dataset = tf.data.TextLineDataset(test_csv_file).map(_parse_csv)  # Map each line to convert the data

  112.    dataset = dataset.batch(100)

  113.    iterator = dataset.make_one_shot_iterator()  # create one shot iterator

  114.    feature, label = iterator.get_next()

  115.    return feature, label

  116. def main(unused_argv):

  117.    # MAIN ENTRY

  118.    # Create the Estimator

  119.    classifier = tf.estimator.Estimator(

  120.        model_fn=model_fn,

  121.        model_dir="/tmp/"+project_name,

  122.        params={

  123.            # PARAMS

  124.        }

  125.    )

  126.    classifier.train(input_fn=data_train_estimator, steps=30000)

  127.    eval_results = classifier.evaluate(input_fn=data_test_estimator)

  128.    tf.summary.scalar("Accuracy", eval_results["accuracy"])

  129.    print(eval_results)

  130. if __name__ == "__main__":

  131.     tf.app.run()

TensorEditor 主要有以下特点:

  • 易于使用:我们只需要添加模块、连接模块并在最后加入评估模块,就能完成搭建。

  • 由易到难:只需要叠加不同的模块,我们就能创建如 VGG 那样的复杂深度网络。

  • 参数直观:可以轻松修改各结点的配置与参数,从而搭建定制化的深度网络。

  • 生成代码:搭建完深度架构,我们就能直接生成可执行的TensorFlow 代码(Python 2.7)。

90 秒的 MNIST 教程

在上面的视频中,开发者展示了如何使用TensorEditor 在 90 秒内快速搭建一个可用于 MNIST 手写数字识别的简单网络。对于TensorEditor 这种构建序贯 CNN 模型的简单工具,我们只需要准备两件事就能开始搭建模型模型:

  • 下载 MNIST 手写数据集:https://github.com/damiles/TensorEditor_SampleData/raw/master/mnist_png.tar.gz

  • 确定网络架构:https://www.tensorflow.org/tutorials/layers#building_the_cnn_mnist_classifier

TensorEditor 接受 CSV 格式的特征数据集或具有 CSV 标签的图像数据集作为数据输入,并且需要训练和测试/评估两个 CSV 文件。当我们从上面的链接下载数据集并提取图像数据时,我们会有两个 CSV 文件和两个包含所有图像的文件夹(测试和训练)。

现在我们就可以在TensorEditor 中创建将要用于手写数字识别的卷积网络架构,下面展示的架构和TensorFlow 文档中保持一致。

  • 卷积层 1:使用 32 个 5x5 大小的卷积核和 ReLU激活函数

  • 池化层 1:使用 2x2 滤波器和步幅为 2 的最大池化运算(池化区域不重叠)

  • 卷积层 2:使用 64 个 5x5 大小的卷积核和 ReLU激活函数

  • 池化层 2:同样使用 2x2 滤波器和步幅为 2 的最大池化运算

  • 全连接层 1:1024 个神经元,Dropout正则化率为 0.4

  • 分类层:10 个神经元,每个神经元表示 0 到 9 这十个数字。

我们只需要按步骤先添加一个输入 csv 数据集模块,并设置 train.csv 和 test.csv 的地址。然后依次添加上述的卷积和全连接等模块,并设置好对应的参数,如卷积核大小、卷积核数量和激活函数等。最后主需要添加 Estimator 模块,并设置损失函数、最优化方法和学习率等配置就能完成架构上的搭建。如下所示为使用可视化方法搭建的架构:

umEBZ3z.png!web

最后上面的网络就能生成对应的代码,我们可直接复制到本地代码编辑器中并执行:

2emAFf6.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK