첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
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,
    rotation_range = 10,
    width_shift_range = 0.2,
    height_shift_range = 0.2,
    shear_range = 0.7,
    zoom_range = [0.9, 2.2],
    horizontal_flip = True,
    vertical_flip = True,
    fill_mode = "nearest")

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 * 100, epochs = 200, 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("모델 사용을 종료합니다.")
728x90
반응형
그리드형(광고전용)
Posted by icodebroker
TAG , ,

댓글을 달아 주세요