# create image data generator object
    test_image_data_generator = keras.preprocessing.image.ImageDataGenerator()

    # create a generator for testing data
    test_generator = CSVGenerator(
        args.test_path,
        args.class_file,
        args.mean_image,
        test_image_data_generator,
        image_min_side=args.image_min_side,
        image_max_side=args.image_max_side)
    # start collecting results
    results = []
    image_ids = []
    for i in range(len(test_generator.image_names)):
        image = test_generator.load_image(i)
        image = test_generator.preprocess_image(image)
        image, scale = test_generator.resize_image(image)

        # run network
        _, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0))

        # clip to image shape
        detections[:, :, 0] = np.maximum(0, detections[:, :, 0])
        detections[:, :, 1] = np.maximum(0, detections[:, :, 1])
        detections[:, :, 2] = np.minimum(image.shape[1], detections[:, :, 2])
        detections[:, :, 3] = np.minimum(image.shape[0], detections[:, :, 3])

        # correct boxes for image scale
        detections[0, :, :4] /= scale
Пример #2
0
def create_tfrecords(annotations_file,
                     class_file,
                     backbone_model="resnet50",
                     image_min_side=800,
                     size=1,
                     savedir="./"):
    """
    Args:
        annotations_file: path to 6 column data in form image_path, xmin, ymin, xmax, ymax, label
        backbone_model: A keras retinanet backbone
        image_min_side: resized image object minimum size
        size: Number of images per tfrecord
        savedir: dir path to save tfrecords files
    
    Returns:
        written_files: A list of path names of written tfrecords
    """
    memory_used = []

    #Image preprocess function
    backbone = models.backbone(backbone_model)

    #filebase name
    image_basename = os.path.splitext(os.path.basename(annotations_file))[0]

    ## Syntax checks
    ##Check annotations file only JPEG, PNG, GIF, or BMP are allowed.
    #df = pd.read_csv(annotations_file, names=["image_path","xmin","ymin","xmax","ymax","label"])
    #df['FileType'] = df.image_path.str.split('.').str[-1].str.lower()
    #bad_files = df[~df['FileType'].isin(["jpeg","jpg","png","gif","bmp"])]

    #if not bad_files.empty:
    #raise ValueError("Check annotations file, only JPEG, PNG, GIF, or BMP are allowed, {} incorrect files found /n {}: ".format(bad_files.shape[0],bad_files.head()))

    #Check dtypes, cannot use pandas, or will coerce in the presence of NAs
    with open(annotations_file, 'r') as f:
        reader = csv.reader(f, delimiter=',')
        row = next(reader)
        if row[1].count(".") > 0:
            raise ValueError(
                "Annotation files should be headerless with integer box, {} is not a int"
                .format(row[1]))

    #Create generator - because of how retinanet yields data, this should always be 1. Shape problems in the future?
    train_generator = CSVGenerator(annotations_file,
                                   class_file,
                                   batch_size=1,
                                   image_min_side=image_min_side,
                                   preprocess_image=backbone.preprocess_image)

    #chunk size
    indices = np.arange(train_generator.size())
    chunks = [
        indices[i * size:(i * size) + size]
        for i in range(ceil(len(indices) / size))
    ]

    written_files = []
    for chunk in chunks:
        #Create tfrecord dataset and save it for output
        fname = savedir + "{}_{}.tfrecord".format(image_basename, chunk[0])
        written_files.append(fname)
        writer = tf.io.TFRecordWriter(fname)
        images = []
        regression_targets = []
        class_targets = []
        filename = []
        original_image = []
        for i in chunk:

            #Original image
            original_image.append(train_generator.load_image(i))

            batch = train_generator.__getitem__(i),

            #split into images and tar  gets
            inputs, targets = batch[0]

            #grab image, asssume batch size of 1, squeeze
            images.append(inputs[0, ...])

            #Grab anchor targets
            regression_batch, labels_batch = targets

            #grab regression anchors
            #regression_batch: batch that contains bounding-box regression targets for an image & anchor states (np.array of shape (batch_size, N, 4 + 1),
            #where N is the number of anchors for an image, the first 4 columns define regression targets for (x1, y1, x2, y2) and the
            #last column defines anchor states (-1 for ignore, 0 for bg, 1 for fg).
            regression_anchors = regression_batch[0, ...]
            regression_targets.append(regression_anchors)

            #grab class labels - squeeze out batch size
            #From retinanet: labels_batch: batch that contains labels & anchor states (np.array of shape (batch_size, N, num_classes + 1),
            #where N is the number of anchors for an image and the last column defines the anchor state (-1 for ignore, 0 for bg, 1 for fg).
            labels = labels_batch[0, ...]
            print("Label shape is: {}".format(labels.shape))
            class_targets.append(labels)

            #append filename by looking at group index
            current_index = train_generator.groups[i][0]

            #Grab filename and append to the full path
            fname = train_generator.image_names[current_index]
            fname = os.path.join(train_generator.base_dir, fname)

            filename.append(fname)

        for image, regression_target, class_target, fname, orig_image in zip(
                images, regression_targets, class_targets, filename,
                original_image):
            tf_example = create_tf_example(image, regression_target,
                                           class_target, fname, orig_image)
            writer.write(tf_example.SerializeToString())

        memory_used.append(psutil.virtual_memory().used / 2**30)

    plt.plot(memory_used)
    plt.title('Evolution of memory')
    plt.xlabel('iteration')
    plt.ylabel('memory used (GB)')
    plt.savefig(os.path.join(savedir, "memory.png"))

    return written_files
