Last active
January 5, 2021 22:35
-
-
Save kumon/c61d9b6717ce54a89a737d7bf31e0853 to your computer and use it in GitHub Desktop.
画像特徴空間の可視化 [TensorFlowでDeep Learning 19] ref: https://qiita.com/kumonkumon/items/ab56708eebc01077d0ee
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| import tensorflow as tf | |
| import tensorflow_hub as hub | |
| module = hub.Module("https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/2") | |
| height, width = hub.get_expected_image_size(module) | |
| ch = 3 | |
| batch_size = 10 | |
| image_list = tf.convert_to_tensor(['images/img_{:04d}.jpg'.format(i+1) for i in range(2500)]) | |
| input_queue = tf.train.slice_input_producer([image_list], num_epochs=1, shuffle=False) | |
| image_bytes = tf.read_file(input_queue[0]) | |
| image = tf.image.decode_image(image_bytes, channels=ch) | |
| image = tf.image.resize_bilinear([image], [height, width]) | |
| image = tf.reshape(image, tf.stack([height, width, ch])) | |
| images = tf.train.batch([image], batch_size=batch_size, allow_smaller_final_batch=True) | |
| features = module(images) | |
| with open('feature_vecs.tsv', 'w') as fw: | |
| csv_writer = csv.writer(fw, delimiter='\t') | |
| with tf.Session() as sess: | |
| sess.run([tf.local_variables_initializer(), | |
| tf.global_variables_initializer()]) | |
| coord = tf.train.Coordinator() | |
| threads = tf.train.start_queue_runners(sess=sess, coord=coord) | |
| try: | |
| while not coord.should_stop(): | |
| fvecs = sess.run(features) | |
| csv_writer.writerows(fvecs) | |
| except: | |
| pass | |
| finally: | |
| coord.request_stop() | |
| coord.join(threads) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ montage images/img_*.jpg -tile 50x50 -geometry 50x50! sprite.jpg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| import tensorflow as tf | |
| import tensorflow_hub as hub | |
| module = hub.Module("https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/2") | |
| height, width = hub.get_expected_image_size(module) | |
| ch = 3 | |
| batch_size = 10 | |
| image_list = tf.convert_to_tensor(['images/img_{:04d}.jpg'.format(i+1) for i in range(2500)]) | |
| input_queue = tf.train.slice_input_producer([image_list], num_epochs=1, shuffle=False) | |
| image_bytes = tf.read_file(input_queue[0]) | |
| image = tf.image.decode_image(image_bytes, channels=ch) | |
| image = tf.image.resize_bilinear([image], [height, width]) | |
| image = tf.reshape(image, tf.stack([height, width, ch])) | |
| images = tf.train.batch([image], batch_size=batch_size, allow_smaller_final_batch=True) | |
| features = module(images) | |
| with open('feature_vecs.tsv', 'w') as fw: | |
| csv_writer = csv.writer(fw, delimiter='\t') | |
| with tf.Session() as sess: | |
| sess.run([tf.local_variables_initializer(), | |
| tf.global_variables_initializer()]) | |
| coord = tf.train.Coordinator() | |
| threads = tf.train.start_queue_runners(sess=sess, coord=coord) | |
| try: | |
| while not coord.should_stop(): | |
| fvecs = sess.run(features) | |
| csv_writer.writerows(fvecs) | |
| except: | |
| pass | |
| finally: | |
| coord.request_stop() | |
| coord.join(threads) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| import tensorflow as tf | |
| import tensorflow_hub as hub | |
| module = hub.Module("https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/2") | |
| height, width = hub.get_expected_image_size(module) | |
| ch = 3 | |
| filename = tf.placeholder(tf.string) | |
| image_bytes = tf.read_file(filename) | |
| image = tf.image.decode_image(image_bytes, channels=ch) | |
| image = tf.image.resize_bilinear([image], [height, width]) | |
| features = module(image) | |
| with open('feature_vecs.tsv', 'w') as fw: | |
| csv_writer = csv.writer(fw, delimiter='\t') | |
| with tf.Session() as sess: | |
| sess.run(tf.global_variables_initializer()) | |
| for image_path in ['images/img_{:04d}.jpg'.format(i+1) for i in range(2500)]: | |
| fvecs = sess.run(features, feed_dict={filename: image_path}) | |
| csv_writer.writerows(fvecs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ cat projector_config.pbtxt | |
| embeddings { | |
| tensor_path: "feature_vecs.tsv" | |
| sprite { | |
| image_path: "sprite.jpg" | |
| single_image_dim: 50 | |
| single_image_dim: 50 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ tensorboard --logdir . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| embeddings { | |
| tensor_path: "feature_vecs.tsv" | |
| sprite { | |
| image_path: "sprite.jpg" | |
| single_image_dim: 50 | |
| single_image_dim: 50 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| import tensorflow as tf | |
| import tensorflow_hub as hub | |
| module = hub.Module("https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/2") | |
| height, width = hub.get_expected_image_size(module) | |
| ch = 3 | |
| filename = tf.placeholder(tf.string) | |
| image_bytes = tf.read_file(filename) | |
| image = tf.image.decode_image(image_bytes, channels=ch) | |
| image = tf.image.resize_bilinear([image], [height, width]) | |
| features = module(image) | |
| with open('feature_vecs.tsv', 'w') as fw: | |
| csv_writer = csv.writer(fw, delimiter='\t') | |
| with tf.Session() as sess: | |
| sess.run(tf.global_variables_initializer()) | |
| for image_path in ['images/img_{:04d}.jpg'.format(i+1) for i in range(2500)]: | |
| fvecs = sess.run(features, feed_dict={filename: image_path}) | |
| csv_writer.writerows(fvecs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment