如何从 Tensorflow 获取二值图像分类的标签预测
How to get label prediction of binary image classification from Tensorflow
如何从 Tensorflow 获取二值图像分类的标签预测?
环境:
- Google colab
- 张量流 2.7.0
- Python 3.7.12
数据集结构:
/training/<br/>
---/COVID19/<br/>
------/img1.jpg<br/>
------/img2.jpg<br/>
------/img3.jpg<br/>
---/NORMAL/<br/>
------/img4.jpg<br/>
------/img5.jpg<br/>
------/img6.jpg<br/>
制作数据集代码:
batch_size = 32
img_height = 300
img_width = 300
epochs = 10
input_shape = (img_width, img_height, 3)
AUTOTUNE = tf.data.AUTOTUNE
dataset_url = "https://storage.googleapis.com/fdataset/Dataset.tgz"
data_dir = tf.keras.utils.get_file('training', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count)
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
seed=123,
subset="training",
validation_split=0.8,
image_size=(img_width, img_height),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
seed=123,
subset="validation",
validation_split=0.2,
image_size=(img_width, img_height),
batch_size=batch_size)
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
模特:
model = tf.keras.Sequential()
base_model = tf.keras.applications.DenseNet121(input_shape=input_shape,include_top=False)
base_model.trainable=True
model.add(base_model)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(16,activation='relu'))
model.add(tf.keras.layers.Dense(1, activation="sigmoid"))
损失函数:binary_crossentropy
优化器:RMSprop
指标:accuracy
在我制作模型并对其进行训练后,我使用此代码对验证数据集进行了预测
(model.predict(val_ds) > 0.5).astype("int32")
所以我得到了这样的结果
array([[0],
[1],
[1],
[0],
[0]], dtype=int32)
然后如何将其再次转换为“COVID19”或“NORMAL”等标签,示例如下:
array([["COVID19"],
["NORMAL"],
["NORMAL"],
["COVID19"],
["COVID19"]], dtype=int32)
将所需的值映射到数组中
mapper = {1: "NORMAL", 0: "COVID19"}
np.vectorize(mapper.get)(output)
如何从 Tensorflow 获取二值图像分类的标签预测?
环境:
- Google colab
- 张量流 2.7.0
- Python 3.7.12
数据集结构:
/training/<br/>
---/COVID19/<br/>
------/img1.jpg<br/>
------/img2.jpg<br/>
------/img3.jpg<br/>
---/NORMAL/<br/>
------/img4.jpg<br/>
------/img5.jpg<br/>
------/img6.jpg<br/>
制作数据集代码:
batch_size = 32
img_height = 300
img_width = 300
epochs = 10
input_shape = (img_width, img_height, 3)
AUTOTUNE = tf.data.AUTOTUNE
dataset_url = "https://storage.googleapis.com/fdataset/Dataset.tgz"
data_dir = tf.keras.utils.get_file('training', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count)
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
seed=123,
subset="training",
validation_split=0.8,
image_size=(img_width, img_height),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
seed=123,
subset="validation",
validation_split=0.2,
image_size=(img_width, img_height),
batch_size=batch_size)
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
模特:
model = tf.keras.Sequential()
base_model = tf.keras.applications.DenseNet121(input_shape=input_shape,include_top=False)
base_model.trainable=True
model.add(base_model)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(16,activation='relu'))
model.add(tf.keras.layers.Dense(1, activation="sigmoid"))
损失函数:binary_crossentropy
优化器:RMSprop
指标:accuracy
在我制作模型并对其进行训练后,我使用此代码对验证数据集进行了预测
(model.predict(val_ds) > 0.5).astype("int32")
所以我得到了这样的结果
array([[0],
[1],
[1],
[0],
[0]], dtype=int32)
然后如何将其再次转换为“COVID19”或“NORMAL”等标签,示例如下:
array([["COVID19"],
["NORMAL"],
["NORMAL"],
["COVID19"],
["COVID19"]], dtype=int32)
将所需的值映射到数组中
mapper = {1: "NORMAL", 0: "COVID19"}
np.vectorize(mapper.get)(output)