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

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/22 03:13
2022/01/22 07:26
2022/01/23 00:39