How to convert the .wav file to tfrecord file? - tensorflow

I am new in the tensorflow part and hope someone can help me.
I've seen this document https://www.tensorflow.org/api_docs/python/tf/audio/decode_wav and executed tf.audio.decode_wav( contents, desired_channels=-1, desired_samples=-1, name=None )
The only thing I've changed is the contents, changing to my path name.
But still get an error!
Any methods to convert it? And I want to output something like this. .tfrecord-00000-of-00008

Read the .wav file into a string of bytes and then decode it:
import tensorflow as tf
wav_contents = tf.io.read_file("file.wav")
audio, sample_rate = tf.audio.decode_wav(contents=wav_contents)
audio.shape
This example was borrowed from the TensorFlow tutorial on reading audio files.

Related

Code for changing multiple .img files into .png, then into numpy array

I'm doing a convolutional neural network classification and currently all my tiles are in .img format (thanks ArcMap). I know I need to get them in .png format, but haven't found code that could convert a whole folder of them. Is that doable?
Eventually I also need to get all those .pngs into a numpy array. I found basic code that will do it for just .png, but is there a way to convert the whole folder at once?
Thanks everyone!
Yes it is doable, just use Python Pil! and loop over all files in the folder with glob.
Some example code:
import os
from PIL import Image
import glob
counter = 0
for image in glob.glob("/Users/Testfolder/*.jpg"):
counter = counter + 1
img = Image.open(image)
img.save('/Users/Testfolder/' + (str(counter)+'img.png'))

"Bad audio format for WAV" when reading a wav file

I have a set of wav files that I want to generate a spectrogram of. But when I use the tf.audio.decode_wav function, I get the following error:
InvalidArgumentError: Bad audio format for WAV: Expected 1 (PCM), but
got7 [Op:DecodeWav]
How do I circumvent this error? Are there any other ways to generate a log mel spectrogram for wav files using tensorflow?
I am aware of librosa package, but I would prefer tensorflow.
The code is:
def decode_audio(audio_binary):
audio, _ = tf.audio.decode_wav(audio_binary)
return tf.squeeze(audio, axis=-1)
def get_waveform_and_label(file_path):
audio_binary = tf.io.read_file(file_path)
waveform = decode_audio(audio_binary)
return waveform
The error tells you that your files indicate that they have samples encoded as 8-bit mulaw.
As described in the TensorFlow documentation for tf.audio.decode_wav, only 16-bit PCM WAV is supported by this method.
You would need to re-encode your wave files prior to passing them to tensorflow. Something like ffmpeg could help here.

Remove header from .MAT files loaded by loadmat() from scipy.io library

I am new to both python and tensorflow.
I am trying to make a input pipeline for a generative adversarial network with input complex number data in .mat format and loaded it with loadmat() from scipy.io library. Now I am trying to prepare my data for giving input to my network and i tried from_tensor_slices(). But it can not be converted into tensor because of the headers in it. I looked up how to remove header from files by python and found some techniques that can be applied to .csv file but nothing on .mat files. How can I remove the header from .mat files? Also, the loadmat() function returns a list of dictionary I think. How can I extract the data from the file under such condition? Thank you.

Tensorflow Serving With Object Detection

I am trying to deploy a model based on Object Detection example to do some tests and I am getting this error:
"Expects arg[0] to be uint8 but float is provided"
In that case I am using this to load my data:
request.inputs['inputs'].CopyFrom(
tf.contrib.util.make_tensor_proto({FLAGS.input_image}))
where FLAGS.input_image is my image data in bytes.
I was thinking that maybe that I should convert my image bytes to something that this input understands, but I haven't found yet.
What could I do to fix this issue?
Thanks !!!!
To convert the image to bytes, use the following in client code (python)
with open(FLAGS.image, 'rb') as f:
data = f.read()
Also please find a sample client (for inception model in python) as follows https://github.com/tensorflow/serving/blob/master/tensorflow_serving/example/inception_client.py

No Tensorflow decoder for TIFF images?

I have noticed that Tensorflow provides standard procedures for decoding jpeg, png and gif images after reading files. For instance for png:
import tensorflow as tf
filename_queue = tf.train.string_input_producer(['/Image.png']) # list of files to read
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
decoded_image = tf.image.decode_png(value) # use png or jpg decoder based on your files.
However, the tiff format decoder seems to be missing.
So what solutions exist for tiff files? Surely, I could convert my input images to png, but this doesn't seem to be a very smart solution.
There's currently no decoder for TIFF images. Look in tensorflow/core/kernels and you see
decode_csv_op.cc
decode_gif_op.cc
decode_jpeg_op.cc
decode_png_op.cc
decode_raw_op.cc
No decode_tiff_op.cc. This could be a good target for community contribution.
As of February 2019, some (limited & experimental) TIFF support has been added as part of the Tensorflow I/O library:
Added a very preliminary TIFF support. TIFF format is rather complex so compressions such as JPEG have not been supported yet, but could be added if needed.
The following methods are currently available:
tfio.experimental.image.decode_tiff
Decode a TIFF-encoded image to a uint8 tensor.
tfio.experimental.image.decode_tiff_info
Decode a TIFF-encoded image meta data.
An example usage from a Tensorflow tutorial:
import tensorflow as tf
import tensorflow.io as tfio
...
def parse_image(img_path: str) -> dict:
...
image = tf.io.read_file(img_path)
tfio.experimental.image.decode_tiff(image)
...
If tf.experimental.image.decode_tiff() won't work for you (as it won't work with my 32-bit TIFF files), you could try using cv2 as described in the answer to this post.
Other options are to use the .map() function with (a) rasterio, (b) skimage, or (c) pillow packages.