使用 keras-retinanet 训练的记录

 

数据样本图集 设置三个类别 巴士 bus 车轮wheel 刹车盘shache


识别的结果

使用 keras-retinanet 训练的记录_第1张图片使用 keras-retinanet 训练的记录_第2张图片

 

使用 keras-retinanet 训练的记录_第3张图片

这张识别错误

使用 keras-retinanet 训练的记录_第4张图片

 

 

使用 keras-retinanet 训练的记录_第5张图片使用 keras-retinanet 训练的记录_第6张图片使用 keras-retinanet 训练的记录_第7张图片

这张错误识别wheel

使用 keras-retinanet 训练的记录_第8张图片

 

这张两张范围有点大

使用 keras-retinanet 训练的记录_第9张图片

 

使用 keras-retinanet 训练的记录_第10张图片

尽管有这样那样的问题,流程是通了,做个记录。

记录开始:

官方
https://github.com/fizyr/keras-retinanet

 

我们准备号了csv格式的数据之后,我们就可以进行训练了。

满怀期待的运行

 retinanet-train csv annotations.csv classes.csv


遇到报错

......
Epoch 1/50
Exception in thread Thread-2:
Traceback (most recent call last):
  File "d:\program files\python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "d:\program files\python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "d:\program files\python37\lib\site-packages\keras\utils\data_utils.py", line 565, in _run
    with closing(self.executor_fn(_SHARED_SEQUENCES)) as executor:
  File "d:\program files\python37\lib\site-packages\keras\utils\data_utils.py", line 548, in 
    initargs=(seqs,))
  File "d:\program files\python37\lib\multiprocessing\context.py", line 119, in Pool
    context=self.get_context())
  File "d:\program files\python37\lib\multiprocessing\pool.py", line 176, in __init__
    self._repopulate_pool()
  File "d:\program files\python37\lib\multiprocessing\pool.py", line 241, in _repopulate_pool
    w.start()
  File "d:\program files\python37\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "d:\program files\python37\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "d:\program files\python37\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
    reduction.dump(process_obj, to_child)
  File "d:\program files\python37\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle generator objects

Traceback (most recent call last):
  File "", line 1, in 
  File "d:\program files\python37\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "d:\program files\python37\lib\multiprocessing\spawn.py", line 115, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input

错误在所难免,搜索半天没有什么有意义的结果

请教官方说是线程的问题(windows系统对线程支持不好??)

看训练的源码有参数可以禁止使用多线程

# Fit generator arguments
    parser.add_argument('--workers', help='Number of multiprocessing workers. To disable multiprocessing, set workers to 0', type=int, default=1)

--workers设置0禁止多线程

后来请教这篇的博主文章也有介绍,谢谢博主啦。
https://blog.csdn.net/mingyang_wang/article/details/86022825

 


改成这样

retinanet-train --workers 0 csv annotations.csv classes.csv


不再报错


这回聪明一些,看看源码,修改其他参数

    parser.add_argument('--gpu',              help='Id of the GPU to use (as reported by nvidia-smi).')
    parser.add_argument('--multi-gpu',        help='Number of GPUs to use for parallel processing.', type=int, default=0)
    parser.add_argument('--multi-gpu-force',  help='Extra flag needed to enable (experimental) multi-gpu support.', action='store_true')
    parser.add_argument('--epochs',           help='Number of epochs to train.', type=int, default=50)
    parser.add_argument('--steps',            help='Number of steps per epoch.', type=int, default=10000)


测试训练参数

retinanet-train --gpu 00000000:01:00.0 --multi-gpu 1 --steps 100 --workers 0 csv annotations.csv classes.csv


经过个把个小时,训练结束snapshots下会生成模型文件的结果(.h5文件)
//CPU训练效率太差,我的环境估计差不多是GPU6-7倍的时间

我们可以利用这个.h5文件来看看识别的效果

源码示例:

# import keras
import keras

# import keras_retinanet
from keras_retinanet import models
from keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image
from keras_retinanet.utils.visualization import draw_box, draw_caption
from keras_retinanet.utils.colors import label_color

# import miscellaneous modules
import matplotlib.pyplot as plt
import cv2
import os
import numpy as np
import time

# set tf backend to allow memory to grow, instead of claiming everything
import tensorflow as tf

def get_session():
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    return tf.Session(config=config)

# use this environment flag to change which GPU to use
#os.environ["CUDA_VISIBLE_DEVICES"] = "1"

# set the modified tf session as backend in keras
keras.backend.tensorflow_backend.set_session(get_session())

# adjust this to point to your downloaded/trained model
# models can be downloaded here: https://github.com/fizyr/keras-retinanet/releases
model_path = os.path.join('..', 'snapshots', 'resnet50_coco_best_v2.1.0.h5')
model_path ="resnet50_csv_50.h5"
#model_path 是训练模型结果的路径
# load retinanet model
model = models.load_model(model_path, backbone_name='resnet50')

# if the model is not converted to an inference model, use the line below
# see: https://github.com/fizyr/keras-retinanet#converting-a-training-model-to-inference-model
#这个地方需要注意转换一下
model = models.convert_model(model)

print(model.summary())

# load label to names mapping for visualization purposes
labels_to_names = {0: 'bus', 1: 'shache',2:'wheel'}

# load image
#image = read_image_bgr('385.jpg')
#image = read_image_bgr('cc4.jpg')
image = read_image_bgr('sc6.jpg')
# copy to draw on
draw = image.copy()
draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

# preprocess image for network
image = preprocess_image(image)
image, scale = resize_image(image)

# process image
start = time.time()
boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
print("processing time: ", time.time() - start)

# correct for image scale
boxes /= scale
print(boxes)
print(scores)
print(labels)
# visualize detections
for box, score, label in zip(boxes[0], scores[0], labels[0]):

    # scores are sorted so we can break
    #原来是0.5 由于自己的数据量和训练次数太少,适当放宽了要求
    if score < 0.33:
        break

    color = label_color(label)

    b = box.astype(int)
    draw_box(draw, b, color=color)

    caption = "{} {:.3f}".format(labels_to_names[label], score)
    draw_caption(draw, b, caption)

plt.figure(figsize=(15, 15))
plt.axis('off')
plt.imshow(draw)
plt.show()


while(1):
    time.sleep(1)
    print(1)

 

运行这段python就可以看到我们识别的结果了。

你可能感兴趣的:(深度学习,python)