国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

基于Tensorflow批量數(shù)據(jù)的輸入實(shí)現(xiàn)方式

2020-02-15 21:23:05
字體:
供稿:網(wǎng)友

基于Tensorflow下的批量數(shù)據(jù)的輸入處理:

1.Tensor TFrecords格式

2.h5py的庫的數(shù)組方法

在tensorflow的框架下寫CNN代碼,我在書寫過程中,感覺不是框架內(nèi)容難寫, 更多的是我在對圖像的預(yù)處理和輸入這部分花了很多精神。

使用了兩種方法:

方法一:

Tensor 以Tfrecords的格式存儲(chǔ)數(shù)據(jù),如果對數(shù)據(jù)進(jìn)行標(biāo)簽,可以同時(shí)做到數(shù)據(jù)打標(biāo)簽。

①創(chuàng)建TFrecords文件

orig_image = '/home/images/train_image/'gen_image = '/home/images/image_train.tfrecords'def create_record():  writer = tf.python_io.TFRecordWriter(gen_image)  class_path = orig_image  for img_name in os.listdir(class_path): #讀取每一幅圖像    img_path = class_path + img_name     img = Image.open(img_path) #讀取圖像    #img = img.resize((256, 256)) #設(shè)置圖片大小, 在這里可以對圖像進(jìn)行處理    img_raw = img.tobytes() #將圖片轉(zhuǎn)化為原聲bytes     example = tf.train.Example(         features=tf.train.Features(feature={             'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[0])), #打標(biāo)簽             'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))#存儲(chǔ)數(shù)據(jù)             }))    writer.write(example.SerializeToString())  writer.close()

②讀取TFrecords文件

def read_and_decode(filename):  #創(chuàng)建文件隊(duì)列,不限讀取的數(shù)據(jù)  filename_queue = tf.train.string_input_producer([filename])  reader = tf.TFRecordReader()  _, serialized_example = reader.read(filename_queue)  features = tf.parse_single_example(      serialized_example,      features={          'label': tf.FixedLenFeature([], tf.int64),          'img_raw': tf.FixedLenFeature([], tf.string)})  label = features['label']  img = features['img_raw']  img = tf.decode_raw(img, tf.uint8) #tf.float32  img = tf.image.convert_image_dtype(img, dtype=tf.float32)  img = tf.reshape(img, [256, 256, 1])  label = tf.cast(label, tf.int32)  return img, label

③批量讀取數(shù)據(jù),使用tf.train.batch

min_after_dequeue = 10000capacity = min_after_dequeue + 3 * batch_sizenum_samples= len(os.listdir(orig_image))create_record()img, label = read_and_decode(gen_image)total_batch = int(num_samples/batch_size)image_batch, label_batch = tf.train.batch([img, label], batch_size=batch_size,                      num_threads=32, capacity=capacity) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())with tf.Session() as sess:  sess.run(init_op)  coord = tf.train.Coordinator()  threads = tf.train.start_queue_runners(coord=coord)  for i in range(total_batch):     cur_image_batch, cur_label_batch = sess.run([image_batch, label_batch])  coord.request_stop()  coord.join(threads)

方法二:

使用h5py就是使用數(shù)組的格式來存儲(chǔ)數(shù)據(jù)

這個(gè)方法比較好,在CNN的過程中,會(huì)使用到多個(gè)數(shù)據(jù)類存儲(chǔ),比較好用, 比如一個(gè)數(shù)據(jù)進(jìn)行了兩種以上的變化,并且分類存儲(chǔ),我認(rèn)為這個(gè)方法會(huì)比較好用。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 桑植县| 马公市| 安宁市| 榆社县| 南陵县| 兴宁市| 博罗县| 新乡县| 南昌市| 闽侯县| 耿马| 马边| 许昌市| 瑞金市| 新密市| 昂仁县| 蓝山县| 都兰县| 朝阳县| 西城区| 丁青县| 来宾市| 同江市| 襄樊市| 灯塔市| 从化市| 汝阳县| 绵竹市| 大港区| 霸州市| 南安市| 马鞍山市| 通州区| 余干县| 怀柔区| 和林格尔县| 大厂| 韶关市| 巨野县| 曲沃县| 隆昌县|