import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ["KERAS_BACKEND"] = "tensorflow"
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
# !curl -O http://saliencydetection.net/duts/download/DUTS-TE.zip
原始的还有个512的特征层,太大了,目前训练不了,本来有7个输出,只有第一个是真正的模型预测
我目前没真正搞懂它为啥要7个,所以我精简成1个,这个是彩色图片预测掩码,所以比牙齿切割要难,
而且有些掩码像素并不是二值化,是有边缘过度的,这个模型训练好,是能提前他认为的前景特征的
但是要训练好,得需要大的数据集,这个不像牙齿切割只需要提取牙齿特征,这个给模型的是真实的rgb彩色图片,模型需要预测掩码,这是很难的,模型会根据训练预测,这个难在不是只预测一个特别的掩码,而是很多物体
import numpy as np
from glob import glob
import matplotlib.pyplot as plt
import keras_cv
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, backend
# ! pip install --upgrade ipywidgets
gpus = tf.config.list_physical_devices('GPU')
if gpus:
# 如果有GPU,设置GPU资源使用率
try:
# 允许GPU内存按需增长
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
# 设置可见的GPU设备(这里实际上不需要,因为已经通过内存增长设置了每个GPU)
# tf.config.set_visible_devices(gpus, 'GPU')
print("GPU可用并已设置内存增长模式。")
except RuntimeError as e:
# 虚拟设备未就绪时可能无法设置GPU
print(f"设置GPU时发生错误: {e}")
else:
# 如果没有GPU
print("没有检测到GPU设备。")
IMAGE_SIZE = 160
BATCH_SIZE = 4
OUT_CLASSES = 1
TRAIN_SPLIT_RATIO = 0.90
DATA_DIR = "./datasets/DUTS-TE/"
def load_paths(path, split_ratio):#加载图片掩码路径,切分路径
images = sorted(glob(os.path.join(path, "DUTS-TE-Image/*")))[:3200]#排序后的
masks = sorted(glob(os.path.join(path, "DUTS-TE-Mask/*")))[:3200]
len_ = int(len(images) * split_ratio)
return (images[:len_], masks[:len_]), (images[len_:], masks[len_:])#返回训练集,验证集
def read_image(path, size, mode):
x = keras.utils.load_img(path, target_size=size, color_mode=mode)
x = keras.utils.img_to_array(x)
x = (x / 255.0).astype(np.float32)
return x
def preprocess(x_batch, y_batch, img_size, out_classes):
def f(_x, _y):
_x, _y = _x.decode(), _y.decode()
_x = read_image(_x,(img_size, img_size), mode="rgb") # image
_y = read_image(_y,(img_size, img_size), mode="grayscale") # mask
return _x, _y
images, masks = tf.numpy_function(f, [x_batch, y_batch], [tf.float32, tf.float32])
images.set_shape([img_size, img_size, 3])
masks.set_shape([img_size, img_size, out_classes])
return images, masks
def load_dataset(image_paths, mask_paths, img_size, out_classes, batch, shuffle=True):
dataset = tf.data.Dataset.from_tensor_slices((image_paths, mask_paths))
if shuffle:
dataset = dataset.cache().shuffle(buffer_size=1000)
dataset = dataset.map(
lambda x, y: preprocess(x, y, img_size, out_classes),
num_parallel_calls=tf.data.AUTOTUNE,
)
dataset = dataset.batch(batch)
d