I want to use the pre-trained model MobileNetV2 in order to classify the Binary Alpha Data. However, this data comes in shape (20, 16, 1)
(greyscale one channel) and not as needed (20, 16, 3)
(3 RGB channel). Actually I also have to resize, since 20x16 is neither a valid input, but I know how to do this. So my question is how can I get a 1 channel grescale (DatasetV1Adapter
) into a 3 channel?
My code so far:
import tensorflow as tf
import os
import PIL
import numpy as np
import matplotlib.pyplot as plt
import tensorflow_datasets as tfds
import tensorflow_hub as hub
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import keras_preprocessing
from keras_preprocessing import image
from tensorflow.python.keras.utils.version_utils import training
from tensorflow.keras.optimizers import RMSprop
(raw_train, raw_test, raw_validation), metadata = tfds.load(
'binary_alpha_digits',
split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'],
with_info=True,
as_supervised=True,
)
IMG_SIZE = 96
def format_example(image, label):
image = tf.cast(image, tf.float32)
image = image*1/255.0
image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
return image, label
train = raw_train.map(format_example)
validation = raw_validation.map(format_example)
test = raw_test.map(format_example)
BATCH_SIZE = 32
SHUFFLE_BUFFER_SIZE = 1000
train_batches = train.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
test_batches = test.batch(BATCH_SIZE)
validation_batches = validation.batch(BATCH_SIZE)
When I check train_batches
I get the output:
<DatasetV1Adapter shapes: ((None, 96, 96, 1), (None,)), types: (tf.float32, tf.int64)>
This gives an error when trying to fit later:
ValueError: The input must have 3 channels; got `input_shape=(96, 96, 1)`
So according to this post I tried:
def load_image_into_numpy_array(image):
# The function supports only grayscale images
# assert len(image.shape) == 2, "Not a grayscale input image"
last_axis = -1
dim_to_repeat = 2
repeats = 3
grscale_img_3dims = np.expand_dims(image, last_axis)
training_image = np.repeat(grscale_img_3dims, repeats, dim_to_repeat).astype('uint8')
assert len(training_image.shape) == 3
assert training_image.shape[-1] == 3
return training_image
train_mod=load_image_into_numpy_array(raw_train)
But I get an error:
AxisError: axis 2 is out of bounds for array of dimension 1
How can I get this into input_shape=(96, 96, 3)
?