我想寻求帮助,以解决我在开始训练U-Net时遇到的错误。我的网络旨在检测对象。我的模型有2218个训练图像和50个测试图像。所有图像都带有不同目录中的掩码。图像在训练集和测试集中都是平衡的,具有相等数量的包含和不包含结节的图像(一半有结节,一半没有结节)。
对于那些想要重现错误的人,这里是笔记本的副本:https://colab.research.google.com/drive/1D60Ve_4LlpViPsiaT6S935HTjxruiMQo?usp=sharing
下面是数据的副本:https://drive.google.com/drive/folders/1vo7frsJ3EMa_OFinoh7sPFsBpJFTD0-m?usp=sharing
这就是错误:
Epoch 1/100
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-9-5239c1cdf89c> in <cell line: 76>()
74
75 # Training the model using the train_generator
---> 76 history = model.fit(train_generator, steps_per_epoch=steps_per_epoch, epochs=EPOCHS,
77 callbacks=callbacks, validation_data=(test_images, test_masks),
78 validation_steps=validation_steps)
1 frames
/usr/local/lib/python3.10/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 for t in inputs
59 ]
---> 60 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
61 inputs, attrs, num_outputs)
62 except core._NotOkStatusException as e:
InvalidArgumentError: Graph execution error:
Detected at node mul_1 defined at (most recent call last):
File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
File "/usr/local/lib/python3.10/dist-packages/colab_kernel_launcher.py", line 37, in <module>
File "/usr/local/lib/python3.10/dist-packages/traitlets/config/application.py", line 992, in launch_instance
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py", line 619, in start
File "/usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py", line 195, in start
File "/usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
File "/usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
File "/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py", line 685, in <lambda>
File "/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py", line 738, in _run_callback
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 825, in inner
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 786, in run
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 377, in dispatch_queue
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 250, in wrapper
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 748, in __init__
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 786, in run
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 361, in process_one
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 261, in dispatch_shell
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 539, in execute_request
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
File "/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 302, in do_execute
File "/usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 539, in run_cell
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 2975, in run_cell
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3030, in _run_cell
File "/usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 78, in _pseudo_sync_runner
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3257, in run_cell_async
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3473, in run_ast_nodes
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
File "<ipython-input-9-5239c1cdf89c>", line 76, in <cell line: 76>
File "/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1783, in fit
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1377, in train_function
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1360, in step_function
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1349, in run_step
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1131, in train_step
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1225, in compute_metrics
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/compile_utils.py", line 620, in update_state
File "/usr/local/lib/python3.10/dist-packages/keras/src/utils/metrics_utils.py", line 77, in decorated
File "/usr/local/lib/python3.10/dist-packages/keras/src/metrics/base_metric.py", line 140, in update_state_fn
File "/usr/local/lib/python3.10/dist-packages/keras/src/metrics/base_metric.py", line 723, in update_state
File "<ipython-input-6-16eec044ce63>", line 16, in iou
required broadcastable shapes
[[{{node mul_1}}]] [Op:__inference_train_function_6791]
字符串
以下是为那些不喜欢Google Colab的人提供的模型的完整代码:
import tensorflow as tf
from tensorflow.keras import Input
from tensorflow.keras.models import save_model
from tensorflow.keras.layers import Activation, BatchNormalization, Dropout, Lambda, Conv2DTranspose, concatenate
from tensorflow.keras.optimizers import Adam
from tensorflow.keras import backend as K
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import pandas as pd
import cv2
import numpy as np
import os
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Conv2D, MaxPooling2D, UpSampling2D, Concatenate
from sklearn.metrics import accuracy_score
from scipy.ndimage.measurements import label
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras.models import load_model
from tensorflow.keras.metrics import Metric
import tensorflow
from tensorflow.keras.callbacks import Callback
from tensorflow.keras.metrics import AUC
from sklearn.metrics import roc_auc_score
from sklearn.metrics import precision_score, recall_score, f1_score, jaccard_score, average_precision_score
from tensorflow.keras import callbacks
# Defina os diretórios dos dados de treinamento e teste
diretorio_treinamento_imagens = '/content/drive/MyDrive/Node21/imagens_mascaras/train/'
diretorio_treinamento_mascaras = '/content/drive/MyDrive/Node21/imagens_mascaras/train_masks/'
diretorio_teste_imagens = '/content/drive/MyDrive/Node21/imagens_mascaras/test/'
diretorio_teste_mascaras = '/content/drive/MyDrive/Node21/imagens_mascaras/test_masks/'
# Função para carregar imagens e máscaras
def carregar_imagens_mascaras(diretorio_imagens, diretorio_mascaras):
imagens = []
mascaras = []
for filename in os.listdir(diretorio_imagens):
if filename.endswith(".png"):
# Carregar imagem
imagem = cv2.imread(os.path.join(diretorio_imagens, filename), cv2.IMREAD_GRAYSCALE)
imagens.append(imagem)
# Carregar máscara correspondente
mascara_nome = filename.replace(".png", "_mask.png")
mascara = cv2.imread(os.path.join(diretorio_mascaras, mascara_nome), cv2.IMREAD_GRAYSCALE)
mascaras.append(mascara)
return imagens, mascaras
# Carregar imagens de treinamento e suas máscaras
imagens_treinamento, mascaras_treinamento = carregar_imagens_mascaras(diretorio_treinamento_imagens, diretorio_treinamento_mascaras)
# Carregar imagens de teste e suas máscaras
imagens_teste, mascaras_teste = carregar_imagens_mascaras(diretorio_teste_imagens, diretorio_teste_mascaras)
# Converter listas de imagens e máscaras para arrays NumPy
imagens_treinamento = np.array(imagens_treinamento)
mascaras_treinamento = np.array(mascaras_treinamento)
imagens_teste = np.array(imagens_teste)
mascaras_teste = np.array(mascaras_teste)
# Verificar o formato das imagens e máscaras
print("Formato das imagens de treinamento:", imagens_treinamento.shape)
print("Formato das máscaras de treinamento:", mascaras_treinamento.shape)
print("Formato das imagens de teste:", imagens_teste.shape)
print("Formato das máscaras de teste:", mascaras_teste.shape)
smooth = 1.0
def dice_coef(y_true, y_pred):
y_truef = K.flatten(y_true)
y_predf = K.flatten(y_pred)
intersection = K.sum(y_truef * y_predf)
return (2.0 * intersection + smooth) / (K.sum(y_truef) + K.sum(y_predf) + smooth)
def dice_coef_loss(y_true, y_pred):
y_truef = K.flatten(y_true)
y_predf = K.flatten(y_pred)
intersection = K.sum(y_truef * y_predf)
return 1.0 - (2.0 * intersection + smooth) / (K.sum(y_truef) + K.sum(y_predf) + smooth)
def iou(y_true, y_pred):
intersection = K.sum(y_true * y_pred)
union = K.sum(K.maximum(y_true, y_pred))
jac = (intersection + smooth) / (union + smooth)
return jac
def jac_distance(y_true, y_pred):
return -iou(y_true, y_pred)
print()
def unet_with_batch_norm(input_shape=(512, 512, 1), dropout_rate=0.3, learning_rate=0.001):
inputs = Input(input_shape)
# Parte de codificação (downsampling)
conv1 = Conv2D(64, 3, activation='relu', padding='same')(inputs)
conv1 = Conv2D(64, 3, activation='relu', padding='same')(conv1)
bn1 = BatchNormalization()(conv1)
pool1 = MaxPooling2D(pool_size=(3, 3))(bn1)
pool1 = tensorflow.keras.layers.Dropout(dropout_rate)(pool1)
conv2 = Conv2D(128, 3, activation='relu', padding='same')(pool1)
conv2 = Conv2D(128, 3, activation='relu', padding='same')(conv2)
bn2 = BatchNormalization()(conv2)
pool2 = MaxPooling2D(pool_size=(3, 3))(bn2)
pool2 = tensorflow.keras.layers.Dropout(dropout_rate)(pool2)
conv3 = Conv2D(256, 3, activation='relu', padding='same')(pool2)
conv3 = Conv2D(256, 3, activation='relu', padding='same')(conv3)
bn3 = BatchNormalization()(conv3)
pool3 = MaxPooling2D(pool_size=(3, 3))(bn3)
pool3 = tensorflow.keras.layers.Dropout(dropout_rate)(pool3)
conv4 = Conv2D(512, 3, activation='relu', padding='same')(pool3)
conv4 = Conv2D(512, 3, activation='relu', padding='same')(conv4)
bn4 = BatchNormalization()(conv4)
pool4 = MaxPooling2D(pool_size=(4, 4))(bn4)
pool4 = tensorflow.keras.layers.Dropout(dropout_rate)(pool4)
# Parte de decodificação (upsampling)
up6 = UpSampling2D(size=(4, 4))(pool4)
conv6 = Conv2D(512, 3, activation='relu', padding='same')(up6)
conv6 = Conv2D(512, 3, activation='relu', padding='same')(conv6)
bn6 = BatchNormalization()(conv6)
up7 = UpSampling2D(size=(2, 2))(bn6)
conv7 = Conv2D(256, 3, activation='relu', padding='same')(up7)
conv7 = Conv2D(256, 3, activation='relu', padding='same')(conv7)
bn7 = BatchNormalization()(conv7)
up8 = UpSampling2D(size=(2, 2))(bn7)
conv8 = Conv2D(128, 3, activation='relu', padding='same')(up8)
conv8 = Conv2D(128, 3, activation='relu', padding='same')(conv8)
bn8 = BatchNormalization()(conv8)
up9 = UpSampling2D(size=(2, 2))(bn8)
conv9 = Conv2D(64, 3, activation='relu', padding='same')(up9)
conv9 = Conv2D(64, 3, activation='relu', padding='same')(conv9)
bn9 = BatchNormalization()(conv9)
outputs = Conv2D(1, 1, activation='sigmoid')(bn9)
model = Model(inputs=inputs, outputs=outputs)
optimizer = Adam(learning_rate=learning_rate)
model.compile(optimizer=optimizer, loss=dice_coef_loss, metrics=['accuracy', iou, dice_coef])
return model
model = unet_with_batch_norm()
# Visualização da arquitetura da U-Net
model.summary()
import os
import cv2
import numpy as np
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D
from tensorflow.keras.models import Model
# Carregar imagens de treinamento e suas máscaras
imagens_treinamento, mascaras_treinamento = carregar_imagens_mascaras(diretorio_treinamento_imagens, diretorio_treinamento_mascaras)
# Carregar imagens de teste e suas máscaras
imagens_teste, mascaras_teste = carregar_imagens_mascaras(diretorio_teste_imagens, diretorio_teste_mascaras)
# Certifique-se de que as imagens e máscaras tenham a mesma resolução (512x512)
im_height, im_width = 512, 512
imagens_treinamento = [cv2.resize(img, (im_width, im_height)) for img in imagens_treinamento]
mascaras_treinamento = [cv2.resize(mask, (im_width, im_height), interpolation=cv2.INTER_NEAREST) for mask in mascaras_treinamento]
imagens_teste = [cv2.resize(img, (im_width, im_height)) for img in imagens_teste]
mascaras_teste = [cv2.resize(mask, (im_width, im_height), interpolation=cv2.INTER_NEAREST) for mask in mascaras_teste]
# Converter listas de imagens e máscaras para arrays NumPy
imagens_treinamento = np.array(imagens_treinamento)
mascaras_treinamento = np.array(mascaras_treinamento)
imagens_teste = np.array(imagens_teste)
mascaras_teste = np.array(mascaras_teste)
# Verificar o formato das imagens e máscaras
print("Formato das imagens de treinamento:", imagens_treinamento.shape)
print("Formato das máscaras de treinamento:", mascaras_treinamento.shape)
print("Formato das imagens de teste:", imagens_teste.shape)
print("Formato das máscaras de teste:", mascaras_teste.shape)
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
import tensorflow as tf
# Definindo transformações de imagem para o gerador de dados
data_gen_args = dict(
rotation_range=0.2,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=0.05,
zoom_range=0.05,
horizontal_flip=True,
fill_mode='nearest'
)
im_height = 512
im_width = 512
BATCH_SIZE = 8
EPOCHS = 100
# Ajuste o diretório de saída para as imagens aumentadas, se necessário
output_dir = '/content/train_aug/'
# Garanta que o diretório de saída exista
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Adicione uma dimensão extra para o canal de cores (escala de cinza)
imagens_treinamento = np.expand_dims(imagens_treinamento, axis=-1)
mascaras_treinamento = np.expand_dims(mascaras_treinamento, axis=-1)
# Criando geradores de dados a partir das matrizes
image_datagen = ImageDataGenerator(**data_gen_args)
mask_datagen = ImageDataGenerator(**data_gen_args)
seed = 1 # Semente aleatória para garantir correspondência entre imagens e máscaras
# Criando geradores de treinamento e teste
image_generator = image_datagen.flow(imagens_treinamento, batch_size=BATCH_SIZE, seed=seed)
mask_generator = mask_datagen.flow(mascaras_treinamento, batch_size=BATCH_SIZE, seed=seed)
train_generator = zip(image_generator, mask_generator)
# Adicione uma dimensão extra para o canal de cores (escala de cinza)
#imagens_teste = np.expand_dims(imagens_teste, axis=-1)
#mascaras_teste = np.expand_dims(mascaras_teste, axis=-1)
# Criando geradores de dados de teste (sem aumento de dados)
test_datagen = ImageDataGenerator()
# Crie geradores de dados a partir das matrizes, sem adicionar uma dimensão extra
image_generator = image_datagen.flow(imagens_treinamento, batch_size=BATCH_SIZE, seed=seed)
mask_generator = mask_datagen.flow(mascaras_treinamento, batch_size=BATCH_SIZE, seed=seed)
#test_gener = zip(test_image_generator, test_mask_generator)
# Adicione uma dimensão extra para o canal de cores (escala de cinza) apenas ao definir o modelo
model = unet_with_batch_norm(input_shape=(im_height, im_width, 1))
# Defining the optimizer
opt = tf.keras.optimizers.Adam(learning_rate=0.001)
# Setting callbacks
early_stop = EarlyStopping(monitor='val_loss', patience=10, mode='min', verbose=1)
model_checkpoint = ModelCheckpoint('unet_lung_nodule_detection.hdf5', verbose=1, save_best_only=True)
callbacks = [model_checkpoint, early_stop]
# Calculating the number of steps per epoch and validation steps
steps_per_epoch = len(imagens_treinamento) // BATCH_SIZE
validation_steps = len(imagens_teste) // BATCH_SIZE
# Compiling the model
model.compile(optimizer=opt, loss=dice_coef_loss, metrics=["binary_accuracy", iou, dice_coef])
# Training the model using the train_generator
history = model.fit(train_generator, steps_per_epoch=steps_per_epoch, epochs=EPOCHS,
callbacks=callbacks, validation_data=(imagens_teste, mascaras_teste),
validation_steps=validation_steps)
型
1条答案
按热度按时间qxsslcnc1#
一个可能的尝试可以是:
变化
字符串
到
型