Has troubled me for a few days have bosses can tell me where I wrong
This is my code, only to change the address
# coding: utf-8
The import OS
Import the random
The import sys
The import time
The import tensorflow as tf
The from tfrecord_utils import _process_image_withoutcoder, _convert_to_example_simple
Def _add_to_tfrecord (filename, image_example tfrecord_writer) :
"" "Loads data from the image and annotations files and add them to a TFRecord.
The Args:
Filename: the Dataset directory;
Name: Image name to add to the TFRecord;
Tfrecord_writer: The TFRecord writer to use for writing.
"" "
# print (' - ', filename)
# imaga_data: array to a string
# height: the original image 's height
# width: the original image 's width
# image_example dict contains image 's info
Image_data, height, width=_process_image_withoutcoder (filename)
Example=_convert_to_example_simple (image_example, image_data)
Tfrecord_writer. Write (example. SerializeToString ())
Def _get_output_filename (output_dir, name, net) :
# st=time. Strftime (" % % Y - m - H: % d % % m: % S ", the time the localtime ())
# return '% s s_ s_/% % % s.t frecord' % (output_dir, name, net, st)
Return '% s/train_PNet_landmark tfrecord' % (output_dir)
Def run (dataset_dir,.net, output_dir, name='MTCNN', shuffling=False) :
"" "Runs the conversion operation.
The Args:
Dataset_dir: The dataset directory where The dataset is stored.
Output_dir: the Output directory.
"" "
# tfrecord name
Tf_filename=_get_output_filename (output_dir, name, net)
If tf. IO. Gfile. The exists (tf_filename) :
Print (' Dataset files already exist. The Exiting without re - creating them. ')
Return
# GET the Dataset, and shuffling.
Dataset=get_dataset (dataset_dir,.net=net)
# filenames=dataset [' filename ']
If shuffling:
Tf_filename=tf_filename + '_shuffle'
# the random seed (12345454)
The random shuffle (dataset)
# Process dataset files.
# for the data to tfrecord
Print (' lala ')
With tf.com pat. V1. Python_io. TFRecordWriter (tf_filename) as tfrecord_writer:
For I, image_example enumerate in (dataset) :
If (I + 1) % 100==0:
Sys. Stdout. Write (' \ r> % % d/d images has had converted '% (I + 1, len (dataset)))
# sys. Stdout. Write (' \ r> Converting the image % d/% d '% (I + 1, len (dataset)))
Sys. Stdout. Flush ()
Filename=image_example [' filename ']
_add_to_tfrecord (filename, image_example tfrecord_writer)
Finally, for the labels file:
# labels_to_class_names=dict (zip (range (len (_CLASS_NAMES)), _CLASS_NAMES))
# dataset_utils. Write_label_file (labels_to_class_names dataset_dir)
Print (' \ nFinished converting the MTCNN dataset. ')
Def get_dataset (dir,.net='PNet) :
# get the file name, label and anotation
# item='imglists/PNet train_ % s_raw. The TXT' %.net
Item="D:/MTCNN - Tensorflow - master/prepare_data/DATA/imglists/PNet/train_ % s_landmark. TXT '%.net
Dataset_dir=OS. Path. Join (dir, item)
# print (dataset_dir)
Imagelist=open (dataset_dir, 'r')
The dataset=[]
For the line in imagelist. Readlines () :
Info=line. Strip (). The split (' ')
Data_example=dict ()
Bbox=dict ()
Data_example [' filename ']=info [0]
# print (data_example [' filename '])
Data_example [' label ']=int (info [1])
Bbox [' xmin]=0
Bbox [' ymin]=0
Bbox [' xmax]=0
Bbox [' ymax]=0
Bbox [' xlefteye]=0
Bbox [' ylefteye]=0
Bbox [' xrighteye]=0
Bbox [' yrighteye]=0
Bbox [' xnose]=0
Bbox [' ynose]=0
Bbox [' xleftmouth]=0
Bbox [' yleftmouth]=0
Bbox [' xrightmouth]=0
Bbox [' yrightmouth]=0
If len (info)==6:
Bbox [' xmin]=float (info [2])
Bbox [' ymin]=float (info [3])
Bbox [' xmax]=float (info [4])
Bbox [' ymax]=float (info [5])
If len (info)==12:
Bbox [' xlefteye]=float (info [2])
Bbox [' ylefteye]=float (info [3])
Bbox [' xrighteye]=float (info [4])
Bbox [' yrighteye]=float (info [5])
Bbox [' xnose]=float (info [6])
Bbox [' ynose]=float (info [7])
Bbox [' xleftmouth]=float (info [8])
Bbox [' yleftmouth]=float (info [9])
Bbox [' xrightmouth]=float (info [10])
Bbox [' yrightmouth]=float (info [11])
Data_example [' bbox]=bbox
The dataset. Append (data_example)
Return the dataset
If __name__=="__main__ ':
Dir="D:/MTCNN - Tensorflow - master/prepare_data/DATA/'
Net='PNet'
Output_directory="D:/MTCNN - Tensorflow - master/prepare_data/DATA/imglists/PNet '
The run (dir,.net, output_directory, shuffling=True)
CodePudding user response:
Another called program is this, tfrecord_utils. Py# coding: utf-8
The import tensorflow as tf
The import OS
The import cv2
The from PIL import Image
Def _int64_feature (value) :
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull