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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| from zipfile import ZipFile from keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D from keras.preprocessing.image import ImageDataGenerator
directory = './' with ZipFile('TeamA_name2.zip') as f : x = [f.extract(file, directory) for file in f.namelist() if file.endswith('jpg')]
train_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory(directory='./TeamA_name2/train', target_size = (24,24), batch_size = 3, class_mode = 'categorical') test_datagen = ImageDataGenerator(rescale =1./255) test_generator = test_datagen.flow_from_directory(directory='./TeamA_name2/test', target_size = (24,24), batch_size = 3, class_mode = 'categorical')
model = Sequential() model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(24,24,3))) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dense(4, activation='softmax')) model.compile(optimizer='adam', loss = 'categorical_crossentropy', metrics =['accuracy']) model.fit( train_generator, steps_per_epoch=15, epochs=30, validation_data=test_generator, validation_steps=5) scores = model.evaluate(test_generator, steps=5) print("%s: %.2f%%" %(model.metrics_names[1], scores[1]*100))
train_datagen = 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, brightness_range=(0.1, 0.9), fill_mode='nearest')
train2_generator = train_datagen.flow_from_directory( './TeamA_name2/train', target_size=(24, 24), batch_size=3, class_mode='categorical') test2_datagen = ImageDataGenerator(rescale=1./255.) test2_generator=test2_datagen.flow_from_directory('./TeamA_name2/test', target_size=(24,24), batch_size=3, class_mode='categorical')
model = Sequential() model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(24,24,3))) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dense(4, activation='softmax')) model.compile(optimizer='adam', loss = 'categorical_crossentropy', metrics =['accuracy'])
model.fit( train2_generator, steps_per_epoch=15, epochs=30, validation_data=test2_generator, validation_steps=5)
scores = model.evaluate(test2_generator, steps=5) print("%s: %.2f%%" %(model.metrics_names[1], scores[1]*100))
from tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
def load_image(filename): img = load_img(filename, target_size=(24, 24)) img = img_to_array(img) img = img.reshape(1, 24, 24, 3) img = img.astype('float32') return img
def run_example(): img = load_image('file_name.png') result=model.predict(img) ans = [k for k, v in test_generator.class_indices.items() if v == np.argmax(result[0])][0] print(f'{max(result[0])*100}% 확률로 "{ans}"가 작성한 signature 입니다.')
run_example()
|