import random import numpy as np import matplotlib.pyplot as plt
from PIL import Image from keras.models import * from keras.layers import * from keras.callbacks import * from keras import backend as K from sklearn.utils import shuffle from sklearn.model_selection import train_test_split
defdecode(y): y = np.argmax(np.array(y), axis=2)[:,0] return''.join([characters[x] for x in y])
defget_data(filename): file_handle = open(filename) file_content = file_handle.read().split('\n') if file_content[-1] == '': del file_content[-1] batch_size = len(file_content) X = np.zeros((batch_size, height, width, 3), dtype=np.uint8) y = [np.zeros((batch_size, n_class), dtype=np.uint8) for i in range(n_len)]
input_tensor = Input((height, width, 3)) x = input_tensor
for i in range(4): x = Conv2D(32*2**i, (3, 3), padding='same', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(32*2**i, (3, 3), padding='same', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = MaxPooling2D((2, 2))(x)
x = Flatten()(x) x = Dropout(0.25)(x) x = [Dense(n_class, activation='softmax', name='c%d'%(i+1))(x) for i in range(n_len)]
model = Model(input_tensor, x) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
from keras.models import * from keras.layers import *
rnn_size = 128
input_tensor = Input((width, height, 3)) x = input_tensor
for i in range(3): x = Conv2D(32, (3, 3), padding='same', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(32, (3, 3), padding='same', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = MaxPooling2D((2, 2))(x) cnn_model = Model(input_tensor, x, name='CNN') x = cnn_model(input_tensor)
conv_shape = x.get_shape() x = Reshape((int(conv_shape[1]), int(conv_shape[3] * conv_shape[2])))(x)
x = Dense(32)(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Dropout(0.25)(x)