2017-02-27 2 views
0

나는 다음과 같은 Tensorflow 코드가 있습니다Tensor 데이터 유형을 문자열로?

import datetime 
import matplotlib.pyplot as plt 
import numpy as np 
import os 
from PIL import Image 
import tensorflow as tf 

image_width = 202 
image_height = 180 
num_channels = 3 

filenames = tf.train.match_filenames_once("./train/Resized/*.jpg") 

def label(label_string): 
    if label_string == 'cat': label = [1,0] 
    if label_string == 'dog': label = [0,1] 

    return label 

def read_image(filename_queue): 
    image_reader = tf.WholeFileReader() 
    key, image_filename = image_reader.read(filename_queue) 
    image = tf.image.decode_jpeg(image_filename) 
    image.set_shape((image_height, image_width, 3)) 

    name = os.path.basename(image_filename) # example "dog.2148.jpg" 
    s = name.split('.') 
    label_string = s[0] 
    label = label(label_string) 

    return image, label 

def input_pipeline(filenames, batch_size, num_epochs=None): 
    filename_queue = tf.train.string_input_producer(filenames, num_epochs=num_epochs, shuffle=True) 
    image, label = read_image(filename_queue) 
    min_after_dequeue = 1000 
    capacity = min_after_dequeue + 3 * batch_size 
    image_batch, label_batch = tf.train.shuffle_batch(
     [image, label], batch_size=batch_size, capacity=capacity, 
     min_after_dequeue=min_after_dequeue) 
    return image_batch, label_batch 

image_batch, label_batch = input_pipeline(filenames, 10) 

마지막 문은 다음과 같은 오류와 함께 실패합니다

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-21-0224ec735c33> in <module>() 
----> 1 image_batch, label_batch = input_pipeline(filenames, 10) 

<ipython-input-20-277e29dc1ae3> in input_pipeline(filenames, batch_size, num_epochs) 
     1 def input_pipeline(filenames, batch_size, num_epochs=None): 
     2  filename_queue = tf.train.string_input_producer(filenames, num_epochs=num_epochs, shuffle=True) 
----> 3  image, label = read_image(filename_queue) 
     4  min_after_dequeue = 1000 
     5  capacity = min_after_dequeue + 3 * batch_size 

<ipython-input-19-ffe4ec8c3e25> in read_image(filename_queue) 
     5  image.set_shape((image_height, image_width, 3)) 
     6 
----> 7  name = os.path.basename(image_filename) # example "dog.2148.jpg" 
     8  s = name.split('.') 
     9  label_string = s[0] 

C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\cntk-py35\lib\ntpath.py in basename(p) 
    230 def basename(p): 
    231  """Returns the final component of a pathname""" 
--> 232  return split(p)[1] 
    233 
    234 

C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\cntk-py35\lib\ntpath.py in split(p) 
    202 
    203  seps = _get_bothseps(p) 
--> 204  d, p = splitdrive(p) 
    205  # set i to index beyond p's last slash 
    206  i = len(p) 

C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\cntk-py35\lib\ntpath.py in splitdrive(p) 
    137 
    138  """ 
--> 139  if len(p) >= 2: 
    140   if isinstance(p, bytes): 
    141    sep = b'\\' 

TypeError: object of type 'Tensor' has no len() 

나는 문제가 문자열 데이터 타입 대 텐서 데이터 타입에 관한 생각을. 어떻게 os.path.basename 함수에 image_filename이 문자열인지 올바르게 표시 할 수 있습니까?

답변

0

문제는 match_filenames_once가

A variable that is initialized to the list of files matching pattern.

를 반환한다는 것입니다 (여기 참조 : https://www.tensorflow.org/api_docs/python/tf/train/match_filenames_once을).

os.path.basename과 string.split은 텐셔르가 아닌 문자열에서 작동하는 함수입니다.

텐 토플 플로우 파이프 라인 외부로 이미지를로드하는 것이 좋습니다. 이렇게하면 쉽게 레이블링이 가능합니다.

+0

그래도 Tensorflow 파이프 라인 내부에서 그런 종류의 변수에 대한 간단한 처리를 수행 할 수 있어야합니까? – OlavT

관련 문제