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

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.

主站蜘蛛池模板: 阿鲁科尔沁旗| 株洲市| 大港区| 吉水县| 神池县| 昭平县| 合肥市| 留坝县| 乌拉特中旗| 儋州市| 府谷县| 锡林郭勒盟| 冷水江市| 介休市| 大田县| 京山县| 涟源市| 无棣县| 泰安市| 西平县| 信宜市| 饶平县| 萨嘎县| 广河县| 子洲县| 英德市| 武川县| 桐庐县| 都昌县| 泗洪县| 唐河县| 辽宁省| 社会| 红桥区| 治县。| 满洲里市| 肥西县| 青神县| 聊城市| 广灵县| 盐津县|