Exemple #1
0
def test_heap_one():
    #-----------------------------------------------------------------
    # 1: Set some necessary parameters
    weights_path = 'model/convnet_227_weights_epoch02_loss0.0030.h5'
    size = 227
    labels = {
        '0': 0,
        '1': 1,
        '2': 2,
        '3': 3,
        '4': 4,
        '5': 5,
        '6': 6,
        '7': 7,
        '8': 8,
        '9': 9,
        '10': 10,
        '15': 11,
        '16': 12
    }

    #-----------------------------------------------------------------
    # 2: Build the Keras model
    model = convnet('alexnet', weights_path=weights_path, heatmap=True)
    model.save_weights('test_model/conv.h5')

    #-----------------------------------------------------------------
    # 5: Create the validation set batch generator
    data_generator = icdar.generator(input_size=227,
                                     batch_size=1,
                                     labels=labels,
                                     vis=False)
    cnt = 0
    right = 0
    while True:
        cnt += 1
        X, y_true = next(data_generator)
        y_pred = model.predict(X)
        y_true_label = np.argmax(y_true)
        y_pred_label = np.argmax(y_pred)
        if y_pred_label == 0:
            print 'y_pred is background'
        elif y_pred_label == 1:
            print 'y_pred is negedge'
        else:
            print 'y_pred is posedge'
        if y_true_label == y_pred_label:
            print True
            right += 1
        else:
            print False
        print 'the accuracy is %f' % (float(right) / cnt)
    def load(self):	
        load_index = self.index[self.idx: min((self.idx + self.batch_size), self.nb_img)]
        self.idx += self.batch_size
        if self.idx >= self.nb_img:
            np.random.shuffle(self.index)
            self.idx = 0
        batch_data = generator(input_size = self.patch_size,
                               basedir = self.basedir,
                               image_list = self.fnLst,
                               load_index = load_index)
        input_images = (np.array(batch_data[0])).transpose(0,3,1,2)
        input_score_maps = (np.array(batch_data[2])).transpose(0,3,1,2)
        input_geo_maps = (np.array(batch_data[3])).transpose(0,3,1,2)

        return (input_images, input_score_maps, input_geo_maps)     
Exemple #3
0
def test_compare():
    #-----------------------------------------------------------------
    # 1: Set some necessary parameters
    heap_weights_path = 'model/convnet_227_weights_epoch02_loss0.0030.h5'
    nohp_weights_path = 'model/convnet_227_weights_epoch02_loss0.0030.h5'
    size = 227
    labels = {
        '0': 0,
        '1': 1,
        '2': 2,
        '3': 3,
        '4': 4,
        '5': 5,
        '6': 6,
        '7': 7,
        '8': 8,
        '9': 9,
        '10': 10,
        '15': 11,
        '16': 12
    }

    #-----------------------------------------------------------------
    # 5: Create the validation set batch generator
    data_generator = icdar.generator(input_size=227,
                                     batch_size=1,
                                     labels=labels,
                                     vis=False)
    with tf.Session() as sess:
        nohp_model = convnet('alexnet',
                             weights_path=nohp_weights_path,
                             heatmap=False)
        heap_model = convnet('alexnet',
                             weights_path=heap_weights_path,
                             heatmap=True)
        X, y_true = next(data_generator)
        nohp_y_pred = nohp_model.predict(X)
        heap_y_pred = heap_model.predict(X)
        graph = tf.get_default_graph()
        ops = [v for v in graph.get_operations()]
        plcers = [i for i in ops if i.type == 'Placeholder']
        pool0 = graph.get_tensor_by_name('convpool_5/MaxPool:0')
        pool1 = graph.get_tensor_by_name('convpool_5_1/MaxPool:0')
        pool2 = graph.get_tensor_by_name('convpool_5_2/MaxPool:0')
        a, b, c = sess.run([pool0, pool1, pool2],
                           feed_dict={
                               'input_1:0': X,
                               'input_2:0': X,
                               'input_3:0': X
                           })
        dense0 = graph.get_tensor_by_name('dense_1/Relu:0')
        dense1 = graph.get_tensor_by_name('dense_1_1/Relu:0')
        dense2 = graph.get_tensor_by_name('dense_1_2/Relu:0')
        a, b, c = sess.run([dense0, dense1, dense2],
                           feed_dict={
                               'input_1:0': X,
                               'input_2:0': X,
                               'input_3:0': X
                           })
        dense0 = graph.get_tensor_by_name('dense_2/Relu:0')
        dense1 = graph.get_tensor_by_name('dense_2_1/Relu:0')
        dense2 = graph.get_tensor_by_name('dense_2_2/Relu:0')
        a, b, c = sess.run(
            [dense0, dense1, dense2],
            feed_dict={
                'input_1:0': X,
                'input_2:0': X,
                'input_3:0': X,
                'dropout_1/keras_learning_phase:0': False
            })
        dense0 = graph.get_tensor_by_name('dense_3/BiasAdd:0')
        dense1 = graph.get_tensor_by_name('dense_3_1/BiasAdd:0')
        dense2 = graph.get_tensor_by_name('dense_3_2/BiasAdd:0')
        a, b, c = sess.run(
            [dense0, dense1, dense2],
            feed_dict={
                'input_1:0': X,
                'input_2:0': X,
                'input_3:0': X,
                'dropout_1/keras_learning_phase:0': False
            })
        dense0 = graph.get_tensor_by_name('dense_4/BiasAdd:0')
        dense1 = graph.get_tensor_by_name('dense_4_1/BiasAdd:0')
        dense2 = graph.get_tensor_by_name('dense_4_2/BiasAdd:0')
        a, b, c = sess.run(
            [dense0, dense1, dense2],
            feed_dict={
                'input_1:0': X,
                'input_2:0': X,
                'input_3:0': X,
                'dropout_1/keras_learning_phase:0': False
            })
        dense0 = graph.get_tensor_by_name('softmax/Softmax:0')
        dense1 = graph.get_tensor_by_name('softmax_1/Softmax:0')
        dense2 = graph.get_tensor_by_name('softmax_2/Reshape_1:0')
        a, b, c = sess.run(
            [dense0, dense1, dense2],
            feed_dict={
                'input_1:0': X,
                'input_2:0': X,
                'input_3:0': X,
                'dropout_1/keras_learning_phase:0': False
            })
        print 'end'
