官术网_书友最值得收藏!

Using Keras to classify images of objects

With Keras, it's easy to create neural nets, but it's also easy to download test datasets. Let's try to use the CIFAR-10 (Canadian Institute For Advanced Research, https://www.cs.toronto.edu/~kriz/cifar.html) dataset instead of MNIST. It consists of 60,000 32x32 RGB images, divided into 10 classes of objects, namely: airplanes, automobiles, birds, cats, deers, dogs, frogs, horses, ships, and trucks:

  1. We'll import CIFAR-10 in the same way as we did MNIST:
from keras.datasets import cifar10
from keras.layers.core import Dense, Activation
from keras.models import Sequential
from keras.utils import np_utils
  1. Then, we'll split the data into 50,000 training images and 10,000 testing images. Once again, we need to reshape the image to a one-dimensional array. In this case, each image has 3 color channels (red, green, and blue) of 32x32 pixels, hence 3 x32x3 = 3072:
(X_train, Y_train), (X_test, Y_test) = cifar10.load_data() 

X_train = X_train.reshape(50000, 3072) X_test = X_test.reshape(10000, 3072)
classes = 10
Y_train = np_utils.to_categorical(Y_train, classes)
Y_test = np_utils.to_categorical(Y_test, classes)

input_size = 3072
batch_size = 100
epochs = 100
  1. This dataset is more complex than MNIST and the network has to reflect that. Let's try to use a network with three hidden layers and more hidden neurons than the previous example:
model = Sequential([
Dense(1024, input_dim=input_size),
Activation('relu'),
Dense(512),
Activation('relu'),
Dense(512),
Activation('sigmoid'),
Dense(classes),
Activation('softmax')
])
  1. We'll run the training with one additional parameter, validation_data=(X_test, Y_test), which will use the test data as a validation set:
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='sgd')
model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, validation_data=(X_test, Y_test), verbose=1)
  1. Next, we'll visualize the weights of 100 random neurons from the first layer. We'll reshape the weights to 32x32 arrays and we'll compute the mean value of the 3 color channels to produce a grayscale image:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.gridspec as gridspec
import numpy
import random

fig = plt.figure()
outer_grid = gridspec.GridSpec(10, 10, wspace=0.0, hspace=0.0)

weights = model.layers[0].get_weights()

w = weights[0].T

for i, neuron in enumerate(random.sample(range(0, 1023), 100)):
ax = plt.Subplot(fig, outer_grid[i])
ax.imshow(numpy.mean(numpy.reshape(w[i], (32, 32, 3)), axis=2), cmap=cm.Greys_r)
ax.set_xticks([])
ax.set_yticks([])
fig.add_subplot(ax)

plt.show()

 If everything goes as planned, we'll see the result in the following image:

Composite figure with the weights of 100 random neurons from the first layer. Unlike MNIST, there is no clear indication of what the neurons might have learned

Compared to the MNIST example, training takes much longer. But by the end, we'll have about 60% training accuracy and only about 51% test accuracy, despite the larger network. This is due to the higher complexity of the data. The accuracy of the training keeps increasing, but the validation accuracy plateaus at some point, showing that the network starts to overfit and to saturate some parameters.

主站蜘蛛池模板: 宝坻区| 工布江达县| 哈密市| 综艺| 福海县| 英德市| 中牟县| 塔河县| 黔江区| 大冶市| 涡阳县| 隆德县| 沾化县| 沈阳市| 中方县| 蓬溪县| 鄂尔多斯市| 玛多县| 阿拉善右旗| 山阳县| 吉安市| 奉节县| 屯昌县| 固阳县| 万荣县| 景德镇市| 磐安县| 台安县| 珲春市| 盐边县| 建始县| 清远市| 周宁县| 兴化市| 辽宁省| 任丘市| 孝昌县| 蛟河市| 博客| 湖南省| 原平市|