`VGG16`

## 2. 车辆转弯角度预测

### 2.1 数据集读取与预处理

```import os
import numpy as np
import matplotlib.pyplot as plt
from scipy import pi
import cv2
x = []
y = []
train_batch_pointer = 0
test_batch_pointer = 0
with open(DATA_FILE, 'r') as f:
for line in f:
image_name, angle = line.split()
image_path = os.path.join(DATA_FOLDER, image_name)
x.append(image_path)
angle_radians = float(angle) * (pi / 180)
y = np.array(y)```

```split_ratio = int(len(x) * 0.8)
train_x = x[:split_ratio]
train_y = y[:split_ratio]
test_x = x[split_ratio:]
test_y = y[split_ratio:]
fig = plt.figure(figsize=(10,7))
plt.hist(train_y,bins=50,histtype='step',color='r')
plt.hist(test_y,bins=50,histtype='step',color='b')
plt.title("Steering Wheel angle in train and test")
plt.xlabel("Angle")
plt.ylabel("Bin count")
plt.show()```

```x = []
y = []
for i in range(12000):
# 删除图像中前100行中的像素
im = im[100:, :, :]
im = vgg19.preprocess_input(im.reshape(1, im.shape[0], im.shape[1], 3))
vgg_im = vgg19_model.predict(im)
x.append(vgg_im)
y.append(train_y[i])
x1 = np.array(x)
x1 = x1.reshape(x1.shape[0], x1.shape[2], x1.shape[3], x1.shape[4])
y1 = np.array(y)```

### 2.2 模型构建预训练

```model = Sequential()
model.summary()```

```Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
flatten (Flatten)            (None, 28672)             0
_________________________________________________________________
dense (Dense)                (None, 512)               14680576
_________________________________________________________________
batch_normalization (BatchNo (None, 512)               2048
_________________________________________________________________
re_lu (ReLU)                 (None, 512)               0
_________________________________________________________________
dropout (Dropout)            (None, 512)               0
_________________________________________________________________
dense_1 (Dense)              (None, 128)               65664
_________________________________________________________________
batch_normalization_1 (Batch (None, 128)               512
_________________________________________________________________
dropout_1 (Dropout)          (None, 128)               0
_________________________________________________________________
dense_2 (Dense)              (None, 64)                8256
_________________________________________________________________
batch_normalization_2 (Batch (None, 64)                256
_________________________________________________________________
dropout_2 (Dropout)          (None, 64)                0
_________________________________________________________________
dense_3 (Dense)              (None, 16)                1040
_________________________________________________________________
batch_normalization_3 (Batch (None, 16)                64
_________________________________________________________________
dropout_3 (Dropout)          (None, 16)                0
_________________________________________________________________
dense_4 (Dense)              (None, 1)                 17
=================================================================
Total params: 14,758,433
Trainable params: 14,756,993
Non-trainable params: 1,440
_________________________________________________________________```

```model.compile(loss='mean_squared_error', optimizer='adam')
history = model.fit(x1/np.max(x1), y1,
batch_size=32,
epochs=10,
validation_split=0.1,
verbose=1)```

### 2.3 模型测试

```k=-450
model.predict(x1[k].reshape(1,4,14,512)/np.max(x1))
plt.subplot(211)
plt.imshow(cv2.cvtColor(im[100:,:,:], cv2.COLOR_BGR2RGB))
plt.title('Predicted angle: {}, actual angle:{}'.format(str(round(model.predict(x1[k].reshape(1,4,14,512)/np.max(x1))[0][0],2)), str(round(y1[k],2))))
k=-550
model.predict(x1[k].reshape(1,4,14,512)/np.max(x1))
plt.subplot(212)