scorematrix = np.random.randn(C + 1, T) scorematrix -= np.max(scorematrix, axis=0) scorematrix = np.exp(scorematrix) scorematrix /= np.sum(scorematrix, axis=0) blank = C B = 1 for _ in range(10): seq = np.floor(np.random.rand(L) * C).astype(np.int32) time0 = time.time() NLL_cython = ctc_cython(scorematrix=scorematrix, queryseq=np.array(seq, dtype=np.int32), blank=blank) time1 = time.time() NLL_numba, alphas = ctc_numba(scorematrix, seq, blank=blank) time2 = time.time() NLL_theano_nonbatch = f2(scorematrix, seq, blank) time3 = time.time() y = seq.reshape([L, B]) yhat = scorematrix.T.reshape([T, C + 1, B]) NLL_theano_batch = f1(y, yhat, np.ones_like(y, dtype=np.float32), np.ones([T, 1], dtype=np.float32), blank) time4 = time.time() print( "NLL_cython = %f, NLL_numba = %f, NLL_theano_nonbatch = %f, NLL_theano_batch = %f" % (NLL_cython, NLL_numba, NLL_theano_nonbatch[0], NLL_theano_batch) ) print(
TE += te TD += td LOSS += ctcloss*batch n += batch aveloss = LOSS / n print('epoch = %d, batch = %d, ave_loss = %0.4f, ave_CER = %0.4f, progress = %0.1f' % (j, i, aveloss, TE/TD, n/B)) # print('batch loss = %0.4f, CER = %0.4f' % (ctcloss, cer)) # print() b, t, cp = scorematrixT.shape scorematrix = scorematrixT.T.reshape(cp, t) resultseq2 = best_path_decode(scorematrix) rl = np.sum(resultseq_mask) queryseq = y_train2[i,:].astype(np.int32) ctcloss2 = ctc_numba(scorematrix, queryseq)[0] te2 = editdist(resultseq2, list(queryseq)) td2 = len(queryseq) TE2 += te2 TD2 += td2 print('batch loss = %0.4f, ctc_numba = %0.4f, batch_CER = %0.4f, batch_CER2 = %0.4f' % (ctcloss, ctcloss2, cer, te2/td2)) print('ave_CER = %0.4f, ave_CER2 = %0.4f' % (TE/TD, TE2/TD2)) print() resultseq1 = list((resultseq[0:rl].T).astype(np.int32)[0]) if resultseq1 != resultseq2: print('result seq not same') print('resultseq1 =', resultseq1) print('resultseq2 =', resultseq2)
scorematrix = np.random.randn(C+1, T) scorematrix -= np.max(scorematrix, axis=0) scorematrix = np.exp(scorematrix) scorematrix /= np.sum(scorematrix, axis=0) queryseq = np.array([0, 1, 2, 1, 1]) blank = -1 # with open('myctcdebug2.pkl', 'rb') as fl: # scorematrix, queryseq, blank = pickle.load(fl) # fl.close() # print(scorematrix.shape) # print(queryseq.shape) # print(blank) time0 = time.time() result0 = ctc_numba(scorematrix, queryseq, blank) NLL0 = result0[0] time1 = time.time() result1 = f(scorematrix, queryseq, blank) NLL1 = result1[0] time2 = time.time() print('NLL0 = %f, NLL1 = %f' % (NLL0, NLL1)) print('time0 = %0.2f, time1 = %0.2f' %(time1-time0, time2-time1)) # f.profile.print_summary() # print(result1[1][-1]) # print('result1.shape = ', result1[1][-1].shape) # print(result0[1]) # print('result0.shape = ', result0[1].shape)