Home > Blockchain >  Require 'flatttened_input' when building ANN
Require 'flatttened_input' when building ANN

Time:09-14

Hello i need to build an ANN using binary_alpha_digits from tensorflow but i am unable to pass in the train data inside as it requires 'flatten_input' but I am passing in ['image','label'] dictionary. How do i solve this problem? Appreciate any help on this problem thanks.

from matplotlib import pyplot as plt
import tensorflow as tf
from tensorflow.keras import layers

train_ds, test_ds = tfds.load('BinaryAlphaDigits', 
                              split=['train[:60%]', 'train[60%:]'])
model = tf.keras.Sequential()
model.add(layers.Flatten(input_shape=(28, 28)))         
model.add(layers.Dense(10, activation=tf.nn.relu))      
model.add(layers.Dense(10, activation=tf.nn.relu))     
model.add(layers.Dense(10, activation=tf.nn.softmax))   

model.compile(optimizer= tf.optimizers.Adam(), 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

epochs = 10
model.fit(train_ds, epochs=epochs)


CodePudding user response:

as you feed images into model, so the input shape must have defined in shape (Height, Width, Channel) which refers to image dimensions and color mode and the second one is that you should preprocess dataset before fitting model on it.

Even notice the output layers units for multi-class classification is not set correctly for this dataset, while there are more than 10 labels, based on dataset it contains 39 labels and so the last layer units would be set to 39.

Here i would implement code which work correctly for you with preprocessing function for images and labels, And even notice the images of the dataset are in shape (20, 16, 1) so you could resize images to set it into (28, 28, 1) or just fed model with the images in their size.

After preprocessing, images are grouped by creating batches or mini-batches, and even shuffle training set to avoid high variance on testing set, so the operations below will be have done cause of that

from matplotlib import pyplot as plt
import tensorflow as tf
from tensorflow.keras import layers
import tensorflow_datasets as tfds

train_ds, test_ds = tfds.load('BinaryAlphaDigits', split=['train[:60%]', 'train[60%:]'])

def preprocess(data):
    image = data['image']
    image = tf.image.resize(image, (28, 28))
    label = data['label']
    return image, label

train_ds = train_ds.map(preprocess)
train_ds = train_ds.shuffle(1024)
train_ds = train_ds.batch(batch_size = 32)

test_ds = test_ds.map(preprocess)
test_ds = test_ds.batch(batch_size = 32)

model = tf.keras.Sequential()
model.add(layers.Flatten(input_shape=(28, 28, 1)))         
model.add(layers.Dense(10, activation=tf.nn.relu))      
model.add(layers.Dense(10, activation=tf.nn.relu))     
model.add(layers.Dense(39, activation=tf.nn.softmax))  

model.compile(optimizer= tf.optimizers.Adam(), loss='sparse_categorical_crossentropy', metrics=['accuracy'])

epochs = 10
model.fit(train_ds, epochs=epochs)

CodePudding user response:

tfds.load by default gives a dictionary with image and label as the keys.

train_ds, test_ds = tfds.load('BinaryAlphaDigits', 
                              split=['train[:60%]', 'train[60%:]'])
train_ds = train_ds.shuffle(1024).batch(4)

for x in train_ds.take(1):
  print(type(x))
  print(x['image'].shape, x['label'])

>>>
<class 'dict'>
(4, 20, 16, 1) tf.Tensor([ 6 32  6 12], shape=(4,), dtype=int64)

There is a setting called as_supervised that gives it as a proper dataset. Check docs here

If you use that setting and use proper input and output sizes, your model works

train_ds, test_ds = tfds.load('BinaryAlphaDigits', 
                              split=['train[:60%]', 'train[60%:]'],as_supervised=True)
train_ds = train_ds.shuffle(1024).batch(4)

for x in train_ds.take(1):
  print(type(x))
  print(x[0].shape, x[1])
>>>
<class 'tuple'>
(4, 20, 16, 1) tf.Tensor([13 13 22 31], shape=(4,), dtype=int64)

model = tf.keras.Sequential()
model.add(layers.Flatten(input_shape=(20, 16,1)))         
model.add(layers.Dense(10, activation=tf.nn.relu))      
model.add(layers.Dense(10, activation=tf.nn.relu))     
model.add(layers.Dense(36, activation=tf.nn.softmax))

model.compile(optimizer= tf.optimizers.Adam(), 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

epochs = 10
model.fit(train_ds, epochs=epochs)
>>>
Epoch 1/10
211/211 [==============================] - 1s 3ms/step - loss: 3.5428 - accuracy: 0.0629
Epoch 2/10
211/211 [==============================] - 0s 2ms/step - loss: 3.2828 - accuracy: 0.1105
  • Related