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

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

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

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

Q&A

1回答

3465閲覧

TypeError:ufuncの原因が知りたい

tiroha

総合スコア109

Python

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

0グッド

0クリップ

投稿2022/01/22 02:33

下のプログラムを実行したところ、エラー分が出ました。
文法的に間違っていないと思うのですが。何が原因でしょうか?

Traceback (most recent call last): File "/share/home/nakayama/test/PyTorch-2D-3D-UNet-Tutorial/NewUNet/eval_3d.py", line 64, in <module> result_img = result_imgs[i] & 1 TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

ちなみにファイルを読み込む際にのas_gray=Trueをなくすと実行はされます。
しかし、グレースケールとして読み込まれないです。

python

1result_imgs = [imread(str(result_name),as_gray=True) for result_name in result_names] 2target_imgs = [imread(str(target_name),as_gray=True) for target_name in target_names]

python

1import pathlib 2import argparse 3import sys 4 5################################################################ 6def get_filenames_of_path(path: pathlib.Path, ext: str = "*"): 7 """Returns a list of files in a directory/path. Uses pathlib.""" 8 filenames = [file for file in path.glob(ext) if file.is_file()] 9 filenames.sort() 10 return filenames 11 12################################################################ 13def get_args(): 14 parser = argparse.ArgumentParser(description='Evaluate result', 15 formatter_class=argparse.ArgumentDefaultsHelpFormatter) 16 parser.add_argument('target_dir', type=str) 17 parser.add_argument('result_dir', type=str) 18 19 return parser.parse_args() 20 21################################################################ 22args = get_args() 23 24result_dir = pathlib.Path.cwd() / args.result_dir 25target_dir = pathlib.Path.cwd() / args.target_dir 26 27################################################################ 28import numpy as np 29import torch 30from skimage.io import imread, imsave 31 32from skimage.color import rgb2gray 33# input and target files 34result_names = get_filenames_of_path(result_dir) 35target_names = get_filenames_of_path(target_dir) 36 37# read images and store them in memory 38result_imgs = [imread(str(result_name),as_gray=True) for result_name in result_names] 39target_imgs = [imread(str(target_name),as_gray=True) for target_name in target_names] 40 41if len(result_imgs) == 0 or len(target_imgs) == 0: 42 print("result or target is empty!!") 43 sys.exit() 44 45img_num = min(len(target_imgs), len(result_imgs)) 46 47target_sum = 0 48target_diff = 0 49 50prev_sum = 0 51prev_diff = 0 52prev_img = None 53for i in range(img_num): 54 result_img = result_imgs[i] & 1 55 # target_img = target_imgs[i] * 255 56 target_img = target_imgs[i] & 1 57 58 # print(result_img.shape) 59 # print(target_img.shape) 60 61 ''' 62 if not result_img.shape == target_img.shape: 63 print(f"{result_img.shape}") 64 print(f"{target_img.shape}") 65 assert False # 処理を強制的に止める 66''' 67 68 69 #3Dのときは3で割る 70 #target_diff = np.count_nonzero(result_img != target_img)/3 71 target_diff = np.count_nonzero(result_img != target_img) 72 if i > 0: 73 #prev_diff = np.count_nonzero(result_img != prev_img)/3 74 prev_diff = np.count_nonzero(result_img != prev_img) 75 76 print(f"{result_names[i].name} {target_names[i].name} {target_diff} {prev_diff}") 77 #print(f"{target_diff}") 78 #print(f"{prev_diff}") 79 80 target_sum += target_diff 81 prev_sum += prev_diff 82 83 prev_img = result_img 84 85print(target_sum/img_num) 86print(prev_sum/(img_num-1)) 87

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

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

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

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

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

guest

回答1

0

原因は、

python

1>>> i=0 2>>> print(result_imgs[i].dtype) 3float64

となっているからです。
この場合以下のようにエラーとなります。

python

1>>> result_img = result_imgs[i] & 1 2Traceback (most recent call last): 3 File "<stdin>", line 1, in <module> 4TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

投稿2022/01/22 03:10

ppaul

総合スコア24666

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

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

tiroha

2022/01/22 03:13

回答ありがとうございます。 データの型が違うということですかね?
ppaul

2022/01/22 07:26

result_imgs[i]のdtypeがfloatであることが原因です。
ppaul

2022/01/23 00:39

意味が分からないのなら、 result_img = (result_imgs[i]*255).astype(int) & 1 とやれば、「このエラー」はなくなるでしょう。 そのあとのことは別の問題です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問