現在、機械学習を学習していてテストデータを使用して学習させたモデルがあります。
そのモデルを使用して、評価を実施したいのですが、ファイルの読み込みPASSを下記のように記述しました。。。
ライブラリは
import numpy as image_np
from PIL import Image
を使用して実施したいです。
モデルを作成しているときは、書籍等を参考にして作成したのですが、(サンプルデータ_ラベルがついているもの)
いざ、実践的に、自分PC上のフォルダ(./number_file/*.jpg)に4つ画像ファイルがあります。から読み込もうとしたときどうしたらいいのか。そこからどうに数値化したらいいのか。
アドバイスお願いします。
modelに作成したモデルを載せておきます
python
1path = glob.glob("./number_file/*.jpg") 2N = 4 3img_list =[] 4for i in range(N): 5 img = image_np.array(Image.open(path[i])) 6 7 img_list.append(img) 8 #print(img) 9 #img.show() 10 11 12 13print(img.shape)
model
1%matplotlib inline 2#from __future__ import division, print_function 3 4from keras import backend as K 5from keras.datasets import mnist 6from keras.layers import Activation, Dense, Dropout 7from keras.layers.normalization import BatchNormalization 8from keras.models import Sequential 9from keras.utils.np_utils import to_categorical 10import numpy as np 11from sklearn.metrics import classification_report, confusion_matrix 12# Kerasに付属の手書き数字画像データをダウンロード 13np.random.seed(0) 14(X_train, labels_train), (X_test, labels_test) = mnist.load_data() 15# 各画像は行列なのでベクトルに変換→X_trainとX_testを作成 16X_train = X_train.reshape(len(X_train),-1) 17X_test = X_test.reshape(len(X_test),-1) 18# ラベルをone-hotベクトル(値がひとつだけ1で他が0のベクトル)に変換→Y_trainとY_testを作成 19Y_train = to_categorical(labels_train) 20Y_test = to_categorical(labels_test) 21# モデルの準備 22model = Sequential() 23model.add(Dense(10,input_shape=(784,))) 24model.add(Activation("softmax")) 25model.compile(loss="categorical_crossentropy",optimizer="adam",metrics=["accuracy"]) 26# Trainingの実施 27model.fit(X_train,Y_train,batch_size=128,nb_epoch=10,verbose=1) 28# Test dataを用いてモデルを評価(accuracyを算出) 29_,acc = model.evaluate(X_test,Y_test,verbose=0) 30print("accuracy:{}".format(acc)) 31# Classification_report関数で評価結果を表示 32labels_pred = model.predict_classes(X_test,verbose=0) 33print(confusion_matrix(labels_test,labels_pred)) 34print(classification_report(labels_test,labels_pred)) 35
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/04 08:44
2018/12/04 08:56
2018/12/05 03:04