0
$\begingroup$

I've uploaded data from tfds(car196) and I've done some transformation to it(found some guides online) First questions if can I change the data type from :MapDataSet/BatchDataSet to some kind of numpy array?

second is when i try to fit the model with the data i get the following error for some reason

ValueError: Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 300, 300, 3), found shape=(None, None, 3)

and the code is :

import tensorflow as tf
from tensorflow.keras import layers,models
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import os
import numpy as np
import tensorflow_datasets as tfds
tfds.disable_progress_bar()

(raw_train,raw_val,raw_test),dataset_info=tfds.load('cars196',split= 
['train[:80%]','train[80%:90%]','test[90%:]'],
with_info=True,as_supervised=True)
get_label_name=dataset_info.features['label'].int2str
def format_data(image,label):
   image=tf.cast(image,tf.float32)
   image=(image/225.)
   return image,label

train = raw_train.map(format_data)
val = raw_val.map(format_data)
test = raw_test.map(format_data)
BATCH_SIZE = 32
SHUFFLE_BUFFER_SIZE = 1000

train_batches = train.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
validation_batches = val.batch(BATCH_SIZE)
test_batches = test.batch(BATCH_SIZE)
model=tf.keras.Sequential()

model.add(layers.Conv2D(64,(3,3),input_shape=(300,300,3),name='conv2d-1'))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
model.add(layers.Conv2D(32,(3,3),name='conv2d-2'))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
model.add(layers.Conv2D(16,(3,3),name='conv2d-3'))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
model.add(layers.Flatten())
model.add(layers.Dense(500,activation='relu'))
model.add(layers.Dense(196,activation='softmax'))

model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
callback=tf.keras.callbacks.EarlyStopping(monitor='loss',patience=3)
history=model.fit(train,epochs=15,validation_data=validation_batches,callbacks= 
[callback])
$\endgroup$

1 Answer 1

1
$\begingroup$
  1. You can convert any subclass of tf.data.Dataset into numpy via: tfds.as_numpy()

  2. According to knowyourdata, the sizes of images vary. So in the format_data() function, you can simply use tf.image.resize() or tf.image.resize_with_pad() (if you want to avoid distortion) to resize it to a fix dimension(300x300).

I'm having trouble loading the data on colab so let me know if this works and there are no additional bugs in the code. Please put the entire stack of error when you do so.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.