读取数据
#reading the images
tumor = []
path = 'D:\\data\\Tumor_detection\\archive\\brain_tumor_dataset\\yes\\*.jpg'#*表示所有
for f in glob.iglob(path):#遍历所有的yes图片
img = cv2.imread(f)
img = cv2.resize(img, (128, 128))#相当于reshape改变图片大小
b, g, r = cv2.split(img)#分割为单独的通道
img = cv2.merge([r, g, b])#改变通道顺序
tumor.append(img)
healthy = []
path = 'D:\\data\\Tumor_detection\\archive\\brain_tumor_dataset\\no\\*.jpg'#*表示所有
for f in glob.iglob(path):#遍历所有的yes图片
img = cv2.imread(f)
img = cv2.resize(img, (128, 128))#类似于reshape改变图片大小
b, g, r = cv2.split(img)#分割为单独的通道
img = cv2.merge([r, g, b])#改变通道顺序
healthy.append(img)
#our images
#改变数据形式
tumor = np.array(tumor, dtype=np.float32)
healthy = np.array(healthy, dtype=np.float32)
print(tumor.shape)#(154, 128, 128, 3)154表示154张图, 128 rows, 128 columns
print(healthy.shape)#(91, 128, 128, 3)91表示91张图, 128 rows, 128 columns
All = np.concatenate((healthy, tumor))
print(All.shape)#(245, 128, 128, 3)
Brain MRI 可视化
- 显示一张图片
plt.imshow(healthy[0])
plt.show()
- 取随机数
np.random.choice(5, 3)#[0, 5)中随机选3个int, e.g.[1 3 0]
- 定义一个随机取图片的函数
def plot_random(healthy, tumor, num=5):
#随机返回5张healthy和tumor
healthy_imgs = healthy[np.random.choice(healthy.shape[0], 5, replace=False)]
tumor_imgs = tumor[np.random.choice(tumor.shape[0], 5, replace=False)]
plt.figure(figsize=(16, 9))#fix size
#显示healthy
for i in range(num):
plt.subplot(1, num, i+1)
plt.title('healthy')
plt.imshow(healthy_imgs[i])
# 打开显示的图片
plt.show()
##显示tumor
for i in range(num):
plt.subplot(1, num, i+1)
plt.title('tumor')
plt.imshow(tumor_imgs[i])
#打开显示的图片
plt.show()
plot_random(healthy=healthy, tumor=tumor)
继承抽象类Dataset, Overriding getitem, len, add
- getitem: 通过index检索数据集的特定数据
- len: 得到数据集的长度
- add: 两个数据集相加
构造一个简单的用于数组的类
class MRI(Dataset):
def __init__(self, scores):
self.x = scores
def __getitem__(self, index):
return self.x[index]
def __len__(self):
return len(self.x)
s1 = [1, 2, 3, 4]
d1 = MRI(s1)#s1代入构造函数中的scores
print(d1.x)#[1, 2, 3, 4]
print(d1[2])#自动调用getitem, 输出3
s2 = [100, 200, 300, 400]
d2 = MRI(s2)
#会自动调用父类中的__add__
d = d1 + d2
自定义用于MRI的dataset类
class MRI(Dataset):
def __init__(self):
# reading the images
tumor = []
path = 'D:\\data\\Tumor_detection\\archive\\brain_tumor_dataset\\yes\\*.jpg' # *表示所有
for f in glob.iglob(path): # 遍历所有的yes图片
img = cv2.imread(f)
img = cv2.resize(img, (128, 128)) # 相当于reshape改变图片大小
b, g, r = cv2.split(img) # 分割为单独的通道
img = cv2.merge([r, g, b]) # 改变通道顺序
tumor.append(img)
healthy = []
path = 'D:\\data\\Tumor_detection\\archive\\brain_tumor_dataset\\no\\*.jpg' # *表示所有
for f in glob.iglob(path): # 遍历所有的yes图片
img = cv2.imread(f)
img = cv2.resize(img, (128, 128)) # 相当于reshape改变图片大小
b, g, r = cv2.split(img) # 分割为单独的通道
img = cv2.merge([r, g, b]) # 改变通道顺序
healthy.append(img)
# our images
# 改变数据形式
tumor = np.array(tumor, dtype=np.float32)
healthy = np.array(healthy, dtype=np.float32)
# our labels
tumor_label = np.ones(tumor.shape[0], dtype=np.float32) # tumor的label用1, 这里生成一堆1; tumor.shape[0]表示有多少张tumor图片
healthy_label = np.zeros(healthy.shape[0], dtype=np.float32) # healthy的label用0, 这里生成一堆0
#定义实例变量; 并且连接
#在第0轴连接: e.g.(100, 512, 512, 3)和(200, 512, 512, 3)-->(300, 512, 512, 3)最后得到300张图片[第0轴是图片数量]
self.images = np.concatenate((tumor, healthy), axis=0)
self.labels = np.concatenate((tumor_label, healthy_label))
def __len__(self):
return self.images.shape[0]#在定义类时实例变量都要加self;这里返回有多少张图
#不仅返回image, 也要返回label; 这里返回一个包含两者的字典
def __getitem__(self, index):
#不仅要返回对应的图片, 也要返回label
sample = {'image': self.images[index], 'label': self.labels[index]}
return sample
def normalize(self):
self.images = self.images/255.0#255是np.max(mri)
mri = MRI()
print(len(mri))
print(mri.__getitem__(5)['image'])#打印图片的矩阵
print(mri.__getitem__(5)['image'].shape)#(128, 128, 3)宽128高128通道3
print(mri.__getitem__(5)['label'])#打印label值
标签:img,healthy,self,tumor,np,pytorch,脑瘤,128,MRI
From: https://www.cnblogs.com/nameless233/p/17206223.html