Skip to content

Commit

Permalink
训练模型,预测模型控制小车
Browse files Browse the repository at this point in the history
  • Loading branch information
Timthony committed Oct 2, 2018
1 parent 5e9b182 commit 08a4cb4
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 55 deletions.
6 changes: 3 additions & 3 deletions process_img_to_npz.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ def process_img(img_path, key):
print('prcess error')

# 去掉第0位的全零图像数组,全零图像数组是 train_imgs = np.zeros([1,120,160,3]) 初始化生成的
train_imgs = train_imgs[1:, :]
train_imgs = train_imgs[1:, :] # 从第一位开始取,因为第0位是初始化的
train_labels = train_labels[1:, :]
file_name = str(int(time()))
directory = "training_data_npz"
file_name = str(int(time())) # 文件名直接取时间
directory = "training_data_npz" # 文件夹

if not os.path.exists(directory):
os.makedirs(directory)
Expand Down
12 changes: 12 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
keep_prob = 0.5
learning_rate = 0.0001
nb_epoch = 100
samples_per_epoch = 3000
batch_size = 40

print('keep_prob = ', keep_prob)
print('learning_rate = ', learning_rate)
print('nb_epoch = ', nb_epoch)
print('samples_per_epoch = ', samples_per_epoch)
print('batch_size = ', batch_size)
print('-' * 30)
2 changes: 1 addition & 1 deletion train_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS = 120, 160, 3
INPUT_SHAPE = (IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS)
(

def load_data():
"""
Load training data and split it into training and validation set
Expand Down
2 changes: 1 addition & 1 deletion zth_collect_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 收集数据,赛道照片和对应的前、后、左、右、
# 收集数据,赛道照片和对应的前、后、左、右、4停
# 对应图片和相应的标签值
import io
import zth_car_control
Expand Down
155 changes: 155 additions & 0 deletions zth_drive.py
Original file line number Diff line number Diff line change
@@ -1 +1,156 @@
# 利用训练好的模型操作小车,实现自动驾驶
# 树莓派上跑,加上训练出来的模型,不会立刻能跑起来,需要时间读入模型
# 自动驾驶模型真实道路模拟行驶
# 多线程处理
import os
import io
import glob
import time
import threading
#import picamera.array
#import picamera
from PIL import Image
import numpy as np

import zth_car_control
from keras.models import load_model
import tensorflow as tf

# 找到最大的可能性
def get_max_prob_num(predictions_array):
prediction_edit = np.zeros([1, 5])
for i in range(0, 5):
if predictions_array[0][i] == predictions_array.max():
prediction_edit[0][i] = 1
return i
return 2
# 根据神经网络预测的结果来控制小车
def control_car(action_num):
if action_num == 0:
print("Left")
zth_car_control.car_turn_left()
time.sleep(0.25)
elif action_num == 1:
print("Right")
zth_car_control.car_turn_right()
time.sleep(0.25)
elif action_num == 2:
print("Forward")
zth_car_control.car_move_forward()
elif action_num == 3:
zth_car_control.car_move_backward()
print("Backward")
else:
zth_car_control.car_stop()
print('stop')


# 利用神经网络的模型预测图像
class ImageProcessor(threading.Thread):
def __init__(self, owner):
super(ImageProcessor, self).__init__()
self.stream = io.BytesIO()
self.event = threading.Event()
self.terminated = False
self.owner = owner
self.start()
def run(self):
global latest_time, model, graph
while not self.terminated:
if self.event.wait(1):
try:
self.stream.seek(0)
image = Image.open(self.stream)
image_np = np.array(image)
camera_data_array = np.expand_dims(image_np, axis=0)
current_time = time.time()
if current_time > latest_time:
if current_time - latest_time > 1:
print("*"*30)
print(current_time-latest_time)
print("*"*30)
latest_time = current_time
with graph.as_default():
prediction_array = model.predict(camera_data_array, batch_size=20, verbode=1)
# 输出的是概率,比如[0.1,0.1,0.8,0.05,0.04]
print(prediction_array)
action_num = get_max_prob_num(prediction_array)
control_car(action_num)
finally:
self.stream.seek(0)
self.stream.truncate()
self.event.clear()
with self.owner.lock:
self.owner.pool.append(self)

# 多线程处理
class ProcessOutput(object):
def __init__(self):
self.done = False
self.lock = threading.Lock()
self.pool = [ImageProcessor(self) for i in range(4)]
self.processor = None
def write(self, buf):
if buf.startswith(b'\xff\xd8'):
if self.processor:
self.processor.event.set()
with self.lock:
if self.pool:
self.processor = self.pool.pop()
else:
self.processor = None
if self.processor:
self.processor.stream.write(buf)

def flush(self):
if self.processor:
with self.lock:
self.pool.append(self.processor)
self.processor = None
while True:
with self.lock:
try:
proc = self.pool.pop()
except IndexError:
pass
proc.terminated = True
proc.join()











def main():
"""获取数据,然后预测获得的数据,编辑数据,控制车行驶"""
global model, graph
model_loaded = glob.glob('model/*.h5') # glob.glob()匹配指定的文件
for single_mod in model_loaded:
model = load_model(single_mod)
graph = tf.get_default_graph()

try:
with picamera.Picamera(resolution=(160, 120)) as camera:
time.sleep(2)
output = ProcessOutput()












if __name__ == '__main__':
global latest_time
latest_time = time.time()
main()
23 changes: 11 additions & 12 deletions zth_process_img.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
# 将图片处理为npz格式

# 自动驾驶模型真实道路模拟行驶
import os
import numpy as np
import matplotlib.image as mpimg
from time import time
import math
from PIL import Image

CHUNK_SIZE = 256

CHUNK_SIZE = 128 # 将图片压缩,每256个做一次处理



# 本段不一样
def process_img(img_path, key):

print(img_path, key)
image = Image.open(img_path)
image_array = np.array(image)
image_array = np.expand_dims(image_array, axis=0)


image_array = np.expand_dims(image_array, axis=0) # 增加一个维度


#image_array = mpimg.imread(img_path)
Expand All @@ -44,21 +42,22 @@ def process_img(img_path, key):

if __name__ == '__main__':
path = "training_data"
files = os.listdir(path) # 将该路径下的文件名都存入列表
turns = int(math.ceil(len(files) / CHUNK_SIZE))
files = os.listdir(path) # 将该路径下的文件名都存入列表
turns = int(math.ceil(len(files) / CHUNK_SIZE)) # 取整,把所有图片分为这么多轮,每CHUNK_SIZE张一轮
print("number of files: {}".format(len(files)))
print("turns: {}".format(turns))

for turn in range(0, turns):
train_labels = np.zeros((1, 5), 'float')
train_imgs = np.zeros([1, 120, 160, 3])
train_labels = np.zeros((1, 5), 'float') # 初始化标签数组
train_imgs = np.zeros([1, 120, 160, 3]) # 初始化图像数组

CHUNK_files = files[turn * CHUNK_SIZE: (turn + 1) * CHUNK_SIZE]
CHUNK_files = files[turn * CHUNK_SIZE: (turn + 1) * CHUNK_SIZE] # 取出当前这一轮图片
print("number of CHUNK files: {}".format(len(CHUNK_files)))
for file in CHUNK_files:
# 不是文件夹,并且是jpg文件
if not os.path.isdir(file) and file[len(file) - 3:len(file)] == 'jpg':
try:
key = int(file[0])
key = int(file[0]) # 取第一个字符为key
image_array, label_array = process_img(path + "/" + file, key)
train_imgs = np.vstack((train_imgs, image_array))
train_labels = np.vstack((train_labels, label_array))
Expand Down
Loading

0 comments on commit 08a4cb4

Please sign in to comment.