0

I loaded a dataset from the AstroNN library. Since i believe the color of images is not a factor for classifying galaxy formations, I want to convert all the dataset to Grayscale to reduce the size of images. How should i do this to the entire dataset?

here's part of my code which loads dataset and splits it:

import matplotlib.pyplot as plt
import numpy as np
import os
import tensorflow as tf
import tensorflow.keras.layers as tfl
from astroNN.datasets import load_galaxy10
from tensorflow.keras import utils
import numpy as np
import tensorflow_datasets as tfds
from tensorflow.keras.utils import to_categorical
import h5py
import matplotlib.pyplot as plt
from matplotlib.pyplot import imread
import scipy
import pandas as pd
import math
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras import layers , models
from tensorflow.keras.callbacks import EarlyStopping
from sklearn.model_selection import train_test_split
import lodgepole.image_tools as lit



# To load images and labels (will download automatically at the first time)
# First time downloading location will be ~/.astroNN/datasets/
images, labels = load_galaxy10()

# To convert the labels to categorical 10 classes
labels = utils.to_categorical(labels, 10)

# To convert to desirable type
labels = labels.astype(np.float32)
images = images.astype(np.float32)


#Split into train and test set

train_idx, test_idx = train_test_split(np.arange(labels.shape[0]), test_size=0.1)
train_ds, train_labels, test_ds, test_labels = images[train_idx], labels[train_idx], images[test_idx], labels[test_idx]
maryam gh
  • 163
  • 1
  • 5
  • 11

1 Answers1

0

You can do it by getting the mean over the channel axis for each image with np.mean(array,axis=2).

I've downloaded your data and looked at it again. Since your first dimension is your batch_size, you don't want to average over the 3rd axis. You color channels on the 4th axis so we'll average on it and get the desired form.

enter image description here

You don't have to make them greyscale in order to train your network but if you especially want it you can go and check the following link: How can I convert an RGB image into grayscale in Python?

Hakan Akgün
  • 872
  • 5
  • 13