运行Keras版本的Faster R-CNN

时间:2022-03-15 14:01:59

Keras版本的Faster R-CNN源码下载地址:https://github.com/yhenon/keras-frcnn
下载以后,用PyCharm打开(前提是已经安装了Tensorflow-gpu和Keras),打开以后可以看到项目的结构:

运行Keras版本的Faster R-CNN

修改requirements.txt,设置Keras到已安装的版本,如

Keras==2.0.8

建议版本不要太高,否则会出现错误:

TypeError: softmax() got an unexpected keyword argument 'axis'

然后看看需要运行的文件train_frcnn.py的参数相关代码:

parser.add_option("-p", "--path", dest="train_path", help="Path to training data.")
parser.add_option("-o", "--parser", dest="parser", help="Parser to use. One of simple or pascal_voc",
                default="pascal_voc")
parser.add_option("-n", "--num_rois", type="int", dest="num_rois", help="Number of RoIs to process at once.", default=32)
parser.add_option("--network", dest="network", help="Base network to use. Supports vgg or resnet50.", default='vgg')
parser.add_option("--hf", dest="horizontal_flips", help="Augment with horizontal flips in training. (Default=false).", action="store_true", default=False)
parser.add_option("--vf", dest="vertical_flips", help="Augment with vertical flips in training. (Default=false).", action="store_true", default=False)
parser.add_option("--rot", "--rot_90", dest="rot_90", help="Augment with 90 degree rotations in training. (Default=false).",
                  action="store_true", default=False)
parser.add_option("--num_epochs", type="int", dest="num_epochs", help="Number of epochs.", default=2000)
parser.add_option("--config_filename", dest="config_filename", help=
                "Location to store all the metadata related to the training (to be used when testing).",
                default="config.pickle")
parser.add_option("--output_weight_path", dest="output_weight_path", help="Output path for weights.", default='./model_frcnn.hdf5')
parser.add_option("--input_weight_path", dest="input_weight_path", help="Input path for weights. If not specified, will try to load default weights provided by keras.")

原来的代码默认的网络是Resnet50,这里改成了VGG。

VOC2007下载地址:https://pjreddie.com/projects/pascal-voc-dataset-mirror/

下载以后解压。由于只下载了VOC2007,需要把pascal_voc_parser.py的语句:

data_paths = [os.path.join(input_path,s) for s in ['VOC2007', 'VOC2012']]

中的VOC2012删掉,否则会报错。

要运行train_frcnn.py还需要下载Resnet50或者VGG的权重文件:

Resnet50下载地址:https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5

VGG下载地址:https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5

下载以后解压到相应目录,就可以输入命令运行了:

python train_frcnn.py -p D:\PythonWorkSpace\VOC2007\VOCdevkit --input_weight_path D:\PythonWorkSpace\Models

其中-p后面是VOC2007的路径,--input_weight_path是VGG权重文件的路径。

运行的画面:

运行Keras版本的Faster R-CNN