Exemplo n.º 1
0
    NUM_CLASSES = 21
    input_shape = (300, 300, 3)
    priors = get_anchors()
    bbox_util = BBoxUtility(NUM_CLASSES, priors)

    # 0.1用于验证,0.9用于训练
    val_split = 0.1
    with open(annotation_path) as f:
        lines = f.readlines()
    np.random.seed(10101)
    np.random.shuffle(lines)
    np.random.seed(None)
    num_val = int(len(lines)*val_split)
    num_train = len(lines) - num_val
    
    model = SSD300(input_shape, num_classes=NUM_CLASSES)
    #------------------------------------------------------#
    #   权值文件请看README,百度网盘下载
    #   训练自己的数据集时提示维度不匹配正常
    #   预测的东西都不一样了自然维度不匹配
    #------------------------------------------------------#
    model.load_weights('model_data/mobilenet_ssd_weights.h5', by_name=True, skip_mismatch=True)
    # 训练参数设置
    logging = TensorBoard(log_dir=log_dir)
    checkpoint = ModelCheckpoint(log_dir + 'ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5',
        monitor='val_loss', save_weights_only=True, save_best_only=True, period=1)
    reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, verbose=1)
    early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1)

    BATCH_SIZE = 16
    gen = Generator(bbox_util, BATCH_SIZE, lines[:num_train], lines[num_train:],
Exemplo n.º 2
0
        strategy = tf.distribute.MirroredStrategy()
    else:
        strategy = None
    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, anchors_size)

    if ngpus_per_node > 1:
        with strategy.scope():
            model = SSD300((input_shape[0], input_shape[1], 3),
                           num_classes,
                           weight_decay=weight_decay)
            if model_path != '':
                #------------------------------------------------------#
                #   载入预训练权重
                #------------------------------------------------------#
                print('Load weights {}.'.format(model_path))
                model.load_weights(model_path,
                                   by_name=True,
                                   skip_mismatch=True)
    else:
        model = SSD300((input_shape[0], input_shape[1], 3),
                       num_classes,
                       weight_decay=weight_decay)
        if model_path != '':
            #------------------------------------------------------#
Exemplo n.º 3
0
    #   这样也可以用比较大的图片训练,对于小目标有好处
    #----------------------------------------------------#
    input_shape = [300, 300, 3]

    #----------------------------------------------------#
    #   可用于设定先验框的大小,默认的anchors_size
    #   是根据voc数据集设定的,大多数情况下都是通用的!
    #   如果想要检测小物体,可以修改anchors_size
    #   一般调小浅层先验框的大小就行了!因为浅层负责小物体检测!
    #   比如anchors_size = [21,45,99,153,207,261,315]
    #----------------------------------------------------#
    anchors_size = [30, 60, 111, 162, 213, 264, 315]
    priors = get_anchors((input_shape[0], input_shape[1]), anchors_size)
    bbox_util = BBoxUtility(NUM_CLASSES, priors)

    model = SSD300(input_shape, NUM_CLASSES, anchors_size)
    #------------------------------------------------------#
    #   权值文件请看README,百度网盘下载
    #   训练自己的数据集时提示维度不匹配正常
    #   预测的东西都不一样了自然维度不匹配
    #------------------------------------------------------#
    model_path = 'model_data/essay_mobilenet_ssd_weights.h5'
    model.load_weights(model_path, by_name=True, skip_mismatch=True)

    #-------------------------------------------------------------------------------#
    #   训练参数的设置
    #   logging表示tensorboard的保存地址
    #   checkpoint用于设置权值保存的细节,period用于修改多少epoch保存一次
    #   reduce_lr用于设置学习率下降的方式
    #   early_stopping用于设定早停,val_loss多次不下降自动结束训练,表示模型基本收敛
    #-------------------------------------------------------------------------------#
Exemplo n.º 4
0
    log_dir = "logs/"  # use for tensorboard
    annotation_path = 'train.txt'
    ssd_params = SSD_PARAMS(
        img_shape=(300, 300, 3),  # last channel,
        num_classes=2,
        val_split=0.1)

    priors = get_anchors()
    bbox_util = BBoxUtility(ssd_params.num_classes, priors)
    with open(annotation_path) as f:
        lines = f.readlines()
    np.random.shuffle(lines)
    num_val = int(len(lines) * ssd_params.val_split)
    num_train = len(lines) - num_val

    model = SSD300(ssd_params.img_shape, num_classes=ssd_params.num_classes)
    #model.load_weights('model_data/ssd_weights.h5', by_name=True, skip_mismatch=True)

    logging = TensorBoard(log_dir=log_dir)  # user for visdom
    checkPoint = ModelCheckpoint(
        log_dir + 'ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5',
        monitor='val_loss',
        save_weights_only=True,
        save_best_only=True,
        period=10)
    reduce_lr = ReduceLROnPlateau(monitor='val_loss',
                                  factor=0.5,
                                  patience=2,
                                  verbose=1)
    early_stopping = EarlyStopping(monitor='val_loss',
                                   min_delta=0,