Esempio n. 1
0
def main(args):
    config_path = args.conf
    num_anchors = args.anchors

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    if config['parser_annotation_type'] == 'xml':
        # parse annotations of the training set
        train_imgs, train_labels = parse_annotation_xml(
            config['train']['train_annot_folder'],
            config['train']['train_image_folder'], config['model']['labels'])
    elif config['parser_annotation_type'] == 'csv':
        # parse annotations of the training set
        train_imgs, train_labels = parse_annotation_csv(
            config['train']['train_csv_file'], config['model']['labels'],
            config['train']['train_csv_base_path'])

    input_size = (config['model']['input_size_h'],
                  config['model']['input_size_w'], 3)
    feature_extractor = import_feature_extractor(config['model']['backend'],
                                                 input_size)

    # 300/10 = 30
    grid_w = config['model'][
        'input_size_w'] / feature_extractor.get_output_shape()[1]
    grid_h = config['model'][
        'input_size_h'] / feature_extractor.get_output_shape()[0]

    # run k_mean to find the anchors
    annotation_dims = []
    for image in train_imgs:

        # convert to a "new" grid size if the image w/h deviates from wanted image size
        # 300 / 10 = 10
        cell_w = image['width'] / grid_w
        cell_h = image['height'] / grid_h

        for obj in image['object']:
            # ... divided by 10 (a grid size)
            # 90 / 10 = 9
            relative_w = (float(obj['xmax']) - float(obj['xmin'])) / cell_w
            relatice_h = (float(obj["ymax"]) - float(obj['ymin'])) / cell_h
            annotation_dims.append(tuple(map(float, (relative_w, relatice_h))))

    annotation_dims = np.array(annotation_dims)
    centroids = run_kmeans(annotation_dims, num_anchors)

    # write anchors to file
    print('\naverage IOU for', num_anchors, 'anchors:',
          '%0.2f' % avg_iou(annotation_dims, centroids))
    print_anchors(centroids)
Esempio n. 2
0
def main(args):
    config_path = args.conf

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    if config['parser_annotation_type'] == 'xml':
        # parse annotations of the training set
        train_imgs, train_labels = parse_annotation_xml(config['train']['train_annot_folder'],
                                                        config['train']['train_image_folder'],
                                                        config['model']['labels'])
    elif config['parser_annotation_type'] == 'csv':
        # parse annotations of the training set
        train_imgs, train_labels = parse_annotation_csv(config['train']['train_csv_file'],
                                                        config['model']['labels'],
                                                        config['train']['train_csv_base_path'])

    input_size = (config['model']['input_size_h'], config['model']['input_size_w'], 3)
    feature_extractor = import_feature_extractor(config['model']['backend'], input_size)
    grid_cell_w = round(config['model']['input_size_w'] / feature_extractor.get_output_shape()[1])
    grid_cell_h = round(config['model']['input_size_h'] / feature_extractor.get_output_shape()[0])

    image_w = int(config['model']['input_size_w'])
    image_h = int(config['model']['input_size_h'])

    print("grid cell width:", grid_cell_w)
    print("grid cell height:", grid_cell_h)
    print("grid size:", feature_extractor.get_output_shape())
    for train_img in train_imgs:
        image = cv2.imread(train_img["filename"])
        image = cv2.resize(image, (image_w, image_h))
        for i in range(0, image_w, grid_cell_w):
            cv2.line(image, (i, 0), (i, image_h), (0, 0, 255), 2)
        for i in range(0, image_h, grid_cell_h):
            cv2.line(image, (0, i), (image_w, i), (0, 0, 255), 2)

        cv2.imshow("grid viewer", image)
        key = cv2.waitKey(0)
        if key == ord("q"):
            break
