Up 画像の整形 (変換) 作成: 2021-04-28
更新: 2021-04-28


    「画像リストの CSVファイルの読み込み」に続く内容として,「画像の整形 (変換)」を示す。


    「画像リストの CSVファイルの読み込み」は,reader を構築したところまで:
      $ source [パス]/venv/bin/activate (venv) $ python >>> import tensorflow as tf >>> tf.enable_eager_execution() >>> csv_name = '(パス)/all_image_paths.csv' >>> csvfile = open(csv_name, 'r') >>> import csv >>> reader = csv.reader(csvfile, delimiter=',')
    以下,これの続き:


    1. リサイズ
    データの画像は,サイズがまちまちである。
    モデルに合わせて,これらを定形にリサイズする。

    5x3, 3x5, 4x3 の3つの画像を,3x3 にリサイズ:
      >>> def resize_image_raw(image_raw): ... image = tf.image.decode_image(image_raw, channels=3) ... image = tf.image.resize(image, [3, 3]) ... return image ... >>> def resize_image(path): ... image_raw = tf.io.read_file(path) ... return resize_image_raw(image_raw) ... >>> for row in reader: ... path = row[0] ... image = resize_image(path) ... image ... <tf.Tensor: shape=(3, 3, 3), dtype=float32, numpy= array([[[ 79. , 72. , 64. ], [138.33333 , 141.66667 , 135. ], [110.666664, 115.666664, 109.666664]], [[ 74. , 71. , 54. ], [125.666664, 116.666664, 76. ], [103.66667 , 82.66667 , 51.666668]], [[ 66. , 59. , 31. ], [126.666664, 98.666664, 52. ], [108.66667 , 85.66667 , 43.666668]]], dtype=float32)> <tf.Tensor: shape=(3, 3, 3), dtype=float32, numpy= array([[[187. , 197. , 222. ], [176. , 181. , 200. ], [174. , 184. , 186. ]], [[126.333336, 158.33333 , 197.33333 ], [126.333336, 139.33333 , 170.66667 ], [112.666664, 114.333336, 122. ]], [[142.33333 , 169.33333 , 214.33333 ], [152.33333 , 172.33333 , 199.33333 ], [138.66667 , 136.66667 , 139.66667 ]]], dtype=float32)> <tf.Tensor: shape=(3, 3, 3), dtype=float32, numpy= array([[[120. , 93. , 46. ], [126.666664, 97. , 54.666668], [ 82. , 69.666664, 37.333332]], [[106. , 89. , 59. ], [127. , 116.333336, 82. ], [ 88.33333 , 84.33333 , 62.666664]], [[116. , 121. , 115. ], [140.33333 , 140.66667 , 130.66667 ], [102.33333 , 97.33333 , 87.33333 ]]], dtype=float32)> >>>


    2. 画素値の範囲 [0, 255] を,[m, n] に変換
    ここでは,範囲 [0, 1] に変換する:
      >>> csvfile = open(csv_name, 'r') >>> reader = csv.reader(csvfile, delimiter=',') >>> for row in reader: ... path = row[0] ... image = resize_image(path) ... image /= 255.0 ... image ... <tf.Tensor: shape=(3, 3, 3), dtype=float32, numpy= array([[[0.3098039 , 0.28235292, 0.25098038], [0.5424836 , 0.5555555 , 0.52941173], [0.4339869 , 0.45359474, 0.43006533]], [[0.29019606, 0.27843136, 0.2117647 ], [0.49281043, 0.4575163 , 0.2980392 ], [0.40653595, 0.32418302, 0.20261437]], [[0.2588235 , 0.23137254, 0.12156862], [0.496732 , 0.38692808, 0.20392156], [0.42614383, 0.33594772, 0.17124183]]], dtype=float32)> <tf.Tensor: shape=(3, 3, 3), dtype=float32, numpy= array([[[0.7333333 , 0.772549 , 0.8705882 ], [0.69019604, 0.7098039 , 0.7843137 ], [0.6823529 , 0.7215686 , 0.7294117 ]], [[0.4954248 , 0.620915 , 0.77385616], [0.4954248 , 0.5464052 , 0.669281 ], [0.44183004, 0.448366 , 0.47843134]], [[0.5581699 , 0.66405225, 0.8405228 ], [0.5973856 , 0.67581695, 0.7816993 ], [0.5437909 , 0.53594774, 0.54771245]]], dtype=float32)> <tf.Tensor: shape=(3, 3, 3), dtype=float32, numpy= array([[[0.4705882 , 0.36470586, 0.18039215], [0.496732 , 0.38039213, 0.21437907], [0.3215686 , 0.2732026 , 0.14640522]], [[0.41568625, 0.3490196 , 0.23137254], [0.4980392 , 0.45620912, 0.3215686 ], [0.34640518, 0.3307189 , 0.2457516 ]], [[0.45490193, 0.47450978, 0.45098037], [0.55032676, 0.55163395, 0.51241827], [0.40130717, 0.38169932, 0.34248364]]], dtype=float32)> >>>