加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
convert_npz_STARE.py 2.19 KB
一键复制 编辑 原始数据 按行查看 历史
SharifAmit 提交于 2021-05-30 01:51 . npz py files STARE
import numpy as np
from numpy import asarray,savez_compressed
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import load_img
import argparse
import glob
#load all images in a directory into memory
def load_images(imgpath,maskpath,labelpath, n_crops,size=(128,128)):
src_list, mask_list, label_list = list(), list(), list()
img_list = glob.glob("Stare/training/stare-original-images/*.ppm")
for i in img_list:
i = i.split('\\')
i = i[1].split('.')
#print(i[0])
for j in range(n_crops):
# load and resize the image
filename = i[0]+"_"+str(j+1)+".png"
mask_name = i[0]+"_mask_" + str(j+1)+".png"
label_name = i[0]+"_label_" + str(j+1)+".png"
img = load_img(imgpath + filename, target_size=size)
fundus_img = img_to_array(img)
mask = load_img(maskpath + mask_name, target_size=size,color_mode="grayscale")
mask_img = img_to_array(mask)
label = load_img(labelpath + label_name, target_size=size,color_mode="grayscale")
label_img = img_to_array(label)
# split into satellite and map
src_list.append(fundus_img)
mask_list.append(mask_img)
label_list.append(label_img)
return [asarray(src_list), asarray(mask_list), asarray(label_list)]
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--input_dim', type=int, default=(128,128))
parser.add_argument('--n_crops', type=int, default=270)
parser.add_argument('--outfile_name', type=str, default='STARE')
args = parser.parse_args()
# dataset path
imgpath = 'Stare_crop/Images/'
maskpath = 'Stare_crop/Masks/'
labelpath = 'Stare_crop/labels/'
# load dataset
[src_images, mask_images, label_images] = load_images(imgpath,maskpath,labelpath,args.n_crops,args.input_dim)
print('Loaded: ', src_images.shape, mask_images.shape, label_images.shape)
# save as compressed numpy array
filename = args.outfile_name+'.npz'
savez_compressed(filename, src_images, mask_images, label_images)
print('Saved dataset: ', filename)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化