เขียนสัญลักษณ์ TF 1.x และ compat.v1 API ใหม่โดยอัตโนมัติ

ดูบน TensorFlow.org ทำงานใน Google Colab ดูแหล่งที่มาบน GitHub ดาวน์โหลดโน๊ตบุ๊ค

TensorFlow 2.x มีการเปลี่ยนแปลง API มากมายจาก TF 1.x และ tf.compat.v1 API เช่น การจัดลำดับอาร์กิวเมนต์ใหม่ การเปลี่ยนชื่อสัญลักษณ์ และการเปลี่ยนค่าเริ่มต้นสำหรับพารามิเตอร์ การดำเนินการแก้ไขเหล่านี้ทั้งหมดด้วยตนเองจะเป็นเรื่องที่น่าเบื่อหน่ายและมีแนวโน้มที่จะเกิดข้อผิดพลาด เพื่อปรับปรุงการเปลี่ยนแปลง และเพื่อให้การเปลี่ยนไปใช้ TF 2.x เป็นไปอย่างราบรื่นที่สุด ทีมงาน TensorFlow ได้สร้างยูทิลิตี้ tf_upgrade_v2 เพื่อช่วยเปลี่ยนรหัสเดิมไปเป็น API ใหม่

การใช้งานทั่วไปมีลักษณะดังนี้:

tf_upgrade_v2 \
  --intree my_project/ \
  --outtree my_project_v2/ \
  --reportfile report.txt

มันจะเร่งกระบวนการอัปเกรดของคุณโดยการแปลงสคริปต์ Python ของ TensorFlow 1.x ที่มีอยู่เป็น TensorFlow 2.x

สคริปต์การแปลงจะทำการแปลง API เชิงกลโดยอัตโนมัติ แม้ว่า API จำนวนมากจะไม่สามารถย้ายโดยอัตโนมัติได้ นอกจากนี้ยังไม่สามารถทำให้โค้ดของคุณเข้ากันได้กับพฤติกรรม TF2 และ API ได้อย่างเต็มที่ ดังนั้นจึงเป็นเพียงส่วนหนึ่งของเส้นทางการย้ายถิ่นของคุณ

โมดูลความเข้ากันได้

สัญลักษณ์ API บางตัวไม่สามารถอัพเกรดได้ง่ายๆ โดยใช้การแทนที่สตริง รายการที่ไม่สามารถอัปเกรดโดยอัตโนมัติจะถูกแมปไปยังตำแหน่งของตนในโมดูล compat.v1 โมดูลนี้จะแทนที่สัญลักษณ์ TF 1.x เช่น tf.foo ด้วยการอ้างอิง tf.compat.v1.foo ที่เทียบเท่า หากคุณใช้ compat.v1 API อยู่แล้วโดยการนำเข้า TF ผ่าน import tensorflow.compat.v1 as tf สคริปต์ tf_upgrade_v2 จะพยายามแปลงการใช้งานเหล่านี้เป็น API ที่ไม่รองรับหากเป็นไปได้ โปรดทราบว่าแม้ว่า API compat.v1 บางตัวจะเข้ากันได้กับพฤติกรรมของ TF2.x แต่ส่วนมากจะไม่เป็นเช่นนั้น ดังนั้น เราขอแนะนำให้คุณตรวจทานการแทนที่ด้วยตนเองและย้ายไปยัง API ใหม่ในเนมสเปซ tf.* แทนเนมสเปซ tf.compat.v1 โดยเร็วที่สุด

เนื่องจากการเลิกใช้โมดูล TensorFlow 2.x (เช่น tf.flags และ tf.contrib ) การเปลี่ยนแปลงบางอย่างไม่สามารถแก้ไขได้โดยเปลี่ยนเป็น compat.v1 การอัพเกรดรหัสนี้อาจต้องใช้ไลบรารีเพิ่มเติม (เช่น absl.flags ) หรือเปลี่ยนเป็นแพ็คเกจใน tensorflow/addons

