这次是P2项目——交通路牌识别,用到的是简单的卷积网络,2层的卷积层加上4层全连接层,因为用的数据集的图片大小是32x32的,所以不用很复杂的神经网络。
数据地址在这里:https://s3-us-west-1.amazonaws.com/udacity-selfdrivingcar/traffic-signs-data.zip
直接粘贴到迅雷下载就好了。
下载好后解压是有3个文件,test.p train.p valid.p
我这次做到的准确率是93%,网上还有大神做到了98%,https://github.com/kenshiro-o/CarND-Traffic-Sign-Classifier-Project,这是他的github,大家可以看看。
下面开始我的代码
首先先读取数据
# Load pickled data
import pickle
# TODO: Fill this in based on where you saved the training and testing data
training_file = 'train.p'
validation_file='valid.p'
testing_file = 'test.p'
with open(training_file, mode='rb') as f:
train = pickle.load(f)
with open(validation_file, mode='rb') as f:
valid = pickle.load(f)
with open(testing_file, mode='rb') as f:
test = pickle.load(f)
X_train, y_train = train['features'], train['labels']
X_valid, y_valid = valid['features'], valid['labels']
X_test, y_test = test['features'], test['labels']
然后分析数据
### Replace each question mark with the appropriate value.
### Use python, pandas or numpy methods rather than hard coding the results
# TODO: Number of training examples
n_train = len(X_train)
# TODO: Number of validation examples
n_validation = len(X_valid)
# TODO: Number of testing examples.
n_test = len(X_test)
# TODO: What's the shape of an traffic sign image?
image_shape = X_train[0].shape
# TODO: How many unique classes/labels there are in the dataset.
n_classes = len(set(y_train))
print("Number of training examples =", n_train)
print("Number of testing examples =", n_test)
print("Image data shape =", image_shape)
print("Number of classes =", n_classes)
可以看到train有3万多张图片 大小是32x32x3 总共有43种分类
下面是随机show一张图片和分析train,valid,test中各各类别图片的数量
### Data exploration visualization code goes here.
### Feel free to use as many code cells as needed.
import matplotlib.pyplot as plt
from collections import Counter
import numpy as np
# Visualizations will be shown in the notebook.
%matplotlib inline
index=np.random.randint(n_train)
plt.imshow(X_train[index],cmap='gray')
print(y_train[index])
sign_count_test = Counter(y_train)
range_x = np.array(range(n_classes))
range_y = [sign_count_test[i] for i in range_x]
plt.figure(figsize=(9,5))
plt.bar(range_x,range_y)
plt.xticks(list(range(n_classes)))
plt.xlabel("class")
plt.ylabel("numbers")
plt.ti