# 使用深度学习自动识别限速标志：这里有一份Keras和TensorFlow教程

「雷克世界」编译：嗯~阿童木呀、多啦A亮

import os

import skimage.data

directories = [d for d in os.listdir(data_dir)

labels = []

images = []

for d in directories:

for f in os.listdir(label_dir) if f.endswith(".ppm")]

for f in file_names:

labels.append(int(d))

return images, labels

import matplotlib

import matplotlib.pyplot as plt

def display_images_and_labels(images, labels):

"""Display the first image of each label."""

unique_labels = set(labels)

plt.figure(figsize=(15, 15))

i = 1

for label in unique_labels:

# Pick the first image for each label.

image = images[labels.index(label)]

plt.subplot(8, 8, i) # A grid of 8 rows x 8 columns

plt.axis(&#39;off&#39;)

plt.title("Label ()".format(label, labels.count(label)))

i += 1

_ = plt.imshow(image)

plt.show()

display_images_and_labels(images, labels)

for image in images[:5]:

print("shape: , min: , max: ".format(image.shape, image.min(), image.max()))

Output:

shape: (21, 22, 3), min: 27, max: 248

shape: (23, 23, 3), min: 10, max: 255

shape: (43, 42, 3), min: 11, max: 254

shape: (61, 58, 3), min: 3, max: 255

shape: (28, 27, 3), min: 8, max: 65

import skimage.transform

import numpy as np

y = np.array(labels)

X = np.array(images64)

num_categories = 6

y = to_categorical(y, num_categories)

CNN架构，来源http://cs231n.github.io/convolutional-netw

from keras.models import Model

from keras.layers import Dense, Flatten, Input, Dropout

from keras.layers import Convolution2D, MaxPooling2D

from sklearn.model_selection import train_test_split

inputs = Input(shape=(64,64,3))

x = Convolution2D(32, 4,4, border_mode=&#39;same&#39;, activation=&#39;relu&#39;)(inputs)

x = Convolution2D(32, 4,4, border_mode=&#39;same&#39;, activation=&#39;relu&#39;)(x)

x = MaxPooling2D(pool_size=(2,2))(x)

x = Dropout(0.25)(x)

# one more block

x = Convolution2D(64, 4, 4, border_mode=&#39;same&#39;, activation=&#39;relu&#39;)(x)

x = MaxPooling2D(pool_size=(2, 2))(x)

x = Dropout(0.25)(x)

x = Flatten()(x)

# fully connected, 256 nodes

x = Dense(256, activation=&#39;relu&#39;)(x)

x = Dropout(0.50)(x)

# softmax activation, 6 categories

predictions = Dense(6, activation=&#39;softmax&#39;)(x)

model = Model(input=inputs, output=predictions)

model.compile(optimizer=&#39;rmsprop&#39;,

loss=&#39;categorical_crossentropy&#39;,

metrics=[&#39;accuracy&#39;])

model.fit(X_train, y_train, nb_epoch=50, batch_size=100)

train_loss, train_accuracy = model.evaluate(X_train,y_train, batch_size=32)

train_loss, train_accuracy

test_loss, test_accuracy = model.evaluate(X_test, y_test, batch_size=32)

test_loss, test_accuracy

import random

random.seed(3)

sample_indexes = random.sample(range(len(X_test)), 10)

sample_images = [X_test[i] for i in sample_indexes]

sample_labels = [y_test[i] for i in sample_indexes]

#get the indices of the array using argmax

ground_truth = np.argmax(sample_labels, axis=1)

X_sample = np.array(sample_images)

prediction = model.predict(X_sample)

predicted_categories = np.argmax(prediction, axis=1)

# Display the predictions and the ground truth visually.

def display_prediction (images, true_labels, predicted_labels):

fig = plt.figure(figsize=(10, 10))

for i in range(len(true_labels)):

truth = true_labels[i]

prediction = predicted_labels[i]

plt.subplot(5, 2,1+i)

plt.axis(&#39;off&#39;)

color=&#39;green&#39; if truth == prediction else &#39;red&#39;

plt.text(80, 10, "Truth: \nPrediction: ".format(truth, prediction),

fontsize=12, color=color)

plt.imshow(images[i])

display_prediction(sample_images, ground_truth, predicted_categories)

• 发表于:
• 原文链接：http://kuaibao.qq.com/s/20171210A07RWZ00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。

2019-03-25

2018-11-05

2018-06-14

2018-05-31

2018-05-21

2018-04-23

2019-03-25