2017-12-24 6 views
1

저는 로컬 또는 클라우드에서 학습 할 수있는 기계 학습 코드를 작성하고 있습니다. 후드 아래에서 PIL을 사용하는 이미지를로드하려면 keras.preprocessing을 사용하고 있습니다. 로컬 파일은 정상적으로 작동하지만 'gs : // ...'와 같은 Google Cloud Storage 경로는 이해할 수 없습니다.keras.preprocessing을 사용하여 Google Cloud Storage에서 이미지를로드하는 방법

.../lib/python2.7/site-packages/keras/preprocessing/image.py", line 320, in load_img img = pil_image.open(path) File .../lib/python2.7/site-packages/PIL/Image.py", line 2530, in open fp = builtins.open(filename, "rb") IOError: [Errno 2] No such file or directory: 'gs://myapp-some-bucket/123.png'

이 일을 올바른 방법은 무엇입니까 :

from keras.preprocessing import image image.load_img("gs://myapp-some-bucket/123.png")

이 오류를 준다? 나는 궁극적으로 단일 폴더 이미지 (디코딩 된 이미지와 그레이 스케일)가되도록 이미지 폴더가 필요합니다.

+0

저는 Keras에 익숙하지 않습니다. 직접 GCS 액세스를 지원하지 않을 수도 있습니다. 데이터 세트가 크지 않은 경우 미리 설치된 gsutil cli를 사용하여 데이터 세트를 먼저 VM에 복사 할 수 있습니다. 파이썬에서는 os.system ('gsutil cp YOUR_GCS_FILE.')을 호출 할 수 있습니다. –

답변

0

GCS를 이해하는 keras.preprocessing.image.load_img를 대체합니다. 나는 또한 ... 전체 폴더를 읽고, 훈련을위한 하나의 NumPy와 배열에 폴더에있는 모든 이미지를 설정하는 더 많은 코드를 포함

import os import tensorflow as tf from tensorflow.python.platform import gfile filelist = gfile.ListDirectory("gs://myapp-some-bucket") sess = tf.Session() with sess.as_default(): x = np.array([np.array(tf.image.decode_png(tf.read_file(os.path.join(train_files_dir, filename))).eval()) for filename in filelist])

0

이미지로드 :

image_path = 'gs://xxxxxxx.jpg' 
image = tf.read_file(image_path) 
image = tf.image.decode_jpeg(image) 
image_array = sess.run(image) 

이미지 저장 :

job_dir = 'gs://xxxxxxxx' 
image = tf.image.encode_jpeg(image_array) 
file_name = 'xxx.jpg' 
write_op = tf.write_file(os.path.join(job_dir, file_name), image) 
sess.run(write_op) 
관련 문제