예제 #1
0
파일: detect.py 프로젝트: 0x3a/deep-anpr
def detect(im, param_vals):
    """
    Detect number plates in an image.

    :param im:
        Image to detect number plates in.

    :param param_vals:
        Model parameters to use. These are the parameters output by the `train`
        module.

    :returns:
        Iterable of `bbox_tl, bbox_br, letter_probs`, defining the bounding box
        top-left and bottom-right corners respectively, and a 7,36 matrix
        giving the probability distributions of each letter.

    """

    # Convert the image to various scales.
    scaled_ims = list(make_scaled_ims(im, model.WINDOW_SHAPE))

    # Load the model which detects number plates over a sliding window.
    x, y, params = model.get_detect_model()

    # Execute the model at each scale.
    with tf.Session(config=tf.ConfigProto()) as sess:
        y_vals = []
        for scaled_im in scaled_ims:
            feed_dict = {x: numpy.stack([scaled_im])}
            feed_dict.update(dict(zip(params, param_vals)))
            y_vals.append(sess.run(y, feed_dict=feed_dict))

    # Interpret the results in terms of bounding boxes in the input image.
    # Do this by identifying windows (at all scales) where the model predicts a
    # number plate has a greater than 50% probability of appearing.
    #
    # To obtain pixel coordinates, the window coordinates are scaled according
    # to the stride size, and pixel coordinates.
    for i, (scaled_im, y_val) in enumerate(zip(scaled_ims, y_vals)):
        for window_coords in numpy.argwhere(y_val[0, :, :, 0] >
                                                       -math.log(1./0.99 - 1)):
            letter_probs = (y_val[0,
                                  window_coords[0],
                                  window_coords[1], 1:].reshape(
                                    7, len(common.CHARS)))
            letter_probs = common.softmax(letter_probs)

            img_scale = float(im.shape[0]) / scaled_im.shape[0]

            bbox_tl = window_coords * (8, 4) * img_scale
            bbox_size = numpy.array(model.WINDOW_SHAPE) * img_scale

            present_prob = common.sigmoid(
                               y_val[0, window_coords[0], window_coords[1], 0])

            yield bbox_tl, bbox_tl + bbox_size, present_prob, letter_probs
예제 #2
0
def forward_progation(w):
    a = np.dot(x, w)
    y = softmax(a)
    return y
예제 #3
0
 def loss(self, x, t):
     y = softmax(self.predict(x))
     return crossEntropyError(y, t)
예제 #4
0
checkpoint = tf.train.Checkpoint(model=Model)
checkpoint.restore(tf.train.latest_checkpoint(string))

#Set a grid
grid = np.meshgrid(np.arange(config.cellule_x, dtype=np.float32),
                   np.arange(config.cellule_y, dtype=np.float32))
grid = np.expand_dims(np.stack(grid, axis=-1), axis=2)
grid = np.tile(grid, (1, 1, config.nbr_boxes, 1))

for i in range(len(images)):
    img = common.prepare_image(images[i], labels[i], False)
    img2 = images[i].copy()
    predictions = Model(np.array([images[i]]))
    pred_boxes = predictions[0, :, :, :, 0:4]
    pred_conf = common.sigmoid(predictions[0, :, :, :, 4])
    pred_classes = common.softmax(predictions[0, :, :, :, 5:])
    ids = np.argmax(pred_classes, axis=-1)

    x_center = ((grid[:, :, :, 0] + common.sigmoid(pred_boxes[:, :, :, 0])) *
                config.r_x)
    y_center = ((grid[:, :, :, 1] + common.sigmoid(pred_boxes[:, :, :, 1])) *
                config.r_y)
    w = (np.exp(pred_boxes[:, :, :, 2]) * config.anchors[:, 0] * config.r_x)
    h = (np.exp(pred_boxes[:, :, :, 3]) * config.anchors[:, 1] * config.r_y)

    x_min = (x_center - w / 2).astype(np.int32)
    y_min = (y_center - h / 2).astype(np.int32)
    x_max = (x_center + w / 2).astype(np.int32)
    y_max = (y_center + h / 2).astype(np.int32)

    for y in range(config.cellule_y):
