'Is there another way of generating tfrecords?

I am trying to run the following command to

!py C:/Users/Desktop/dataset/workspace/annotations/Annotations/generate_tfrecord.py -x C:/Users/Desktop/dataset/Workspace/images/train' -l 'C:/Users/Desktop/dataset/Workspace/annotations/Annotations/Train' 

However I Have this error raised

'C:/Users/Desktop/dataset/Workspace/annotations/Annotations/Train/train.record' : The filename, directory name, or volume label syntax is incorrect.

; no protocol option

Any thoughts on how to tackle this issue



Solution 1:[1]

that is possible this way I also test with images input, rad it and convert it back to a dataset. I re-solution the picture to scales that fit the functions but the final results are the same as the examples.

[ Sample ]:

import os
from os.path import exists

import tensorflow as tf
import tensorflow_io as tfio

import matplotlib.pyplot as plt

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
print(physical_devices)
print(config)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
PATH = os.path.join('F:\\datasets\\downloads\\Actors\\train\\Pikaploy', '*.tif')
PATH_2 = os.path.join('F:\\datasets\\downloads\\Actors\\train\\Candidt Kibt', '*.tif')
files = tf.data.Dataset.list_files(PATH)
files_2 = tf.data.Dataset.list_files(PATH_2)

list_file = []
list_file_actual = []
list_label = []
list_label_actual = [ 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt' ]
for file in files.take(15):
    image = tf.io.read_file( file )
    image = tfio.experimental.image.decode_tiff(image, index=0)
    list_file_actual.append(image)
    image = tf.image.resize(image, [32,32], method='nearest')
    image = tfio.experimental.color.rgba_to_rgb( image )
    list_file.append(image)
    list_label.append(1)

for file in files_2.take(18):
    image = tf.io.read_file( file )
    image = tfio.experimental.image.decode_tiff(image, index=0)
    list_file_actual.append(image)
    image = tf.image.resize(image, [32,32], method='nearest')
    image = tfio.experimental.color.rgba_to_rgb( image )
    list_file.append(image)
    list_label.append(9)

TFRecord_path = "F:\\models\\checkpoint\\" + os.path.basename(__file__).split('.')[0] + "\\TF_DataSets_01.h5"
TFRecord_dir = os.path.dirname(TFRecord_path)

if not exists(TFRecord_dir) : 
    os.mkdir(TFRecord_dir)
    print("Create directory: " + TFRecord_dir)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Function
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
# Read the data back out.
def decode_fn(record_bytes):
  return tf.io.parse_single_example(
      # Data
      record_bytes,

      # Schema
      {"images": tf.io.FixedLenFeature(shape=( 32 * 32 * 3 ), dtype=tf.int64),
       "labels": tf.io.FixedLenFeature(shape=( 1 ), dtype=tf.int64)}
  )

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: DataSet
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
dataset = tf.data.Dataset.from_tensor_slices((tf.constant(tf.cast(list_file, dtype=tf.int64), shape=(33, 1, 32, 32, 3), dtype=tf.int64),tf.constant(list_label, shape=(33, 1, 1), dtype=tf.int64)))

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = tf.keras.models.Sequential([
    tf.keras.layers.InputLayer(input_shape=( 32, 32, 3 )),
    tf.keras.layers.Normalization(mean=3., variance=2.),
    tf.keras.layers.Normalization(mean=4., variance=6.),
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Reshape((128, 225)),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96, return_sequences=True, return_state=False)),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(192, activation='relu'),
    tf.keras.layers.Dense(10),
])

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Optimizer
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
optimizer = tf.keras.optimizers.Nadam(
    learning_rate=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-07,
    name='Nadam'
)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Loss Fn
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""                               
lossfn = tf.keras.losses.SparseCategoricalCrossentropy(
    from_logits=False,
    reduction=tf.keras.losses.Reduction.AUTO,
    name='sparse_categorical_crossentropy'
)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Summary
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Create TFRecordFile
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
with tf.io.TFRecordWriter( TFRecord_dir + "\\test.tfrecord" ) as file_writer:
    for images, labels in dataset.take(33):
        images = tf.constant( images, shape=( 32 * 32 * 3 ) ).numpy()
        labels = tf.constant( labels, shape=( 1 ) ).numpy()
    
        record_bytes = tf.train.Example(features=tf.train.Features(feature={
            "images": tf.train.Feature(int64_list=tf.train.Int64List(value=images)),
            "labels": tf.train.Feature(int64_list=tf.train.Int64List(value=labels)),
        })).SerializeToString()
        file_writer.write(record_bytes)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Read TFRecordFile
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
filenames = TFRecord_dir + "\\test.tfrecord"

for batch in tf.data.TFRecordDataset([filenames]).map(decode_fn):
    image = tf.constant( batch['images'], shape=( 32, 32, 3 ) )
    image = tf.keras.preprocessing.image.array_to_img( image )
    plt.imshow( image )
    plt.show( )
    plt.close( )

input('...')

[ Output ]: Sample

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1