首页 > 解决方案 > 使用 tf.gfile.GFile 时 Python 退出

问题描述

当我尝试使用经过训练的模型时,我曾经使用tf.gfile.GFile以下代码部分来阅读我的标签:

import sys
import os
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import copy
import cv2
# Disable tensorflow compilation warnings
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
def predict(image_data):

    predictions = sess.run(softmax_tensor, \
             {'DecodeJpeg/contents:0': image_data})

    # Sort to show labels of first prediction in order of confidence
    top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]

    max_score = 0.0
    res = ''
    for node_id in top_k:
        human_string = label_lines[node_id]
        score = predictions[0][node_id]
        if score > max_score:
            max_score = score
            res = human_string
    return res, max_score
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
                   in tf.gfile.GFile("logs/output_labels.txt")]

问题在于这一行:

label_lines = [line.rstrip() for line in tf.gfile.GFile("logs/output_labels.txt")]

Python进程退出

在此处输入图像描述

我只收到以下警告:

WARNING:tensorflow:From classify_webcam.py:35: The name tf.gfile.GFile is deprecated. Please use tf.io.gfile.GFile instead.

是什么导致了这个问题?

注意我正在使用tensorflow 1.14.0

标签: pythontensorflow

解决方案


检查我的logs文件夹后,我发现output_labels.txt没有生成,所以在重新训练我的模型并生成标签文件后,tf.io.gfile.GFile工作正常!


推荐阅读