Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Tensorflow를 사용하여 꽃 데이터 세트의 파일 경로를 사용하여 쌍을 만드는 방법은 무엇입니까?

<시간/>

(이미지, 레이블) 쌍을 생성하려면 경로가 먼저 경로 구성 요소 목록으로 변환됩니다. 그런 다음 마지막에서 두 번째 값이 디렉토리에 추가됩니다. 그런 다음 레이블이 정수 형식으로 인코딩됩니다. 압축된 문자열은 텐서로 변환된 다음 필요한 크기로 변형됩니다.

자세히 알아보기: TensorFlow란 무엇이며 Keras가 TensorFlow와 함께 신경망을 생성하는 방법은 무엇입니까?

수천 개의 꽃 이미지가 포함된 꽃 데이터 세트를 사용할 것입니다. 5개의 하위 디렉토리를 포함하며 모든 클래스에 대해 하나의 하위 디렉토리가 있습니다.

Google Colaboratory를 사용하여 아래 코드를 실행하고 있습니다. Google Colab 또는 Colaboratory는 브라우저를 통해 Python 코드를 실행하는 데 도움이 되며 구성이 필요 없고 GPU(그래픽 처리 장치)에 대한 무료 액세스가 필요합니다. Colaboratory는 Jupyter Notebook을 기반으로 구축되었습니다.

print("Function to convert file path to (image,label) pair")
print("First, path is converted to list of path components")
print("Then, the second to last value is added to class directory")
print("The label is integer encoded")
def get_label(file_path):
   parts = tf.strings.split(file_path, os.path.sep)
   one_hot = parts[-2] == class_names
   return tf.argmax(one_hot)

print("The compressed string is converted to a 3 dimensional int tensor")
print("The image is resized to the required size")
def decode_img(img):
   img = tf.image.decode_jpeg(img, channels=3)
   return tf.image.resize(img, [img_height, img_width])

print("The raw data is loaded from the file as a string value")
def process_path(file_path):
   label = get_label(file_path)
   img = tf.io.read_file(file_path)
   img = decode_img(img)
   return img, label

코드 크레딧:https://www.tensorflow.org/tutorials/load_data/images

출력

Function to convert file path to (image,label) pair
First, path is converted to list of path components
Then, the second to last value is added to class directory
The label is integer encoded
The compressed string is converted to a 3 dimensional int tensor
The image is resized to the required size
The raw data is loaded from the file as a string value

설명

  • 파일 경로를 (이미지, 레이블) 쌍으로 변환하는 'get_label' 함수가 정의되었습니다.
  • 파일 경로가 경로 구성 요소 목록으로 변환됩니다.
  • 마지막에서 두 번째 값이 클래스 디렉토리에 추가됩니다.
  • 다음으로 레이블은 정수로 인코딩됩니다.
  • 'decode_img'라는 또 다른 함수는 이미지의 크기를 조정하고 반환하는 데 사용됩니다.
  • 먼저 압축된 문자열이 3차원 정수 텐서로 변환된 다음 크기가 조정됩니다.
  • 파일에서 원시 데이터를 문자열 값으로 로드하는 'process_path'라는 또 다른 함수가 정의되어 있습니다.