예제 #5
0
def detect(im, param_vals):
    """
    Detect all bounding boxes of number plates in an image.
    :param im:
        Image to detect number plates in.
    :param param_vals:
        Model parameters to use. These are the parameters output by the `train`
        module.
    :returns:
        Iterable of `bbox_tl, bbox_br, letter_probs`, defining the bounding box
        top-left and bottom-right corners respectively, and a 7,36 matrix
        giving the probability distributions of each letter.
    """

    # Convert the image to various scales.
    scaled_ims = list(make_scaled_ims(im, model.WINDOW_SHAPE))

    # Load the model which detects number plates over a sliding window.
    x, y, params = model.get_detect_model()

    # Execute the model at each scale.
    with tf.Session(config=tf.ConfigProto()) as sess:
        y_vals = []
        
        for scaled_im in scaled_ims:
            feed_dict = {x: numpy.stack([scaled_im])}
            feed_dict.update(dict(zip(params, param_vals)))
            y_vals.append(sess.run(y, feed_dict=feed_dict))
            #plt.imshow(scaled_im)
            #plt.show()
    writer = tf.summary.FileWriter("logs/", sess.graph)

    # Interpret the results in terms of bounding boxes in the input image.
    # Do this by identifying windows (at all scales) where the model predicts a
    # number plate has a greater than 50% probability of appearing.
    #
    # To obtain pixel coordinates, the window coordinates are scaled according
    # to the stride size, and pixel coordinates.
    count_detect = 0
    for i, (scaled_im, y_val) in enumerate(zip(scaled_ims, y_vals)):
        for window_coords in numpy.argwhere(y_val[0, :, :, 0] >
                                            -math.log(1. / 0.99 - 1)):
            letter_probs = (y_val[0,
                            window_coords[0],
                            window_coords[1], 1:].reshape(
                7, len(common.CHARS)))
            letter_probs = common.softmax(letter_probs)

            img_scale = float(im.shape[0]) / scaled_im.shape[0]

            bbox_tl = window_coords * (8, 4) * img_scale
            bbox_size = numpy.array(model.WINDOW_SHAPE) * img_scale

            present_prob = common.sigmoid(
                y_val[0, window_coords[0], window_coords[1], 0])
            count_detect += 1
            yield bbox_tl, bbox_tl + bbox_size, present_prob, letter_probs
            print("count detect:", count_detect)
            print("show return window: ", bbox_tl, "return windows box: ", bbox_tl + bbox_size)
            print("present: ", present_prob)
            print("letter: ", letter_probs_to_code(letter_probs))
예제 #6
0
def loss(w):
    a = np.dot(x, w)
    y = softmax(a)  # softmax(x @ w)
    e = cross_entropy_error(y, t)

    return e
예제 #7
0
def detect(im, param_vals):
    """
    Detect number plates in an image.

    :param im:
        Image to detect number plates in.

    :param param_vals:
        Model parameters to use. These are the parameters output by the `train`
        module.

    :returns:
        Iterable of `bbox_tl, bbox_br, letter_probs`, defining the bounding box
        top-left and bottom-right corners respectively, and a 7,36 matrix
        giving the probability distributions of each letter.

    """

    # Convert the image to various scales.
    scaled_ims = list(make_scaled_ims(im,
                                      model.WINDOW_SHAPE))  # 针对需检测图片产生迭代缩放图片列表

    # Load the model which detects number plates over a sliding window.
    x, y, params = model.get_detect_model()  # 获取检测网络(5个卷积层)的输入,输出以及所有参数

    # Execute the model at each scale.
    with tf.Session(config=tf.ConfigProto()) as sess:  # 建立会话
        y_vals = []
        for scaled_im in scaled_ims:  # 遍历缩放图片列表
            feed_dict = {x: numpy.stack([scaled_im])}  # 建立字典feed_dict{输入:缩放图片}
            feed_dict.update(dict(zip(
                params,
                param_vals)))  # 将字典{参数:参数值}更新到字典feed_dict中->{输入:缩放图片, 参数:参数值}
            y_vals.append(sess.run(y,
                                   feed_dict=feed_dict))  # 提供检测图片和网络参数值,执行检测操作

    # Interpret the results in terms of bounding boxes in the input image.
    # Do this by identifying windows (at all scales) where the model predicts a
    # number plate has a greater than 50% probability of appearing.
    #
    # To obtain pixel coordinates, the window coordinates are scaled according
    # to the stride size, and pixel coordinates.
    for i, (scaled_im,
            y_val) in enumerate(zip(scaled_ims,
                                    y_vals)):  # 枚举元组(缩放图片, 检测输出)及对应下标
        for window_coords in numpy.argwhere(
                y_val[0, :, :, 0] > -math.log(1. / 0.99 - 1)
        ):  # 对应sigmoid输出号码牌存在概率大于0.99的下标
            letter_probs = (y_val[0, window_coords[0], window_coords[1],
                                  1:].reshape(7, len(common.CHARS)))
            letter_probs = common.softmax(letter_probs)  # 车牌号识别正确的概率分布

            img_scale = float(im.shape[0]) / scaled_im.shape[0]  # 缩放比例

            bbox_tl = window_coords * (
                8, 4) * img_scale  # 计算缩放图片上号码牌的boudingbox的左上角坐标
            bbox_size = numpy.array(
                model.WINDOW_SHAPE) * img_scale  # 号码牌的boudingbox的尺寸

            present_prob = common.sigmoid(y_val[0, window_coords[0],
                                                window_coords[1],
                                                0])  # 号码牌是否存在的概率分布

            yield bbox_tl, bbox_tl + bbox_size, present_prob, letter_probs  # 返回号码牌boudingbox的左上角坐标,右下角坐标,
