赞
踩
matlab在图像处理上有着非常大的优势,而在某些情况下预处理完的数据需要在python上训练。
在数据导入的环节往往会出现许多问题。
训练数据集是我自己拍摄的红外三通道图像,经预处理后为131张单通道的规模为1430*1072的图像,保存在train.mat中,标签名为B
测试集是后60张图片,保存在test.mat。
训练标签是保存在Excel里的分类目标
- from scipy.io import loadmat
- import pandas as pd
- import numpy
- train_labels = pd.read_excel('训练.xlsx')
- print(train_labels)
- test_labels = pd.read_excel('测试.xlsx')
- train_labels = train_labels.to_numpy()
- test_labels = test_labels.to_numpy()
- path = r"train.mat" # mat文件路径
- data1 = loadmat(path) # 读取mat文件
- path = r"test.mat" # mat文件路径
- data2 = loadmat(path)
- #print(data1.keys()) # 查看mat文件中包含的变量
-
- #Out:
- #dict_keys(['__header__', '__version__', '__globals__', 'A', 'C', 'n', 's'])
- train_images = data1['B']
- train_images = numpy.stack(train_images[:, 0], axis=0)
- train_images = numpy.expand_dims(train_images, axis=-1)
- test_images = data2['C']
- test_images = numpy.stack(test_images[:, 0], axis=0)
- test_images = numpy.expand_dims(test_images, axis=-1)
解释:
xlsx文件标签集的读取选择使用pandas库,读入后的数据结构为dataframe,为了格式统一将其转换成数组形式;
- train_labels = train_labels.to_numpy()
- test_labels = test_labels.to_numpy()
mat文件的读取使用scipy.io中的loadmat,他会返回一个字典,指定对应的标签可获得一个数组。但是这里我希望的数组应该是一个三维图片数组(131,维度,维度),而返回的仅仅为(131,1),因此需要对其中的数据堆叠成能够适用于模型的大小;
- train_images = data1['B']
- train_images = numpy.stack(train_images[:, 0], axis=0)
- train_images = numpy.expand_dims(train_images, axis=-1)
- # 构建卷积神经网络模型
- model = models.Sequential()
- model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(1430,1072,1)))
- model.add(layers.MaxPooling2D((2, 2)))
- model.add(layers.Conv2D(64, (3, 3), activation='relu'))
-
- # 添加全连接层
- model.add(layers.Flatten())
- model.add(layers.Dense(1, activation='sigmoid'))
-
- # 编译模型
- model.compile(optimizer='adam',
- loss=tf.keras.losses.BinaryCrossentropy(),
- metrics=['accuracy'])
-
- # 训练模型
- history = model.fit(train_images, train_labels, epochs=10, batch_size=10,
- validation_data=(test_images, test_labels))
- predictions = model.predict(test_images)
- predictions = numpy.round(predictions).astype(int)
- print(predictions)
-
一个简单的卷积神经网络代码。
- import tensorflow as tf
- from tensorflow.keras import datasets, layers, models
- from scipy.io import loadmat
- import pandas as pd
- import numpy
- train_labels = pd.read_excel('训练.xlsx')
- print(train_labels)
- test_labels = pd.read_excel('测试.xlsx')
- train_labels = train_labels.to_numpy()
- test_labels = test_labels.to_numpy()
- path = r"train.mat" # mat文件路径
- data1 = loadmat(path) # 读取mat文件
- path = r"test.mat" # mat文件路径
- data2 = loadmat(path)
- #print(data1.keys()) # 查看mat文件中包含的变量
-
- #Out:
- #dict_keys(['__header__', '__version__', '__globals__', 'A', 'C', 'n', 's'])
- train_images = data1['B']
- train_images = numpy.stack(train_images[:, 0], axis=0)
- train_images = numpy.expand_dims(train_images, axis=-1)
- test_images = data2['C']
- test_images = numpy.stack(test_images[:, 0], axis=0)
- test_images = numpy.expand_dims(test_images, axis=-1)
-
-
- # 构建卷积神经网络模型
- model = models.Sequential()
- model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(1430,1072,1)))
- model.add(layers.MaxPooling2D((2, 2)))
- model.add(layers.Conv2D(64, (3, 3), activation='relu'))
-
- # 添加全连接层
- model.add(layers.Flatten())
- model.add(layers.Dense(1, activation='sigmoid'))
-
- # 编译模型
- model.compile(optimizer='adam',
- loss=tf.keras.losses.BinaryCrossentropy(),
- metrics=['accuracy'])
-
- # 训练模型
- history = model.fit(train_images, train_labels, epochs=10, batch_size=10,
- validation_data=(test_images, test_labels))
- predictions = model.predict(test_images)
- predictions = numpy.round(predictions).astype(int)
- print(predictions)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。