示例#1
0
    def forward(self, input, target, labels):
        _assert_no_grad(target)

        loss = torch.abs(input - target)

        lptnums = [lptnum_of(label) for label in labels]

        img_chars_l = []
        for i, lptnum in enumerate(lptnums):
            for j, _ in enumerate(loss[i, :, self.hb:self.he, :]):
                img_chars_l.append(
                    self._get_img_chars(lptnum, loss[i, j,
                                                     self.hb:self.he, :]))

        # test only
        # for i in range(len(img_chars_l)):
        # k=0
        # for im, c in zip(img_chars_l[i][0],img_chars_l[i][1]):
        # utils.save_image(im,"{}_{}_{}.png".format(i,c,k),normalize=True, range=(0,255))
        # k += 1
        # raise SystemExit

        # Split img_char by char
        splited_chars_l = {}
        for i in range(len(img_chars_l)):
            for im, c in zip(img_chars_l[i][0], img_chars_l[i][1]):
                splited_chars_l[c] = splited_chars_l.get(c, [])
                splited_chars_l[c].append(im)

        min = 1000000
        max = 0
        hist = [[c, len(splited_chars_l[c])] for c in splited_chars_l.keys()]
        for h in hist:
            if min > h[1]:
                min = h[1]
            if max < h[1]:
                max = h[1]
        for i, h in enumerate(hist):
            if max - min == 0:
                hist[i][1] = 2
            else:
                hist[i][1] = 2 - (h[1] - min) / (max - min)

        for c in splited_chars_l.keys():
            l = splited_chars_l[c]
            for i, _ in enumerate(l):
                for h in hist:
                    if h[0] == c:
                        l[i] *= h[1]
                        break

        return loss.mean()
    def forward(self, input, target, labels):
        _assert_no_grad(target)

        loss = torch.abs(input - target)

        lptnums = [lptnum_of(label) for label in labels]

        img_chars_l = []
        for i, lptnum in enumerate(lptnums):
            for j, _ in enumerate(loss[i, :, self.hb:self.he, :]):
                img_chars_l.append(
                    self._get_img_chars(lptnum, loss[i, j,
                                                     self.hb:self.he, :]))

        # test only
        # for i in range(len(img_chars_l)):
        # k=0
        # for im, c in zip(img_chars_l[i][0],img_chars_l[i][1]):
        # utils.save_image(im,"{}_{}_{}.png".format(i,c,k),normalize=True, range=(0,255))
        # k += 1
        # raise SystemExit

        # Split img_char by char
        splited_chars_l = {}
        for i in range(len(img_chars_l)):
            for im, c in zip(img_chars_l[i][0], img_chars_l[i][1]):
                splited_chars_l[c] = splited_chars_l.get(c, [])
                splited_chars_l[c].append(im)

        # Calculate mean loss
        mean_char_loss = []
        total_char_area = 0
        for k in splited_chars_l.keys():
            sum_k = 0
            sum_s = 0
            for i in range(len(splited_chars_l[k])):
                sum_k += splited_chars_l[k][i].sum()
                sum_s += product(splited_chars_l[k][i].size())
                splited_chars_l[k][i] = 0
            mean_char_loss.append(sum_k / sum_s)
            total_char_area += sum_s

        bg_loss = loss.sum() / (product(loss.size()) - total_char_area)
        mean_char_loss.append(bg_loss)

        return sum(mean_char_loss) / len(mean_char_loss)
示例#3
0
    L1 = [(Cx[0],Cy[0]-1)]
    L1.append((L1[0][0] + char_size[0] + Cx[1], Cy[0]-1))
    L1.append((L1[1][0] + char_size[0] + Cx[2]+1, Cy[0]-1))
    L1.append((L1[2][0] + char_size[0] + Cx[1], Cy[0]-1))
    L1.append((Cx[0], Cy[0] + char_size[1] + Cy[1]))
    L1.append((L1[4][0] + char_size[0] + Cx[3], L1[4][1]))
    L1.append((L1[5][0] + char_size[0] + Cx[3], L1[4][1]))
    L1.append((L1[6][0] + char_size[0] + Cx[3], L1[4][1]))
    L2 = [(Dx[0], Cy[0] + char_size[1] + Cy[1])]
    L2.append((L2[0][0] + char_size[0] + Dx[1], L2[0][1]))
    L2.append((L2[1][0] + char_size[0] + Dx[1], L2[0][1]))
    L2.append((L2[2][0] + char_size[0] + Dx[2], L2[0][1]))
    L2.append((L2[3][0] + char_size[0] + Dx[1], L2[0][1]))

    # print(path.basename(filename)) # debug
    lpt_num = lptnum_of(path.basename(filename), sep=opt.sep)
    lptnum = lpt_num.split("_")
    lptnum[1] = lptnum[1].replace(".","")

    if len(lptnum[0]) == 5: # e.g. 59MĐ1_123.45
        raise NotImplementedError
    elif len(lptnum[0]) == 3: # e.g. 59F_123.45
        raise NotImplementedError
    elif len(lptnum[0]) == 4: # e.g. 59Z1_123.45
        img_chars = [img[L1[i][1]:char_size[1]+L1[i][1], L1[i][0]:char_size[0]+L1[i][0]] for i in range(len(lptnum[0]))]
        compareimg_chars = [compareimg[L1[i][1]:char_size[1]+L1[i][1], L1[i][0]:char_size[0]+L1[i][0]] for i in range(len(lptnum[0]))]
    else:
        raise ValueError("len(lptnum[0]) != 3 or 4 or 5")

    if len(lptnum[1]) == 4: # e.g. 59Z1_1234
        img_chars.extend([img[L1[i+len(lptnum[0])][1]:L1[i+len(lptnum[0])][1]+char_size[1], L1[i+len(lptnum[0])][0]:L1[i+len(lptnum[0])][0]+char_size[0]] for i in range(len(lptnum[1]))])
with open(opt.textfile) as f:
    lines = f.read().splitlines()

alphabet = [
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
    'F', 'G', 'H', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'X', 'Y',
    'Z'
]

char_count = {}
hist = {}
for line in lines:
    linesplt = line.split(":")
    try:
        truth = lptnum_of(linesplt[0])
    except IndexError:
        truth = None
    if truth == None:
        print("The string \"{}\" is wrong lptnum format.".format(linesplt[0]))
        continue
    recover = linesplt[-1].strip(' ')
    for t, r in zip(truth, recover):
        if t == "_" or t == ".":
            continue
        if t != r:
            hist[(t, r)] = hist.get((t, r), 0) + 1
            char_count[t] = char_count.get(t, 0) + 1

for k in hist.keys():
    hist[k] /= char_count[k[0]]