如何在识别卷积神经网络的图像中添加层
How do I add layers to my image recognizing convolutional neural network
我正在尝试构建一个卷积神经网络来检测情绪(因此是一个简单的图像识别 ai)。到目前为止,我已经能够将我的图像调整为 32、32 大小,并将每个像素的 RGB 值添加到列表中。我现在应该如何进行?我一直在尝试添加一个卷积层,但在实现时遇到了一些问题,我只是不确定隐藏层的实际构造应该是什么样子。
使用 google colab 和 MNIST 数据集回答示例。
第一步:
- 进行导入,获取数据集,然后缩放并拆分为训练和测试。
- 定义模型(我使用的是我发现非常好的模型,更简单的模型也可以)
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from google.colab import files
from keras.preprocessing import image
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('acc')>0.998):
print("\nReached 99.8% accuracy so cancelling training!")
self.model.stop_training = True
callbacks = myCallback()
mnist = tf.keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
training_images, test_images = training_images/255.0, test_images/255.0
training_images = training_images.reshape(60000, 28, 28, 1)
test_images = test_images.reshape(10000, 28, 28, 1)
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.20),
tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding = "same"),
tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding = "same"),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding = "same"),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Dense(10, activation='softmax')
])
print (model.summary())
第 2 步:
编译并训练模型(训练时也使用测试集进行评估)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(training_images, training_labels, epochs=20,callbacks =[callbacks])
test_loss = model.evaluate(test_images, test_labels)
可选:
使用您自己的图像进行测试。此上传部分特定于 google colab。
uploaded = files.upload()
for fn in uploaded.keys():
path = '/content/' + fn
img = image.load_img(path, target_size=(28, 28),color_mode = "grayscale")
在我们预测之前,需要进行一些缩放和调整才能制作测试图片 "MNIST"。
im2arr = np.array(img)
im2arr = im2arr-255.0
im2arr = im2arr*-1
image = im2arr
im2arr_scale = im2arr/255.0
im2arr = im2arr.reshape(1,28,28,1)
im2arr_scale = im2arr_scale.reshape(1,28,28,1)
y_pred = model.predict(im2arr)
y_pred2 = model.predict(im2arr_scale)
print("non scaled : " + str(np.argmax(y_pred)) + " probability: " + str(y_pred[0][np.argmax(y_pred)]))
print("scaled: " + str(np.argmax(y_pred2)) + " probability: " + str(y_pred2[0][np.argmax(y_pred2)]))
print (y_pred)
print (y_pred2)
绘制测试图像
plt.grid(False)
plt.gray()
plt.axis('off')
plt.imshow(image)
plt.show()
我正在尝试构建一个卷积神经网络来检测情绪(因此是一个简单的图像识别 ai)。到目前为止,我已经能够将我的图像调整为 32、32 大小,并将每个像素的 RGB 值添加到列表中。我现在应该如何进行?我一直在尝试添加一个卷积层,但在实现时遇到了一些问题,我只是不确定隐藏层的实际构造应该是什么样子。
使用 google colab 和 MNIST 数据集回答示例。
第一步: - 进行导入,获取数据集,然后缩放并拆分为训练和测试。 - 定义模型(我使用的是我发现非常好的模型,更简单的模型也可以)
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from google.colab import files
from keras.preprocessing import image
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('acc')>0.998):
print("\nReached 99.8% accuracy so cancelling training!")
self.model.stop_training = True
callbacks = myCallback()
mnist = tf.keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
training_images, test_images = training_images/255.0, test_images/255.0
training_images = training_images.reshape(60000, 28, 28, 1)
test_images = test_images.reshape(10000, 28, 28, 1)
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.20),
tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding = "same"),
tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding = "same"),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding = "same"),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Dense(10, activation='softmax')
])
print (model.summary())
第 2 步: 编译并训练模型(训练时也使用测试集进行评估)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(training_images, training_labels, epochs=20,callbacks =[callbacks])
test_loss = model.evaluate(test_images, test_labels)
可选: 使用您自己的图像进行测试。此上传部分特定于 google colab。
uploaded = files.upload()
for fn in uploaded.keys():
path = '/content/' + fn
img = image.load_img(path, target_size=(28, 28),color_mode = "grayscale")
在我们预测之前,需要进行一些缩放和调整才能制作测试图片 "MNIST"。
im2arr = np.array(img)
im2arr = im2arr-255.0
im2arr = im2arr*-1
image = im2arr
im2arr_scale = im2arr/255.0
im2arr = im2arr.reshape(1,28,28,1)
im2arr_scale = im2arr_scale.reshape(1,28,28,1)
y_pred = model.predict(im2arr)
y_pred2 = model.predict(im2arr_scale)
print("non scaled : " + str(np.argmax(y_pred)) + " probability: " + str(y_pred[0][np.argmax(y_pred)]))
print("scaled: " + str(np.argmax(y_pred2)) + " probability: " + str(y_pred2[0][np.argmax(y_pred2)]))
print (y_pred)
print (y_pred2)
绘制测试图像
plt.grid(False)
plt.gray()
plt.axis('off')
plt.imshow(image)
plt.show()