ส่วนที่เหลือของคู่มือนี้จะสาธิตวิธีการใช้สคริปต์การเขียนสัญลักษณ์ใหม่ แม้ว่าสคริปต์จะใช้งานง่าย แต่ขอแนะนำอย่างยิ่งให้คุณใช้สคริปต์นี้เป็นส่วนหนึ่งของกระบวนการต่อไปนี้:

  1. การทดสอบหน่วย : ตรวจสอบให้แน่ใจว่ารหัสที่คุณกำลังอัปเกรดมีชุดทดสอบหน่วยที่มีความครอบคลุมพอสมควร นี่คือรหัส Python ดังนั้นภาษาจะไม่ปกป้องคุณจากข้อผิดพลาดหลายคลาส ตรวจสอบให้แน่ใจด้วยว่าการพึ่งพาที่คุณได้รับการอัปเกรดให้เข้ากันได้กับ TensorFlow 2.x แล้ว

  2. ติดตั้ง TensorFlow 1.15 : อัปเกรด TensorFlow ของคุณเป็น TensorFlow 1.x เวอร์ชันล่าสุด อย่างน้อย 1.15 ซึ่งรวมถึง TensorFlow 2.0 API สุดท้ายใน tf.compat.v2

  3. ทดสอบด้วย 1.15 : ตรวจสอบให้แน่ใจว่าการทดสอบหน่วยของคุณผ่าน ณ จุดนี้ คุณจะใช้งานมันซ้ำๆ ในขณะที่คุณอัปเกรด ดังนั้นการเริ่มต้นจากสีเขียวจึงเป็นสิ่งสำคัญ

  4. เรียกใช้สคริปต์การอัปเกรด : เรียกใช้ tf_upgrade_v2 บนแผนผังต้นทางทั้งหมดของคุณ รวมการทดสอบ การดำเนินการนี้จะอัปเกรดโค้ดของคุณเป็นรูปแบบที่ใช้เฉพาะสัญลักษณ์ที่มีอยู่ใน TensorFlow 2.0 สัญลักษณ์ที่เลิกใช้แล้วจะเข้าถึงได้ด้วย tf.compat.v1 ในที่สุดสิ่งเหล่านี้จะต้องได้รับการเอาใจใส่จากเจ้าหน้าที่ แต่ไม่ใช่ในทันที

  5. เรียกใช้การทดสอบที่แปลงแล้วด้วย TensorFlow 1.15 : รหัสของคุณควรจะยังทำงานได้ดีใน TensorFlow 1.15 เรียกใช้การทดสอบหน่วยของคุณอีกครั้ง ข้อผิดพลาดใดๆ ในการทดสอบของคุณที่นี่หมายความว่ามีจุดบกพร่องในสคริปต์อัปเกรด กรุณาแจ้งให้เราทราบ

  6. ตรวจสอบรายงานการอัปเกรดสำหรับคำเตือนและข้อผิดพลาด : สคริปต์จะเขียนไฟล์รายงานที่อธิบาย Conversion ที่คุณควรตรวจสอบซ้ำ หรือการดำเนินการโดยเจ้าหน้าที่ใดๆ ที่คุณต้องดำเนินการ ตัวอย่างเช่น: อินสแตนซ์ที่เหลือของ contrib จะต้องมีการดำเนินการโดยเจ้าหน้าที่เพื่อนำออก โปรดปรึกษา RFC สำหรับคำแนะนำเพิ่มเติม

  7. ติดตั้ง TensorFlow 2.x : ณ จุดนี้ ควรจะปลอดภัยที่จะเปลี่ยนไปใช้ไบนารี TensorFlow 2.x แม้ว่าคุณจะใช้งานแบบเดิมก็ตาม

  8. ทดสอบด้วย v1.disable_v2_behavior : เรียกใช้การทดสอบของคุณอีกครั้งด้วย v1.disable_v2_behavior() ในฟังก์ชันหลักของการทดสอบควรให้ผลลัพธ์เช่นเดียวกับการทำงานภายใต้ 1.15

  9. เปิดใช้งานพฤติกรรม V2 : เมื่อการทดสอบของคุณทำงานโดยใช้ไบนารี TF2 ตอนนี้คุณสามารถเริ่มย้ายรหัสของคุณเพื่อหลีกเลี่ยง tf.estimator และใช้เฉพาะพฤติกรรม TF2 ที่รองรับเท่านั้น (โดยไม่มีการปิดใช้งานพฤติกรรม TF2) ดู คู่มือการย้าย ข้อมูลสำหรับรายละเอียด

