Skip to content

Commit

Permalink
Added support for all kaldi compression methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
t13m committed Dec 13, 2017
1 parent 71adbec commit c3f83fe
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 3 deletions.
Binary file added example/data/matrix-comp1.ark
Binary file not shown.
2 changes: 2 additions & 0 deletions example/data/matrix-comp1.scp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mat-1 matrix-comp1.ark:6
mat-2 matrix-comp1.ark:50
File renamed without changes.
2 changes: 2 additions & 0 deletions example/data/matrix-comp2.scp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mat-1 matrix-comp2.ark:6
mat-2 matrix-comp2.ark:73
Binary file added example/data/matrix-comp3.ark
Binary file not shown.
2 changes: 2 additions & 0 deletions example/data/matrix-comp3.scp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mat-1 matrix-comp3.ark:6
mat-2 matrix-comp3.ark:50
Binary file added example/data/matrix-comp4.ark
Binary file not shown.
2 changes: 2 additions & 0 deletions example/data/matrix-comp4.scp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mat-1 matrix-comp4.ark:6
mat-2 matrix-comp4.ark:50
Binary file added example/data/matrix-comp5.ark
Binary file not shown.
2 changes: 2 additions & 0 deletions example/data/matrix-comp5.scp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mat-1 matrix-comp5.ark:6
mat-2 matrix-comp5.ark:42
Binary file added example/data/matrix-comp6.ark
Binary file not shown.
2 changes: 2 additions & 0 deletions example/data/matrix-comp6.scp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mat-1 matrix-comp6.ark:6
mat-2 matrix-comp6.ark:42
Binary file added example/data/matrix-comp7.ark
Binary file not shown.
2 changes: 2 additions & 0 deletions example/data/matrix-comp7.scp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mat-1 matrix-comp7.ark:6
mat-2 matrix-comp7.ark:42
2 changes: 0 additions & 2 deletions example/data/matrix.compressed.scp

This file was deleted.

16 changes: 16 additions & 0 deletions example/data/matrix.scp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mat-1 matrix-comp1.ark:6
mat-2 matrix-comp1.ark:50
mat-1 matrix-comp2.ark:6
mat-2 matrix-comp2.ark:73
mat-1 matrix-comp3.ark:6
mat-2 matrix-comp3.ark:50
mat-1 matrix-comp4.ark:6
mat-2 matrix-comp4.ark:50
mat-1 matrix-comp5.ark:6
mat-2 matrix-comp5.ark:42
mat-1 matrix-comp6.ark:6
mat-2 matrix-comp6.ark:42
mat-1 matrix-comp7.ark:6
mat-2 matrix-comp7.ark:42
mat-1 matrix.nocompress.ark:6
mat-2 matrix.nocompress.ark:59
63 changes: 63 additions & 0 deletions example/data/ref.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
comp1
mat-1 [
1 2.000015 2.999969 3.999985
2.000015 2.999969 3.999985 5 ]
mat-2 [
7 7 7 7
2 3 4 5 ]

comp2
mat-1 [
1 2.000015 2.999969 3.999985
2.000015 3 4 5 ]
mat-2 [
7 7 7 7
2 3 4 5 ]

comp3
mat-1 [
1 2.000015 2.999969 3.999985
2.000015 2.999969 3.999985 5 ]
mat-2 [
7 7 7 7
2 3 4 5 ]

comp4
mat-1 [
1 2 3 4
2 3 4 5 ]
mat-2 [
7 7 7 7
2 3 4 5 ]

comp5
mat-1 [
1 2.003922 2.992157 3.996078
2.003922 2.992157 3.996078 5 ]
mat-2 [
7 7 7 7
2 3 4 5 ]

comp6
mat-1 [
1 2 3 4
2 3 4 5 ]
mat-2 [
7 7 7 7
2 3 4 5 ]

comp7
mat-1 [
1 1 1 1
1 1 1 1 ]
mat-2 [
1 1 1 1
1 1 1 1 ]

nocomp
mat-1 [
1 2 3 4
2 3 4 5 ]
mat-2 [
7 7 7 7
2 3 4 5 ]
27 changes: 27 additions & 0 deletions example/read-matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sys
import tensorflow as tf
kaldi_module = tf.load_op_library("../../cmake-build-debug/libkaldi_readers.so")

def main():
if len(sys.argv) != 2:
print("Usage: python read-matrix.py /path/to/filename.scp")
return 1
scpfile = sys.argv[1]
with open(scpfile) as fin:
scplist = fin.readlines()
scplist = [scpitem.strip().split()[1] for scpitem in scplist]
value_rspecific = "./data/matrix.nocompress.ark:59"
rspec = tf.placeholder(tf.string)
feats_value = kaldi_module.read_and_decode_kaldi_matrix(rspec, left_padding=3, right_padding=4)
#feats_value.set_shape([None, 4])
feats_value.set_shape([None, None])
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for rspec_value in scplist:
feats = sess.run(feats_value, feed_dict={rspec: rspec_value})
print(rspec_value)
print(feats)


if __name__ == "__main__":
main()
111 changes: 110 additions & 1 deletion kaldi-matrix-direct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,93 @@ namespace tensorflow {
}
}

