前提・実現したいこと
https://qiita.com/hiroeorz@github/items/ecb39ed4042ebdc0a957
のサイトを写経していますが、途中でエラーがでて行き詰ってしまいました。
入力コード
from keras.models import Sequential from keras.layers import Activation, Dense, Dropout from keras.utils.np_utils import to_categorical from keras.optimizers import Adagrad from keras.optimizers import Adam import numpy as np from PIL import Image import os # 学習用のデータを作る. image_list = [] label_list = [] # ./data/train 以下のorange,appleディレクトリ以下の画像を読み込む。 for dir in os.listdir("data/train"): if dir == ".DS_Store": continue dir1 = "data/train/" + dir label = 0 if dir == "apple": # appleはラベル0 label = 0 elif dir == "orange": # orangeはラベル1 label = 1 for file in os.listdir(dir1): if file != ".DS_Store": # 配列label_listに正解ラベルを追加(りんご:0 オレンジ:1) label_list.append(label) filepath = dir1 + "/" + file # 画像を25x25pixelに変換し、1要素が[R,G,B]3要素を含む配列の25x25の2次元配列として読み込む。 # [R,G,B]はそれぞれが0-255の配列。 image = np.array(Image.open(filepath).resize((25, 25))) print(filepath) # 配列を変換し、[[Redの配列],[Greenの配列],[Blueの配列]] のような形にする。 image = image.transpose(2, 0, 1) # さらにフラットな1次元配列に変換。最初の1/3はRed、次がGreenの、最後がBlueの要素がフラットに並ぶ。 image = image.reshape(1, image.shape[0] * image.shape[1] * image.shape[2]).astype("float32")[0] # 出来上がった配列をimage_listに追加。 image_list.append(image / 255.) # kerasに渡すためにnumpy配列に変換。 image_list = np.array(image_list) # ラベルの配列を1と0からなるラベル配列に変更 # 0 -> [1,0], 1 -> [0,1] という感じ。 Y = to_categorical(label_list)
エラーメッセージ
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-26-d3f456e65bae> in <module> 1 # ラベルの配列を1と0からなるラベル配列に変更 2 # 0 -> [1,0], 1 -> [0,1] という感じ。 ----> 3 Y = to_categorical(label_list) NameError: name 'to_categorical' is not defined
試したこと
エラーを無視してそのまま進めるとYが定義されていないとなって解析ができない状況です。
以上、解決方法をご教授願います。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。