Пример #3
0
def main():

    os.makedirs(RESULTS_DIR, exist_ok=True)

    # os.environ["CUDA_VISIBLE_DEVICES"] = "0"
    keras.backend.tensorflow_backend.set_session(get_session())

    if SNAPSHOT_FILE:
        snapshot_file = SNAPSHOT_FILE
    else:
        snapshots = os.listdir(SNAPSHOT_DIR)
        if not snapshots:
            raise Exception("There are no snapshots")

        snapshot_file = os.path.join(SNAPSHOT_DIR,
                                     sorted(snapshots, reverse=True)[0])

    print("snapshot: {}".format(snapshot_file))

    model = keras.models.load_model(
        snapshot_file, custom_objects=custom_objects)
    # print(model.summary())

    # create image data generator object
    val_image_data_generator = keras.preprocessing.image.ImageDataGenerator()

    # create a generator for testing data
    val_generator = CSVGenerator(
        ANNOTATIONS,
        CLASSES,
        val_image_data_generator,
        batch_size=BATCH_SIZE,
        base_dir=BASE_DIR)

    for image_index in range(val_generator.size()):

        # load image
        image = val_generator.load_image(image_index)

        # copy to draw on
        draw = image.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

        # preprocess image for network
        image = val_generator.preprocess_image(image)
        image, scale = val_generator.resize_image(image)
        annotations = val_generator.load_annotations(image_index)

        # process image
        start = time.time()
        _, _, detections = model.predict_on_batch(
            np.expand_dims(image, axis=0))
        print("processing time: ", time.time() - start)

        # compute predicted labels and scores
        predicted_labels = np.argmax(detections[0, :, 4:], axis=1)
        scores = detections[
            0, np.arange(detections.shape[1]), 4 + predicted_labels]

        # correct for image scale
        detections[0, :, :4] /= scale

        # visualize detections
        detected = False
        for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
            if score < 0.5:
                continue

            detected = True

            if VISUALIZE_RESULTS:

                b = detections[0, idx, :4].astype(int)
                cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
                caption = "{} {:.3f}".format(
                    val_generator.label_to_name(label), score)
                cv2.putText(draw, caption, (b[0], b[1] - 10),
                            cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 3)
                cv2.putText(draw, caption, (b[0], b[1] - 10),
                            cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255), 2)

        if VISUALIZE_RESULTS and detected:

            # visualize annotations
            for annotation in annotations:

                label = int(annotation[4])
                b = annotation[:4].astype(int)
                cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 255, 0), 2)
                caption = "{}".format(val_generator.label_to_name(label))
                cv2.putText(draw, caption, (b[0], b[1] - 10),
                            cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 3)
                cv2.putText(draw, caption, (b[0], b[1] - 10),
                            cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255), 2)

            plt.figure(figsize=(15, 15))
            plt.axis('off')
            plt.imshow(draw)
            plt.show()