暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Docker下运行TensorFlow过程

阿桔的笔记 2021-07-06
949

1. 官网下载Docker Desktop:

https://hub.docker.com/editions/community/docker-ce-desktop-windows

注意说明:Requires Microsoft Windows 10 Professional or Enterprise 64-bit, or Windows 10 Home 64-bit with WSL 2.


2. 下载TensorFlow镜像:

Windows PowerShell输入:docker pull tensorflow/tensorflow

    PS C:\Users\86159> docker pull tensorflow/tensorflow
    Using default tag: latest
    latest: Pulling from tensorflow/tensorflow
    01bf7da0a88c: Pull complete
    f3b4a5f15c7a: Pull complete
    57ffbe87baa1: Pull complete
    82b03aaebc12: Pull complete
    ded4361a2673: Pull complete
    bea159c77425: Pull complete
    cec93399b3cf: Pull complete
    8c7aca956461: Pull complete
    0297cbd0f431: Pull complete
    c1f5822d5655: Pull complete
    Digest: sha256:788c345613ff6cfe617b911dda22b1a900558c28c75afe6c05f8fa0d02bd9811
    Status: Downloaded newer image for tensorflow/tensorflow:latest
    docker.io/tensorflow/tensorflow:latest


    3. 观察Docker Desktop生成对应image:


    4. 运行TensorFlow:

      PS C:\Users\86159> docker run -it --rm -v D:\00_Work\GitHub\Tensorflow\:/mnt -w mnt tensorflow/tensorflow


      ________ _______________
      ___ __/__________________________________ ____/__ ________ __
      __ _ _ \_ __ \_ ___/ __ \_ ___/_ _ __ _ __ \_ | |
      _ __/ (__ )/ /_/ / / _ __/ _ / / /_/ /_ |/ |/ /
      /_/ \___//_/ /_//____/ \____//_/ /_/ /_/ \____/____/|__/




      WARNING: You are running this container as root, which can cause new files in
      mounted volumes to be created as the root user on your host machine.


      To avoid this, run the container by specifying your user's userid:


      $ docker run -u $(id -u):$(id -g) args...
      root@8076016fcb20:/mnt#
      root@8076016fcb20:/mnt# ls
      __pycache__ hello.py

      Windows PowerShell运行TensorFlow,并挂载Windows代码目录到/mnt目录,设置工作目录为/mnt:


      5. 观察Docker Desktop的Containers/Apps中显示的运行实例:


      6. 在Windows代码目录中编辑python脚本hello.py:

        import tensorflow as tf
        mnist = tf.keras.datasets.mnist


        (x_train, y_train),(x_test, y_test) = mnist.load_data()
        x_train, x_test = x_train / 255.0, x_test / 255.0


        model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(input_shape=(28, 28)),
        tf.keras.layers.Dense(512, activation=tf.nn.relu),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Dense(10, activation=tf.nn.softmax)
        ])


        model.compile(optimizer='adam',
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])


        model.fit(x_train, y_train, epochs=5)
        model.evaluate(x_test, y_test)

        此脚本使用TensorFlow中的高级API - Keras开发,训练一个简单的单层神经网络来识别手写数字,训练数据库为MNIST。


        7. 运行hello.py:

          root@8076016fcb20:/mnt# python hello.py
          Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
          11493376/11490434 [==============================] - 3s 0us/step
          2021-07-05 07:01:35.191422: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
          To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
          2021-07-05 07:01:35.411998: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)
          2021-07-05 07:01:35.412332: I tensorflow/core/platform/profile_utils/cpu_utils.cc:114] CPU Frequency: 2591990000 Hz
          Epoch 1/5
          1875/1875 [==============================] - 5s 2ms/step - loss: 0.2218 - accuracy: 0.9349
          Epoch 2/5
          1875/1875 [==============================] - 4s 2ms/step - loss: 0.0975 - accuracy: 0.9706
          Epoch 3/5
          1875/1875 [==============================] - 4s 2ms/step - loss: 0.0701 - accuracy: 0.9781
          Epoch 4/5
          1875/1875 [==============================] - 4s 2ms/step - loss: 0.0524 - accuracy: 0.9829
          Epoch 5/5
          1875/1875 [==============================] - 4s 2ms/step - loss: 0.0439 - accuracy: 0.9858
          313/313 [==============================] - 1s 1ms/step - loss: 0.0591 - accuracy: 0.9831


          可以看到,模型的预测精确度达到98%。

          文章转载自阿桔的笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

          评论