gpt4 book ai didi

python - 如何保存用CNN正确分类的图像?

转载 作者:太空宇宙 更新时间:2023-11-03 20:10:04 25 4
gpt4 key购买 nike

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,28, 1)
test_X = test_X.reshape(-1, 28,28, 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, 28, 1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(64, (3,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()

最佳答案

将正确分类的测试图像保存在其标签的相应文件夹中的完整代码
(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,28, 1)
test_X = test_X.reshape(-1, 28,28, 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, 28, 1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(64, (3,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, batch_size=64, epochs=1)

#test_loss, test_acc = model.evaluate(test_X, test_Y_one_hot)
test_loss, test_acc = model.evaluate(test_X, 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)

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

关于python - 如何保存用CNN正确分类的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58761711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com