예제 #8
0
hit = 0
xlen = len(test_x)
batch_size = 100

for idx, batch_sidx in enumerate(range(0, xlen, batch_size)):
    print(idx + 1, batch_sidx)
    batch_x = test_x[batch_sidx:batch_sidx + batch_size]

    a1 = np.dot(batch_x, w1) + b1
    z1 = sigmoid(a1)

    a2 = np.dot(z1, w2) + b2
    z2 = sigmoid(a2)

    a3 = np.dot(z2, w3) + b3
    batch_y = softmax(a3)

    batch_predict = np.argmax(batch_y, axis=1)
    # print(batch_predict.shape)

    batch_t = test_t[batch_sidx:batch_sidx + batch_size]
    # print(batch_t)

    batch_hit = np.sum(batch_predict == batch_t)
    hit += batch_hit

    print(f'batch #{idx+1}, batch_hit: {batch_hit}, total hit: {hit}')

# 정확도(Accuracy)
print(f'Accuracy: {hit/xlen}')
예제 #9
0
print(f'a2 = {a2}')  # 100 vector

print('\n== 신호전달 구현4: 은닉 2층 활성함수 h() 적용 ============================')

print(f'a2 dimension: {a2.shape}')  # 100 vector
z2 = sigmoid(a2)
print(f'z2 = {z2}')

print('\n== 신호전달 구현5: 출력층 전달 ============================')

print(f'z2 dimension: {z2.shape}')  # 100 vector
w3 = network['W3']
print(f'w3 dimension: {w3.shape}')  # 100 x 10 matrix
b3 = network['b3']
print(f'b3 dimension: {b3.shape}')  # 10 vector
a3 = np.dot(z2, w3) + b3
print(f'a3 = {a3}')

print('\n== 신호전달 구현6: 출력층 활성함수 σ() 적용 =======================')
print(f'a3 dimension: {a3.shape}')  # 10 vector
y = softmax(a3)  # 10 vector, index = 0~9는 각각 이미지가 0일 확률, 1일 확률 등을 나타낸다.
print(f'y = {y}')  # y의 10개 데이터의 합은 1이다.

print('\n== 예측 결과 ======================================')
predict = np.argmax(
    y)  # 최댓값의 index를 반환한다. np.argmax(y, axis = 0)은 배열의 경우 0열끼리, 1열끼리 한줄씩 비교한다.
print(f'{randidx+1} 번째 이미지 예측: {predict}')

print('\n== 정답 ======================================')
t = test_t[randidx]
print(f'{randidx+1} 번째 이미지 레이블: {t}')  # 실제 들어있는 데이터의 숫자
예제 #10
0
# 출력함수(출력층 활성함수) σ() - 소프트맥스함수(softmax Function)
import os
import sys
import numpy as np
from pathlib import Path
try:
    sys.path.append(os.path.join(Path(os.getcwd()).parent, 'lib'))
    from common import softmax, softmax_overflow
except ImportError:
    print('Library Module Can Not Found')

# test1
a = np.array([0.3, 1., 0.78])
y = softmax(a)
print(y, np.sum(y))

# # test2 큰값: 800.
# a = np.array([0.3, 800., 0.78])
# y = softmax_overflow(a)
# print(y)

# test2: 큰값: 800.
a = np.array([0.3, 800., 0.78])
y = softmax(a)
print(y, np.sum(y))
예제 #11
0
 def loss(self, x, t):
     z = self.predict(x)
     y = softmax(z)
     loss = cross_entropy_error(y, t)
     return loss
