def load_data(tol_num,train_num): # input,tol_num: the numbers of all samples(train and test) # input,train_num: the numbers of training samples # output,(X_train,y_train):trainging data # ouput,(X_test,y_test):test data MAX_CAPTCHA = captcha_params.get_captcha_size() CHAR_SET_LEN = captcha_params.get_char_set_len() # data = np.empty((tol_num, 1, height, width),dtype="float32") data = np.empty((tol_num, height, int(width / captcha_params.get_captcha_nb_letters())),dtype="float32") label = np.empty(tol_num,dtype="uint8") # data dir imgs = os.listdir(captcha_params.DIR_NAME) nbImgs = len(imgs) print("loading images : "+str(tol_num)+ " / "+ str(nbImgs)) for i in range(tol_num): imgIdx = int(random.random() * nbImgs) # load the images and convert them into gray images img = get_image_from_file(captcha_params.DIR_NAME+"/"+imgs[imgIdx]) arr = np.asarray(img,dtype="float32") part = 1 # on ignore la 1ere partie = bordure widthOfPart = int(width / captcha_params.get_captcha_nb_letters()) startOfPart = part * widthOfPart endOfPart = (part +1) * widthOfPart data[i,:,:] = arr[:,startOfPart:endOfPart] captcha_text = imgs[imgIdx].split('.')[0] label[i]= get_index(captcha_text[0])# char2pos(capcha_text)# text2vec(captcha_text) # print(" 2 "+captcha_text+" -> " +str(label[i])) print("load data shuffle") # the data, shuffled and split between train and test sets rr = [i for i in range(tol_num)] random.shuffle(rr) x_train = data[rr][:train_num] y_train = label[rr][:train_num] x_test = data[rr][train_num:] y_test = label[rr][train_num:] # print("labels " + str(label[:50])) # print("y_train " + str(y_train[:50])) return (x_train, y_train), (x_test, y_test)
from keras.utils import np_utils from keras import backend as K from load_data import * import h5py from keras.models import model_from_json from keras.utils import plot_model from keras.preprocessing.image import ImageDataGenerator import matplotlib from matplotlib import pyplot as plt batch_size = 512 nb_epoch = 100 step_epoch = 7 MAX_CAPTCHA = captcha_params.get_captcha_size() CHAR_SET_LEN = captcha_params.get_char_set_len() # input image dimensions img_rows, img_cols = captcha_params.get_height(), captcha_params.get_width() # number of convolutional filters to use nb_filters1 = 32 nb_filters2 = 64 nb_filters3 = 64 # size of pooling area for max pooling pool_size = (2, 2) # convolution kernel size kernel_size = (3, 3)