numpy 通过加载我自己的img来训练MNIST(带有答案标签,加载img 6告诉AI是6)

oxf4rvwz  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(104)

我已经通读了下面的讨论(没有说如何将pic加载到MNIST数据库)
MNIST trained network tested with my own samples
我也计划通过输入img来训练我自己的mnist,但是大多数教程都没有教如何加载我们的个人img(答案是,教AI识别)

    • 例如将所有img "5"图像加载到MNIST数据库中,并教他们数字5**

我们怎样才能做到呢?
下面的. py脚本是由MNIST自己的数据库训练的(* credit by student_DC *),然后重新识别,但是准确率结果不理想(大约10%),所以我在做MNIST之前也来训练我的样本img

    • 但如果我将img存储在本地文件中,如何将其加载到MNIST模型中进行训练?**
  • 经过训练的脚本(训练和测试从MNIST图像数据库)可以加载我自己的图像预测
import keras
from keras.datasets import mnist
import matplotlib.pyplot as plt
import PIL
from PIL import Image
(train_images,train_labels),(test_images,test_labels) = mnist.load_data()
train_images.shape
len(train_labels)
train_labels
test_images.shape
len(test_labels)
test_labels

from keras import models
from keras import layers
network = models.Sequential()
network.add(layers.Dense(512,activation='relu',input_shape=(28*28,)))
network.add(layers.Dense(10,activation='softmax'))

network.compile(optimizer='rmsprop',
                loss='categorical_crossentropy',
                metrics=['accuracy'])

train_images = train_images.reshape((60000,28*28))
train_images = train_images.astype('float32')/255
test_images = test_images.reshape((10000,28*28))
test_images = test_images.astype('float32')/255

from keras.utils import to_categorical

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

network.fit(train_images,train_labels,epochs= 3 ,batch_size=128)

test_loss , test_acc = network.evaluate(test_images,test_labels)
print('test_acc:',test_acc)


network.save('m_lenet.h5')

#########

import numpy as np
from keras.models import load_model
import matplotlib.pyplot as plt
from PIL import Image

model = load_model('/content/m_lenet.h5')

picPath = '/content/02_a.png'
img = Image.open(picPath)

reIm = img.resize((28,28),Image.ANTIALIAS)

plt.imshow(reIm)
plt.savefig('/content/result.png')

im1 = np.array(reIm.convert("L"))


im1 = im1.reshape((1,28*28))

im1 = im1.astype('float32')/255

# predict = model.predict_classes(im1)

predict_x=model.predict(im1) 
classes_x=np.argmax(predict_x,axis=1)

print ("---------------------------------")

print ('predict as:')
print (predict_x)

print ("")
print ("")

print ('predict number as:')
print (classes_x)
print ("---------------------------------")
print ("Original img : ")

示例img屏幕截图:

  • 按导出建议:

为了将img传递到model.fit
挖掘network.fit(train_images,train_labels,epochs= 3 ,batch_size=128)并让AI逻辑地训练我的img可以解决这个问题
我在如何创建一个火车图像阵列和相应的标签阵列库存现在,我在网上搜索没有得到类似的教程(与主题:* 通过加载我自己的img来培训MNIST *)

bjg7j2ky

bjg7j2ky1#

cridet由这个网站,似乎这是你什么加载自己的img训练
https://blog.tanka.la/2018/10/28/build-the-mnist-model-with-your-own-handwritten-digits-using-tensorflow-keras-and-python/

from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.optimizers import Adam
from keras.utils import np_utils
from PIL import Image
import numpy as np
import os
import matplotlib.pyplot as plt

# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Reshaping to format which CNN expects (batch, height, width, channels)
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2], 1).astype('float32')

# To load images to features and labels
def load_images_to_data(image_label, image_directory, features_data, label_data):
    list_of_files = os.listdir(image_directory)
    for file in list_of_files:
        image_file_name = os.path.join(image_directory, file)
        if ".png" in image_file_name:
            img = Image.open(image_file_name).convert("L")
            img = np.resize(img, (28,28,1))
            im2arr = np.array(img)
            im2arr = im2arr.reshape(1,28,28,1)
            features_data = np.append(features_data, im2arr, axis=0)
            label_data = np.append(label_data, [image_label], axis=0)
    return features_data, label_data

# Load your own images to training and test data
X_train, y_train = load_images_to_data('1', '/content/01', X_train, y_train)
X_test, y_test = load_images_to_data('1', '/content/01', X_test, y_test)

# normalize inputs from 0-255 to 0-1
X_train/=255
X_test/=255

# one hot encode
number_of_classes = 10
y_train = np_utils.to_categorical(y_train, number_of_classes)
y_test = np_utils.to_categorical(y_test, number_of_classes)

# create model
model = Sequential()
model.add(Conv2D(32, (5, 5), input_shape=(X_train.shape[1], X_train.shape[2], 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(number_of_classes, activation='softmax'))

# Compile model
model.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['accuracy'])

# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=1, batch_size=200)

img = Image.open('/content/01/userlmn_01.png').convert("L")
plt.imshow(img)
img = np.resize(img, (28,28,1))

im2arr = np.array(img)
im2arr = im2arr.reshape(1,28,28,1)

# y_pred = model.predict_classes(im2arr)

predict_x=model.predict(im2arr) 
classes_x=np.argmax(predict_x,axis=1)

print(classes_x)

相关问题