Opencv maskrcnn(三)TensorFlow Object Detection API 训练 maskrcnn

1. json转record文件

1.1 整体目录结构

整体目录结构如下(之后需要往该文件夹中逐渐增加一些文件):
在这里插入图片描述

1.1.1 datasets

文件夹datasets中包含train(训练集)和val(验证集),images中是jpg格式的原图,json是通过labelme打标得到的json文件。不会制作数据集的可以参考制作数据集

1.1.2 Abyssinian_label_map.pbtxt

name是类名,根据自己的情况修改,如果有更多的类往下增加即可。

item {
    id: 1
    name: '0.0'
}
item {
    id: 2
    name: '0.4'
}
item {
    id: 3
    name: '0.8'
}
item {
    id: 4
    name: '1.2'
}
item {
    id: 5
    name: '1.6'
}
item {
    id: 6
    name: 'point'
}

1.1.3 read_pbtxt_file.py

# -*- coding: utf-8 -*-
 
import tensorflow as tf
 
from google.protobuf import text_format
 
import string_int_label_map_pb2
 
 
def load_pbtxt_file(path):
    """Read .pbtxt file.
    
    Args: 
        path: Path to StringIntLabelMap proto text file (.pbtxt file).
        
    Returns:
        A StringIntLabelMapProto.
        
    Raises:
        ValueError: If path is not exist.
    """
    if not tf.gfile.Exists(path):
        raise ValueError('`path` is not exist.')
        
    with tf.gfile.GFile(path, 'r') as fid:
        pbtxt_string = fid.read()
        pbtxt = string_int_label_map_pb2.StringIntLabelMap()
        try:
            text_format.Merge(pbtxt_string, pbtxt)
        except text_format.ParseError:
            pbtxt.ParseFromString(pbtxt_string)
    return pbtxt
 
 
def get_label_map_dict(path):
    """Reads a .pbtxt file and returns a dictionary.
    
    Args:
        path: Path to StringIntLabelMap proto text file.
        
    Returns:
        A dictionary mapping class names to indices.
    """
    pbtxt = load_pbtxt_file(path)
    
    result_dict = {}
    for item in pbtxt.item:
        result_dict[item.name] = item.id
    return result_dict

1.1.4 create_tf_record.py

# -*- coding: utf-8 -*-

import cv2
import glob
import hashlib
import io
import json
import numpy as np
import os
import PIL.Image
import tensorflow as tf
 
import read_pbtxt_file
 
 
flags = tf.app.flags
 
flags.DEFINE_string('images_dir', None, 'Path to images directory.')
flags.DEFINE_string('annotations_json_dir', 'datasets/annotations', 
                    'Path to annotations directory.')
flags.DEFINE_string('label_map_path', None, 'Path to label map proto.')
flags.DEFINE_string('output_path', None, 'Path to the output tfrecord.')
 
FLAGS = flags.FLAGS
 
 
def int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
 
 
def int64_list_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
 
 
def bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
 
 
def bytes_list_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))
 
 
def float_list_feature(value):
    return tf.train.Feature(float_list=tf.train.FloatList(value=value))
 
 
def create_tf_example(annotation_dict, label_map_dict=None):
    """Converts image and annotations to a tf.Example proto.
    
    Args:
        annotation_dict: A dictionary containing the following keys:
            ['height', 'width', 'filename', 'sha256_key', 'encoded_jpg',
             'format', 'xmins', 'xmaxs', 'ymins', 'ymaxs', 'masks',
             'class_names'].
        label_map_dict: A dictionary maping class_names to indices.
            
    Returns:
        example: The converted tf.Example.
        
    Raises:
        ValueError: If label_map_dict is None or is not containing a class_name.
    """
    if annotation_dict is None:
        return None
    if label_map_dict is None:
        raise ValueError('`label_map_di
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值