下面的代码用于从胸部X射线进行肺部分割(2D)。它应该使用训练模型"trained_model.hdf5"从胸部X射线生成肺部遮罩。在将胸部X射线作为输入时,它应该能够识别哪些是肺部,并相应地创建肺部的单独遮罩。trained_model.hdf5
包含在JSRT数据集上训练的模型。
#from load_data import loadDataJSRT, loadDataMontgomery
import numpy as np
import pandas as pd
from keras.models import load_model
from skimage import morphology, io, color, exposure, img_as_float, transform
from keras.preprocessing.image import ImageDataGenerator
def loadDataGeneral(df, path, im_shape):
X = []
for i, item in df.iterrows():
img = img_as_float(io.imread(path + str(item[0])))
#mask = io.imread(path + item[1])
img = transform.resize(img, im_shape)
img = exposure.equalize_hist(img)
img = np.expand_dims(img, -1)
#mask = transform.resize(mask, im_shape)
#mask = np.expand_dims(mask, -1)
X.append(img)
#y.append(mask)
X = np.array(X)
#y = np.array(y)
X -= X.mean()
X /= X.std()
print( '### Dataset loaded')
print( '\t{}'.format(path))
#print( '\t{}\t{}'.format(X.shape, y.shape))
#print( '\tX:{:.1f}-{:.1f}\ty:{:.1f}-{:.1f}\n'.format(X.min(), X.max(), y.min(), y.max()))
print( '\tX.mean = {}, X.std = {}'.format(X.mean(), X.std()))
return X
if __name__ == '__main__':
# Path to csv-file. File should contain X-ray filenames as first column,
# mask filenames as second column.
csv_path = 'idx.csv'
# Path to the folder with images. Images will be read from path + path_from_csv
path = 'Data/'
df = pd.read_csv(csv_path)
# Load test data
im_shape = (256, 256)
X = loadDataGeneral(df, path, im_shape)
#print('***X= ',X)
n_test = X.shape[0]
inp_shape = X[0].shape
# Load model
model_name = 'trained_model.hdf5'
UNet = load_model(model_name)
# For inference standard keras ImageGenerator is used.
test_gen = ImageDataGenerator(rescale=1.)
ious = np.zeros(n_test)
dices = np.zeros(n_test)
i = 0
print("TEST_GEN ",test_gen)
print(len(X))
for xx in test_gen.flow(X, batch_size=1):
xx = xx[0:4]
img = exposure.rescale_intensity(np.squeeze(xx), out_range=(0,1))
pred = UNet.predict(xx)[..., 0].reshape(inp_shape[:2])
#mask = yy[..., 0].reshape(inp_shape[:2])
# Binarize masks
#gt = mask > 0.5
pr = pred > 0.5
# Remove regions smaller than 2% of the image
pr = remove_small_regions(pr, 0.02 * np.prod(im_shape))
io.imsave('results/{}'.format(df.iloc[i][0]), masked(img, pr, 1))
#ious[i] = IoU(gt, pr)
#dices[i] = Dice(gt, pr)
#print(df.iloc[i][0], ious[i], dices[i])
i += 1
if i == n_test:
break
但是,我得到这个错误:
### Dataset loaded
Data/
X.mean = -1.042684457293793e-15, X.std = 1.0000000000000002
2018-09-28 09:45:55.598419: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2018-09-28 09:45:55.605772: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\saving.py:304: UserWarning: Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer.
warnings.warn('Error in loading the saved optimizer '
TEST_GEN <keras_preprocessing.image.ImageDataGenerator object at 0x00000159CC91AEB8>
5
Traceback (most recent call last):
File "inference_1.py", line 67, in <module>
for xx in test_gen.flow(X, batch_size=1):
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras_preprocessing\image.py", line 867, in flow
subset=subset)
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras_preprocessing\image.py", line 1427, in __init__
'with shape', self.x.shape)
ValueError: ('Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (5, 256, 256, 3, 1))
我该如何重塑Tensor?我做错了什么?
1条答案
按热度按时间kyks70gy1#
ImageDataGenerator
期望输入的形状是(samples, height, width, channels)
,但是在你的例子中,有一个额外的维度,但是你的输入X
的形状是(samples, height, width, channels, 1)
,所以你需要先去掉最后一个维度。要回答有关重塑Tensor的问题,有多种方法可供选择。
或
或
或