2017-07-17 2 views
1

Ich bin ein Python und Tensor Neuling Flow und frage mich ...Multi Layer Tiff-Datensatz Umwandlung gekennzeichnet, die Tensor Fluss zu formatieren für Modelloptimierung nutzen können

Wie man am besten einen markierten Datensatz von Multi-Layer konvertieren Tiffs in ein Format, das Tensor Flow zur Modelloptimierung/Feinabstimmung verwenden kann?

Ich habe derzeit diesen Code, der jede Schicht eines Ordners von Multi-Tiffs in ein 3D-Array legt, aber ich muss das Etikett oder den Dateinamen der Multi-Tiffs beibehalten. Ich habe einige Tensor-Flow-Skripte gesehen, die in TFRecords konvertiert werden. Ich bin mir jedoch nicht sicher, ob diese den Dateinamen beibehalten. Wie würdest du das am besten machen? Es wird ein ziemlich großer Datensatz sein.

Jede Hilfe sehr geschätzt

import os # For file handling 
from PIL import Image# Import Pillow image processing library 
import numpy 
CroppedMultiTiffs = "MultiTiffs/" 

for filename in os.listdir(MultiTiffs): 
## Imports Multi-Layer TIFF into 3D Numpy Array. 

    img = Image.open(MultiTiffs + filename) 
    imgArray = numpy.zeros((img.n_frames, img.size[1], img.size[0]),numpy.uint8) 
try: 
# for frames in range, img.n_frames for whole folder. 
    for frame in range(2,img.n_frames): 
     img.seek(frame) 
     imgArray[frame,:,:] = img 
     frame = frame + 1 
except (EOFError): img.seek(0) 
    # output error if it doesn't find a file. 
pass 

print(imgArray.shape) # imgArray is now 3D 
print(imgArray.size) 

besten Wünsche

TWP

Antwort

0

in Ordnung, so dass ich es herausgefunden den Faden von Daniils Blog mit http://warmspringwinds.github.io/tensorflow/tf-slim/2016/12/21/tfrecords-guide/

aber meine aktuellen implimentation schafft mehrere TFRecords, und ich denke, es muss ein einzelner TFRecord sein, also versuche herauszufinden, wie um es zu einem einzigen TFRecord zu machen. Wie mache ich das?

Dann kann ich es mit einem TFRecord Leseskript validieren, um es zurückzulesen und zu überprüfen, ob es im richtigen Format für Tensor Flow ist. Ich erhalte derzeit Fehler mit dem Leseskript.

from PIL import Image 
import numpy as np 
import tensorflow as tf 
import os 

def _bytes_feature(value): 
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) 

def _int64_feature(value): 
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) 

path = 'test/' 
output = 'output/' 

fileList = [os.path.join(dirpath, f) for dirpath, dirnames, files in os.walk(path) for f in files if f.endswith('.tif')] 

print (fileList) 
for filename in fileList: 

    basename = os.path.basename(filename) 
    file_name = basename[:-4] 
    print ("processing file: " , filename) 
    print (file_name) 

    if not os.path.exists(output): 
     os.mkdir(output) 

    writer = tf.python_io.TFRecordWriter(output+ file_name + '.tfrecord') 
    img = Image.open(filename) 
    imgArray = np.zeros((img.n_frames, img.size[1], img.size[0]),np.uint8) 
    ## Imports Multi-Layer file into 3D Numpy Array. 
    try: 
     for frame in range(0,img.n_frames): 
      img.seek(frame) 
      imgArray[frame,:,:] = img 
      frame = frame + 1 
    except (EOFError): img.seek(0) 

    pass 

    print ("print img size:" , img.size) 
    print ("print image shape: " , imgArray.shape) 
    print ("print image size: " , imgArray.size) 

    annotation = np.array(Image.open(filename)) 

    height = imgArray.shape[0] 
    width = imgArray.shape[1] 
    depth = imgArray.shape[2] 

    img_raw = imgArray.tostring() 
    annotation_raw = annotation.tostring() 

    example = tf.train.Example(features=tf.train.Features(feature={ 
     'height': _int64_feature(height), 
     'width': _int64_feature(width), 
     'depth': _int64_feature(depth), # for 3rd dimension 
     'image_raw': _bytes_feature(img_raw), 
     'mask_raw': _bytes_feature(annotation_raw)})) 

    writer.write(example.SerializeToString()) 

Mein aktueller TFRecords Lese Skript

import tensorflow as tf 
import os 

def read_and_decode(filename_queue): 
    reader = tf.TFRecordReader() 
    _, serialized_example = reader.read(filename_queue) 
    features = tf.parse_single_example(
     serialized_example, 
     # Defaults are not specified since both keys are required. 
     features={ 
      'image_raw': tf.FixedLenFeature([], tf.string), 
      'label': tf.FixedLenFeature([], tf.int64), 
      'height': tf.FixedLenFeature([], tf.int64), 
      'width': tf.FixedLenFeature([], tf.int64), 
      'depth': tf.FixedLenFeature([], tf.int64) 
     }) 
    image = tf.decode_raw(features['image_raw'], tf.uint8) 
    label = tf.cast(features['label'], tf.int32) 
    height = tf.cast(features['height'], tf.int32) 
    width = tf.cast(features['width'], tf.int32) 
    depth = tf.cast(features['depth'], tf.int32) 
    return image, label, height, width, depth 

with tf.Session() as sess: 
    filename_queue = tf.train.string_input_producer(["output/A.3.1.tfrecord"]) 
    image, label, height, width, depth = read_and_decode(filename_queue) 
    image = tf.reshape(image, tf.stack([height, width, 3])) 
    image.set_shape([32,32,3]) 
    init_op = tf.initialize_all_variables() 
    sess.run(init_op) 
    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(coord=coord) 
    for i in range(1000): 
    example, l = sess.run([image, label]) 
    print (example,l) 
    coord.request_stop() 
    coord.join(threads) 

die Fehler zu erhalten: -

InvalidArgumentError (siehe oben für Traceback): Name:, Feature: Label (Datentyp: int64) erforderlich aber konnte nicht gefunden werden.

Bilder sind Graustufen mehrseitig

Verwandte Themen