■ 컨볼루션 신경망 만들기 (손글씨)

----------------------------------------------------------------------------------------------------


handwriting_shape.zip


import keras.layers as layers

import keras.layers.convolutional as convolutional

import keras.models as models

import keras.preprocessing.image as image

import numpy as np

 

np.random.seed(3)

 

print("데이터 로드를 시작합니다.")

 

trainImageDataGenerator = image.ImageDataGenerator(rescale = 1. / 255)

 

trainDirectoryIterator = trainImageDataGenerator.flow_from_directory("handwriting_shape/train", target_size = (24, 24),

    batch_size = 3, class_mode = "categorical")

 

testImageDataGenerator = image.ImageDataGenerator(rescale = 1. / 255)

 

testDirectoryIterator = testImageDataGenerator.flow_from_directory("handwriting_shape/test", target_size = (24, 24),

    batch_size = 3, class_mode = "categorical")

 

print("데이터 로드를 종료합니다.")

 

print("모델 정의를 시작합니다.")

 

model = models.Sequential()

 

model.add(convolutional.Conv2D(32, kernel_size = (3, 3), activation = "relu", input_shape = (24, 24, 3)))

model.add(convolutional.Conv2D(64, (3, 3), activation = "relu"))

model.add(convolutional.MaxPooling2D(pool_size = (2, 2)))

model.add(layers.Flatten())

model.add(layers.Dense(128, activation = "relu"   ))

model.add(layers.Dense(3  , activation = "softmax"))

 

model.compile(loss = "categorical_crossentropy", optimizer = "adam", metrics = ["accuracy"])

 

print("모델 정의를 종료합니다.")

 

print("모델 학습을 시작합니다.")

 

model.fit_generator(trainDirectoryIterator, steps_per_epoch = 15, epochs = 50, validation_data = testDirectoryIterator,

    validation_steps = 5)

 

print("모델 학습을 종료합니다.")

 

print("모델 평가를 시작합니다.")

 

evaluationList = model.evaluate_generator(testDirectoryIterator, steps = 5)

 

print("%s : %.2f%%" % (model.metrics_names[1], evaluationList[1] * 100))

 

print("모델 평가를 종료합니다.")

 

print("모델 사용을 시작합니다.")

 

outputNDArray = model.predict_generator(testDirectoryIterator, steps = 5)

 

np.set_printoptions(formatter = {"float": lambda x: "{0:0.3f}".format(x)})

 

classDictionary = testDirectoryIterator.class_indices

 

print(classDictionary)

print(outputNDArray)

 

print("모델 사용을 종료합니다.")

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요