Recently, Heart of machine discovered an interesting tool that makes it easy to visually add convolutional, fully connected, and pooled layers to generate executable TensorFlow code. In addition, we also tried to build a simple convolution architecture and test the generated code in a local TensorFlow environment.


Tools to address: https://www.tensoreditor.com/


TensorEditor is a powerful machine learning tool that allows even Bai to quickly generate code for entire models in a visual manner. With TensorEditor, Xiaomai can connect the convolutional layer, the full connection layer and the pooling layer to create the entire model, and we can convert them into TensorFlow and Python code, and further run them in our own environment.


Basically, TensorEditor’s steps are to define our data set, images, or features, then create a deep neural network, download Python 2.7 code, and then run it in our own TensorFLow environment.


With TensorEditor, we can not only create deep networks and avoid some common code problems, but also generate efficient code based on TensorFlow Estimator. As shown below, the Heart of the Machine tried to build a simple convolutional network. We used two convolutional layers, two pooling layers and one fully connected layer, and the cross entropy loss function and Adagrad optimization method were used in the final Estimator.



The convolutional network simply built above can also generate fully executable code, which can avoid a large number of general code problems and repetitive work.


import tensorflow as tfimport pandas as pdtf.logging.set_verbosity(tf.logging.INFO)project_name="CNN"train_csv_file=""test_csv_file=""Image_resize =[28,28]def model_fn(features, labels, mode, params): image_resize=[28,28]def model_fn(features, labels, mode, params): Convolutional_2d_1 = tf. Layers. Conv2d (inputs=features, filters=32, kernel_size=[3,3], strides=(1,1), padding="same",            data_format="channels_last", dilation_rate = (1, 1), the activation = tf. Nn. Relu, Use_bias =True) max_pool_2d_1 = tf. Layers. Max_pooling2d (inputs=convolutional_2d_1, pool_size=[2,2], strides=[2,2], padding='same',        data_format='channels_last'Convolutional_2d_2 = tf. Layers. Conv2d (inputs=max_pool_2d_1, filters=64, kernel_size=[3,3], strides=(1,1), padding="same",            data_format="channels_last", dilation_rate = (1, 1), the activation = tf. Nn. Relu, Use_bias =True) max_pool_2d_2 = tf.layers.max_pooling2d(inputs=max_pool_2d_1, pool_size=[2,2], strides=[2,2], padding='same',        data_format='channels_last'Convolutional_2d_3 = tf. Layers. Conv2d (inputs=max_pool_2d_2, filters=128, kernel_size=[3,3], strides=(1,1), padding="same",            data_format="channels_last", dilation_rate = (1, 1), the activation = tf. Nn. Relu, Use_bias =True) max_pool_2d_3 = tf. Layers. Max_pooling2d (inputs=convolutional_2d_3, pool_size=[2,2], strides=[2,2], padding='same',        data_format='channels_last') flatten_1 = tf.reshape(max_pool_2d_3, [-1, 2048]) dense_1 = tf.layers.dense(inputs=flatten_1, units=1024, The activation = tf. Nn. Relu) dropout_1 = tf. The layers. The dropout (inputs = dense_1 rate = 0.4, training=mode == tf.estimator.ModeKeys.TRAIN) dense_2 = tf.layers.dense(inputs=dropout_1, units=256, activation=tf.nn.relu) logits=dense_2 predictions = {"classes": tf.argmax(input=logits, axis=1),        "probabilities": tf.nn.softmax(logits, name="softmax_tensor")}#Prediction and training if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions) # Calculate Loss (for both TRAIN and EVAL modes) onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=256) loss = tf.losses.softmax_cross_entropy( onehot_labels=onehot_labels, logits=logits) # Compute evaluation metrics. accuracy = tf.metrics.accuracy(labels=labels, predictions=predictions["classes"], name='acc_op') metrics = {'accuracy': accuracy} tf.summary.scalar('accuracy', accuracy[1]) # Configure the Training Op (for TRAIN mode) if mode == tf.estimator.ModeKeys.TRAIN: Optimizer = tf. Train. AdagradOptimizer (learning_rate = 0.001) train_op = optimizer. Minimize (loss = loss, global_step=tf.train.get_global_step()) return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op) # Add evaluation metrics (for EVAL mode) eval_metric_ops = { "accuracy": tf.metrics.accuracy( labels=labels, predictions=predictions["classes"])} return tf.estimator.EstimatorSpec( mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)# Parse CSV input file and resize imagedef _parse_csv(line): parsed_line= tf.decode_csv(line, [[""], []]) filename = parsed_line[0] label = parsed_line[1] image_string = tf.read_file(filename) image_decoded = tf.image.decode_jpeg(image_string, channels=3) image_resized = tf.image.resize_images(image_decoded, image_resize) image_gray = tf.image.rgb_to_grayscale(image_resized) return image_gray, labeldef data_train_estimator(): dataset = tf.data.TextLineDataset(train_csv_file).map(_parse_csv) # Map each line to convert the data dataset = dataset.batch(100) dataset = dataset.shuffle(1000) dataset = dataset.repeat() iterator = dataset.make_one_shot_iterator() # create one shot iterator feature, label = iterator.get_next() return feature, labeldef data_test_estimator(): dataset = tf.data.TextLineDataset(test_csv_file).map(_parse_csv) # Map each line to convert the data dataset = dataset.batch(100) iterator = dataset.make_one_shot_iterator() # create one shot iterator feature, label = iterator.get_next() return feature, labeldef main(unused_argv): # MAIN ENTRY # Create the Estimator classifier = tf.estimator.Estimator( model_fn=model_fn, model_dir="/tmp/"+project_name, params={ # PARAMS } ) classifier.train(input_fn=data_train_estimator, steps=30000) eval_results = classifier.evaluate(input_fn=data_test_estimator) tf.summary.scalar("Accuracy", eval_results["accuracy"]) print(eval_results)if __name__ == "__main__": tf.app.run()Copy the code


TensorEditor has the following features:


  • Easy to use: We just need to add modules, connect modules, and add an evaluation module at the end to complete the setup.

  • From easy to hard: We can create complex deep networks like VGG simply by stacking different modules.

  • Intuitive parameters: the configuration and parameters of each node can be easily modified to build a customized deep network.

  • Generating code: With the deep architecture in place, we can directly generate executable TensorFlow code (Python 2.7).


90 second MNIST tutorial




In the video above, the developer shows how to use TensorEditor to quickly build a simple network for MNIST handwritten number recognition in just 90 seconds. For TensorEditor, a simple tool for building sequential CNN models, we only need two things to start building models:


  • Download MNIST handwritten data sets: https://github.com/damiles/TensorEditor_SampleData/raw/master/mnist_png.tar.gz

  • To determine the network architecture: https://www.tensorflow.org/tutorials/layers#building_the_cnn_mnist_classifier


TensorEditor accepts feature datasets in CSV format or image datasets with CSV tags as data input and requires training and testing/evaluation of two CSV files. When we download the dataset from the link above and extract the image data, we have two CSV files and two folders containing all the images (test and training).


We can now create in TensorEditor the convolutional network architecture to be used for handwritten number recognition. The architecture shown below is consistent with the TensorFlow documentation.


  • Convolution layer 1:32 5×5 convolution kernels and ReLU activation functions are used

  • Pooling layer 1: Maximum pooling using a 2×2 filter and step 2 (pooling regions do not overlap)

  • Convolution layer 2:64 5×5 convolution kernels and ReLU activation functions are used

  • Pooling layer 2: again using a 2×2 filter and maximum pooling with step 2

  • The Dropout regularization rate was 0.4

  • Classification layer: 10 neurons, each representing the ten numbers from 0 to 9.


All we need to do is add an input CSV data set module and set the train.csv and test.csv addresses. Then, the above modules such as convolution and full connection are successively added, and corresponding parameters, such as convolution kernel size, number of convolution kernel and activation function, are set. Finally, it is necessary to add the Estimator module, and set the loss function, optimization method and learning rate to complete the construction of the architecture. The following is a visual architecture:



Finally, the network generates the corresponding code, which we can copy directly into the local code editor and execute:



This article is compiled for the Heart of the Machine,Reprint please contact this official number for authorization.