コード例 #1
0
ファイル: train.py プロジェクト: hangxzzz/faster-rcnn-tf2
#----------------------------------------------------#
if __name__ == "__main__":
    config = Config()
    #----------------------------------------------------#
    #   训练之前一定要修改NUM_CLASSES
    #   修改成所需要区分的类的个数+1。
    #----------------------------------------------------#
    NUM_CLASSES = 21
    #-----------------------------------------------------#
    #   input_shape是输入图片的大小,默认为800,800,3
    #   随着输入图片的增大,占用显存会增大
    #   视频上为600,600,3,多次训练测试后发现800,800,3更优
    #-----------------------------------------------------#
    input_shape = [800, 800, 3]

    model_rpn, model_all = get_model(config, NUM_CLASSES)
    #------------------------------------------------------#
    #   权值文件请看README,百度网盘下载
    #   训练自己的数据集时提示维度不匹配正常
    #   预测的东西都不一样了自然维度不匹配
    #------------------------------------------------------#
    base_net_weights = "model_data/voc_weights.h5"
    model_rpn.load_weights(base_net_weights, by_name=True)
    model_all.load_weights(base_net_weights, by_name=True)

    bbox_util = BBoxUtility(overlap_threshold=config.rpn_max_overlap,
                            ignore_threshold=config.rpn_min_overlap,
                            top_k=config.num_RPN_train_pre)

    #--------------------------------------------#
    #   训练参数的设置
コード例 #2
0
    config = Config()
    NUM_CLASSES = 21
    # 训练100世代
    EPOCH = 100
    # 开始使用1e-4训练,每过10个世代降低为原来的1/2
    Learning_rate = 1e-4
    bbox_util = BBoxUtility(overlap_threshold=config.rpn_max_overlap,
                            ignore_threshold=config.rpn_min_overlap)
    annotation_path = '2007_train.txt'

    #------------------------------------------------------#
    #   权值文件请看README,百度网盘下载
    #   训练自己的数据集时提示维度不匹配正常
    #   预测的东西都不一样了自然维度不匹配
    #------------------------------------------------------#
    model_rpn, model_classifier, model_all = get_model(config, NUM_CLASSES)
    base_net_weights = "model_data/voc_weights.h5"

    model_all.summary()
    model_rpn.load_weights(base_net_weights, by_name=True)
    model_classifier.load_weights(base_net_weights, by_name=True)

    with open(annotation_path) as f:
        lines = f.readlines()
    np.random.seed(10101)
    np.random.shuffle(lines)
    np.random.seed(None)

    # 每个世代训练数据集长度的步数
    # 根据数据集大小进行指定
    EPOCH_LENGTH = len(lines)
コード例 #3
0
#--------------------------------------------#
#   该部分代码用于看网络结构
#--------------------------------------------#
from nets.frcnn import get_model
from utils.utils import net_flops

if __name__ == "__main__":
    input_shape = [600, 600]
    num_classes = 21

    _, model = get_model(num_classes, 'vgg', input_shape=[input_shape[0], input_shape[1], 3])
    #--------------------------------------------------------#
    #   由于faster-rcnn-keras里面存在一些不可计算层
    #   如ROIpooling
    #   因此无法正确计算FLOPs,可参考pytorch版本
    #--------------------------------------------------------#
    #--------------------------------------------#
    #   查看网络结构网络结构
    #--------------------------------------------#
    model.summary()
    #--------------------------------------------#
    #   计算网络的FLOPS
    #--------------------------------------------#
    net_flops(model, table=False)
    
    #--------------------------------------------#
    #   获得网络每个层的名称与序号
    #--------------------------------------------#
    # for i,layer in enumerate(model.layers):
    #     print(i,layer.name)
コード例 #4
0
#--------------------------------------------#
#   该部分代码用于看网络结构
#--------------------------------------------#
from nets.frcnn import get_model

if __name__ == "__main__":
    num_classes = 21

    _, model = get_model(num_classes, 'resnet50')
    model.summary()

    for i, layer in enumerate(model.layers):
        print(i, layer.name)
コード例 #5
0
    Freeze_Train = True
    #----------------------------------------------------#
    #   获得图片路径和标签
    #----------------------------------------------------#
    train_annotation_path = '2007_train.txt'
    val_annotation_path = '2007_val.txt'

    #----------------------------------------------------#
    #   获取classes和anchor
    #----------------------------------------------------#
    class_names, num_classes = get_classes(classes_path)
    num_classes += 1
    anchors = get_anchors(input_shape, backbone, anchors_size)

    K.clear_session()
    model_rpn, model_all = get_model(num_classes, backbone=backbone)
    if model_path != '':
        #------------------------------------------------------#
        #   载入预训练权重
        #------------------------------------------------------#
        print('Load weights {}.'.format(model_path))
        model_rpn.load_weights(model_path, by_name=True)
        model_all.load_weights(model_path, by_name=True)

    #--------------------------------------------#
    #   训练参数的设置
    #--------------------------------------------#
    callback = tf.summary.create_file_writer("logs")
    loss_history = LossHistory("logs/")

    bbox_util = BBoxUtility(num_classes)
コード例 #6
0
    #------------------------------------------------------#
    #   设置用到的显卡
    #------------------------------------------------------#
    os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(str(x) for x in train_gpu)
    ngpus_per_node = len(train_gpu)
    print('Number of devices: {}'.format(ngpus_per_node))

    #----------------------------------------------------#
    #   获取classes和anchor
    #----------------------------------------------------#
    class_names, num_classes = get_classes(classes_path)
    num_classes += 1
    anchors = get_anchors(input_shape, backbone, anchors_size)

    K.clear_session()
    model_rpn_body, model_all_body = get_model(num_classes, backbone=backbone)
    if model_path != '':
        #------------------------------------------------------#
        #   载入预训练权重
        #------------------------------------------------------#
        print('Load weights {}.'.format(model_path))
        model_rpn_body.load_weights(model_path, by_name=True)
        model_all_body.load_weights(model_path, by_name=True)

    if ngpus_per_node > 1:
        model_rpn = multi_gpu_model(model_rpn_body, gpus=ngpus_per_node)
        model_all = multi_gpu_model(model_all_body, gpus=ngpus_per_node)
    else:
        model_rpn = model_rpn_body
        model_all = model_all_body