0

This is the code for the input pipeline. It's resizing images to (224,224,3) as input and (224,224,2) as output.

image_path_list = glob.glob('/content/drive/My Drive/datasets/imagenette/*')
data = tf.data.Dataset.list_files(image_path_list)

def tf_rgb2lab(image):
  im_shape = image.shape
  [image,] = tf.py_function(color.rgb2lab, [image], [tf.float32])
  image.set_shape(im_shape)
  return image

def preprocess(path):
  image = tf.io.read_file(path)
  image = tf.image.decode_jpeg(image, channels=3)
  image = tf.image.convert_image_dtype(image, tf.float32)
  image = tf.image.resize(image, [224, 224])
  image = tf_rgb2lab(image)
  L = image[:,:,0]/100.
  ab = image[:,:,1:]/128.
  input = tf.stack([L,L,L], axis=2)
  return input, ab

train_ds = data.map(preprocess, tf.data.experimental.AUTOTUNE).batch(64).repeat()
train_ds = data.prefetch(tf.data.experimental.AUTOTUNE)

The following is the code for the model. I don't think anything is wrong with the model, since it works when I call model.predict() on an image. So I'm assuming something is wrong with the input pipeline but I can't figure out what is since its my first time working with tf.data.

vggmodel = tf.keras.applications.VGG16(include_top=False, weights='imagenet')
model = tf.keras.Sequential()
for i,layer in enumerate(vggmodel.layers):
  model.add(layer)
for layer in model.layers:
  layer.trainable=False

model.add(tf.keras.layers.Conv2D(256, (3,3), padding='same', activation='relu'))
model.add(tf.keras.layers.UpSampling2D((2,2)))
model.add(tf.keras.layers.Conv2D(128, (3,3), padding='same', activation='relu'))
model.add(tf.keras.layers.UpSampling2D((2,2)))
model.add(tf.keras.layers.Conv2D(64, (3,3), padding='same', activation='relu'))
model.add(tf.keras.layers.UpSampling2D((2,2)))
model.add(tf.keras.layers.Conv2D(16, (3,3), padding='same', activation='relu'))
model.add(tf.keras.layers.UpSampling2D((2,2)))
model.add(tf.keras.layers.Conv2D(8, (3,3), padding='same', activation='relu'))
model.add(tf.keras.layers.Conv2D(2, (3,3), padding='same', activation='tanh'))
model.add(tf.keras.layers.UpSampling2D((2,2)))

Anyways when I print(train_ds) I get:

<PrefetchDataset shapes: (), types: tf.string>

I tried the following piece of code:

path = next(iter(train_ds))
L,ab = preprocess(path)
L.shape

and I got

TensorShape([224, 224, 3])

which means it is returning a 3 dimensional tensor. Then why do I get the error when I call:

model.fit(train_ds, epochs=1, steps_per_epoch=steps, callbacks=[model_checkpoint_callback, early_stopping_callback])

2 Answers 2

0

layer.trainable=False & model.fit are contrary . While the former says to set the model for inference only and closes backpropogation, model.fit is used for training. Probably you were looking for model.predict?

Sign up to request clarification or add additional context in comments.

2 Comments

layer.trainable = False, freezes the layer thus not updating its parameters and not performing backprop on those layers. However layers following it have been added manually and can be trained. So that's not the issue. And the error is related with the input dimensions being wrong.
Oops, missed that he was adding layers later on, my bad!
0

So yeah, It took some time but I figured it out. It was a pretty stupid mistake.

train_ds = data.map(preprocess, tf.data.experimental.AUTOTUNE).batch(64).repeat()
train_ds = data.prefetch(tf.data.experimental.AUTOTUNE)

It actually should be:

train_ds = data.map(preprocess, tf.data.experimental.AUTOTUNE).batch(64).repeat().prefetch(tf.data.experimental.AUTOTUNE)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.