From f20b79d0bb1ec96994bf5529736a5738b2708d85 Mon Sep 17 00:00:00 2001 From: zhai_pro Date: Sun, 27 Jan 2019 12:31:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A9=E5=8D=B7=E7=A7=AF=E7=A5=9E=E7=BB=8F?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E7=94=A8=E7=BB=9F=E8=AE=A1=E5=AD=A6=E5=BE=97?= =?UTF-8?q?=E5=87=BA=E7=9A=84=E6=95=B0=E6=8D=AE=E9=9B=86=E6=9D=A5=E5=AD=A6?= =?UTF-8?q?=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mlearn.py | 2 +- mlearn_for_image.py | 66 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 mlearn_for_image.py diff --git a/mlearn.py b/mlearn.py index 7fff3c1..9437788 100644 --- a/mlearn.py +++ b/mlearn.py @@ -61,7 +61,7 @@ def main(): validation_data=(test_x, test_y), callbacks=[reduce_lr]) savefig(history, start=10) - model.save('model.h5') + model.save('model.h5', include_optimizer=False) def load_data_v2(): diff --git a/mlearn_for_image.py b/mlearn_for_image.py new file mode 100644 index 0000000..2fb483b --- /dev/null +++ b/mlearn_for_image.py @@ -0,0 +1,66 @@ +# coding: utf-8 +import cv2 +import numpy as np +from keras import models +from keras import layers +from keras import optimizers +from keras.applications import VGG16 +from keras.callbacks import ReduceLROnPlateau +from keras.preprocessing.image import ImageDataGenerator + + +def load_data(): + # 这是统计学专家提供的训练集 + data = np.load('captcha.npz') + train_x, train_y = data['images'], data['labels'] + train_x = train_x / 255.0 + # 由于是统计得来的信息,所以在此给定可信度 + sample_weight = train_y.max(axis=1) / np.sqrt(train_y.sum(axis=1)) + sample_weight /= sample_weight.mean() + train_y = train_y.argmax(axis=1) + + # 这是人工提供的验证集 + data = np.load('captcha.test.npz') + test_x, test_y = data['images'], data['labels'] + # resize + n = test_x.shape[0] + new_test_x = np.empty((n, 67, 67, 3), dtype=np.uint8) + for idx in range(n): + new_test_x[idx] = cv2.resize(test_x[idx], (67, 67)) + test_x = new_test_x / 255.0 + return (train_x, train_y, sample_weight), (test_x, test_y) + + +def main(): + (train_x, train_y, sample_weight), (test_x, test_y) = load_data() + datagen = ImageDataGenerator(horizontal_flip=True, + vertical_flip=True) + train_generator = datagen.flow(train_x, train_y, sample_weight=sample_weight) + _, h, w, c = train_x.shape + base = VGG16(weights='imagenet', include_top=False, input_shape=(h, w, c)) + for layer in base.layers[:-4]: + layer.trainable = False + model = models.Sequential([ + base, + layers.Conv2D(64, (3, 3), activation='relu', padding='same'), + layers.GlobalAveragePooling2D(), + layers.Dense(64, activation='relu'), + layers.Dropout(0.20), + layers.Dense(80, activation='softmax') + ]) + model.compile(optimizer=optimizers.RMSprop(lr=1e-5), + loss='sparse_categorical_crossentropy', + metrics=['accuracy']) + model.summary() + reduce_lr = ReduceLROnPlateau(verbose=1) + model.fit_generator(train_generator, epochs=400, + steps_per_epoch=100, + validation_data=(test_x[:800], test_y[:800]), + callbacks=[reduce_lr]) + result = model.evaluate(test_x, test_y) + print(result) + model.save('12306.image.model.h5', include_optimizer=False) + + +if __name__ == '__main__': + main()