Python UnicodeDecodeError:&x27;utf-8';编解码器可以';t解码位置38中的字节0xff:无效的开始字节

Python UnicodeDecodeError:&x27;utf-8';编解码器可以';t解码位置38中的字节0xff:无效的开始字节,python,exception,tensorflow,utf-8,Python,Exception,Tensorflow,Utf 8,我正在从Tensorflow导入我的模型,只想使用以下代码优化经过训练的模型: input_graph_def = tf.GraphDef() with tf.gfile.Open(output_frozen_graph_name, "r") as f: data = f.read() input_graph_def.ParseFromString(data) output_graph_def = optimize_for_inference_lib.optimize_for_

我正在从Tensorflow导入我的模型,只想使用以下代码优化经过训练的模型:

input_graph_def = tf.GraphDef()
with tf.gfile.Open(output_frozen_graph_name, "r") as f:
    data = f.read()
    input_graph_def.ParseFromString(data)

output_graph_def = optimize_for_inference_lib.optimize_for_inference(
        input_graph_def,
        ["input"], 
        ["y_"],
        tf.float32.as_datatype_enum)

f = tf.gfile.FastGFile("optimized_shoaib-har_agm.pb", "w")
f.write(output_graph_def.SerializeToString())
它显示了这个错误:

回溯(最近一次调用):文件“”,第2行,在 data=f.read()文件“C:\Users\Chaine\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\lib\io\File\u io.py”, 第125行,已读 pywrap\u tensorflow.ReadFromStream(self.\u read\u buf,长度,状态))文件 “C:\Users\Chaine\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\lib\io\file\u io.py”, 第93行,输入值 返回compat.as_str_any(val)文件“C:\Users\Chaine\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\util\compat.py”, 第106行,如有 返回为\u str(value)文件“C:\Users\Chaine\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\util\compat.py”, 第84行,作为文本 返回字节\u或\u文本。解码(编码)UnicodeDecodeError:“utf-8”编解码器无法解码位置38处的字节0xff:无效的开始字节

在它正常工作之前。我甚至已经把它导入了Android Studio。现在我突然得到了这个例外。是因为我在我的机器上安装了东西吗


我能够在我的智能手机上安装应用程序,没有任何错误。现在它给了我错误。我使用的是相同的代码。

Python正在尝试解码文件“output\u freezed\u graph\u name”中的所有字符。不确定文件中发生了什么变化(如果以前对您有效),但很明显,某些字符与“UTF-8”不兼容。现在,它们可以是“UTF-16”或其他编解码器格式。一种方法是读取字节格式的内容,然后自己解码。请尝试阅读以下内容以检查内容:

input_graph_def = tf.GraphDef()
with tf.gfile.Open(output_frozen_graph_name, "rb") as f:
    data = f.read()
    input_graph_def.ParseFromString(data)

output_graph_def = optimize_for_inference_lib.optimize_for_inference(
        input_graph_def,
        ["input"], 
        ["y_"],
        tf.float32.as_datatype_enum)

f = tf.gfile.FastGFile("optimized_shoaib-har_agm.pb", "w")
f.write(output_graph_def.SerializeToString())

安卓工作室认为文件里面有什么?我可以在我的智能手机上安装它,没有任何错误。现在它给了我错误。我使用的是相同的代码。我上周使用的代码没有任何错误。现在我又试着运行了一次,它给了我错误。“一种方法是读取字节格式的内容,然后自己解码”-我怎么做?对不起,但是,我如何使用您提供的这行代码?是否也可能是我在我的计算机中安装了可能导致此错误的东西?您是否尝试将代码更改为以“rb”模式打开文件?@Chaine当您以二进制模式打开文件时,Python不再尝试将字节转换为字符串。所以你不能得到一个UnicodeDecodeError。