def get_model(img_h, nclass):
    input = Input(shape=(img_h, None, 1), name='the_input')
    y_pred = densenet.dense_cnn(input, nclass)
    # y_pred = densenet.dense_blstm(input, nclass)

    basemodel = Model(inputs=input, outputs=y_pred)
    basemodel.summary()

    labels = Input(name='the_labels', shape=[None], dtype='float32')
    input_length = Input(name='input_length', shape=[1], dtype='int64')
    label_length = Input(name='label_length', shape=[1], dtype='int64')

    loss_out = Lambda(ctc_lambda_func, output_shape=(1, ),
                      name='ctc')([y_pred, labels, input_length, label_length])

    model = Model(inputs=[input, labels, input_length, label_length],
                  outputs=loss_out)

    if GPU_NUM > 1:
        model = multi_gpu_model(model, gpus=GPU_NUM)

    model.compile(loss={
        'ctc': lambda y_true, y_pred: y_pred
    },
                  optimizer='adam',
                  metrics=['accuracy'])

    return basemodel, model
def getOldModel(img_h, nclass=None):
    """
    获得预训练模型
    :param img_h:
    :param nclass:
    :return:
    """
    if nclass == None:
        nclass = 5990
    input_base = Input(shape=(img_h, None, 1), name='the_input')
    y_pred = densenet.dense_cnn(input_base, nclass)  # 预训练的模型是5990个字符
    basemodel = Model(inputs=input_base, outputs=y_pred)

    pretrainModelPath = './models/pretrainedModel/weights_densenet.h5'
    print(
        "--------------------------------------------------------------------------------------"
    )
    if os.path.exists(pretrainModelPath):
        print("Loading model weights...")
        basemodel.load_weights(pretrainModelPath)
        print('done!')
    else:
        print("No pre-trained model loaded!")
    print(
        "--------------------------------------------------------------------------------------"
    )

    return basemodel
Ejemplo n.º 3
0
def get_model(img_h, nclass):
    input = Input(shape=(img_h, None, 1), name='the_input')
    y_pred = densenet.dense_cnn(input, nclass)

    basemodel = Model(inputs=input, outputs=y_pred)
    basemodel.summary()

    labels = Input(name='the_labels', shape=[None], dtype='float32')
    input_length = Input(name='input_length', shape=[1], dtype='int64')
    label_length = Input(name='label_length', shape=[1], dtype='int64')

    loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length])

    model = Model(inputs=[input, labels, input_length, label_length], outputs=loss_out)
    model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer='adam', metrics=['accuracy'])

    return basemodel, model
Ejemplo n.º 4
0
def get_model(img_h, nclass):
    input = Input(shape=(img_h, None, 1), name='the_input')
    y_pred = densenet.dense_cnn(input, nclass)

    basemodel = Model(inputs=input, outputs=y_pred)
    basemodel.summary()

    labels = Input(name='the_labels', shape=[
        None,
    ], dtype='float32')
    input_length = Input(name='input_length', shape=[1], dtype='int64')
    label_length = Input(name='label_length', shape=[1], dtype='int64')

    loss_out = Lambda(ctc_lambda_func, output_shape=(1, ),
                      name='ctc')([y_pred, labels, input_length, label_length])

    model = Model(inputs=[input, labels, input_length, label_length],
                  outputs=loss_out)

    return basemodel, model
Ejemplo n.º 5
0
    def load_model(self,):
        from keras.layers import Input, Conv2D, MaxPooling2D, ZeroPadding2D
        from keras.models import Model
        from densenet import dense_cnn
        self.g2 = tf.Graph()
        self.sess2 = tf.Session(graph=self.g2 )
        with self.sess2.as_default():
           with self.g2.as_default():
            input = Input(shape=(32, None, 1), name='the_input')
            nclass = 5991
            y_pred = dense_cnn(input, nclass)
            self.basemodel = Model(inputs=input, outputs=y_pred)
			#---------------------test--------------------------------
            jsonFile = self.basemodel.to_json()
            with open(r'./test_modle.json', 'w') as file:
                file.write(jsonFile)
			#---------------------------------------------------------	
            modelPath = r'weights-densent-13.hdf5'#手写体数字5991
            self.basemodel.load_weights(modelPath)
            print('xxxxxxxxxxxxxxxxxxxxxxx')
            return self.basemodel
