Ejemplo n.º 1
0
def _main_(args):
    config_path = args.conf
    weights_path = args.weights
    image_path = args.input
    use_camera = args.real_time

    videos_format = [".mp4", "avi"]
    keras.backend.tensorflow_backend.set_session(get_session())

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

    if weights_path == '':
        weights_path = config['train']['pretrained_weights"']

    ###################
    #   Make the model
    ###################

    yolo = YOLO(backend=config['model']['backend'],
                input_size=(config['model']['input_size_h'],
                            config['model']['input_size_w']),
                labels=config['model']['labels'],
                anchors=config['model']['anchors'],
                gray_mode=config['model']['gray_mode'])

    #########################
    #   Load trained weights
    #########################

    yolo.load_weights(weights_path)

    ###########################
    #   Predict bounding boxes
    ###########################

    if use_camera:
        video_reader = cv2.VideoCapture(int(image_path))
        pbar = tqdm()
        while True:
            pbar.update(1)
            ret, frame = video_reader.read()
            if not ret:
                break
            boxes = yolo.predict(frame)
            frame = draw_boxes(frame, boxes, config['model']['labels'])
            cv2.imshow("frame", frame)
            key = cv2.waitKey(1)
            if key == ord("q") or key == 27:
                break
        pbar.close()
    elif os.path.splitext(image_path)[1] in videos_format:
        file, ext = os.path.splitext(image_path)
        video_out = '{}_detected.avi'.format(file)
        video_reader = cv2.VideoCapture(image_path)

        nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT))
        frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
        frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH))
        print(video_out)
        video_writer = cv2.VideoWriter(video_out,
                                       cv2.VideoWriter_fourcc(*'XVID'), 50.0,
                                       (frame_w, frame_h))

        for _ in tqdm(range(nb_frames)):
            _, image = video_reader.read()
            boxes = yolo.predict(
                image,
                iou_threshold=config['valid']['iou_threshold'],
                score_threshold=config['valid']['score_threshold'])

            image = draw_boxes(image, boxes, config['model']['labels'])
            video_writer.write(np.uint8(image))

        video_reader.release()
        video_writer.release()
    else:
        if os.path.isfile(image_path):
            image = cv2.imread(image_path)
            boxes = yolo.predict(
                image,
                iou_threshold=config['valid']['iou_threshold'],
                score_threshold=config['valid']['score_threshold'])
            image = draw_boxes(image, boxes, config['model']['labels'])

            print(len(boxes), 'boxes are found')
            cv2.imwrite(image_path[:-4] + '_detected' + image_path[-4:], image)
        else:
            detected_images_path = os.path.join(image_path, "detected")
            if not os.path.exists(detected_images_path):
                os.mkdir(detected_images_path)
            images = list(list_images(image_path))
            for fname in tqdm(images):
                image = cv2.imread(fname)
                boxes = yolo.predict(image)
                image = draw_boxes(image, boxes, config['model']['labels'])
                fname = os.path.basename(fname)
                cv2.imwrite(os.path.join(image_path, "detected", fname), image)
Ejemplo 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['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)
Ejemplo n.º 3
0
"""
this callback has the purpose to do a very specific job that imgaug can't do easily, for example in this example
the callback changes the background imagem from the original one, using a random picture from kitti dataset
"""
from sys import path
path.append("..")
from keras_yolov2.utils import list_images
import cv2

IMAGES_PATH = "C:\\datasets\\kitti\\data_object_image_2\\training\\image_2"
images = list(list_images(IMAGES_PATH))
counter = 0


def aug_callback(image, instance):
    """
    instance dict structure:
    {
      'object': [
       {'xmin': int, 'xmax': int, 'ymin': int, 'ymax': int, 'name': str},
       .
       .
       .
       {'xmin': int, 'xmax': int, 'ymin': int, 'ymax': int, 'name': str},
      ],
      'filename': str,
      'width': int,
      'height': int
    }
    :param image: [np.array]
    :param instance: [dict]