Exemple #4
0
load_models = False
model_sharedconv = Backbone(backbone='mobilenet')
model_detection = Detection()
model_RoIrotate = RoIRotate(features_stride=1)
model_recognition = Recognition(num_classes=len(CHAR_VECTOR) + 1)
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001, clipnorm=5)

if load_models:
    model_sharedconv.load_weights(cpkt_dir + 'sharedconv')
    model_detection.load_weights(cpkt_dir + 'detection')
    model_recognition.load_weights(cpkt_dir + 'recognition')

# -------- #
max_iter = 10000
iter = 0
data_gen = generator(input_size=640, batch_size=1)  # 160 / 480 / 640 / 800
for x_batch in data_gen:

    features, ws = model_RoIrotate(x_batch['images'], x_batch['rboxes'])
    img_copy = x_batch['images'].copy()

    _, _, img_shape, _ = x_batch['images'].shape
    for i in range(np.clip(features.shape[0], a_max=15, a_min=0)):
        # print(i, x_batch['images'][0, img_shape - 32*(i+1): img_shape - 32*(i+1-1), img_shape-256: img_shape, :].shape)
        img_copy[0, img_shape - 32 * (i + 1):img_shape - 32 * (i + 1 - 1),
                 img_shape - 256:img_shape, :] = features[i, :, :, :].numpy()

    # cv2.imshow('org', img_copy[0, :, :, :].astype(np.uint8))
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
Exemple #5
0
from model_detection import Detection
from model_roirotate import RoIRotate, tfa_enabled
from model_recognition import Recognition

# init
input_shape = [640, 640, 3]  # 160 / 480 / 640 / 800
model_sharedconv = Backbone(backbone='mobilenet', input_shape=input_shape)
model_detection = Detection()
model_RoIrotate = RoIRotate(tfa_enabled=tfa_enabled)
model_recognition = Recognition(num_classes=len(CHAR_VECTOR)+1, training=True, drop_prob=0.05)

# -------- #

data_gen = generator(input_size=input_shape[0],
                     batch_size=1,
                     min_text_size=8,
                     random_scale=np.array([1.0])
                     )
x_batch = data_gen.__next__()

for loss_id in range(4):
    with tf.GradientTape() as tape:

        # forward-prop
        sharedconv = model_sharedconv(x_batch['images'].copy())
        f_score_, geo_score_ = model_detection(sharedconv)
        features, ws = model_RoIrotate(sharedconv, x_batch['rboxes'], expand_px=1)
        logits = model_recognition(features)

        # loss
        loss_cls = model_detection.loss_classification2(x_batch['score_maps'], f_score_, x_batch['training_masks'])