2

I have made this network which seems to work ok.

datagen = tf.keras.preprocessing.image.ImageDataGenerator(
    #zoom_range=0.2,
    #shear_range=0.2,
    #rotation_range=10,
    rescale=1/255,
    validation_split=0.2,
    # other things...
)

train_ds = datagen.flow_from_directory(
    data_dir,
    subset="training",
    class_mode='binary',
    target_size=target_size,
    batch_size=batch_size,
)

val_ds = datagen.flow_from_directory(
    data_dir,
    subset="validation",
    class_mode='binary',
    target_size=target_size,
    batch_size=batch_size,
)

pre_trained_model = InceptionV3(input_shape=(128,128,3),
                                include_top=False,
                                weights='imagenet')

for layer in pre_trained_model.layers:
  layer.trainable = False


global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
Dl_1 = tf.keras.layers.Dropout(rate = 0.2)
prediction_layer = tf.keras.layers.Dense(1,activation='sigmoid')

model_V3 = tf.keras.Sequential([
  pre_trained_model,
  global_average_layer,
  Dl_1,
  prediction_layer
])

model_V3.compile(optimizer='adam',
                 loss='binary_crossentropy',
                 metrics=['accuracy'])

lr_reduce = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, verbose=2, mode='max')

hist = model_V3.fit(
    train_ds, 
    epochs=5,
    steps_per_epoch=len(train_ds), 
    validation_data=val_ds, 
    validation_steps=len(val_ds), 
    callbacks=[lr_reduce])

However the issue here is that the network is built for RGB images. My data is grayscale images. Right now I am just replicating channels so that R,G and B get same value for each pixel. The problem is that this is extremely memory consuming and slow. Is there a way to make the network use graylevel images instead of rgb? Is there maybe another pretrained network which is better to classify grayscale images of elliptic cell-like structures?

Michael Hansen
  • 237
  • 2
  • 8

1 Answers1

-2

When importing InceptionV3 try this:

pre_trained_model = InceptionV3(input_shape=(128,128,1),
                                include_top=False,
                                weights='imagenet')

128 and 128 are the physical dimensions of your image and the 3 (or 1 in my case) are the color channels.

theastronomist
  • 955
  • 2
  • 13
  • 33
  • 1
    This won't work because the pre-trained weights are for RGB images, the weight shapes will not match – Dr. Snoopy May 27 '20 at 22:14
  • yes - of course that does not work. I read this in a forum: "For a kxk kernel with 3 input channels (RGB) and N output channels, your weights are a 3 x k x k x N array (dimension order depends on library). If you sum your array along the first axis - I.e a.sum(axis=0), your new weights (expand to 1 x k x k x N) would give you the exact same output for a grayscale image as the original weights would for a grayscale image repeated across three channels." But I do not know how to apply this change. Anyone could provide an example? – Michael Hansen May 27 '20 at 22:32