Q&A
TypeError: expected str, bytes or os.PathLike object, not NoneType とエラーが出ました。
エラーの詳細は以下のようです。
Traceback (most recent call last):
File "visualize_weights.py", line 73, in <module>
visualize(FLAGS.graph_file)
File "visualize_weights.py", line 20, in visualize
basename = os.path.basename(graph_file)
File "/Users/XXX/anaconda/envs/py36/lib/python3.6/posixpath.py", line 144, in basename
p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not NoneType
python visualize_weights.py --file ./cifar 3/cifar-10-batches-bin 2/data_batch_1.bin
とファイルを実行したのですがうまく行きません。
cifar 3(アプリ名)/cifar-10-batches-bin 2/data_batch_1.bin(実行したいファイル)のディレクトリに実行したいファイルがあるのですが、ファイルの指定の方法が間違っているのでしょうか?
コードには
# coding: utf-8 from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import tensorflow as tf from PIL import Image import numpy as np FLAGS = tf.app.flags.FLAGS tf.app.flags.DEFINE_string('graph_file',None,"処理するグラフのファイルのパス") FILTER_COUNT =64 GRID_SIZE_WIDTH = 8 GRID_SIZE_HEIGHT = 8 def visualize(graph_file): basename = os.path.basename(graph_file) path = os.path.dirname(graph_file) with tf.gfile.FastGFile(graph_file,'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _=tf.import_graph_def(graph_def,name='') with tf.Session() as sess: for node in sess.graph_def.node: print(node.name) weights = sess.graph.get_tensor_by_name('conv1/weights:0') weights_data = weights.eval() image_data = _color(weights_data) image_data = _make_padding(image_data) rows = None #FILTER_COUNT個のフィルターをグリッドに整形 for index in range(GRID_SIZE_HEIGHT): start = index * GRID_SIZE_WIDTH end = start + GRID_SIZE_WIDTH row = np.hstack(image_data[start:end]) if rows in None: rows = row else: rows = np.vstack((rows,row)) print(rows.shape) file_path = os.path.join(path,basename) + '.bmp' with open(file_path,mode='wb') as fp: Image.fromarray(rows).save(fp,format='bmp') def _color(weights_data): x_min = np.amin(weights_data) x_max = np.amax(weights_data) weights_0_to_1 = (weights_data - x_min)/(x_max - x_min) weights_0_to_255_uint8 = (weights_0_to_1 * 255).astype(np.uint8) image_data = np.transpose(weights_0_to_255_uint8,[3,0,1,2]) return image_data def _make_padding(image_data): list=[] for data in image_data: data = np.pad(data,pad_width=((1,1),(1,1),(0,0)),mode='constant',constant_values=0) list.append(data) return list if __name__ == '__main__': visualize(FLAGS.graph_file)
と書きました。
python visualize_weights.py --graph_file ./cifar-10-batches-bin 2/data_batch_1.bin と--graph_file に書き直してファイルを実行すると
Traceback (most recent call last): File "visualize_weights.py", line 73, in <module> visualize(FLAGS.graph_file) File "visualize_weights.py", line 25, in visualize graph_def.ParseFromString(f.read()) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/tensorflow/python/lib/io/file_io.py", line 106, in read self._preread_check() File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/tensorflow/python/lib/io/file_io.py", line 73, in _preread_check compat.as_bytes(self.__name), 1024 * 512, status) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/contextlib.py", line 89, in __exit__ next(self.gen) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 469, in raise_exception_on_not_ok_status pywrap_tensorflow.TF_GetCode(status)) tensorflow.python.framework.errors_impl.NotFoundError: ./cifar-10-batches-bin
とエラーが出ました。アプリを置いている場所からファイルのパスを指定して
python visualize_weights.py --graph_file Desktop/cifar 3/cifar-10-batches-bin 2/data_batch_1.bin
も
tensorflow.python.framework.errors_impl.NotFoundError: Desktop/cifartest/cifarbatches/data_batch_1.bin
とエラーが出ました。
教えてもらったファイルを
import tensorflow as tf def main(argv): print(tf.app.flags.FLAGS.graph_file) if __name__ == "__main__": tf.app.flags.DEFINE_string("graph_file", None, "File path") tf.app.run(main)
test.pyと置いてpython test.py --graph_file Desktop/cifar 3/cifar-10-batches-bin 2/data_batch_1.bin と実行すると
Desktop/cifar と出力され正常に動いていると思いました。
こちらの質問が複数のユーザーから「過去の低評価」という指摘を受けました。
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
退会済みユーザー
2017/05/03 02:17
退会済みユーザー
2017/05/03 02:26
2017/05/03 08:15
2017/05/03 08:19