質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

15118閲覧

TypeError: expected str, bytes or os.PathLike object, not NoneType

退会済みユーザー

退会済みユーザー

総合スコア0

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2017/05/01 13:52

編集2017/05/03 02:25

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 と出力され正常に動いていると思いました。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

tf.app.flags.DEFINE_string('graph_file',None,"処理するグラフのファイルのパス")

とコマンドラインオプションの処理を書いているので

python visualize_weights.py --graph_file <ファイル名>

という実行方法なのでは?というかtf.app.run()も必要そうですし、もう少し小さいスクリプトから始めることをおすすめします。(※DEFINE_stringの第3ではなく第2引数の方にファイルパスをべた書きすれば動くような気もしますが。)

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)
$ python visualize_weights.py --help usage: visualize_weights.py [-h] [--graph_file GRAPH_FILE] optional arguments: -h, --help show this help message and exit --graph_file GRAPH_FILE

投稿2017/05/02 01:38

YouheiSakurai

総合スコア6142

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2017/05/03 02:17

ありがとうございます。まだ、問題が解決していなく、質問に追記したのでもしお分かりでしたらよろしくお願いいたします。
退会済みユーザー

退会済みユーザー

2017/05/03 02:26

tf.app.run() が必要な場所って、 if __name__ == '__main__': visualize(FLAGS.graph_file) の下でしょうか?
YouheiSakurai

2017/05/03 08:15

質問への追記分はtensorflowをちゃんと使ったことがないのでわからないです。 tf.app.run()を使用するならmain()がエントリーポイント(色々と自分が実行したい処理を書く場所)なのでその中にvisualize()等を書いていきます。 https://www.tensorflow.org/api_docs/python/tf/app/run 上記がtf.app.run()のドキュメントです。ちなみに2つ目のコメントにある質問内容はかなり的外れなので私の書いた小さいスクリプトを完全理解するところから始めることをお勧めします。「動いたor動かない」だけではなく一行一行どのような役割を担うコードなのか、試しにどれか一行を削ってみると結果がどう変わるのか、それらが理解できれば「的外れ」の理由もわかると思います。
YouheiSakurai

2017/05/03 08:19

あとファイルのパスに半角スペースが含まれる時にはダブルorシングルクォーテーションでくくるようにしましょう。 python test.py --graph_file "/path to/file"
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問