예제 #12
0
def loss(w, x, t):
    a = np.dot(x, w)
    y = softmax(a)
    e = cross_entropy_error(y, t)

    return e
예제 #13
0
def calcul_map(model, dataset, beta=1., seuil=0.5):
    seuil_depasse = 0
    grid = np.meshgrid(np.arange(config.cellule_x, dtype=np.float32),
                       np.arange(config.cellule_y, dtype=np.float32))
    grid = np.expand_dims(np.stack(grid, axis=-1), axis=2)
    grid = np.tile(grid, (1, 1, 1, config.nbr_boxes, 1))

    index_labels2 = 0
    labels2_ = labels2 * [
        config.r_x, config.r_y, config.r_x, config.r_y, 1, 1, 1
    ]
    score = []
    tab_nbr_reponse = []
    tab_tp = []
    tab_true_boxes = []

    for images, labels in dataset:

        predictions = np.array(model(images))

        pred_conf = common.sigmoid(predictions[:, :, :, :, 4])
        pred_classes = common.softmax(predictions[:, :, :, :, 5:])
        pred_ids = np.argmax(pred_classes, axis=-1)

        x_center = ((grid[:, :, :, :, 0] +
                     common.sigmoid(predictions[:, :, :, :, 0])) * config.r_x)
        y_center = ((grid[:, :, :, :, 1] +
                     common.sigmoid(predictions[:, :, :, :, 1])) * config.r_y)
        w = (np.exp(predictions[:, :, :, :, 2]) * config.anchors[:, 0] *
             config.r_x)
        h = (np.exp(predictions[:, :, :, :, 3]) * config.anchors[:, 1] *
             config.r_y)

        x_min = x_center - w / 2
        y_min = y_center - h / 2
        x_max = x_center + w / 2
        y_max = y_center + h / 2

        tab_boxes = np.stack([y_min, x_min, y_max, x_max],
                             axis=-1).astype(np.float32)
        tab_boxes = tab_boxes.reshape(
            -1, config.cellule_y * config.cellule_x * config.nbr_boxes, 4)
        pred_conf = pred_conf.reshape(
            -1, config.cellule_y * config.cellule_x * config.nbr_boxes)
        pred_ids = pred_ids.reshape(
            -1, config.cellule_y * config.cellule_x * config.nbr_boxes)

        for p in range(len(predictions)):
            nbr_reponse = np.zeros(config.nbr_classes)
            tp = np.zeros(config.nbr_classes)
            nbr_true_boxes = np.zeros(config.nbr_classes)
            tab_index = tf.image.non_max_suppression(tab_boxes[p],
                                                     pred_conf[p], 100)
            for id in tab_index:
                if pred_conf[p, id] > 0.10:
                    nbr_reponse[pred_ids[p, id]] += 1
                    for box in labels2_[index_labels2]:
                        if not box[5]:
                            break
                        b1 = [
                            tab_boxes[p, id, 1], tab_boxes[p, id, 0],
                            tab_boxes[p, id, 3], tab_boxes[p, id, 2]
                        ]
                        iou = common.intersection_over_union(b1, box)
                        if iou > seuil and box[6] == pred_ids[p, id]:
                            tp[pred_ids[p, id]] += 1
                            seuil_depasse += 1
            for box in labels2[index_labels2]:
                if not box[5]:
                    break
                nbr_true_boxes[int(box[6])] += 1

            tab_nbr_reponse.append(nbr_reponse)
            tab_tp.append(tp)
            tab_true_boxes.append(nbr_true_boxes)

            index_labels2 = index_labels2 + 1

    tab_nbr_reponse = np.array(tab_nbr_reponse)
    tab_tp = np.array(tab_tp)
    tab_true_boxes = np.array(tab_true_boxes)

    ########################
    precision_globule_rouge = tab_tp[:, 1] / (tab_nbr_reponse[:, 1] + 1E-7)
    precision_trophozoite = tab_tp[:, 4] / (tab_nbr_reponse[:, 4] + 1E-7)

    rappel_globule_rouge = tab_tp[:, 1] / (tab_true_boxes[:, 1] + 1E-7)
    rappel_trophozoite = tab_tp[:, 4] / (tab_true_boxes[:, 4] + 1E-7)

    print(
        "F1 score globule rouge",
        np.mean(2 * precision_globule_rouge * rappel_globule_rouge /
                (precision_globule_rouge + rappel_globule_rouge + 1E-7)))
    print(
        "F1 score trophozoite",
        np.mean(2 * precision_trophozoite * rappel_trophozoite /
                (precision_trophozoite + rappel_trophozoite + 1E-7)))

    precision = (precision_globule_rouge + precision_trophozoite) / 2
    rappel = (rappel_globule_rouge + rappel_trophozoite) / 2

    score = np.mean((1 + beta * beta) * precision * rappel /
                    (beta * beta * precision + rappel + 1E-7))
    print("SCORE (globule rouge/trophozoite)", score)
    ########################

    precision = tab_tp / (tab_nbr_reponse + 1E-7)
    rappel = tab_tp / (tab_true_boxes + 1E-7)
    score = np.mean((1 + beta * beta) * precision * rappel /
                    (beta * beta * precision + rappel + 1E-7))

    return score, seuil_depasse
