Модель: «функциональная»
Код: Выделить всё
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃ Connected to ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━┩
│ input_layer (InputLayer) │ (None, 3, 512, 512) │ 0 │ - │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d (Conv2D) │ (None, 32, 512, 512) │ 896 │ input_layer[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation (Activation) │ (None, 32, 512, 512) │ 0 │ conv2d[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_1 (Conv2D) │ (None, 64, 512, 512) │ 18,496 │ activation[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_1 (Activation) │ (None, 64, 512, 512) │ 0 │ conv2d_1[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ max_pooling2d │ (None, 64, 256, 256) │ 0 │ activation_1[0][0] │
│ (MaxPooling2D) │ │ │ │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_2 (Conv2D) │ (None, 64, 256, 256) │ 36,928 │ max_pooling2d[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_2 (Activation) │ (None, 64, 256, 256) │ 0 │ conv2d_2[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_3 (Conv2D) │ (None, 128, 256, 256) │ 73,856 │ activation_2[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_3 (Activation) │ (None, 128, 256, 256) │ 0 │ conv2d_3[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ max_pooling2d_1 │ (None, 128, 128, 128) │ 0 │ activation_3[0][0] │
│ (MaxPooling2D) │ │ │ │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_4 (Conv2D) │ (None, 128, 128, 128) │ 147,584 │ max_pooling2d_1[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_4 (Activation) │ (None, 128, 128, 128) │ 0 │ conv2d_4[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_5 (Conv2D) │ (None, 256, 128, 128) │ 295,168 │ activation_4[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_5 (Activation) │ (None, 256, 128, 128) │ 0 │ conv2d_5[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ max_pooling2d_2 │ (None, 256, 64, 64) │ 0 │ activation_5[0][0] │
│ (MaxPooling2D) │ │ │ │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_6 (Conv2D) │ (None, 256, 64, 64) │ 590,080 │ max_pooling2d_2[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_6 (Activation) │ (None, 256, 64, 64) │ 0 │ conv2d_6[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_7 (Conv2D) │ (None, 512, 64, 64) │ 1,180,160 │ activation_6[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_7 (Activation) │ (None, 512, 64, 64) │ 0 │ conv2d_7[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ up_sampling2d │ (None, 512, 128, 128) │ 0 │ activation_7[0][0] │
│ (UpSampling2D) │ │ │ │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ concatenate (Concatenate) │ (None, 768, 128, 128) │ 0 │ up_sampling2d[0][0], │
│ │ │ │ activation_5[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_8 (Conv2D) │ (None, 256, 128, 128) │ 1,769,728 │ concatenate[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_8 (Activation) │ (None, 256, 128, 128) │ 0 │ conv2d_8[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_9 (Conv2D) │ (None, 256, 128, 128) │ 590,080 │ activation_8[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_9 (Activation) │ (None, 256, 128, 128) │ 0 │ conv2d_9[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ up_sampling2d_1 │ (None, 256, 256, 256) │ 0 │ activation_9[0][0] │
│ (UpSampling2D) │ │ │ │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ concatenate_1 │ (None, 384, 256, 256) │ 0 │ up_sampling2d_1[0][0], │
│ (Concatenate) │ │ │ activation_3[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_10 (Conv2D) │ (None, 128, 256, 256) │ 442,496 │ concatenate_1[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_10 │ (None, 128, 256, 256) │ 0 │ conv2d_10[0][0] │
│ (Activation) │ │ │ │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_11 (Conv2D) │ (None, 128, 256, 256) │ 147,584 │ activation_10[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_11 │ (None, 128, 256, 256) │ 0 │ conv2d_11[0][0] │
│ (Activation) │ │ │ │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ up_sampling2d_2 │ (None, 128, 512, 512) │ 0 │ activation_11[0][0] │
│ (UpSampling2D) │ │ │ │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ concatenate_2 │ (None, 192, 512, 512) │ 0 │ up_sampling2d_2[0][0], │
│ (Concatenate) │ │ │ activation_1[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_12 (Conv2D) │ (None, 64, 512, 512) │ 110,656 │ concatenate_2[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_12 │ (None, 64, 512, 512) │ 0 │ conv2d_12[0][0] │
│ (Activation) │ │ │ │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_13 (Conv2D) │ (None, 64, 512, 512) │ 36,928 │ activation_12[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_13 │ (None, 64, 512, 512) │ 0 │ conv2d_13[0][0] │
│ (Activation) │ │ │ │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ conv2d_14 (Conv2D) │ (None, 2, 512, 512) │ 130 │ activation_13[0][0] │
├───────────────────────────┼────────────────────────┼────────────────┼────────────────────────┤
│ activation_14 │ (None, 2, 512, 512) │ 0 │ conv2d_14[0][0] │
│ (Activation) │ │ │ │
└───────────────────────────┴────────────────────────┴────────────────┴────────────────────
Код: Выделить всё
# Funtion to load the data and returns their paths only
def load_images_masks(dataset_path):
images= glob(os.path.join(dataset_path,"input/*.png"))
masks = glob(os.path.join(dataset_path,"ground_truth/*.npy"))
return images, masks
# Function to read the input image
def read_image(image_path):
# Load the image as RGB as we need
x = plt.imread(image_path)
# Add channel dimension as the first dimension
x = np.transpose(x, (2, 0, 1))
return x
# Function to read the mask image
def read_mask(mask_path):
# Load the image as RGB as we need
y= np.load(mask_path)
# Convert the data type from float 64 to float 32 to follow that of the input image
y=y.astype(np.float32)
# Add channel dimension as the first dimension
y = np.transpose(y, (2, 0, 1))
return y
# Preprocessing function
def preprocess(x,y):
def f(x,y):
x=read_image(x)
y=read_mask(y)
# Convert them into tensors with data type float 32
x = tf.convert_to_tensor(x, dtype=tf.float32)
y = tf.convert_to_tensor(y, dtype=tf.float32)
if np.isnan(x).any() or np.isinf(x).any():
raise ValueError("NaN or Inf found in image data")
if np.isnan(y).any() or np.isinf(y).any():
raise ValueError("NaN or Inf found in mask data")
return x,y
# Integrate a custom Python function (f) into the TensorFlow graph.
x,y = tf.numpy_function(f,[x,y],[tf.float32, tf.float32])
return x ,y
# Function to return the dataset into patches of images and masks pairs using tf_dataset module
def tf_dataset(x,y, batch_size =32,epochs=30):
# Create a tf dataset with pairs of data points from x and y ( images with their corresponding masks)
dataset = tf.data.Dataset.from_tensor_slices((x,y))
# Shuffle the dataset
dataset= dataset.shuffle(buffer_size=1000)
# Apply a preprocessing function preprocess
dataset = dataset.map(preprocess)
# Group the data points into batch_size batches
dataset=dataset.repeat(epochs).batch(batch_size)
# prefetches 2 batches of data in advance to improve performance by overlapping data loading with model training.
dataset=dataset.prefetch(2)
return dataset
# Define paths
dataset_folder = '/path/to/dataset'
train_dataset_path = os.path.join(dataset_folder,"train")
val_dataset_path = os.path.join(dataset_folder,"val")
# load the paths of the images and masks in the training dataset
images, masks = load_images_masks(train_dataset_path)
# Create the prefetched tf dataset
train_dataset = tf_dataset(images, masks,epochs=30)
images, masks = load_images_masks(val_dataset_path)
# Create the prefetched tf dataset
val_dataset = tf_dataset(images, masks)
Код: Выделить всё
for images, masks in train_dataset.take(1):
print("Image shape:", images.shape)
print("Mask shape:", masks.shape)
Код: Выделить всё
Image shape: (32, 3, 512, 512)
Mask shape: (32, 2, 512, 512)
Код: Выделить всё
model.fit(x = train_dataset, steps_per_epoch= 34, validation_data = val_dataset, validation_steps= 10 , epochs= 30 )
Код: Выделить всё
Epoch 1/30
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
----> 1 model.fit(x = train_dataset, steps_per_epoch= 34, validation_data = val_dataset, validation_steps= 10 , epochs= 30 )
1 frames
/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py in error_handler(*args, **kwargs)
120 # To get the full stack trace, call:
121 # `keras.config.disable_traceback_filtering()`
--> 122 raise e.with_traceback(filtered_tb) from None
123 finally:
124 del filtered_tb
ValueError: as_list() is not defined on an unknown TensorShape.
Подробнее здесь: https://stackoverflow.com/questions/790 ... -the-model