重磅干货,第一时间送达
路面分类
01.RTK数据集
02.路面类型分类
训练数据文件夹结构
classes = os.listdir('training_data')
num_classes = len(classes)
batch_size = 32
validation_size = 0.2
img_size = 128
num_channels = 3
train_path='training_data'
data = dataset.read_train_sets(train_path, img_size, classes, validation_size=validation_size)
def adjust_gamma(image):
gamma = 0.5
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")
return cv2.LUT(image, table)
def increase_brightness(img, value):
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
lim = 255 - value
v[v > lim] = 255
v[v <= lim] += value
final_hsv = cv2.merge((h, s, v))
img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
return img
for fields in classes:
index = classes.index(fields)
print('Now going to read {} files (Index: {})'.format(fields, index))
path = os.path.join(train_path, fields, '*g')
files = glob.glob(path)
for fl in files:
image = cv2.imread(fl)
# Region Of Interest (ROI)
height, width = image.shape[:2]
newHeight = int(round(height/2))
image = image[newHeight-5:height-50, 0:width]
brght_img = increase_brightness(image, value=150)
shaded_img = adjust_gamma(image)
image = cv2.resize(image, (image_size, image_size),0,0, cv2.INTER_LINEAR)
image = image.astype(np.float32)
image = np.multiply(image, 1.0 / 255.0)
brght_img = cv2.resize(brght_img, (image_size, image_size),0,0, cv2.INTER_LINEAR)
brght_img = brght_img.astype(np.float32)
brght_img = np.multiply(brght_img, 1.0 / 255.0)
shaded_img = cv2.resize(shaded_img, (image_size, image_size),0,0, cv2.INTER_LINEAR)
shaded_img = shaded_img.astype(np.float32)
shaded_img = np.multiply(brght_img, 1.0 / 255.0)
if index == 0: #asphalt
images.append(image)
images.append(brght_img)
images.append(shaded_img)
elif index == 1: #paved
for i in range(3):
images.append(image)
images.append(brght_img)
images.append(shaded_img)
elif index == 2: #unpaved
for i in range(6):
images.append(image)
images.append(brght_img)
images.append(shaded_img)
def create_convolutional_layer(input,
num_input_channels,
conv_filter_size,
num_filters):
weights = create_weights(shape=[conv_filter_size, conv_filter_size, num_input_channels, num_filters])
biases = create_biases(num_filters)
layer = tf.nn.conv2d(input=input,
filter=weights,
strides=[1, 1, 1, 1],
padding='SAME')
layer += biases
layer = tf.nn.max_pool(value=layer,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME')
layer = tf.nn.relu(layer)
def create_flatten_layer(layer):
layer_shape = layer.get_shape()
num_features = layer_shape[1:4].num_elements()
layer = tf.reshape(layer, [-1, num_features])
return layer
def create_fc_layer(input,
num_inputs,
num_outputs,
use_relu=True):
weights = create_weights(shape=[num_inputs, num_outputs])
biases = create_biases(num_outputs)
layer = tf.matmul(input, weights) + biases
if use_relu:
layer = tf.nn.relu(layer)
return layer
layer_conv1 = create_convolutional_layer(input=x,
num_input_channels=num_channels,
conv_filter_size=filter_size_conv1,
num_filters=num_filters_conv1)
layer_conv2 = create_convolutional_layer(input=layer_conv1,
num_input_channels=num_filters_conv1,
conv_filter_size=filter_size_conv2,
num_filters=num_filters_conv2)
layer_conv3= create_convolutional_layer(input=layer_conv2,
num_input_channels=num_filters_conv2,
conv_filter_size=filter_size_conv3,
num_filters=num_filters_conv3)
layer_flat = create_flatten_layer(layer_conv3)
layer_fc1 = create_fc_layer(input=layer_flat,
num_inputs=layer_flat.get_shape()[1:4].num_elements(),
num_outputs=fc_layer_size,
use_relu=True)
layer_fc2 = create_fc_layer(input=layer_fc1,
num_inputs=fc_layer_size,
num_outputs=num_classes,
use_relu=False)
y_pred = tf.nn.softmax(layer_fc2,name='y_pred')
y_pred_cls = tf.argmax(y_pred, dimension=1)
session.run(tf.global_variables_initializer())
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=layer_fc2,
labels=y_true)
cost = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)
correct_prediction = tf.equal(y_pred_cls, y_true_cls)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
outputFile = sys.argv[2]
# Opening frames
cap = cv.VideoCapture(sys.argv[1])
vid_writer = cv.VideoWriter(outputFile, cv.VideoWriter_fourcc('M','J','P','G'), 15, (round(cap.get(cv.CAP_PROP_FRAME_WIDTH)),round(cap.get(cv.CAP_PROP_FRAME_HEIGHT))))
sess = tf.Session()
saver = tf.train.import_meta_graph('roadsurface-model.meta')
saver.restore(sess, tf.train.latest_checkpoint('./'))
graph = tf.get_default_graph()
y_pred = graph.get_tensor_by_name("y_pred:0")
x = graph.get_tensor_by_name("x:0")
y_true = graph.get_tensor_by_name("y_true:0")
y_test_images = np.zeros((1, len(os.listdir('training_data'))))
width = int(round(cap.get(cv.CAP_PROP_FRAME_WIDTH)))
height = int(round(cap.get(cv.CAP_PROP_FRAME_HEIGHT)))
newHeight = int(round(height/2))
while cv.waitKey(1) < 0:
hasFrame, images = cap.read()
finalimg = images
images = images[newHeight-5:height-50, 0:width]
images = cv.resize(images, (image_size, image_size), 0, 0, cv.INTER_LINEAR)
images = np.array(images, dtype=np.uint8)
images = images.astype('float32')
images = np.multiply(images, 1.0/255.0)
x_batch = images.reshape(1, image_size, image_size, num_channels)
feed_dict_testing = {x: x_batch, y_true: y_test_images}
result = sess.run(y_pred, feed_dict=feed_dict_testing)
outputs = [result[0,0], result[0,1], result[0,2]]
value = max(outputs)
index = np.argmax(outputs)
if index == 0:
label = 'Asphalt'
prob = str("{0:.2f}".format(value))
color = (0, 0, 0)
elif index == 1:
label = 'Paved'
prob = str("{0:.2f}".format(value))
color = (153, 102, 102)
elif index == 2:
label = 'Unpaved'
prob = str("{0:.2f}".format(value))
color = (0, 153, 255)
cv.rectangle(finalimg, (0, 0), (145, 40), (255, 255, 255), cv.FILLED)
cv.putText(finalimg, 'Class: ', (5,15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1)
cv.putText(finalimg, label, (70,15), cv.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)
cv.putText(finalimg, prob, (5,35), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1)
vid_writer.write(finalimg.astype(np.uint8))
03.路面质量分类
用于质量课程的培训数据文件夹结构
python trainAsphaltQuality.py
python trainPavedQuality.py
python trainUnpavedQuality.py
graph = tf.Graph()
graphAQ = tf.Graph()
graphPQ = tf.Graph()
graphUQ = tf.Graph()
04.模型恢复
with graph.as_default():
saver = tf.train.import_meta_graph('roadsurfaceType-model.meta')
y_pred = graph.get_tensor_by_name("y_pred:0")
x = graph.get_tensor_by_name("x:0")
y_true = graph.get_tensor_by_name("y_true:0")
y_test_images = np.zeros((1, len(os.listdir('training_data_type'))))
sess = tf.Session(graph = graph)
saver.restore(sess, tf.train.latest_checkpoint('typeCheckpoint/'))
with graphAQ.as_default():
saverAQ = tf.train.import_meta_graph('roadsurfaceAsphaltQuality-model.meta')
y_predAQ = graphAQ.get_tensor_by_name("y_pred:0")
xAQ = graphAQ.get_tensor_by_name("x:0")
y_trueAQ = graphAQ.get_tensor_by_name("y_true:0")
y_test_imagesAQ = np.zeros((1, len(os.listdir('training_data_asphalt_quality'))))
sessAQ = tf.Session(graph = graphAQ)
saverAQ.restore(sessAQ, tf.train.latest_checkpoint('asphaltCheckpoint/'))
with graphPQ.as_default():
saverPQ = tf.train.import_meta_graph('roadsurfacePavedQuality-model.meta')
y_predPQ = graphPQ.get_tensor_by_name("y_pred:0")
xPQ = graphPQ.get_tensor_by_name("x:0")
y_truePQ = graphPQ.get_tensor_by_name("y_true:0")
y_test_imagesPQ = np.zeros((1, len(os.listdir('training_data_paved_quality'))))
sessPQ = tf.Session(graph = graphPQ)
saverPQ.restore(sessPQ, tf.train.latest_checkpoint('pavedCheckpoint/'))
with graphUQ.as_default():
saverUQ = tf.train.import_meta_graph('roadsurfaceUnpavedQuality-model.meta')
y_predUQ = graphUQ.get_tensor_by_name("y_pred:0")
xUQ = graphUQ.get_tensor_by_name("x:0")
y_trueUQ = graphUQ.get_tensor_by_name("y_true:0")
y_test_imagesUQ = np.zeros((1, len(os.listdir('training_data_unpaved_quality'))))
sessUQ = tf.Session(graph = graphUQ)
saverUQ.restore(sessUQ, tf.train.latest_checkpoint('unpavedCheckpoint/'))
if index == 0: #Asphalt
label = 'Asphalt'
prob = str("{0:.2f}".format(value))
color = (0, 0, 0)
x_batchAQ = images.reshape(1, image_size, image_size, num_channels)
feed_dict_testingAQ = {xAQ: x_batchAQ, y_trueAQ: y_test_imagesAQ}
resultAQ = sessAQ.run(y_predAQ, feed_dict=feed_dict_testingAQ)
outputsQ = [resultAQ[0,0], resultAQ[0,1], resultAQ[0,2]]
valueQ = max(outputsQ)
indexQ = np.argmax(outputsQ)
if indexQ == 0: #Asphalt - Good
quality = 'Good'
colorQ = (0, 255, 0)
probQ = str("{0:.2f}".format(valueQ))
elif indexQ == 1: #Asphalt - Regular
quality = 'Regular'
colorQ = (0, 204, 255)
probQ = str("{0:.2f}".format(valueQ))
elif indexQ == 2: #Asphalt - Bad
quality = 'Bad'
colorQ = (0, 0, 255)
probQ = str("{0:.2f}".format(valueQ))
elif index == 1: #Paved
label = 'Paved'
prob = str("{0:.2f}".format(value))
color = (153, 102, 102)
x_batchPQ = images.reshape(1, image_size, image_size, num_channels)
feed_dict_testingPQ = {xPQ: x_batchPQ, y_truePQ: y_test_imagesPQ}
resultPQ = sessPQ.run(y_predPQ, feed_dict=feed_dict_testingPQ)
outputsQ = [resultPQ[0,0], resultPQ[0,1], resultPQ[0,2]]
valueQ = max(outputsQ)
indexQ = np.argmax(outputsQ)
if indexQ == 0: #Paved - Good
quality = 'Good'
colorQ = (0, 255, 0)
probQ = str("{0:.2f}".format(valueQ))
elif indexQ == 1: #Paved - Regular
quality = 'Regular'
colorQ = (0, 204, 255)
probQ = str("{0:.2f}".format(valueQ))
elif indexQ == 2: #Paved - Bad
quality = 'Bad'
colorQ = (0, 0, 255)
probQ = str("{0:.2f}".format(valueQ))
elif index == 2: #Unpaved
label = 'Unpaved'
prob = str("{0:.2f}".format(value))
color = (0, 153, 255)
x_batchUQ = images.reshape(1, image_size, image_size, num_channels)
feed_dict_testingUQ = {xUQ: x_batchUQ, y_trueUQ: y_test_imagesUQ}
resultUQ = sessUQ.run(y_predUQ, feed_dict=feed_dict_testingUQ)
outputsQ = [resultUQ[0,0], resultUQ[0,1]]
valueQ = max(outputsQ)
indexQ = np.argmax(outputsQ)
if indexQ == 0: #Unpaved - Regular
quality = 'Regular'
colorQ = (0, 204, 255)
probQ = str("{0:.2f}".format(valueQ))
elif indexQ == 1: #Unpaved - Bad
quality = 'Bad'
colorQ = (0, 0, 255)
probQ = str("{0:.2f}".format(valueQ))
cv.rectangle(finalimg, (0, 0), (145, 80), (255, 255, 255), cv.FILLED)
cv.putText(finalimg, 'Class: ', (5,15), cv.FONT_HERSHEY_DUPLEX, 0.5, (0,0,0))
cv.putText(finalimg, label, (70,15), cv.FONT_HERSHEY_DUPLEX, 0.5, color)
cv.putText(finalimg, prob, (5,35), cv.FONT_HERSHEY_DUPLEX, 0.5, (0,0,0))
cv.putText(finalimg, 'Quality: ', (5,55), cv.FONT_HERSHEY_DUPLEX, 0.5, (0,0,0))
cv.putText(finalimg, quality, (70,55), cv.FONT_HERSHEY_DUPLEX, 0.5, colorQ)
cv.putText(finalimg, probQ, (5,75), cv.FONT_HERSHEY_DUPLEX, 0.5, (0,0,0))
交流群
联系客服