MNISTで不正解ラベルを表示したいです。
保存の書き方がわからず困っています。
どのように直せばよいか教えて頂けると助かります。
よろしくお願い致します。
Python
1from __future__ import print_function 2import numpy as np 3from keras.datasets import mnist 4from keras.models import Sequential 5from keras.layers.core import Dense, Activation 6from keras.optimizers import SGD 7from keras.utils import np_utils 8from make_tensorboard import make_tensorboard 9 10 11np.random.seed(1671) # for reproducibility 12 13# network and training 14NB_EPOCH = 10 15BATCH_SIZE = 128 16VERBOSE = 1 17NB_CLASSES = 10 # number of outputs = number of digits 18OPTIMIZER = SGD() # SGD optimizer, explained later in this chapter 192 # how much TRAIN is reserved for VALIDATION 20N_HIDDEN = 128 21VALIDATION_SPLIT = 0.2 22# data: shuffled and split between train and test sets 23# 24(X_train, y_train), (X_test, y_test) = mnist.load_data() 25 26# X_train is 60000 rows of 28x28 values --> reshaped in 60000 x 784 27RESHAPED = 784 28# 29X_train = X_train.reshape(60000, RESHAPED) 30X_test = X_test.reshape(10000, RESHAPED) 31X_train = X_train.astype('float32') 32X_test = X_test.astype('float32') 33 34# normalize 35# 36X_train /= 255 37X_test /= 255 38print(X_train.shape[0], 'train samples') 39print(X_test.shape[0], 'test samples') 40 41# convert class vectors to binary class matrices 42Y_train = np_utils.to_categorical(y_train, NB_CLASSES) 43Y_test = np_utils.to_categorical(y_test, NB_CLASSES) 44 45# 10 outputs 46# final stage is softmax 47 48model = Sequential() 49#model.add(Dense(NB_CLASSES, input_shape=(RESHAPED,))) 50model.add(Dense(NB_CLASSES, input_shape=(RESHAPED,))) 51model.add(Activation('softmax')) 52 53model.summary() 54 55model.compile(loss='categorical_crossentropy', 56 optimizer=OPTIMIZER, 57 metrics=['accuracy']) 58 59callbacks = [make_tensorboard(set_dir_name='keras_MINST_V1')] 60 61model.fit(X_train, Y_train, 62 batch_size=BATCH_SIZE, epochs=NB_EPOCH, 63 callbacks=callbacks, 64 verbose=VERBOSE, validation_split=VALIDATION_SPLIT) 65 66score = model.evaluate(X_test, Y_test, verbose=VERBOSE) 67print("\nTest score:", score[0]) 68print('Test accuracy:', score[1]) 69 70#########間違えた画像の表示########## 71from PIL import Image 72 73categories = [ "0","1", "2" , "3", "4" , "5", "6" , "7", "8" , "9"] 74 75pre = model.predict(X_test) 76for i,v in enumerate(pre): 77 pre_ans = v.argmax() 78 ans = y_test[i].argmax() 79 dat = X_test[i] 80 if ans == pre_ans: continue 81 82 fname = "NG_photo/" + str(i) + "-" + categories[pre_ans] + categories[ans] + ".png" 83 dat *= 255 84 img = Image.fromarray(dat.reshape((28,28))).convert("RGB") 85 img.save("./fname")#ここでエラーが発生している。ディスクトップに保存したい。
エラー内容
ValueError: unknown file extension:
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。