Esempio n. 3
0
def _main_(args):

    config_path = args.conf
    
    with open(config_path) as config_buffer:    
        config = json.loads(config_buffer.read())

    if config['backup']['create_backup']:
        config = create_backup(config)

    keras.backend.tensorflow_backend.set_session(get_session())

    #path for the training and validation dataset
    datasetTrainPath = os.path.join(args.folder,"train")
    datasetValPath = os.path.join(args.folder,"val")

    for folder in [datasetTrainPath, datasetValPath]:
        if not os.path.isdir(folder):
            raise Exception("{} doesn't exist!".format(folder))

    classesTrain = next(os.walk(datasetTrainPath))[1]
    classesVal = next(os.walk(datasetValPath))[1]

    if not classesVal == classesTrain:
        raise Exception("The training and validation classes must be the same!")
    else:
        folders = classesTrain

    #training configuration
    epochs = config['train']['nb_epochs']
    batchSize = config['train']['batch_size']
    width = config['model']['input_size_w']
    height = config['model']['input_size_h']
    depth = 3 if config['model']['gray_mode'] == False else 1

    #config keras generators
    if len(folders) == 2: #if just have 2 classes, the model will have a binary output
        classes = 1
    else:
        classes = len(folders)

    #count all samples
    imagesTrainPaths = []
    imagesValPaths = []
    for folder in folders: 
        imagesTrainPaths+=list(list_images(os.path.join(datasetTrainPath, folder)))
        imagesValPaths+=list(list_images(os.path.join(datasetValPath, folder)))
    
    generator_config = {
        'IMAGE_H'         : height, 
        'IMAGE_W'         : width,
        'IMAGE_C'         : depth,
        'BATCH_SIZE'      : batchSize
    }  

    #callbacks    
    model_name = config['train']['saved_weights_name']
    checkPointSaverBest=ModelCheckpoint(model_name, monitor='val_acc', verbose=1, 
                                        save_best_only=True, save_weights_only=False, mode='auto', period=1)
    ckp_model_name = os.path.splitext(model_name)[1]+"_ckp.h5"
    checkPointSaver=ModelCheckpoint(ckp_model_name, verbose=1, 
                                save_best_only=False, save_weights_only=False, period=10)

    tb=TensorBoard(log_dir=config['train']['tensorboard_log_dir'], histogram_freq=0, batch_size=batchSize, write_graph=True,
                write_grads=False, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)


    #create the classification model
    # make the feature extractor layers
    if depth == 1:
        input_size = (height, width, 1)
        input_image     = Input(shape=input_size)
    else:
        input_size = (height, width, 3)
        input_image     = Input(shape=input_size)

    feature_extractor = import_feature_extractor(config['model']['backend'], input_size)

    train_generator = BatchGenerator(imagesTrainPaths, 
                                generator_config, 
                                norm=feature_extractor.normalize,
                                jitter=True)
    val_generator = BatchGenerator(imagesValPaths, 
                                    generator_config, 
                                    norm=feature_extractor.normalize,
                                    jitter=False)  

    features = feature_extractor.extract(input_image)          

    # make the model head
    output = Conv2D(classes, (1, 1), padding="same")(features)
    output = BatchNormalization()(output)
    output = LeakyReLU(alpha=0.1)(output)
    output = GlobalAveragePooling2D()(output)
    output = Activation("sigmoid")(output) if classes == 1 else Activation("softmax")(output)

    if config['train']['pretrained_weights'] != "":
        model = load_model(config['model']['pretrained_weights'] )
    else:
        model = Model(input_image, output)   
        opt = Adam()
        model.compile(loss="binary_crossentropy" if classes == 1 else "categorical_crossentropy",
                    optimizer=opt,metrics=["accuracy"])
    model.summary()

    model.fit_generator(
            train_generator,
            steps_per_epoch=len(imagesTrainPaths)//batchSize,
            epochs=epochs,
            validation_data=val_generator,
            validation_steps=len(imagesValPaths)//batchSize,
            callbacks=[checkPointSaverBest,checkPointSaver,tb],
            workers=12,
            max_queue_size=40)