Share
Embed
TensorFlow-20160430
Cloud Vision API & TensorFlow勉強会の発表資料です。
TFRecordWriter / TFRecordReader による画像ファイルの読み込みについて。
ARIYAMA Keiji
April 30, 2016
Transcript
70 slides
-
C-LIS CO., LTD. ༗ࢁܓೋʢ,FJKJ"3*:"."ʣ C-LIS CO., LTD. AndroidΞϓϦ։ൃऀ ػցֶशॳ৺ऀ
ͬͯ·ͤΜ Photo : Koji MORIGUCHI (AUN CREATIVE FIRM) -
C-LIS CO., LTD. ͬͨ͜ͱ ूΊͨσʔλΛ$*'"3ͷϑΝΠϧܗࣜʹ߹ΘͤͯQBDLͯ͠ αϯϓϧϓϩάϥϜʹ༩͑ͨ Label Red Green
Blue 1 0 Label Red Green Blue 1 Label Red Green Blue 2 : -
C-LIS CO., LTD. ંΓΈΛ૿ͯ͠Έͨ ˍύϥϝʔλʔΛௐ # conv0 with tf.variable_scope('conv0') as
scope: kernel = _variable_with_weight_decay('weights', shape=[32, 32, 3, 32], stddev=1e-4, wd=0.0) conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME') biases = _variable_on_cpu('biases', [32], tf.constant_initializer(0.0)) bias = tf.nn.bias_add(conv, biases) conv0 = tf.nn.relu(bias, name=scope.name) _activation_summary(conv0) # pool0 pool0 = tf.nn.max_pool(conv0, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool0') # norm0 norm0 = tf.nn.lrn(pool0, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm0') # conv1 with tf.variable_scope('conv1') as scope: kernel = _variable_with_weight_decay('weights', shape=[16, 16, 32, 64], stddev=1e-4, wd=0.0) conv = tf.nn.conv2d(norm0, kernel, [1, 1, 1, 1], padding='SAME') biases = _variable_on_cpu('biases', [64], tf.constant_initializer(0.0)) bias = tf.nn.bias_add(conv, biases) conv1 = tf.nn.relu(bias, name=scope.name) _activation_summary(conv1) -
C-LIS CO., LTD. $*'"3ܗࣜͷ αΠζ͕େ͖͍ʢ࣮࣭#JUNBQʣ σʔλͷՃҰ෦আ͕͍͠ Label Red Green
Blue 1 0 Label Red Green Blue 1 Label Red Green Blue 2 : -
C-LIS CO., LTD. ը૾ΛಡΜͰʮදࣔʯ͢Δ def load_png_images(label, filename_queue, size=96, depth=3):
reader = tf.WholeFileReader() key, value = reader.read(filename_queue) result = ImageData() result.label = label decoded_png = tf.image.decode_png(value) decoded_png = tf.image.resize_images(decoded_png, size, size) result.image = decoded_png result.width = size result.height = size result.depth = depth return result -
C-LIS CO., LTD. ը૾ΛಡΜͰʮදࣔʯ͢Δ def _read_image(label, path_list): filename_queue =
tf.train.string_input_producer(path_list) image_result = megane_input.load_png_images(label, filename_queue) # ը૾Լॲཧ # image_result.distort() return image_result -
C-LIS CO., LTD. ը૾ΛϥϯμϜͰՃ͢Δ def distort(self, max_delta=60, contrast_lower=0.2, contrast_upper=2.8):
# ͘Γൈ͖Λ͢ΔྖҬΛܭࢉ self.height = int(self.height * 0.8) self.width = int(self.width * 0.8) distorted_image = tf.cast(self.image, tf.float32) distorted_image = tf.random_crop(distorted_image, [self.height, self.width, self.depth]) distorted_image = tf.image.random_flip_left_right(distorted_image) distorted_image = tf.image.random_brightness(distorted_image, max_delta=max_delta) distorted_image = tf.image.random_contrast(distorted_image, lower=contrast_lower, upper=contrast_upper) self.image = distorted_image # ແޮԽ͓ͯ͘͠ # self.image = tf.image.per_image_whitening(self.image) -
C-LIS CO., LTD. ը૾ΛಡΜͰʮදࣔʯ͢Δ def _show_images(target_dir): files = gfile.ListDirectory(target_dir,
False) files = filter(lambda file: file.endswith('.png'), files) path_list = list(map(lambda file: target_dir + file, files)) image = _read_image(-1, path_list) init_op = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) for i in range(len(path_list)): image_raw = image.image.eval() Image.fromarray(np.asarray(image_raw).astype(np.uint8)).show() coord.request_stop() coord.join(threads) -
C-LIS CO., LTD. େ͖ͳಾ͕ʜʜ def _show_images(target_dir): files = gfile.ListDirectory(target_dir,
False) files = filter(lambda file: file.endswith('.png'), files) path_list = list(map(lambda file: target_dir + file, files)) image = _read_image(-1, path_list) init_op = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) for i in range(len(path_list)): image_raw = image.image.eval() Image.fromarray(np.asarray(image_raw).astype(np.uint8)).show() coord.request_stop() coord.join(threads) -
C-LIS CO., LTD. $*'"3ܗࣜ Label Red Green Blue 1
0 Label Red Green Blue 1 Label Red Green Blue 2 : -
C-LIS CO., LTD. 5'3FDPSEܗࣜͷॻ͖ग़͠ class ImageData: def save_tfrecord(self, base_filename):
with tf.python_io.TFRecordWriter(base_filename + ".tfrecord") as writer: example = tf.train.Example(features=tf.train.Features(feature={ 'file_name': _bytes_feature(os.path.basename(self.filename).encode()), 'label': _int64_feature(self.label), 'image': _bytes_feature(self.image), 'height': _int64_feature(self.height), 'width': _int64_feature(self.width), 'depth': _int64_feature(self.depth), })) writer.write(example.SerializeToString()) -
C-LIS CO., LTD. 5'3FDPSEܗࣜͷॻ͖ग़͠ def _convert_image_to_tfrecords(label, file): print("Processing... "
+ file) value = None width = -1 height = -1 with Image.open(file) as img: width = img.width height = img.height with open(file, "rb") as image: value = image.read() image.close() result = ImageData() result.filename = file result.label = label result.image = value result.height = height result.width = width result.depth = 3 result.save_tfrecord(file) -
C-LIS CO., LTD. 5'3FDPSEܗࣜͷಡΈࠐΈ def _read_image_from_tfrecord(path_list): filename_queue = tf.train.string_input_producer(path_list)
reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, features={ 'file_name': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64), 'image': tf.FixedLenFeature([], tf.string), 'height': tf.FixedLenFeature([], tf.int64), 'width': tf.FixedLenFeature([], tf.int64), 'depth': tf.FixedLenFeature([], tf.int64), }) image_data = ImageData() image_data.set_from_features(features) return image_data -
C-LIS CO., LTD. 5'3FDPSEܗࣜͷಡΈࠐΈ def set_from_features(self, features): self.filename =
features['file_name'] self.label = features['label'] self.filename = tf.image.decode_png(features['image']) self.height = features['height'] self.width = features['width'] self.depth = features['depth'] -
C-LIS CO., LTD. 5'3FDPSEͷදࣔ def _view_tfrecord(path): files = filter(lambda
file: file.endswith('.tfrecord'), gfile.ListDirectory(path, False)) path_list = list(map(lambda file: path + file, files)) image_data = _read_image_from_tfrecord(path_list) init_op = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for i in range(len(path_list)): image = image_data.image Image.fromarray(image).show() finally: coord.request_stop() coord.join(threads) -
C-LIS CO., LTD. ಈ͖·ͤΜ Traceback (most recent call last):
File "/Users/keiji_ariyama/PycharmProjects/megane_co/src/megane_co/tfrecord_viewer.py", line 80, in <module> main(sys.argv) File "/Users/keiji_ariyama/PycharmProjects/megane_co/src/megane_co/tfrecord_viewer.py", line 76, in main _view_tfrecord(target_dir) File "/Users/keiji_ariyama/PycharmProjects/megane_co/src/megane_co/tfrecord_viewer.py", line 64, in _view_tfrecord Image.fromarray(image_data.image).show() File "/Users/keiji_ariyama/python_virtualenv/tensorflow/lib/python3.5/site-packages/PIL/ Image.py", line 2154, in fromarray arr = obj.__array_interface__ AttributeError: 'Tensor' object has no attribute '__array_interface__' Process finished with exit code 1 -
C-LIS CO., LTD. FWBM ͢Δͱʜʜ def _view_tfrecord(path): files =
filter(lambda file: file.endswith('.tfrecord'), gfile.ListDirectory(path, False)) path_list = list(map(lambda file: path + file, files)) image_data = _read_image_from_tfrecord(path_list) init_op = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for i in range(len(path_list)): image = image_data.image.eval() Image.fromarray(image).show() finally: coord.request_stop() coord.join(threads) -
C-LIS CO., LTD. ֤͕ਖ਼ৗʹಡΊ͍ͯΔ͔֬ೝ class ImageData: def print(self): result
= str(self.filename.eval()) result += ' ' result += str(self.label.eval()) result += ' ' result += str(self.height.eval()) result += ' ' result += str(self.width.eval()) result += ' ' result += str(self.depth.eval()) print(result) -
C-LIS CO., LTD. ֤͕ਖ਼ৗʹಡΊ͍ͯΔ͔֬ೝ def _view_tfrecord(path): files = filter(lambda
file: file.endswith('.tfrecord'), gfile.ListDirectory(path, False)) files_full_path = list(map(lambda file: path + file, files)) image_data = _read_image_from_tfrecord(files_full_path) init_op = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for i in range(len(files_full_path)): image_data.print() image = image_data.image Image.fromarray(image).show() finally: coord.request_stop() coord.join(threads) -
C-LIS CO., LTD. ൃੜ b'anzu_joy01_0.png' 1 368 650 3
b'anzu_joy01_0.png' 1 368 650 3 b'haruki_joy01_0.png' 1 650 368 3 b'anzu_joy01_0.png' 1 368 650 3 ຊདྷͱҧ͏͕දࣔ͞Ε͍ͯΔ -
C-LIS CO., LTD. ͓͞Β͍ 5FOTPS'MPXɺ4FTTJPOͷதͰॲཧʢ0QFSBUJPOʣΛ ϋϯυϦϯά͢Δ TFTTJPOSVO͢Δ·Ͱɺͯ͢ͷ࣮ମΛ࣋ͨͳ͍ Tensor("DecodePng:0", shape=(?,
?, ?), dtype=uint8) IUUQTXXXUFOTPSqPXPSHWFSTJPOTSHFU@TUBSUFECBTJD@VTBHFIUNM -
C-LIS CO., LTD. ηογϣϯ def _view_tfrecord(path): files = filter(lambda
file: file.endswith('.tfrecord'), gfile.ListDirectory(path, False)) files_full_path = list(map(lambda file: path + file, files)) image_data = _read_image_from_tfrecord(files_full_path) init_op = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for i in range(len(files_full_path)): image_data.print() image = image_data.image Image.fromarray(image).show() finally: coord.request_stop() coord.join(threads) -
C-LIS CO., LTD. ΦϖϨʔγϣϯɾάϥϑ … file4 file3 file2 file1
filename_queue TFRecordReader decode_png Tensor("DecodePng:0", shape=(?, ?, ?), dtype=uint8) -
C-LIS CO., LTD. ΦϖϨʔγϣϯɾάϥϑ … file4 file3 file2 file1
filename_queue TFRecordReader decode_png resize_image Tensor("Squeeze:0", shape=(96, 96, ?), dtype=float32) -
C-LIS CO., LTD. file1 TFRecordReader decode_png resize_image file1 TFRecordReader decode_png
resize_image ධՁʢ࣮ߦʣ … file4 file3 file2 filename_queue resize_image resize_image .eval() or session.run( ) -
C-LIS CO., LTD. ηογϣϯ def _view_tfrecord(path): files = filter(lambda
file: file.endswith('.tfrecord'), gfile.ListDirectory(path, False)) files_full_path = list(map(lambda file: path + file, files)) image_data = _read_image_from_tfrecord(files_full_path) init_op = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for i in range(len(files_full_path)): image = image_data.image.eval() Image.fromarray(image).show() finally: coord.request_stop() coord.join(threads) -
C-LIS CO., LTD. .eval() or session.run( ) feature['file_name'] feature['file_name'] ̍ͷධՁʢ࣮ߦʣ
… file4 file3 file2 filename_queue file1 TFRecordReader feature['file_name'] -
C-LIS CO., LTD. feature['file_name'] feature['file_name'] file1 ̎ͷධՁʢ࣮ߦʣ … file4
file3 file2 filename_queue TFRecordReader feature['file_name'] .eval() or session.run( ) file2 -
C-LIS CO., LTD. FWBM ͝ͱʹ2VFVF͕ਐΉ def print(self): result =
str(self.filename.eval()) result += ' ' result += str(self.label.eval()) result += ' ' result += str(self.height.eval()) result += ' ' result += str(self.width.eval()) result += ' ' result += str(self.depth.eval()) print(result) -
C-LIS CO., LTD. ͳͥؾͮ͘ͷ͕Εͨͷ͔ pMF@RVFVFΛ࡞͢ΔTUSJOH@JOQVU@QSPEVDFSؔͷ ҾTIV⒐Fͷॳظ͕5SVF filename_queue = tf.train.string_input_producer(path_list,
shuffle=True) IUUQTXXXUFOTPSqPXPSHWFSTJPOTSBQJ@EPDTQZUIPO JP@PQTIUNMTUSJOH@JOQVU@QSPEVDFS -
C-LIS CO., LTD. ॲཧ0QFSBUJPOʹด͡ࠐΊΔ with tf.Session() as sess: sess.run(init_op)
coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for i in range(len(files_full_path)): square = tf.mul(image_data.width, image_data.height) print(square.eval()) finally: coord.request_stop() coord.join(threads) 135424 <- 368 * 368 422500 <- 650 * 650 -
C-LIS CO., LTD. mul mul file1 TFRecordReader feature['width'] mul ධՁʢ࣮ߦʣ
… file4 file3 file2 filename_queue .eval() or session.run( ) feature['height'] 422500 -
C-LIS CO., LTD. ͳͥॻ͖ग़͠ͳ͔ͬͨͷ͔ 5'3FDPSE8SJUFSTFTTJPOͷཧԼʹͳ͍ def save_tfrecord(self, base_filename): with
tf.python_io.TFRecordWriter(base_filename + ".tfrecord") as writer: example = tf.train.Example(features=tf.train.Features(feature={ 'file_name': _bytes_feature(os.path.basename(self.filename).encode()), 'label': _int64_feature(self.label), 'image': _bytes_feature(self.image), 'height': _int64_feature(self.height), 'width': _int64_feature(self.width), 'depth': _int64_feature(self.depth), })) writer.write(example.SerializeToString()) -
C-LIS CO., LTD. 5'3FDPSE8SJUFSͱ3FBEFSͷҧ͍ʹҙ def _read_image_from_tfrecord(path_list): filename_queue = tf.train.string_input_producer(path_list)
reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, features={ 'file_name': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64), 'image': tf.FixedLenFeature([], tf.string), 'height': tf.FixedLenFeature([], tf.int64), 'width': tf.FixedLenFeature([], tf.int64), 'depth': tf.FixedLenFeature([], tf.int64), }) image_data = ImageData() image_data.set_from_features(features) return image_data