예제 #14
0
파일: layers.py 프로젝트: Huxhh/SimpleNet
    def forward(self, x, t):
        self.t = t
        self.y = softmax(x)
        self.loss = cross_entropy_error(self.y, self.t)

        return self.loss
예제 #15
0
def forward_propagation(x):
    y = softmax(x)
    return y
예제 #16
0
 def forward(self, a, t):
     self.y = co.softmax(a)
     self.t = t
     self.loss = co.cross_entropy_error(self.y, self.t)
     return self.loss
예제 #17
0
z2 = sigmoid(a2)
print(f'z2 = {z2}')

print('\n== 신호전달 구현5: 출력층 전달 ============================')

print(f'z2 dimension: {z2.shape}')      # 100 vector
w3 = network['W3']
print(f'w3 dimension: {w3.shape}')      # 100 x 10 matrix
b3 = network['b3']
print(f'b3 dimension: {b3.shape}')      # 10 vector
a3 = np.dot(z2, w3) + b3
print(f'a3 = {a3}')

print('\n== 신호전달 구현6: 출력층 활성함수 σ() 적용 =======================')
print(f'a3 dimension: {a3.shape}')      # 10 vector
y = softmax(a3)
print(f'y = {y}')

print('\n== 예측 결과 ======================================')
predict = np.argmax(y)
print(f'{randidx+1} 번재 이미지 예측: {predict}')

print('\n== 정답 ======================================')
t = test_t[randidx]
print(f'{randidx+1} 번재 이미지 레이블: {t}')





예제 #18
0
xlen = len(test_x)  # 10000
batch_size = 100

for idx, batch_sidx in enumerate(range(
        0, xlen, batch_size)):  # 1부터 10000까지의 데이터를 100개 단위로 나누어 연산.
    batch_x = test_x[batch_sidx:batch_sidx +
                     batch_size]  # 0~99 / 100~199 등등 # (100, 784) tensor 2

    a1 = np.dot(batch_x, w1) + b1  # (100, 784) * (784 * 50) = (100 * 50)
    z1 = sigmoid(a1)  # 은닉층 활성 함수

    a2 = np.dot(z1, w2) + b2  # (100 * 50) * (50 * 100) = (100 * 100)
    # b2는 100 vector이지만 Broadcasting되어 100행으로 늘어나 더해진다.
    z2 = sigmoid(a2)  # 은닉층 활성 함수
    a3 = np.dot(z2, w3) + b3  # (100 * 100) * (100 * 10) = (100 * 10)
    batch_y = softmax(a3)  # 출력층 활성 함수
    batch_predict = np.argmax(batch_y, axis=1)
    # batch_y (100 * 10) matrix는 100개의 데이터가 10개의 열을 가지는데 10개의 열의 합은 1이다.
    # 또 각각의 index는 이미지가 나타내는 숫자를 의미하고 데이터 값은 그 숫자일 확률을 의미한다.
    # 고로 각 행의 최댓값의 index를 반환하는 argmax()를 이용하면 예측한 값을 알 수 있다.

    batch_t = test_t[batch_sidx:batch_sidx + batch_size]

    batch_hit = np.sum(batch_predict == batch_t)
    # 'batch_predict == batch_t'를 출력하면 True / False 값이 반환되고
    # 이를 np.sum()해주면 batch 안의 데이터 중 올바르게 예측한 숫자를 알 수 있다.
    hit += batch_hit

    print(f'batch #{idx+1}, batch hit: {batch_hit}, total hit:{hit}')

# 정확도(Accuracy)