Python 诗人的Tensorflow-升级到Tensorflow 2
几年前,我使用“诗人的Tensorflow”教程创建了一个图像分类器。这太神奇了,从那以后我就经常使用它 今天,我尝试将我的图像分类器迁移到一个新的Docker环境,但它正在运行新版本的Tensorflow 2,因此我的脚本中断 有人能帮忙把这个著名的教程脚本升级到Tensorflow 2吗Python 诗人的Tensorflow-升级到Tensorflow 2,python,tensorflow,tensorflow2.0,image-recognition,Python,Tensorflow,Tensorflow2.0,Image Recognition,几年前,我使用“诗人的Tensorflow”教程创建了一个图像分类器。这太神奇了,从那以后我就经常使用它 今天,我尝试将我的图像分类器迁移到一个新的Docker环境,但它正在运行新版本的Tensorflow 2,因此我的脚本中断 有人能帮忙把这个著名的教程脚本升级到Tensorflow 2吗 directory = '/imageFolder' # Tensorflow labels label_lines = [line.rstrip() for line in tf.gfile.GFile
directory = '/imageFolder'
# Tensorflow labels
label_lines = [line.rstrip() for line in tf.gfile.GFile('/tf_files/retrained_labels.txt')]
# Unpersists graph from file
with tf.gfile.FastGFile('/tf_files/retrained_graph.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
with tf.Session() as sess:
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
# Count the folders
def fcount(path, map = {}):
count = 0
for f in os.listdir(path):
child = os.path.join(path, f)
if os.path.isdir(child):
child_count = fcount(child, map)
count += child_count + 1 # unless include self
map[path] = count
return count
map = {}
totalDirectories = fcount(directory, map)
# Walk the directory
for dirpath, dirnames, filenames in os.walk(directory):
splicedDirpath = dirpath[len(directory):]
print "Processing ", splicedDirpath
counter = 0
for name in filenames:
if name.lower().endswith(('.jpg', '.jpeg', '.tiff')):
print name
image_data = tf.gfile.FastGFile(os.path.join(dirpath, name), 'rb').read()
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]
firstElt = top_k[0];
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[0][node_id]
在这本书中,已经有了一个新版本的诗人用的《TensorFlow》 如果需要将代码从TensorFlow 1.x迁移到TensorFlow 2.x。 通过导入tf.compat.v1和执行tf.compat.v1.disable_v2_behavior()基本上可以使用TensorFlow 2.x的compat库
您还可以使用TensorFlow提供的升级脚本来帮助迁移代码,这还将显示需要手动更改的部分代码 使用自动升级脚本的指南如下。
关于将代码从TensorFlow 1.x迁移到2.x的指南,这里有一个更深入的讨论 在这本书中,已经有了一个新版本的诗人用的TensorFlow 如果需要将代码从TensorFlow 1.x迁移到TensorFlow 2.x。 通过导入tf.compat.v1和执行tf.compat.v1.disable_v2_behavior()基本上可以使用TensorFlow 2.x的compat库
您还可以使用TensorFlow提供的升级脚本来帮助迁移代码,这还将显示需要手动更改的部分代码 使用自动升级脚本的指南如下。
关于将代码从TensorFlow 1.x迁移到2.x的指南,这里有一个更深入的讨论 您可以共享
.pb
文件吗?没有它很难测试。文件太大,无法在堆栈溢出时共享。您可以将其放在Google Drive上并共享“可共享链接”吗?您可以共享.pb
文件吗?没有它很难测试。该文件太大,无法在堆栈溢出上共享。您可以将其放在Google Drive上并共享“可共享链接”吗?