1. 程式人生 > >執行Keras版本的Faster R-CNN

執行Keras版本的Faster R-CNN

Keras版本的Faster R-CNN原始碼下載地址:https://github.com/yhenon/keras-frcnn
下載以後,用PyCharm開啟(前提是已經安裝了Tensorflow-gpu和Keras),開啟以後可以看到專案的結構:

修改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權重檔案的路徑。

執行的畫面: