human
Complete this python program:
def _add_to_tfrecord_trainvalsplit(record_dir, image_dir, annotation_dir, split_name):
assistant
#Here is the Python code you requested.

    """Loads image files and writes files to a TFRecord.
    Note: masks and bboxes will lose shape info after converting to string.
    """
    assert (split_name in ['trainval2014', 'minival2014'])
    minival_path = os.path.join(annotation_dir, 'instances_minival2014.json')
    minival2014_url = 'https://dl.dropboxusercontent.com/s/o43o90bna78omob/instances_minival2014.json.zip?dl=0'
    assert os.path.exists(minival_path), ('need    to    download    %s    minival    split    to    %s' % (minival2014_url, minival_path))
    import ujson as json
    with open(minival_path, 'r') as f:
        minival = json.load(f)
    def is_in_minival(img_id, minival):
        for img in minival['images']:
            if (img['id'] == img_id):
                return True
        return False
    annFile = os.path.join(annotation_dir, 'instances_train2014.json')
    coco_train = COCO(annFile)
    annFile = os.path.join(annotation_dir, 'instances_val2014.json')
    coco_val = COCO(annFile)
    cats = coco_train.loadCats(coco_train.getCatIds())
    imgs1 = [(img_id, coco_train.imgs[img_id]) for img_id in coco_train.imgs]
    imgs2 = [(img_id, coco_val.imgs[img_id]) for img_id in coco_val.imgs]
    imgs = (imgs1 + imgs2)
    num_of_train = len(coco_train.imgs)
    num_of_all = len(imgs)
    num_per_shard = 2500
    num_shards = int(np.ceil((((len(imgs) + 0.0) - len(minival['images'])) / num_per_shard)))
    if (split_name == 'minival2014'):
        num_shards = int(np.ceil(((len(minival['images']) + 0.0) / num_per_shard)))
    with tf.Graph().as_default():
        with tf.device('/cpu:0'):
            image_reader = ImageReader()
            mask_placeholder = tf.placeholder(dtype=tf.uint8)
            encoded_image = tf.image.encode_png(mask_placeholder)
            with tf.Session('') as sess:
                cnt = 0
                shard_id = (-1)
                for i in range(len(imgs)):
                    img_id = imgs[i][0]
                    img_name = imgs[i][1]['file_name']
                    split = img_name.split('_')[1]
                    img_name = os.path.join(image_dir, split, img_name)
                    if (str(img_id) == '320612'):
        ...