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

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

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

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

Q&A

解決済

1回答

1318閲覧

PytorchによるUNetが実行できない

tiroha

総合スコア109

Python

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

0グッド

0クリップ

投稿2021/07/01 01:36

編集2021/07/04 01:51

python train.py
とコマンドを入れても、エラー文が出てきます。
どなたか教えてください。

ーエラー文ー
Traceback (most recent call last):
File "train.py", line 4, in <module>
from UNet import model
ModuleNotFoundError: No module named 'UNet

ファイルの場所がおかしいのかと思って確認しても、特に間違っていない。

--フォルダ--
UNet
-dataset
-test
-config.yaml
-model.py
-prediction.py
-prepare_data.py
-train.py

train.py

python

1import tensorflow as tf 2import tensorflow.keras.backend as K 3import yaml 4from UNet import model 5from UNet.prepare_data import data_gen 6 7 8def dice_coef(y_true, y_pred): 9 y_true = K.flatten(y_true) 10 y_pred = K.flatten(y_pred) 11 intersection = K.sum(y_true * y_pred) 12 return 2.0 * intersection / (K.sum(y_true) + K.sum(y_pred) + 1) 13 14 15def dice_coef_loss(y_true, y_pred): 16 return 1.0 - dice_coef(y_true, y_pred) 17 18 19def train(args: dict): 20 lr: float = args["learning_rate"] 21 n_classes: int = args["n_classes"] 22 23 unet = model.UNet(args) 24 unet.compile( 25 optimizer=tf.keras.optimizers.Adam(lr), 26 loss=dice_coef_loss, 27 metrics=["accuracy"], 28 ) 29 unet.summary() 30 31 ckpt = tf.keras.callbacks.ModelCheckpoint( 32 filepath="./UNet/params/model.h5", 33 monitor="loss", 34 save_best_only=True, 35 save_weights_only=True, 36 verbose=1, 37 ) 38 39 segmented_data = args[ 40 "segmented_data" 41 ] # os.path.join(args.train_data, "../segmented_images") 42 generator = data_gen( 43 args["train_data"], segmented_data, args["batch_size"], n_classes 44 ) 45 unet.fit_generator(generator, steps_per_epoch=30, epochs=100, callbacks=[ckpt]) 46 47 48if __name__ == "__main__": 49 if tf.__version__ >= "2.0.0": 50 device = tf.config.experimental.list_physical_devices("GPU") 51 if len(device) > 0: 52 for dev in device: 53 tf.config.experimental.set_memory_growth(dev, True) 54 else: 55 config = tf.ConfigProto() 56 config.gpu_options.allow_growth = True 57 tf.keras.backend.set_session(tf.Session(config=config)) 58 59 with open("./UNet/config.yaml") as f: 60 args = yaml.safe_load(f) 61 62 train(args) 63

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

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

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

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

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

jbpb0

2021/07/03 12:58 編集

> No module named 'UNet Pythonで import os print(os.listdir("./")) を実行した時の結果表示の中に、「UNet」が含まれていますでしょうか?
jbpb0

2021/07/03 13:08 編集

・上記の結果表示の中に、「UNet」が含まれている ・「UNet」ディレクトリの中に「model.py」がある という状態になれば、 > from UNet import model でエラー出なくなると思います > --フォルダ-- UNet 以降に書かれてるのを見ると、「model.py」があるのは、「UNet」ディレクトリの中ではないように見えます ・「UNet」ディレクトリの中に、「dataset」ディレクトリと「test」ディレクトリがある ・その「test」ディレクトリの中に、「model.py」とかがある のですか?
tiroha

2021/07/04 01:50

回答ありがとうございます。 書き方が悪かったです。 ・UNetフォルダにフォルダdataset,testがある。 ・--となっている部分はフォルダUNetに入っている(testには入っていない) 質問文を修正します。
guest

回答1

0

ベストアンサー

python

1from UNet import model 2from UNet.prepare_data import data_gen

を、

python

1import model 2from prepare_data import data_gen

に変更してみると動くかもしれません。

投稿2021/07/04 02:03

ppaul

総合スコア24666

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

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

tiroha

2021/07/04 02:38

変更したところ、その部分はうまくいきました。 他の所でエラー文が出ました。 train(args)のargsを変更ですかね? /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:1940: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators. warnings.warn('`Model.fit_generator` is deprecated and ' Traceback (most recent call last): File "train.py", line 62, in <module> train(args) File "train.py", line 45, in train unet.fit_generator(generator, steps_per_epoch=30, epochs=100, callbacks=[ckpt]) File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py", line 1957, in fit_generator initial_epoch=initial_epoch) File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py", line 1147, in fit steps_per_execution=self._steps_per_execution) File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/data_adapter.py", line 1364, in get_data_handler return DataHandler(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/data_adapter.py", line 1166, in __init__ model=model) File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/data_adapter.py", line 809, in __init__ peek, x = self._peek_and_restore(x) File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/data_adapter.py", line 866, in _peek_and_restore peek = next(x) File "/content/drive/My Drive/UNet-Keras/ML_models-master/UNet/prepare_data.py", line 10, in data_gen inputs = np.array(os.listdir(train_data)) FileNotFoundError: [Errno 2] No such file or directory: './UNet/dataset/raw_images'
jbpb0

2021/07/04 04:12

> No such file or directory: './UNet/dataset/raw_images' エラーの原因は、上記ディレクトリがないこと、のようです
tiroha

2021/07/05 07:13

./dataset/raw_imagesにしたところ、エラー文は無くなりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問