■ 컨볼루션 신경망을 만드는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
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("모델 사용을 종료합니다.") |