質問失礼いたします。初歩的な質問で大変恐縮なのですが、お答え頂ければ幸いです。
https://aiacademy.jp/texts/show/?id=164
こちらのサイトを参考にプログラムしているのですが、検索してもエラーの原因がわかりませんでした。
原因・対策を教えてください。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "c:\Users\----\Desktop\SS\createNPY.py", line 77, in <module> np.save("./dataset.npy", xy) File "<__array_function__ internals>", line 200, in save File "C:\Users\----\AppData\Roaming\Python\Python311\site-packages\numpy\lib\npyio.py", line 521, in save arr = np.asanyarray(arr) ^^^^^^^^^^^^^^^^^^ ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (4,) + inhomogeneous part.
該当のソースコード
Python
1from PIL import Image 2import os, glob 3import numpy as np 4from PIL import ImageFile 5# IOError: image file is truncated (0 bytes not processed)回避のため 6ImageFile.LOAD_TRUNCATED_IMAGES = True 7 8# indexを教師ラベルとして割り当てるため、0にはdogを指定し、1には猫を指定 9classes = ["circle", "square"] 10num_classes = len(classes) 11image_size = 64 12num_testdata = 25 13 14X_train = [] 15X_test = [] 16y_train = [] 17y_test = [] 18 19for index, classlabel in enumerate(classes): 20 photos_dir = "./data/" + classlabel 21 files = glob.glob(photos_dir + "/*.jpg") 22 for i, file in enumerate(files): 23 image = Image.open(file) 24 image = image.convert("RGB") 25 image = image.resize((image_size, image_size)) 26 data = np.asarray(image) 27 if i < num_testdata: 28 X_test.append(data) 29 y_test.append(index) 30 else: 31 32 # angleに代入される値 33 # -20 34 # -15 35 # -10 36 # -5 37 # 0 38 # 5 39 # 10 40 # 15 41 # 画像を5度ずつ回転 42 for angle in range(-20, 20, 5): 43 44 img_r = image.rotate(angle) 45 data = np.asarray(img_r) 46 X_train.append(data) 47 y_train.append(index) 48 # FLIP_LEFT_RIGHT は 左右反転 49 img_trains = img_r.transpose(Image.FLIP_LEFT_RIGHT) 50 data = np.asarray(img_trains) 51 X_train.append(data) 52 y_train.append(index) # indexを教師ラベルとして割り当てるため、0にはdogを指定し、1には猫を指定 53 54X_train = np.array(X_train) 55X_test = np.array(X_test) 56y_train = np.array(y_train) 57y_test = np.array(y_test) 58 59""" 60print(X_train.ndim) 61print(X_test.ndim) 62print(y_train.ndim) 63print(y_test.ndim) 64 65print(X_train.size) 66print(X_test.size) 67print(y_train.size) 68print(y_test.size) 69 70print(X_train.shape) 71print(X_test.shape) 72print(y_train.shape) 73print(y_test.shape) 74""" 75 76xy = (X_train, X_test, y_train, y_test) 77np.save("./dataset.npy", xy)
試したこと
下のコードを入れて次元と要素数を取得しました
Python
1print(X_train.ndim) 2print(X_test.ndim) 3print(y_train.ndim) 4print(y_test.ndim) 5 6print(X_train.size) 7print(X_test.size) 8print(y_train.size) 9print(y_test.size) 10 11print(X_train.shape) 12print(X_test.shape) 13print(y_train.shape) 14print(y_test.shape)
結果↓
4 4 1 1 3922329600 614400 319200 50 (319200, 64, 64, 3) (50, 64, 64, 3) (319200,) (50,)
エラーメッセージには「File "c:\Users\----\Desktop\SS\createNPY.py", line 77, in <module>」とありますが、該当のソースコードは60行までしかありません。該当のソースコードを実行しても同じエラーが発生しますか?
すいません!
print(X_train.ndim)
print(X_test.ndim)
print(y_train.ndim)
print(y_test.ndim)
print(X_train.size)
print(X_test.size)
print(y_train.size)
print(y_test.size)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
このコードをコメントアウトしていたので行数に狂いがありました
更新させていただきました
下記コードは成功しますか?
np.save("./test.npy", X_train)