การใช้สคริปต์ tf_upgrade_v2 การเขียนสัญลักษณ์ใหม่

ติดตั้ง

ก่อนเริ่มต้น ตรวจสอบให้แน่ใจว่าได้ติดตั้ง TensorFlow 2.x แล้ว

import tensorflow as tf

print(tf.__version__)
2.6.0

โคลนที่เก็บ git ของ tensorflow/models เพื่อให้คุณมีโค้ดสำหรับทดสอบ:

git clone --branch r1.13.0 --depth 1 https://github.com/tensorflow/models
Cloning into 'models'...
remote: Enumerating objects: 2927, done.[K
remote: Counting objects: 100% (2927/2927), done.[K
remote: Compressing objects: 100% (2428/2428), done.[K
remote: Total 2927 (delta 504), reused 2113 (delta 424), pack-reused 0[K
Receiving objects: 100% (2927/2927), 369.04 MiB | 27.58 MiB/s, done.
Resolving deltas: 100% (504/504), done.
Checking out files: 100% (2768/2768), done.

อ่านความช่วยเหลือ

ควรติดตั้งสคริปต์ด้วย TensorFlow นี่คือความช่วยเหลือในตัว:

tf_upgrade_v2 -h
usage: tf_upgrade_v2 [-h] [--infile INPUT_FILE] [--outfile OUTPUT_FILE]
                     [--intree INPUT_TREE] [--outtree OUTPUT_TREE]
                     [--copyotherfiles COPY_OTHER_FILES] [--inplace]
                     [--no_import_rename] [--no_upgrade_compat_v1_import]
                     [--reportfile REPORT_FILENAME] [--mode {DEFAULT,SAFETY}]
                     [--print_all]

Convert a TensorFlow Python file from 1.x to 2.0

Simple usage:
  tf_upgrade_v2.py --infile foo.py --outfile bar.py
  tf_upgrade_v2.py --infile foo.ipynb --outfile bar.ipynb
  tf_upgrade_v2.py --intree ~/code/old --outtree ~/code/new

optional arguments:
  -h, --help            show this help message and exit
  --infile INPUT_FILE   If converting a single file, the name of the file to
                        convert
  --outfile OUTPUT_FILE
                        If converting a single file, the output filename.
  --intree INPUT_TREE   If converting a whole tree of files, the directory to
                        read from (relative or absolute).
  --outtree OUTPUT_TREE
                        If converting a whole tree of files, the output
                        directory (relative or absolute).
  --copyotherfiles COPY_OTHER_FILES
                        If converting a whole tree of files, whether to copy
                        the other files.
  --inplace             If converting a set of files, whether to allow the
                        conversion to be performed on the input files.
  --no_import_rename    Not to rename import to compat.v2 explicitly.
  --no_upgrade_compat_v1_import
                        If specified, don't upgrade explicit imports of
                        `tensorflow.compat.v1 as tf` to the v2 APIs.
                        Otherwise, explicit imports of the form
                        `tensorflow.compat.v1 as tf` will be upgraded.
  --reportfile REPORT_FILENAME
                        The name of the file where the report log is
                        stored.(default: report.txt)
  --mode {DEFAULT,SAFETY}
                        Upgrade script mode. Supported modes: DEFAULT: Perform
                        only straightforward conversions to upgrade to 2.0. In
                        more difficult cases, switch to use compat.v1. SAFETY:
                        Keep 1.* code intact and import compat.v1 module.
  --print_all           Print full log to stdout instead of just printing
                        errors

ตัวอย่างรหัส TF1

นี่คือสคริปต์ TensorFlow 1.0 อย่างง่าย:

head -n 65 models/samples/cookbook/regression/custom_regression.py | tail -n 10
# Calculate loss using mean squared error
  average_loss = tf.losses.mean_squared_error(labels, predictions)

  # Pre-made estimators use the total_loss instead of the average,
  # so report total_loss for compatibility.
  batch_size = tf.shape(labels)[0]
  total_loss = tf.to_float(batch_size) * average_loss

  if mode == tf.estimator.ModeKeys.TRAIN:
    optimizer = params.get("optimizer", tf.train.AdamOptimizer)

เมื่อติดตั้ง TensorFlow 2.x แล้ว จะไม่ทำงาน:

(cd models/samples/cookbook/regression && python custom_regression.py)
Traceback (most recent call last):
  File "custom_regression.py", line 162, in <module>
    tf.logging.set_verbosity(tf.logging.INFO)
AttributeError: module 'tensorflow' has no attribute 'logging'

ไฟล์เดียว

สคริปต์สามารถรันบนไฟล์ Python ไฟล์เดียว:

!tf_upgrade_v2 \
  --infile models/samples/cookbook/regression/custom_regression.py \
  --outfile /tmp/custom_regression_v2.py
INFO line 38:8: Renamed 'tf.feature_column.input_layer' to 'tf.compat.v1.feature_column.input_layer'
INFO line 43:10: Renamed 'tf.layers.dense' to 'tf.compat.v1.layers.dense'
INFO line 46:17: Renamed 'tf.layers.dense' to 'tf.compat.v1.layers.dense'
INFO line 57:17: tf.losses.mean_squared_error requires manual check. tf.losses have been replaced with object oriented versions in TF 2.0 and after. The loss function calls have been converted to compat.v1 for backward compatibility. Please update these calls to the TF 2.0 versions.
INFO line 57:17: Renamed 'tf.losses.mean_squared_error' to 'tf.compat.v1.losses.mean_squared_error'
INFO line 61:15: Added keywords to args of function 'tf.shape'
INFO line 62:15: Changed tf.to_float call to tf.cast(..., dtype=tf.float32).
INFO line 65:40: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer'
INFO line 68:39: Renamed 'tf.train.get_global_step' to 'tf.compat.v1.train.get_global_step'
INFO line 83:9: tf.metrics.root_mean_squared_error requires manual check. tf.metrics have been replaced with object oriented versions in TF 2.0 and after. The metric function calls have been converted to compat.v1 for backward compatibility. Please update these calls to the TF 2.0 versions.
INFO line 83:9: Renamed 'tf.metrics.root_mean_squared_error' to 'tf.compat.v1.metrics.root_mean_squared_error'
INFO line 142:23: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer'
INFO line 162:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity'
INFO line 162:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO'
INFO line 163:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run'
TensorFlow 2.0 Upgrade Script
-----------------------------
Converted 1 files
Detected 0 issues that require attention
--------------------------------------------------------------------------------


Make sure to read the detailed log 'report.txt'

สคริปต์จะพิมพ์ข้อผิดพลาดหากไม่สามารถแก้ไขรหัสได้

แผนผังไดเรกทอรี

โครงการทั่วไป รวมถึงตัวอย่างง่ายๆ นี้ จะใช้มากกว่าหนึ่งไฟล์ โดยทั่วไปแล้วต้องการอัปเดตแพ็คเกจทั้งหมด เพื่อให้สามารถเรียกใช้สคริปต์บนแผนผังไดเร็กทอรีได้:

# update the .py files and copy all the other files to the outtree
!tf_upgrade_v2 \
    --intree models/samples/cookbook/regression/ \
    --outtree regression_v2/ \
    --reportfile tree_report.txt
INFO line 82:10: tf.estimator.LinearRegressor: Default value of loss_reduction has been changed to SUM_OVER_BATCH_SIZE; inserting old default value tf.keras.losses.Reduction.SUM.

INFO line 105:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity'
INFO line 105:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO'
INFO line 106:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run'
INFO line 38:8: Renamed 'tf.feature_column.input_layer' to 'tf.compat.v1.feature_column.input_layer'
INFO line 43:10: Renamed 'tf.layers.dense' to 'tf.compat.v1.layers.dense'
INFO line 46:17: Renamed 'tf.layers.dense' to 'tf.compat.v1.layers.dense'
INFO line 57:17: tf.losses.mean_squared_error requires manual check. tf.losses have been replaced with object oriented versions in TF 2.0 and after. The loss function calls have been converted to compat.v1 for backward compatibility. Please update these calls to the TF 2.0 versions.
INFO line 57:17: Renamed 'tf.losses.mean_squared_error' to 'tf.compat.v1.losses.mean_squared_error'
INFO line 61:15: Added keywords to args of function 'tf.shape'
INFO line 62:15: Changed tf.to_float call to tf.cast(..., dtype=tf.float32).
INFO line 65:40: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer'
INFO line 68:39: Renamed 'tf.train.get_global_step' to 'tf.compat.v1.train.get_global_step'
INFO line 83:9: tf.metrics.root_mean_squared_error requires manual check. tf.metrics have been replaced with object oriented versions in TF 2.0 and after. The metric function calls have been converted to compat.v1 for backward compatibility. Please update these calls to the TF 2.0 versions.
INFO line 83:9: Renamed 'tf.metrics.root_mean_squared_error' to 'tf.compat.v1.metrics.root_mean_squared_error'
INFO line 142:23: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer'
INFO line 162:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity'
INFO line 162:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO'
INFO line 163:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run'
INFO line 58:10: tf.estimator.LinearRegressor: Default value of loss_reduction has been changed to SUM_OVER_BATCH_SIZE; inserting old default value tf.keras.losses.Reduction.SUM.

INFO line 101:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity'
INFO line 101:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO'
INFO line 102:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run'
INFO line 72:10: tf.estimator.DNNRegressor: Default value of loss_reduction has been changed to SUM_OVER_BATCH_SIZE; inserting old default value tf.keras.losses.Reduction.SUM.

INFO line 96:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity'
INFO line 96:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO'
INFO line 97:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run'
WARNING line 125:15: Changing dataset.make_one_shot_iterator() to tf.compat.v1.data.make_one_shot_iterator(dataset). Please check this transformation.

INFO line 40:7: Renamed 'tf.test.mock' to 'tf.compat.v1.test.mock'
TensorFlow 2.0 Upgrade Script
-----------------------------
Converted 7 files
Detected 1 issues that require attention
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
File: models/samples/cookbook/regression/automobile_data.py
--------------------------------------------------------------------------------
models/samples/cookbook/regression/automobile_data.py:125:15: WARNING: Changing dataset.make_one_shot_iterator() to tf.compat.v1.data.make_one_shot_iterator(dataset). Please check this transformation.



Make sure to read the detailed log 'tree_report.txt'

โปรดสังเกตคำเตือนหนึ่งข้อเกี่ยวกับฟังก์ชัน dataset.make_one_shot_iterator

ตอนนี้สคริปต์ทำงานร่วมกับ TensorFlow 2.x:

โปรดทราบว่าเนื่องจากโมดูล tf.compat.v1 รวมอยู่ใน TF 1.15 สคริปต์ที่แปลงแล้วจะทำงานใน TensorFlow 1.15 ด้วย

(cd regression_v2 && python custom_regression.py 2>&1) | tail
I0922 22:16:42.778216 140254758430528 estimator.py:2074] Saving dict for global step 1000: global_step = 1000, loss = 651.5428, rmse = 3.684265
INFO:tensorflow:Saving 'checkpoint_path' summary for global step 1000: /tmp/tmpk2_4r192/model.ckpt-1000
I0922 22:16:42.817190 140254758430528 estimator.py:2135] Saving 'checkpoint_path' summary for global step 1000: /tmp/tmpk2_4r192/model.ckpt-1000
Tensor("IteratorGetNext:25", shape=(None,), dtype=float64, device=/device:CPU:0)
Tensor("Squeeze:0", shape=(None,), dtype=float32)

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

RMS error for the test set: $3684

รายงานโดยละเอียด

สคริปต์ยังรายงานรายการการเปลี่ยนแปลงโดยละเอียดอีกด้วย ในตัวอย่างนี้ พบการแปลงที่อาจไม่ปลอดภัยและมีคำเตือนที่ด้านบนของไฟล์:

head -n 20 tree_report.txt
TensorFlow 2.0 Upgrade Script
-----------------------------
Converted 7 files
Detected 1 issues that require attention
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
File: models/samples/cookbook/regression/automobile_data.py
--------------------------------------------------------------------------------
models/samples/cookbook/regression/automobile_data.py:125:15: WARNING: Changing dataset.make_one_shot_iterator() to tf.compat.v1.data.make_one_shot_iterator(dataset). Please check this transformation.

================================================================================
Detailed log follows:

================================================================================
================================================================================
Input tree: 'models/samples/cookbook/regression/'
================================================================================
--------------------------------------------------------------------------------
Processing file 'models/samples/cookbook/regression/__init__.py'
 outputting to 'regression_v2/__init__.py'

โปรดสังเกตคำเตือนอีกครั้งหนึ่งเกี่ยวกับ Dataset.make_one_shot_iterator function

ในกรณีอื่นๆ ผลลัพธ์จะอธิบายเหตุผลสำหรับการเปลี่ยนแปลงที่ไม่สำคัญ:

%%writefile dropout.py
import tensorflow as tf

d = tf.nn.dropout(tf.range(10), 0.2)
z = tf.zeros_like(d, optimize=False)
Writing dropout.py
!tf_upgrade_v2 \
  --infile dropout.py \
  --outfile dropout_v2.py \
  --reportfile dropout_report.txt > /dev/null
cat dropout_report.txt
TensorFlow 2.0 Upgrade Script
-----------------------------
Converted 1 files
Detected 0 issues that require attention
--------------------------------------------------------------------------------
================================================================================
Detailed log follows:

================================================================================
--------------------------------------------------------------------------------
Processing file 'dropout.py'
 outputting to 'dropout_v2.py'
--------------------------------------------------------------------------------

3:4: INFO: Changing keep_prob arg of tf.nn.dropout to rate, and recomputing value.

4:4: INFO: Renaming tf.zeros_like to tf.compat.v1.zeros_like because argument optimize is present. tf.zeros_like no longer takes an optimize argument, and behaves as if optimize=True. This call site specifies something other than optimize=True, so it was converted to compat.v1.
--------------------------------------------------------------------------------

นี่คือเนื้อหาไฟล์ที่แก้ไข โปรดทราบว่าสคริปต์เพิ่มชื่ออาร์กิวเมนต์เพื่อจัดการกับอาร์กิวเมนต์ที่ย้ายและเปลี่ยนชื่ออย่างไร:

cat dropout_v2.py
import tensorflow as tf

d = tf.nn.dropout(tf.range(10), rate=1 - (0.2))
z = tf.compat.v1.zeros_like(d, optimize=False)

โครงการขนาดใหญ่อาจมีข้อผิดพลาดเล็กน้อย ตัวอย่างเช่น การแปลงโมเดล deeplab:

!tf_upgrade_v2 \
    --intree models/research/deeplab \
    --outtree deeplab_v2 \
    --reportfile deeplab_report.txt > /dev/null

มันสร้างไฟล์เอาต์พุต:

ls deeplab_v2
README.md   datasets        input_preprocess.py        train.py
__init__.py deeplab_demo.ipynb  local_test.sh          utils
common.py   eval.py         local_test_mobilenetv2.sh  vis.py
common_test.py  export_model.py     model.py
core        g3doc           model_test.py

แต่มีข้อผิดพลาด รายงานจะช่วยคุณระบุสิ่งที่คุณต้องแก้ไขก่อนที่จะทำงาน นี่คือข้อผิดพลาดสามข้อแรก:

cat deeplab_report.txt | grep -i models/research/deeplab | grep -i error | head -n 3
models/research/deeplab/eval.py:28:7: ERROR: Using member tf.contrib.slim in deprecated module tf.contrib. tf.contrib.slim cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository such as tensorflow/addons, or fork the required code.
models/research/deeplab/eval.py:146:8: ERROR: Using member tf.contrib.metrics.aggregate_metric_map in deprecated module tf.contrib. tf.contrib.metrics.aggregate_metric_map cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository such as tensorflow/addons, or fork the required code.
models/research/deeplab/export_model.py:25:7: ERROR: Using member tf.contrib.slim in deprecated module tf.contrib. tf.contrib.slim cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository such as tensorflow/addons, or fork the required code.

โหมด "ความปลอดภัย"

สคริปต์การแปลงยังมีโหมด SAFETY ที่มีการบุกรุกน้อยกว่า ซึ่งเพียงแค่เปลี่ยนการนำเข้าเพื่อใช้โมดูล tensorflow.compat.v1 :

cat dropout.py
import tensorflow as tf

d = tf.nn.dropout(tf.range(10), 0.2)
z = tf.zeros_like(d, optimize=False)
tf_upgrade_v2 --mode SAFETY --infile dropout.py --outfile dropout_v2_safe.py > /dev/null
cat dropout_v2_safe.py
import tensorflow.compat.v1 as tf

d = tf.nn.dropout(tf.range(10), 0.2)
z = tf.zeros_like(d, optimize=False)

อย่างที่คุณเห็นสิ่งนี้ไม่ได้อัปเกรดโค้ดของคุณ แต่อนุญาตให้โค้ด TensorFlow 1 ทำงานกับไบนารี TensorFlow 2 ได้ โปรดทราบว่านี่ไม่ได้หมายความว่าโค้ดของคุณกำลังใช้งานพฤติกรรม TF 2.x ที่รองรับอยู่!

คำเตือน

  • อย่าอัปเดตโค้ดบางส่วนด้วยตนเองก่อนเรียกใช้สคริปต์นี้ โดยเฉพาะอย่างยิ่ง ฟังก์ชันที่มีการเรียงลำดับอาร์กิวเมนต์ใหม่ เช่น tf.argmax หรือ tf.batch_to_space ทำให้สคริปต์เพิ่มอาร์กิวเมนต์ของคีย์เวิร์ดอย่างไม่ถูกต้องซึ่งทำให้โค้ดที่มีอยู่ของคุณแมปไม่ถูกต้อง

  • สคริปต์ถือว่านำเข้า tensorflow โดยใช้ import tensorflow as tf หรือ import tensorflow.compat.v1 as tf

  • สคริปต์นี้ไม่ได้จัดลำดับอาร์กิวเมนต์ใหม่ สคริปต์จะเพิ่มอาร์กิวเมนต์ของคีย์เวิร์ดให้กับฟังก์ชันที่มีการจัดลำดับอาร์กิวเมนต์ใหม่แทน

  • ลองใช้ tf2up.ml สำหรับเครื่องมือที่สะดวกในการอัปเกรดโน้ตบุ๊ก Jupyter และไฟล์ Python ในที่เก็บ GitHub

หากต้องการรายงานข้อบกพร่องของสคริปต์อัปเกรดหรือขอคุณลักษณะ โปรดแจ้งปัญหาใน GitHub