.predict () ทำงานบน CPU เท่านั้นแม้ว่าจะมี GPU

Sep 01 2020

ฉันใช้สคริปต์นี้เพื่อฝึกโมเดลและคาดการณ์บนเครื่องที่ติดตั้งและเปิดใช้งาน GPU และดูเหมือนว่าจะใช้เฉพาะ CPU ในขั้นตอนการทำนาย

บันทึกตำแหน่งอุปกรณ์ที่ฉันเห็นในระหว่าง.predict()ส่วนนี้มีดังต่อไปนี้:

2020-09-01 06:08:19.085400: I tensorflow/core/common_runtime/eager/execute.cc:573] Executing op RangeDataset in device /job:localhost/replica:0/task:0/device:CPU:0
2020-09-01 06:08:19.085617: I tensorflow/core/common_runtime/eager/execute.cc:573] Executing op RepeatDataset in device /job:localhost/replica:0/task:0/device:CPU:0
2020-09-01 06:08:19.089558: I tensorflow/core/common_runtime/eager/execute.cc:573] Executing op MapDataset in device /job:localhost/replica:0/task:0/device:CPU:0
2020-09-01 06:08:19.090003: I tensorflow/core/common_runtime/eager/execute.cc:573] Executing op PrefetchDataset in device /job:localhost/replica:0/task:0/device:CPU:0
2020-09-01 06:08:19.097064: I tensorflow/core/common_runtime/eager/execute.cc:573] Executing op FlatMapDataset in device /job:localhost/replica:0/task:0/device:CPU:0
2020-09-01 06:08:19.097647: I tensorflow/core/common_runtime/eager/execute.cc:573] Executing op TensorDataset in device /job:localhost/replica:0/task:0/device:CPU:0
2020-09-01 06:08:19.097802: I tensorflow/core/common_runtime/eager/execute.cc:573] Executing op RepeatDataset in device /job:localhost/replica:0/task:0/device:CPU:0
2020-09-01 06:08:19.097957: I tensorflow/core/common_runtime/eager/execute.cc:573] Executing op ZipDataset in device /job:localhost/replica:0/task:0/device:CPU:0
2020-09-01 06:08:19.101284: I tensorflow/core/common_runtime/eager/execute.cc:573] Executing op ParallelMapDataset in device /job:localhost/replica:0/task:0/device:CPU:0
2020-09-01 06:08:19.101865: I tensorflow/core/common_runtime/eager/execute.cc:573] Executing op ModelDataset in device /job:localhost/replica:0/task:0/device:CPU:0

แม้ว่าเมื่อฉันวิ่ง:

print(tf.config.experimental.list_physical_devices('GPU'))

ฉันได้รับ:

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:1', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:2', device_type='GPU')]

ที่ผมใช้รหัสสามารถพบได้ที่นี่ ล็อกเอาท์พุทเต็มรูปแบบสามารถมองเห็นได้ที่นี่

บริบทเพิ่มเติม:
Python: 3.7.7
Tensorflow: 2.1.0
GPU: Nvidia Tesla V100-PCIE-16GB
CPU: Intel Xeon Gold 5218 CPU @ 2.30GHz
RAM: 394851272 KB
OS:Linux

คำตอบ

2 gold_cy Sep 03 2020 at 17:51

ดูเหมือนว่าคุณต้องใช้Distributed Strategyต่อเอกสาร จากนั้นรหัสของคุณจะกลายเป็นสิ่งต่อไปนี้:

tf.debugging.set_log_device_placement(True)
strategy = tf.distribute.MirroredStrategy()

with strategy.scope():
    model = keras.Sequential(
        [
            keras.layers.Flatten(input_shape=(28, 28)),
            keras.layers.Dense(128, activation='relu'),
            keras.layers.Dense(10)
        ]
    )
    model.compile(
        optimizer='adam', 
        loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), 
        metrics=['accuracy']
    )
    model.fit(train_images, train_labels, epochs=10)

    test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
    probability_model = tf.keras.Sequential(
        [model, tf.keras.layers.Softmax()]
    )
    probability_model.predict(test_images)

ต่อเอกสาร, การปฏิบัติที่ดีที่สุดสำหรับการใช้ GPUs หลายคือการใช้ tf.distribute.Strategy

2 RishitDagli Sep 04 2020 at 06:01

ตั้งแต่คุณมี GPU ที่ผมคิดว่าผลตอบแทนtf.test.is_gpu_available() Trueคุณสามารถใช้รหัสนี้เพื่อบังคับTensorFlowให้ใช้อุปกรณ์เฉพาะ -

with tf.device('/gpu:0'):
    // GPU stuff

สิ่งนี้ใช้ได้เช่นกันหากคุณต้องการบังคับให้ใช้ CPU แทนบางส่วนของรหัส -

with tf.device('/cpu:0'):
    // CPU stuff

ส่วนเสริมที่อาจเป็นประโยชน์ในขณะใช้tf.device()งานคุณสามารถใช้ฟังก์ชันนี้เพื่อแสดงรายการอุปกรณ์ทั้งหมดที่คุณมี -

def get_available_devices():
    local_device_protos = device_lib.list_local_devices()
    return [x.name for x in local_device_protos]

get_available_devices()

แม้ว่าในกรณีการใช้งานที่คุณกล่าวถึง แต่ฉันไม่รับประกันว่าจะอนุมานได้เร็วขึ้นด้วย GPU

2 AnchalGupta Sep 08 2020 at 15:41

ฟังก์ชันทำนายของคุณกำลังใช้ GPU และฉันได้คำนวณเวลาบน NVIDIA 1080 GTX ใหม่ด้วยรหัสของคุณและใช้เวลาในการอนุมาน 100 ms

รีบูตระบบหรือตรวจสอบว่ามีการใช้ GPU หรือไม่

นี่คือบรรทัดของรหัสของคุณที่ระบุว่าการอนุมานทำงานบน GPU:

2020-09-01 06: 19: 15.885778: ฉัน tensorflow / core / common_runtime / eager / execute.cc: 573] การดำเนินการ op __inference_distributed_function_58022 ในอุปกรณ์ / job: localhost / replica: 0 / task: 0 / device: GPU: 0

2 Y.Ynot Sep 09 2020 at 08:13

คุณใช้แพ็คเกจเทนเซอร์โฟลว์ที่ถูกต้องหรือไม่? มันสามารถช่วยในการถอนการติดตั้ง tensorflow และติดตั้ง tensorflow-gpu แทน

สำหรับเอกสารโปรดดู: https://www.tensorflow.org/install/gpu

2 dasmehdix Sep 09 2020 at 14:53

กรุณาโทรหา keras จากtensorflow.python?

ชอบ;

from tensorflow.python.keras.models import Sequential

นอกจากนี้โปรดตรวจสอบเวอร์ชัน CUDA และ CuDNN เวอร์ชัน Cuda และ CuDNN ต้องเข้ากันได้กับเวอร์ชันเทนเซอร์โฟลว์ คุณสามารถตรวจสอบได้จากที่นี่ เนื่องจากเวอร์ชันเทนเซอร์โฟลว์ของคุณคือเวอร์ชัน2.1CUDA และ CuDNN จึงต้องเป็น10.1& 7.6ตามลำดับ