|
| 1 | +#include <tensorflow/core/framework/tensor.h> |
| 2 | +#include <tensorflow/core/lib/io/path.h> |
| 3 | +#include <tensorflow/core/platform/env.h> |
| 4 | + |
| 5 | +// Tensorflow provides support to perform I/O operations |
| 6 | +// as well. Normally you should be able to perform these types |
| 7 | +// of operations using the library/framework of your choice. |
| 8 | +// I believe reason tensorflow provides the support is to provide |
| 9 | +// an abstraction layer for various platforms it supports. |
| 10 | +// |
| 11 | +// In this example we are going to look at few I/O related |
| 12 | +// apis. |
| 13 | +// |
| 14 | +// Run this example as: |
| 15 | +// <path_to>/bin/intro-basic-io <path_to>/data/hello.txt |
| 16 | + |
| 17 | +int main(int argc, char** argv) { |
| 18 | + if (argc != 2) { |
| 19 | + std::cerr << "Provide the path to the input file .." << std::endl; |
| 20 | + std::cout << "Usage: intro-basic-io data/hello.txt" << std::endl; |
| 21 | + return -1; |
| 22 | + } |
| 23 | + |
| 24 | + auto filePath = std::string(argv[1]); |
| 25 | + |
| 26 | + // The first thing to do is to get a handle on the underlying |
| 27 | + // platform / OS on which this app is running. |
| 28 | + auto env = tensorflow::Env::Default(); |
| 29 | + |
| 30 | + // check if a file provided as the input argument exists or not |
| 31 | + auto status = env->FileExists(filePath); |
| 32 | + if (status.ok() == false) { |
| 33 | + std::cerr << "Invalid path to the file .." << std::endl; |
| 34 | + return -1; |
| 35 | + } |
| 36 | + |
| 37 | + // get the size of the file |
| 38 | + tensorflow::uint64 file_size; |
| 39 | + status = env->GetFileSize(filePath, &file_size); |
| 40 | + |
| 41 | + if (status.ok() == false) { |
| 42 | + std::cerr << "Unable to get the size of the file .." << std::endl; |
| 43 | + return -1; |
| 44 | + } |
| 45 | + |
| 46 | + std::cout << "File size is - " << file_size << std::endl; |
| 47 | + |
| 48 | + // In order to read the content of the file |
| 49 | + // we first get a file handle |
| 50 | + |
| 51 | + std::unique_ptr<tensorflow::RandomAccessFile> theFile; |
| 52 | + status = env->NewRandomAccessFile(filePath, &theFile); |
| 53 | + |
| 54 | + if (status.ok() == false) { |
| 55 | + std::cerr << "Unable to open the file .." << std::endl; |
| 56 | + return -1; |
| 57 | + } |
| 58 | + |
| 59 | + // now using this handle we read it out |
| 60 | + // |
| 61 | + // it would be required to specify from which offset in the file |
| 62 | + // we want to read and how many bytes we want to read |
| 63 | + // |
| 64 | + // it should be evident that trying to read more bytes than the ones |
| 65 | + // in the file will lead to error very much like it happens in any I/O |
| 66 | + // based API. In other words, it is up to the caller to make sure that the |
| 67 | + // arguments of Read api are sane. |
| 68 | + // |
| 69 | + // Read api takes 4 arguments - offset, number of bytes to read, StringPiece |
| 70 | + // and pointer to address where the api will store the result |
| 71 | + // |
| 72 | + |
| 73 | + // allocate the data where we will store the output of the Read api |
| 74 | + std::string content; |
| 75 | + content.resize(file_size); |
| 76 | + |
| 77 | + // create StringPiece which is nothing but a view of any external data |
| 78 | + // location |
| 79 | + // |
| 80 | + // Note that it is the API that is setting StringPiece to the content |
| 81 | + tensorflow::StringPiece sp; |
| 82 | + |
| 83 | + status = theFile->Read(0, file_size, &sp, &(content)[0]); |
| 84 | + |
| 85 | + if (status.ok() == false) { |
| 86 | + std::cerr << "Failed to read the file .." << std::endl; |
| 87 | + return -1; |
| 88 | + } |
| 89 | + |
| 90 | + std::cout << content << std::endl; |
| 91 | + |
| 92 | + // You can now get the data by StringPiece or |
| 93 | + // the original location and convert it into the tensor |
| 94 | + // (in this particular example it would be Scalar) |
| 95 | + tensorflow::Tensor result(tensorflow::DT_STRING, tensorflow::TensorShape()); |
| 96 | + result.scalar<std::string>()() = sp.ToString(); |
| 97 | + |
| 98 | + // As should be clear from this example one could easily use the |
| 99 | + // I/O api that are not necessarily part of tensorflow |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
0 commit comments