2017-10-15 1 views
0

에 훈련 분류를 공급하는 방법 :나는 다음과 같이 데이터 집합 내 분류를 양성하는 데 사용되는 이미지를 읽어 Tensorflow

filename_strings = [] 
label_strings = [] 
for dirname, dirnames, filenames in os.walk('training'): 
    for filename in filenames: 
     filename_strings.append(dirname + '\\' + filename) 
     label_strings.append(dirname) 

filenames = tf.constant(filename_strings) 
labels = tf.constant(label_strings) 
dataset = tf.contrib.data.Dataset.from_tensor_slices((filenames, labels)) 
dataset_train = dataset.map(_parse_function) 

_parse_function :

# Reads an image from a file, decodes it into a dense tensor, and resizes it 
# to a fixed shape. 
def _parse_function(filename, label): 
    image_string = tf.read_file(filename) 
    image_decoded = tf.image.decode_png(image_string) 
    image_resized = tf.image.resize_images(image_decoded, [28, 28]) 
    return image_decoded, label 

를하지만 지금 내가 기차 단계 :

# Create the Estimator 
mnist_classifier = tf.estimator.Estimator(
model_fn=cnn_model_fn, model_dir="/model") 
# Set up logging for predictions 
tensors_to_log = {"probabilities": "softmax_tensor"} 
logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=50) 

# Train the model 
train_input_fn = tf.estimator.inputs.numpy_input_fn(
    x= {"x": dataset_train }, 
    y= dataset_train, 
    batch_size=100, 
    num_epochs=None, 
    shuffle=True) 
mnist_classifier.train(
    input_fn=train_input_fn, 
    steps=200, 
    hooks=[logging_hook]) 
나는이 튜토리얼 A Guide to TF Layers: Building a Convolutional Neural Network을 따라하려고

하지만 내 자신의 메신저와 먹이를 할 수 없습니다 연령 집합입니다.

열차 단계 피드에 직접 데이터 세트를 사용할 수 있습니까? 제 말은 각 이미지의 특징과 레이블이있는 텐서를 가지고 있다는 것입니다.

+0

: 여기에 간단한 예제입니다 – Maxim

답변

0

참조 할 튜토리얼에서는 데이터를 numpy 배열 (the code here 참조)로 읽고 tf.estimator.inputs.numpy_input_fn과 함께 입력을 전달합니다. 이것이 가장 쉬운 방법입니다.

그러나 처음부터 텐서로 작업하는 것을 선호한다면 custom input function을 사용하는 것도 가능합니다. 그래서`_parse_function`는 NumPy와 배열로 이미지를 반환해야합니다, numpy``에서 예상되는 데이터 집합을 numpy_input_fn``에 대한

def read_images(batch_size): 
    # A stub. Should read the next batch, shuffle it, etc 
    images = tf.zeros([batch_size, 28, 28, 1]) # 4-rank tensor 
    labels = tf.ones([batch_size])    # 1-rank tensor (not one-hot encoded) 
    return images, labels 

def simple_input(): 
    x, labels = read_images(batch_size=10) 
    return {"x": x}, labels 

tensors_to_log = {"probabilities": "softmax_tensor"} 
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=50) 

classifier.train(input_fn=simple_input, 
       steps=10, 
       hooks=[logging_hook]) 
관련 문제