写过去的循环

时间:2017-06-16 10:46:29

标签: python tensorflow

with tf.Session() as sess:
out = open('output.csv', 'a')

for image_path in glob.glob(folder_path+'/*'):

    # Read in the image_data
    image_data = tf.gfile.FastGFile(image_path, 'rb').read()

    # Feed the image_data as input to the graph and get first prediction
    softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')

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

    #print("%s\t%s\t%s\t%s\t%s\t%s\n" % (image_path,predictions[0][1],predictions[0][0],predictions[0][2], predictions[0][3],predictions[0][4]))
    for i in predictions:
        predictions= pd.DataFrame([image_path,i[0][1],i[0][0],i[0][2], i[0][3],i[0][4]], columns = ['predictions']).to_csv('prediction.csv')

    #f = open('/tf_files/testinnggg', 'w')
    #for row in predictions:
    #    f.write(row[0])
    #   f.close()
    #test = [] 
    #test.append([predictions[0][1],predictions[0][0],predictions[0][2], predictions[0][3],predictions[0][4]])
    #THIS ACTUALLY WORKS, I see in my terminal "/tf_files/tested/pic1.jpg   0.00442768  0.995572"
    #np.savetxt('testinnggg', test, delimiter = ',')#,[predictions[0][0],predictions[0][2],predictions[0][3],predictions[0][4],delimiter = ',')
    #out.write("%s\t%s\t%s\n" % (image_path,predictions[0][1],predictions[0][0]))
    #This does not work, because output.csv is not modified
    out.close()

当使用pandas选项保存预测时,唯一保存的预测是最终文件,我认为它会覆盖以前的预测。关于如何在循环中获得所有预测的任何建议。

谢谢

0 个答案:

没有答案