我目前正在研究多任务学习,以对图像是否感染癌症进行分类,第二个任务是使用图像对肺癌类型进行分类
我正在使用 Kaggle 的数据集:https://www.kaggle.com/datasets/mohamedhanyyy/chest-ctscan-images
我已经编写了代码,但当我尝试训练模型时出现错误
请帮忙。 这是我的代码:`# 这个 Python 3 环境安装了许多有用的分析库
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import cv2
import keras
from numpy import random
import tensorflow as tf
import tensorflow_datasets as tfds
from matplotlib import pyplot as plt
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Flatten, Conv2D
from keras.layers import MaxPooling2D, BatchNormalization
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.applications import MobileNet, VGG16, ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
from sklearn.metrics import precision_score, recall_score, accuracy_score
from sklearn.metrics import classification_report ,confusion_matrix
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
import seaborn as sns
import tensorflow_hub as hub
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory
import os
import zipfile
with zipfile.ZipFile('/content/drive/MyDrive/lungcancer.zip', 'r') as zip_ref:
zip_ref.extractall('/content/')
!pip install tensorflow tensorflow_hub
!ls
# Define paths to the dataset
data_dir = '/content/Data'
train_dir = data_dir + '/train'
validation_dir = data_dir + '/valid'
test_dir = data_dir + '/test'
listImageFilePaths = []
for dirname, _, filenames in os.walk('/content/Data/test/normal'):
for filename in filenames:
listImageFilePaths.append( os.path.join(dirname, filename))
print('Total images ', len(listImageFilePaths))
input_shape = (224,224,3)
#num_class = 4
train_datagen = ImageDataGenerator(
dtype='float32',
preprocessing_function=preprocess_input,
rotation_range=10,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
vertical_flip=False
)
val_datagen = ImageDataGenerator(
dtype='float32',
preprocessing_function=preprocess_input,
)
test_datagen = ImageDataGenerator(
dtype='float32',
preprocessing_function=preprocess_input,
)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(224,224),
batch_size=32,
class_mode='categorical',
)
test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=(224,224),
batch_size=32,
class_mode='categorical',
shuffle = False,
)
validation_generator = val_datagen.flow_from_directory(
validation_dir,
target_size=(224,224),
batch_size=32,
class_mode='categorical',
)
# Load a pre-trained feature extractor model from TensorFlow Hub
feature_extractor = hub.KerasLayer("https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/5", trainable=False)
# Create an input layer for the model
input_layer = tf.keras.layers.Input(shape=(224, 224, 3))
# Connect the input layer to the feature extractor
feature_output = feature_extractor(input_layer)
# Define the multi-task head for cancer infection classification
cancer_head = tf.keras.layers.Dense(2, activation='softmax', name='cancer_head')(feature_output)
# Define the multi-task head for lung cancer type classification
lung_cancer_head = tf.keras.layers.Dense(4, activation='softmax', name='lung_cancer_head')(feature_output)
# Combine the inputs and outputs to create the multi-task model
multi_task_model = tf.keras.Model(inputs=input_layer, outputs=[cancer_head, lung_cancer_head])
# Compile the multi-task model with appropriate loss functions and metrics
multi_task_model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss={'cancer_head': 'categorical_crossentropy', 'lung_cancer_head': 'categorical_crossentropy'},
metrics={'cancer_head': 'accuracy', 'lung_cancer_head': 'accuracy'}
)
# Train the multi-task model
history = multi_task_model.fit(train_generator, epochs=10, validation_data=validation_generator)
# Evaluate the multi-task model
eval_scores = multi_task_model.evaluate(validation_generator)
# Print the evaluation scores
print("Cancer Infection - Loss:", eval_scores[1])
print("Cancer Infection - Accuracy:", eval_scores[2])
print("Lung Cancer Type - Loss:", eval_scores[3])
print("Lung Cancer Type - Accuracy:", eval_scores[4])`
Error
Epoch 1/10
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-18-b769f3dab356> in <cell line: 2>()
1 # Train the multi-task model
----> 2 history = multi_task_model.fit(train_generator, epochs=10, validation_data=validation_generator)
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)
50 try:
51 ctx.ensure_initialized()
---> 52 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
53 inputs, attrs, num_outputs)
54 except core._NotOkStatusException as e:
InvalidArgumentError: Graph execution error:
Detected at node 'categorical_crossentropy/softmax_cross_entropy_with_logits' defined at (most recent call last):
File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.10/dist-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.10/dist-packages/traitlets/config/application.py", line 992, in launch_instance
app.start()
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py", line 619, in start
self.io_loop.start()
File "/usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py", line 195, in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
self._run_once()
File "/usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
handle._run()
File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py", line 685, in <lambda>
lambda f: self._run_callback(functools.partial(callback, future))
File "/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py", line 738, in _run_callback
ret = callback()
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 825, in inner
self.ctx_run(self.run)
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 786, in run
yielded = self.gen.send(value)
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 361, in process_one
yield gen.maybe_future(dispatch(*args))
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
yielded = ctx_run(next, result)
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 261, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
yielded = ctx_run(next, result)
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 539, in execute_request
self.do_execute(
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
yielded = ctx_run(next, result)
File "/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 302, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 539, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 2975, in run_cell
result = self._run_cell(
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3030, in _run_cell
return runner(coro)
File "/usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 78, in _pseudo_sync_runner
coro.send(None)
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3257, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3473, in run_ast_nodes
if (await self.run_code(code, result, async_=asy)):
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-18-b769f3dab356>", line 2, in <cell line: 2>
history = multi_task_model.fit(train_generator, epochs=10, validation_data=validation_generator)
File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1685, in fit
tmp_logs = self.train_function(iterator)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1284, in train_function
return step_function(self, iterator)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1268, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1249, in run_step
outputs = model.train_step(data)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1051, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1109, in compute_loss
return self.compiled_loss(
File "/usr/local/lib/python3.10/dist-packages/keras/engine/compile_utils.py", line 265, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.10/dist-packages/keras/losses.py", line 142, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.10/dist-packages/keras/losses.py", line 268, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.10/dist-packages/keras/losses.py", line 1984, in categorical_crossentropy
return backend.categorical_crossentropy(
File "/usr/local/lib/python3.10/dist-packages/keras/backend.py", line 5565, in categorical_crossentropy
return tf.nn.softmax_cross_entropy_with_logits(
Node: 'categorical_crossentropy/softmax_cross_entropy_with_logits'
logits and labels must be broadcastable: logits_size=[32,2] labels_size=[32,4]
[[{{node categorical_crossentropy/softmax_cross_entropy_with_logits}}]] [Op:__inference_train_function_23304]
数据加载过程可能是问题所在,因为这是一个多任务分类问题。您可以尝试创建一个自定义数据加载器,这可能会有所帮助
(免责声明:ML 新手的话)
创建自定义数据加载器的指南:
https://stanford.edu/~shervine/blog/keras-how-to-generate-data-on-the-fly