问题:如何保存使用CNN正确分类的图像?

cnn的手写识别问题。有一个要求:从10000张测试图像中,准确地保存1000张图像(.png或.jpg),每个文件夹分类为100张图像(0-> 9)。我该怎么办?我需要有关代码的说明。谢谢!代码:

    import keras
    from keras.datasets import mnist 
    from keras.layers import Dense,activation,flatten,Conv2D,MaxPooling2D
    from keras.models import Sequential
    from keras.utils import to_categorical
    import numpy as np
    import matplotlib.pyplot as plt

    (train_X,train_Y),(test_X,test_Y) = mnist.load_data()

    train_X = train_X.reshape(-1,28,1)
    test_X = test_X.reshape(-1,1)

    train_X = train_X.astype('float32')
    test_X = test_X.astype('float32')
    test_X = test_X / 255
    train_Y_one_hot = to_categorical(train_Y)
    test_Y_one_hot = to_categorical(test_Y)

    model = Sequential()

    model.add(Conv2D(64,(3,3),input_shape=(28,1)))
    model.add(activation('relu'))
    model.add(MaxPooling2D(pool_size=(2,2)))

    model.add(Conv2D(64,3)))
    model.add(activation('relu'))
    model.add(MaxPooling2D(pool_size=(2,2)))

    model.add(flatten())
    model.add(Dense(64))

    model.add(Dense(10))
    model.add(activation('softmax'))

    model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adam(),metrics=['accuracy'])

    model.fit(train_X,train_Y_one_hot,batch_size=64,epochs=1)

    test_loss,test_acc = model.evaluate(test_X,test_Y_one_hot)
    print('Test loss',test_loss)
    print('Test accuracy',test_acc)
    model.save('123.model')
    predictions = model.predict(test_X)
    print(np.argmax(np.round(predictions[235])))

    plt.imshow(test_X[235].reshape(28,28),cmap = 'Greys_r')
    plt.show()
narcisol 回答:问题:如何保存使用CNN正确分类的图像?

用于将正确分类的测试图像保存在其标签的相应文件夹中的完整代码
(0到9),每个文件夹中提到100张图像:

import keras
from keras.datasets import mnist 
from keras.layers import Dense,Activation,Flatten,Conv2D,MaxPooling2D
from keras.models import Sequential
from keras.utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt

(train_X,train_Y),(test_X,test_Y) = mnist.load_data()

train_X = train_X.reshape(-1,28,1)
test_X = test_X.reshape(-1,1)

train_X = train_X.astype('float32')
train_X = train_X/255
test_X = test_X.astype('float32')
test_X = test_X / 255
#train_Y_one_hot = to_categorical(train_Y)
#test_Y_one_hot = to_categorical(test_Y)

model = Sequential()

model.add(Conv2D(64,(3,3),input_shape=(28,1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(64,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())
model.add(Dense(64))

model.add(Dense(10))
model.add(Activation('softmax'))

model.compile(loss=keras.losses.sparse_categorical_crossentropy,optimizer=keras.optimizers.Adam(),metrics=['accuracy'])

#model.fit(train_X,train_Y_one_hot,batch_size=64,epochs=1)
model.fit(train_X,train_Y,epochs=1)

#test_loss,test_acc = model.evaluate(test_X,test_Y_one_hot)
test_loss,test_Y)
print('Test loss',test_loss)
print('Test accuracy',test_acc)

predictions = model.predict(test_X)

#****Actual Code which you need is mentioned below

import matplotlib
import matplotlib.pyplot as plt
import os

def save_fig(fig_id,Label):
    path = os.path.join('MNIST_Images',Label,fig_id + "." + "png")    
    plt.tight_layout()
    plt.savefig(path,format="png",dpi=300)
    plt.close()

%matplotlib agg 
%matplotlib agg #These 2 lines are required to prohibit Graph being displayed in Jupyter Notebook. You can comment these if you are using other IDE

No_Of_Rows = predictions.shape[0]

Count_Dict = {}
for i in range(10):
    key = 'Count_' + str(i)
    Count_Dict[key] = 0

for Each_Row in range(No_Of_Rows):
    if np.argmax(predictions[Each_Row]) == test_Y[Each_Row]:
        Label = str(test_Y[Each_Row])
        Count_Dict['Count_' + Label] = Count_Dict['Count_' + Label] + 1
        Count_Of_Label = Count_Dict['Count_' + Label]
        if Count_Of_Label <= 100:
            plt.imshow(test_X[Each_Row].reshape(28,28),cmap = 'Greys_r')
            plt.show()
            save_fig(str(Count_Of_Label),Label)

我已经注释了以下不需要的代码行,因为标签已经是数字格式。

train_Y_one_hot = to_categorical(train_Y)
test_Y_one_hot = to_categorical(test_Y)

此外,由于我们不对变量进行编码,因此我在categorical_crossentropy中将sparse_categorical_crossentropy替换为model.compile

本文链接:https://www.f2er.com/3139790.html

大家都在问