for (int64 i = 0; i < left_padding_; i ++) {
for (int j = 0; j < h.num_cols; j ++) {
*(out_data + i * h.num_cols + j) = *(out_data + left_padding_ * h.num_cols + j);
}
}
for (int64 i = left_padding_ + h.num_rows; i < left_padding_ + h.num_rows + right_padding_; i ++) {
for (int j = 0; j < h.num_cols; j ++) {
*(out_data + i * h.num_cols + j) = *(out_data + (left_padding_ + h.num_rows - 1) * h.num_cols + j);
}
}
} else if (data_holder == "CM2") {
rel_offset ++;
GlobalHeader h;
h.format = 2;
OP_REQUIRES_OK(context, file->Read(ark_offset + rel_offset, sizeof(h) - 4, &data_holder,
reinterpret_cast<char*>(&h) + 4));
rel_offset += (sizeof(h) - 4);
out_shape.AddDim(left_padding_ + h.num_rows + right_padding_);
out_shape.AddDim(h.num_cols);
Tensor* output_tensor = nullptr;
OP_REQUIRES_OK(
context, context->allocate_output("output", out_shape, &output_tensor));
auto out = output_tensor->flat<float>();

uint64 size = DataSize(h);
uint64 remaining_size = size - sizeof(GlobalHeader);
string compressed_buffer;
compressed_buffer.resize(remaining_size);
OP_REQUIRES_OK(context, file->Read(ark_offset + rel_offset, remaining_size, &data_holder,
&compressed_buffer[0]));
rel_offset += remaining_size;

float* out_data = out.data();
const char* in_data = compressed_buffer.data();

const uint16 *in_data_uint16 = reinterpret_cast<const uint16*>(in_data);
float min_value = h.min_value;
float increment = h.range * (1.0 / 65535.0);
for (int64 i = left_padding_; i < left_padding_ + h.num_rows; i++) {
for (int64 j = 0; j < h.num_cols; j++) {
*(out_data + i * h.num_cols + j) = min_value + in_data_uint16[j] * increment;
}
in_data_uint16 += h.num_cols;
}
for (int64 i = 0; i < left_padding_; i ++) {
for (int j = 0; j < h.num_cols; j ++) {
*(out_data + i * h.num_cols + j) = *(out_data + left_padding_ * h.num_cols + j);
}
}
for (int64 i = left_padding_ + h.num_rows; i < left_padding_ + h.num_rows + right_padding_; i ++) {
for (int j = 0; j < h.num_cols; j ++) {
*(out_data + i * h.num_cols + j) = *(out_data + (left_padding_ + h.num_rows - 1) * h.num_cols + j);
}
}
} else if (data_holder == "CM3") {
rel_offset ++;
GlobalHeader h;
h.format = 3;
OP_REQUIRES_OK(context, file->Read(ark_offset + rel_offset, sizeof(h) - 4, &data_holder,
reinterpret_cast<char*>(&h) + 4));
rel_offset += (sizeof(h) - 4);
out_shape.AddDim(left_padding_ + h.num_rows + right_padding_);
out_shape.AddDim(h.num_cols);
Tensor* output_tensor = nullptr;
OP_REQUIRES_OK(
context, context->allocate_output("output", out_shape, &output_tensor));
auto out = output_tensor->flat<float>();

uint64 size = DataSize(h);
uint64 remaining_size = size - sizeof(GlobalHeader);
string compressed_buffer;
compressed_buffer.resize(remaining_size);
OP_REQUIRES_OK(context, file->Read(ark_offset + rel_offset, remaining_size, &data_holder,
&compressed_buffer[0]));
rel_offset += remaining_size;

float* out_data = out.data();
const char* in_data = compressed_buffer.data();

float min_value = h.min_value, increment = h.range * (1.0 / 255.0);
const uint8 *in_data_bytes = reinterpret_cast<const uint8*>(in_data);
for (int64 i = left_padding_; i < left_padding_ + h.num_rows; i++) {
for (int64 j = 0; j < h.num_cols; j ++) {
*(out_data + i * h.num_cols + j) = h.min_value + in_data_bytes[j] * increment;
}
in_data_bytes += h.num_cols;
}
for (int64 i = 0; i < left_padding_; i ++) {
for (int j = 0; j < h.num_cols; j ++) {
*(out_data + i * h.num_cols + j) = *(out_data + left_padding_ * h.num_cols + j);
Expand All @@ -150,11 +237,20 @@ namespace tensorflow {
}
}
} else {
OP_REQUIRES_OK(context, Status(error::UNAVAILABLE, "Unknown Kaldi Matrix."));
OP_REQUIRES_OK(context, Status(error::UNAVAILABLE,
"Unknown Kaldi Matrix:" + data_holder.ToString() +
" When reading \"" + half_scp_line + "\"" +
" Ark: " + ark_path +
" OFFSET: " + std::to_string(ark_offset) ));
}
}
private:
int64 left_padding_, right_padding_;
enum DataFormat {
kOneByteWithColHeaders = 1,
kTwoByte = 2,
kOneByte = 3
};
struct GlobalHeader {
int32 format; // Represents the enum DataFormat.
float min_value; // min_value and range represent the ranges of the integer
Expand Down Expand Up @@ -185,6 +281,19 @@ namespace tensorflow {
return p75 + (p100 - p75) * (value - 192) * (1/63.0f);
}
}
uint64 DataSize(const GlobalHeader& header) {
DataFormat format = static_cast<DataFormat>(header.format);
if (format == kOneByteWithColHeaders) {
return sizeof(GlobalHeader) +
header.num_cols * (sizeof(PerColHeader) + header.num_rows);
} else if (format == kTwoByte) {
return sizeof(GlobalHeader) +
2 * header.num_rows * header.num_cols;
} else {
return sizeof(GlobalHeader) +
header.num_rows * header.num_cols;
}
}
};
REGISTER_KERNEL_BUILDER(Name("ReadAndDecodeKaldiMatrix").Device(DEVICE_CPU), ReadAndDecodeKaldiMatrixOp);

Expand Down

0 comments on commit c3f83fe

Please sign in to comment.