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

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.

主站蜘蛛池模板: 通州市| 乐至县| 府谷县| 翁牛特旗| 东丽区| 阳东县| 皮山县| 景泰县| 永济市| 麦盖提县| 伊吾县| 修文县| 子洲县| 洛浦县| 东乡县| 花莲市| 东丰县| 九江县| 阜新| 滦南县| 常州市| 大城县| 十堰市| 满洲里市| 怀集县| 九台市| 调兵山市| 奉新县| 临安市| 汨罗市| 喀喇| 庆安县| 长葛市| 含山县| 繁峙县| 沁水县| 喀喇| 稻城县| 昭平县| 镇远县| 蒙阴县|