1.首先安装GDAL地理信息空间软件,在window上很好装,在Linux系统上安装非常麻烦,所以所有图片在本地进行处理,再用numpy.save 保存成二维数据,然后上传到云服务器,用numpy.load读取,再送入神经网络进行训练
2.代码
import numpy as np
import random
import pandas as pd
from osgeo import gdal
import matplotlib.pyplot as plt
from tensorflow.keras.utils import to_categorical
#path :../data/train ,../data/test
def load_data(choose_path,class_num):
#列表
data = []#数据x
label = []#标签y
dataframe = pd.read_csv(choose_path)
image_paths=dataframe.id.tolist()
#label也是一个列表
label=dataframe.label.tolist()
counter=1
for each_path in image_paths:
#读入imge的完整路径
print(each_path)
#print(type(each_path))读出each_path是string类型
print("循环数",counter)
counter = counter+1
#读入图像 error 96*3*3 expected:96*96*3
image = gdal.Open(each_path)
#显示通道数量:3通道
num_bands = image.RasterCount
print("通道数",num_bands)
#3,96,96 ,ReadAsArray()方法用于把栅格数据转换成数组
tmp_img = image.ReadAsArray()
print("栅格数据转换成的数组:",tmp_img.shape)
#转置矩阵
img = tmp_img.transpose(2, 1, 0)
print("转置矩阵",img.shape)
img_rgb = img[:,:,0:3]
img_rgb = np.array(img_rgb, dtype = np.uint8)
# 行:96 列:3 通道:3
print("传统图像通道img_rgb:",img_rgb.shape)
plt.imshow(img_rgb)
#把numpy矩阵中的整数转换成浮点数
#image = img_to_array(img_rgb)
data.append(img_rgb)
#将列表转化为数组
datas = np.array(data,dtype="float32")/255.0#归一化
label = np.array(label)
labels = to_categorical(label,num_classes=class_num)#one-hot
#在本地电脑端生成二维数组文件,再放到云服务器端进行处理
np.save(r"F:\user-data\newproject\cancer_CNN\process\testdatas.npy",datas)
np.save(r"F:\user-data\newproject\cancer_CNN\process\testlabels.npy",labels)
return datas,labels