Ejemplo n.º 6
0
def get_model_origin_conv(img_h, nclass):
    old_nClass = len(keys.alphabet[:])
    input = Input(shape=(img_h, None, 1), name='the_input')
    y_pred_old = densenet.dense_cnn(input, old_nClass)

    basemodel = Model(inputs=input, outputs=y_pred_old)
    basemodel.load_weights(modelPath)
    flatten = basemodel.get_layer('flatten').output
    y_pred = Dense(nclass, name='y_pred_out', activation='softmax')(flatten)

    labels = Input(name='the_labels', shape=[
        None,
    ], dtype='float32')
    input_length = Input(name='input_length', shape=[1], dtype='int64')
    label_length = Input(name='label_length', shape=[1], dtype='int64')

    loss_out = Lambda(ctc_lambda_func, output_shape=(1, ),
                      name='ctc')([y_pred, labels, input_length, label_length])

    model = Model(inputs=[input, labels, input_length, label_length],
                  outputs=loss_out)

    return basemodel, model
Ejemplo n.º 7
0
    path = 'densenet_savemodel'
    densenet_file = './models/densenet/densenet_single_gpu.h5'

    export_path = './densenet_savemodel/1/'
    if os.path.exists(export_path):
        shutil.rmtree(export_path)
    if not os.path.exists(path):
        os.mkdir(path)

    reload(densenet)
    characters = keys.alphabet[:]
    characters = characters[1:] + u'卍'
    nclass = len(characters)

    input = Input(shape=(32, None, 1), name='the_input')
    y_pred = densenet.dense_cnn(input, nclass)
    basemodel = Model(inputs=input, outputs=y_pred)

    modelPath = densenet_file
    # basemodel = multi_gpu_model(basemodel, gpus=2)
    basemodel.load_weights(modelPath)

    #---------
    K.set_learning_phase(0)
    export_path = os.path.join(tf.compat.as_bytes(path),
                               tf.compat.as_bytes(str(version)))
    builder = tf.saved_model.builder.SavedModelBuilder(export_path)

    print('input info:', basemodel.input_names, '---', basemodel.input)
    print('output info:', basemodel.output_names, '---', basemodel.output)
#-*- coding:utf-8 -*-
from imp import reload

from keras.layers import Input
from keras.models import Model
from keras.utils import plot_model

import densenet

reload(densenet)

input = Input(shape=(32, None, 1), name='the_input')
y_pred = densenet.dense_cnn(input, 5990)
basemodel = Model(inputs=input, outputs=y_pred)

basemodel.load_weights(
    '/home/vcaadmin/zhuangwu/huang/scene_text_recognition/densenet/weights_densenet-04-0.66.h5'
)

plot_model(basemodel, to_file='model.png')
print('OK')
Ejemplo n.º 9
0
            #
            labels[i, :len(str)] = [int(s) - 1 for s in str]

        inputs = {
            'the_input': x,
            'the_labels': labels,
            'input_length': input_length,
            'label_length': label_length,
        }
        outputs = {'ctc': np.zeros([batchsize])}
        yield (inputs, outputs)


input = Input(shape=(img_h, None, 1), name='the_input')

y_pred = densenet.dense_cnn(input, nclass, name='new')

basemodel = Model(inputs=input, outputs=y_pred)
basemodel.summary()

labels = Input(name='the_labels', shape=[maxlabellength], dtype='float32')
input_length = Input(name='input_length', shape=[1], dtype='int64')
label_length = Input(name='label_length', shape=[1], dtype='int64')

loss_out = Lambda(ctc_lambda_func, output_shape=(1, ),
                  name='ctc')([y_pred, labels, input_length, label_length])

model = Model(inputs=[input, labels, input_length, label_length],
              outputs=loss_out)

adam = Adam()
Ejemplo n.º 10
0
char = ''
with open('char_std_5990.txt', encoding='utf-8') as f:
    for ch in f.readlines():
        ch = ch.strip('\r\n')
        char = char + ch

# caffe_ocr中把0作为blank,但是tf 的CTC  the last class is reserved to the blank label.
# https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/util/ctc/ctc_loss_calculator.h
char = char[1:] + '卍'
nclass = len(char)
print('nclass:', len(char))
id_to_char = {i: j for i, j in enumerate(char)}

modelPath = r'model\weights-densent-32-0.9846.hdf5'
_input = Input(shape=(32, None, 1), name='the_input')
y_pred = densenet.dense_cnn(_input, nclass + 1)
basemodel = Model(inputs=_input, outputs=y_pred)
basemodel.load_weights(modelPath)

t = Timer()


def predict(img_path):
    img = Image.open(img_path)
    im = img.convert('L')
    scale = im.size[1] * 1.0 / 32
    w = im.size[0] / scale
    w = int(w)
    print('w:', w)

    im = im.resize((w, 32), Image.ANTIALIAS)