使用keras将彩色数据集更改为灰度
changing colorful dataset to Grayscale using keras
我从 AstroNN 库加载了一个数据集。因为我相信图像的颜色不是对星系形成进行分类的一个因素,所以我想将所有数据集转换为灰度以减小图像的大小。我应该如何对整个数据集执行此操作?
这是我加载数据集并将其拆分的部分代码:
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]
您可以通过使用 np.mean(array,axis=2).
获取每个图像在通道轴上的平均值来实现
我已经下载了您的数据并再次查看了它。由于您的第一个维度是 batch_size,因此您不想在第三个轴上进行平均。您在第 4 轴上为通道着色,以便我们对其进行平均并获得所需的形式。
你不必为了训练你的网络而将它们变成灰度,但如果你特别想要它,你可以去检查以下内容 link:
How can I convert an RGB image into grayscale in Python?
我从 AstroNN 库加载了一个数据集。因为我相信图像的颜色不是对星系形成进行分类的一个因素,所以我想将所有数据集转换为灰度以减小图像的大小。我应该如何对整个数据集执行此操作?
这是我加载数据集并将其拆分的部分代码:
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]
您可以通过使用 np.mean(array,axis=2).
获取每个图像在通道轴上的平均值来实现我已经下载了您的数据并再次查看了它。由于您的第一个维度是 batch_size,因此您不想在第三个轴上进行平均。您在第 4 轴上为通道着色,以便我们对其进行平均并获得所需的形式。
你不必为了训练你的网络而将它们变成灰度,但如果你特别想要它,你可以去检查以下内容 link: How can I convert an RGB image into grayscale in Python?