keras 데이터 세트에서 MNIST를 읽고 PNG 이미지로 출력하는 절차

10838 단어 KerasMNIST

하고 싶은 일


  • python에서 keras 데이터 세트에서 MNIST를로드하고 png 형식의 이미지 파일로 출력합니다.
  • 그 때, 라벨 데이터(0부터 9까지의, 어느 수치인가?)를 파일명에 포함한다.

  • MNIST란?



    구현



    데이터 세트 로드
    import tensorflow as tf
    
    mnist = tf.keras.datasets.mnist
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    

    이곳에서
  • x_train : 학습 데이터 "이미지"
  • y_train : 학습 데이터 "라벨"
  • x_test : 검증 데이터 "이미지".
  • y_test : 검증 데이터 "라벨"

  • 이다.

    내용을 확인해 본다



    x_train : 학습용 데이터 「이미지」



    print(x_train[0])
    [[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   3  18  18  18 126 136  175  26 166 255 247 127   0   0   0   0
     [  0   0   0   0   0   0   0   0  30  36  94 154 170 253 253 253 253 253  225 172 253 242 195  64   0   0   0   0
     [  0   0   0   0   0   0   0  49 238 253 253 253 253 253 253 253 253 251   93  82  82  56  39   0   0   0   0   0
     [  0   0   0   0   0   0   0  18 219 253 253 253 253 253 198 182 247 241    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0  80 156 107 253 253 205  11   0  43 154    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0  14   1 154 253  90   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0 139 253 190   2   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0  11 190 253  70   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0  35 241 225 160 108   1    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0  81 240 253 253 119   25   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0  45 186 253 253  150  27   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  16  93 252  253 187   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 249  253 249  64   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0  46 130 183 253  253 207   2   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0  39 148 229 253 253 253  250 182   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0  24 114 221 253 253 253 253 201   78   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0  23  66 213 253 253 253 253 198  81   2    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0  18 171 219 253 253 253 253 195  80   9   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0  55 172 226 253 253 253 253 244 133  11   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0 136 253 253 253 212 135 132  16   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
    
    



    print(x_train[1])
    [[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  51 159 253  159  50   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0  48 238 252 252  252 237   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0  54 227 253 252 239  233 252  57   6   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0  10  60 224 252 253 252 202   84 252 253 122   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0 163 252 252 252 253 252 252   96 189 253 167   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0  51 238 253 253 190 114 253 228   47  79 255 168   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0  48 238 252 252 179  12  75 121  21    0   0 253 243  50   0   0   0   0   0
     [  0   0   0   0   0   0   0   0  38 165 253 233 208  84   0   0   0   0    0   0 253 252 165   0   0   0   0   0
     [  0   0   0   0   0   0   0   7 178 252 240  71  19  28   0   0   0   0    0   0 253 252 195   0   0   0   0   0
     [  0   0   0   0   0   0   0  57 252 252  63   0   0   0   0   0   0   0    0   0 253 252 195   0   0   0   0   0
     [  0   0   0   0   0   0   0 198 253 190   0   0   0   0   0   0   0   0    0   0 255 253 196   0   0   0   0   0
     [  0   0   0   0   0   0  76 246 252 112   0   0   0   0   0   0   0   0    0   0 253 252 148   0   0   0   0   0
     [  0   0   0   0   0   0  85 252 230  25   0   0   0   0   0   0   0   0    7 135 253 186  12   0   0   0   0   0
     [  0   0   0   0   0   0  85 252 223   0   0   0   0   0   0   0   0   7  131 252 225  71   0   0   0   0   0   0
     [  0   0   0   0   0   0  85 252 145   0   0   0   0   0   0   0  48 165  252 173   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0  86 253 225   0   0   0   0   0   0 114 238 253  162   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0  85 252 249 146  48  29  85 178 225 253 223 167   56   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0  85 252 252 252 229 215 252 252 252 196 130   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0  28 199 252 252 253 252 252 233 145   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0  25 128 252 253 252 141  37   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0
    
    
    



    y_train : 학습 데이터 "라벨"



    print(y_train[0]) 는 5
    print(y_train[1]) 는 0

    이다. 즉, 그 화상이 「무엇의 숫자인가?」의 라벨이며, 0부터 9까지의 수치이다.

    이미지 데이터를 png로 출력하고 라벨 값을 파일 이름에 포함



    구현


    import tensorflow as tf
    from PIL import Image
    
    mnist = tf.keras.datasets.mnist
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    
    for num in range(10):
        label_num = y_train[num]
        np_ary = x_train[num]
        pil_img = Image.fromarray(np_ary)
        pil_img.save("output_" + str(num) + "_label_is_" + str(label_num)  + ".png")
    

    출력





    10장의 png가 출력되어 라벨 값이 파일명에 포함되어 있다.

    좋은 웹페이지 즐겨찾기