Ejemplo n.º 1
0
Archivo: train.py Proyecto: liu-px/ctpn
def build_graph():
    input_image = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='image')
    input_label = tf.placeholder(tf.int32, shape=[None, 1], name='labels')
    input_bbox_targets = tf.placeholder(tf.float32, shape=[None, 4], name='gt_boxes')
    input_bbox_inside_weights = tf.placeholder(tf.float32, shape=[None, 4], name="bbox_inside_weights")
    input_bbox_outside_weights = tf.placeholder(tf.float32, shape=[None, 4], name='bbox_outside_weights')
    learing_rate = tf.placeholder(tf.float32)

    cnn_layers = ResNet()
    rnn_layers = Lstm()

    x = input_image
    x = cnn_layers.build(x, True)
    x = rnn_layers.build(x, 512,128, 512)

    ########################
    #   rpn cls score
    ########################

    y = fc_layer(x, 512, 10 * 2, "fc_rpn_cls")
    dims = tf.shape(y)
    cls_prob = tf.reshape(tf.nn.softmax(tf.reshape(y, [-1, 2])),
                      [dims[0], dims[1], -1, 2])

    #########################
    #   rpn bbox pred
    #########################
    box_pred = fc_layer(x, 512, 10 * 4, "fc_rpn_pred")

    loss_layer = Loss()
    output_loss ,cls_loss,box_loss = loss_layer.build(y,box_pred,input_label,input_bbox_targets,input_bbox_inside_weights,input_bbox_outside_weights)
    train_step = tf.train.AdamOptimizer(learing_rate).minimize(output_loss)

    return [train_step,output_loss,learing_rate,input_image,
            input_label,input_bbox_targets,input_bbox_inside_weights,input_bbox_outside_weights,cls_prob,box_pred,cls_loss,box_loss]
Ejemplo n.º 2
0
def se_resnet152(num_classes=1_000):
    """Constructs a ResNet-152 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(SEBottleneck, [3, 8, 36, 3], num_classes=num_classes)
    model.avgpool = nn.AdaptiveAvgPool2d(1)
    return model
Ejemplo n.º 3
0
def se_resnet34(num_classes=1_000):
    """Constructs a ResNet-34 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(SEBasicBlock, [3, 4, 6, 3], num_classes=num_classes)
    model.avgpool = nn.AdaptiveAvgPool2d(1)
    return model
Ejemplo n.º 4
0
    def __init__(self):
        self.net = ResNet(inference_mode=True)
        self.net.build(input_shape=(None, *IMAGE_SIZE, CHANNELS))
        self.net.load_weights(PATH_TO_MODEL_WEIGHT)
        self.net.summary()

        self.dataset = DataController(PATH_TO_TEST_DATA, True, draw_cloud=True)
        self.data_generator = self.dataset.get_eval_data_generator(
            shuffle=True)
Ejemplo n.º 5
0
def main():
    if VISUALIZATION:
        window_manager = VisualManager('eval')

    dataset = DataController(PATH_TO_TEST_DATA, inference_mode=True)
    measure = YieldMeasure()
    data_generator = dataset.get_eval_data_generator(shuffle=True)

    net = ResNet(inference_mode=True)
    net.build(input_shape=(None, *IMAGE_SIZE, CHANNELS))
    net.load_weights(PATH_TO_MODEL_WEIGHT)
    net.summary()

    inf_time = 0
    inf_num = 0

    for images, gt_labels in data_generator:
        gt_labels = gt_labels.numpy()
        t0 = time()
        pred_labels = tf.nn.softmax(
            net(images)).numpy().argmax(axis=1).astype('int32')
        inf_time += (time() - t0)
        inf_num += 1

        measure.append(gt_labels, pred_labels)

        if VISUALIZATION:
            visual_batch = []
            for i in range(INFER_BATCH_SIZE):
                frame = (images[i] * 255).numpy().astype('uint8')
                window_manager.draw_annotations(frame, pred_labels[i],
                                                gt_labels[i],
                                                dataset.invert_class_dict)
                visual_batch.append(frame)
            window_manager.desktop_show(concatenate(visual_batch, axis=1), 0)
    measure.show_rating()
    print(f"Median inference time: {(inf_time / inf_num) * 1000:.1f} ms")
Ejemplo n.º 6
0
class Inference:
    def __init__(self):
        self.net = ResNet(inference_mode=True)
        self.net.build(input_shape=(None, *IMAGE_SIZE, CHANNELS))
        self.net.load_weights(PATH_TO_MODEL_WEIGHT)
        self.net.summary()

        self.dataset = DataController(PATH_TO_TEST_DATA, True, draw_cloud=True)
        self.data_generator = self.dataset.get_eval_data_generator(
            shuffle=True)

    def __call__(self):
        for images, gt_labels in self.data_generator:
            scores = tf.nn.softmax(self.net(images)).numpy()
            for i in range(INFER_BATCH_SIZE):
                label = int(scores[i][0] < scores[i][1])
                yield 'ALERT' if label else 'PASS'
Ejemplo n.º 7
0
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(SEBasicBlock, [3, 4, 6, 3], num_classes=num_classes)
    model.avgpool = nn.AdaptiveAvgPool2d(1)
    return model


def se_resnet50(num_classes=1_000, pretrained=False):
    """Constructs a ResNet-50 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(SEBottleneck, [3, 4, 6, 3], num_classes=num_classes)
    model.avgpool = nn.AdaptiveAvgPool2d(1)
    if pretrained:
        model.load_state_dict(load_state_dict_from_url(
            "https://github.com/moskomule/senet.pytorch/releases/download/archive/seresnet50-60a8950a85b2b.pkl"))
    return model


def se_resnet101(num_classes=1_000):
    """Constructs a ResNet-101 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(SEBottleneck, [3, 4, 23, 3], num_classes=num_classes)
    model.avgpool = nn.AdaptiveAvgPool2d(1)