- Python Deep Learning
- Ivan Vasilev Daniel Slater Gianmario Spacagna Peter Roelants Valentino Zocca
- 453字
- 2021-07-02 14:31:10
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:
- 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
- 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
- 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')
])
- 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)
- 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:

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.
- Mastering ServiceStack
- 基于Java技術的Web應用開發(fā)
- Java加密與解密的藝術(第2版)
- Data Analysis with Stata
- Building Minecraft Server Modifications
- HTML5入門經典
- Mathematica Data Analysis
- Java程序員面試筆試寶典(第2版)
- LabVIEW虛擬儀器程序設計從入門到精通(第二版)
- 深入實踐Kotlin元編程
- GitHub入門與實踐
- Xcode 6 Essentials
- Illustrator CS6設計與應用任務教程
- Nagios Core Administration Cookbook(Second Edition)
- 單片機原理及應用技術