import numpy as np
import lqmtest_x3_5_pool
import lqmtest_x3_2_load_data
import lqmtest_x3_4_conv_proc

class ModelObj: # 网络对象
    def __init__(self, ObjID, ObjType, ObjLable, ParaString, ObjX, ObjY):
        self.ObjID = ObjID  # 图元号
        self.ObjType = ObjType  # 图元类别
        self.ObjLable = ObjLable  # 对象标签
        self.ParaString = ParaString  # 参数字符串
        self.ObjX = ObjX  # 对象位置x坐标
        self.ObjY = ObjY  # 对象位置y坐标
class FullConn_Class(ModelObj):  # 全连接对象
    def __init__(self, ObjID, ObjType, ObjLable, ParaString, ObjX, ObjY):
        super().__init__(ObjID, ObjType, ObjLable, ParaString, ObjX, ObjY)
        self.FullConnProc = self.fullconn_proc  # 基本操作函数
        self.SetFullConnPara = self.setfullconn_para  # 参数设置函数
    def fullconn_proc(self, data, FullConnPara):
        weights = FullConnPara["weights"] # 获取权重矩阵
        bias = FullConnPara["bias"]  # 偏置向量
        # 对输入进行展平处理，变换为单通道的一维数组格式
        data = data.reshape(1, data.shape[1] * data.shape[2])
        # 计算全连接层的线性变换：data与权重矩阵w进行乘法，再加上偏置向量b
        output = np.dot(data, weights.T) + bias
        return output  # 返回全连接计算后的数组
    # 定义一个函数来设置全连接层的相关参数
    def setfullconn_para(self, poolH, poolW):
        height = poolH
        width = poolW  # 获取池化后的图片数组的长度和宽度
        num_outputs = int(input("请输入全连接层的输出节点数量: "))
        weights = np.random.randn(num_outputs, height * width)
        bias = np.random.randn(1, num_outputs)
        # 返回FullConnPara参数，这里用一个字典来存储
        FullConnPara = {"weights": weights, "bias": bias,
                        "num_outputs": num_outputs}
        return FullConnPara
if __name__ == '__main__':
    DataSet = lqmtest_x3_2_load_data.Data_Class("DataSet1", 1, "数据集1", [], 120, 330)
    # setload_data()函数，获取加载数据集的参数
    DataPara = DataSet.SetDataPara()
    train_images, test_images = DataSet.LoadData(DataPara)

    Conv = lqmtest_x3_4_conv_proc.Conv_Class("Conv1", 2, "卷积1", [], 250, 330)
    ConvPara = Conv.SetConvPara()
    for i in range(len(train_images) // 32):
        images = train_images[i * 32:(i + 1) * 32]
        conv_images = []  # 存储卷积处理后的图片的列表
        for image in images:  # 获取训练集的图片数据
            dim = len(image.shape)  # 获取矩阵的维度
            if dim == 2:  # 如果是二维矩阵,则转化为三维矩阵
                image_h, image_w = image.shape
                image = np.reshape(image, (1, image_h, image_w))
                # 调用ConvProc()函数，根据ConvPara参数完成卷积计算
                output = Conv.ConvProc(image, ConvPara)
                conv_images.append(output)  # 将卷积结果存储到列表
            elif dim == 3:  # 若为三维矩阵，则保持不变直接卷积处理
                output = Conv.ConvProc(image, ConvPara)
                conv_images.append(output)
        # 将卷积处理后的图片列表转换为数组形式，方便后续处理
        conv_images = np.array(conv_images)
    
    Pool = lqmtest_x3_5_pool.Pool_Class("Pool1", 3, "最大池化1", [], 380, 330)
    PoolPara = Pool.SetPollPara()
    pool_images = []  # 存储池化处理后的图片的列表
    for image in conv_images:  # 获取卷积后的图片数据
        output = Pool.MaxPoolProc(image, PoolPara)
        pool_images.append(output)  # 将池化结果存储到列表
    # 将池化处理后的图片列表转换为数组形式，方便后续处理
    pool_images = np.array(pool_images)
    
    _, _, poolH, poolW = pool_images.shape
    FullConn = FullConn_Class("FullConn1", 4, "全连接1", [], 510, 330)
    FullConnPara = FullConn.SetFullConnPara(poolH, poolW)
    
    fullconn_images = []      # 存储全连接处理后的图片的列表
    for image in pool_images: # 获取池化后的图片数据
        output = FullConn.FullConnProc(image, FullConnPara)
        fullconn_images.append(output)  # 将全连接处理后的结果存储到列表
    # 将全连接处理后的图片列表转换为数组形式，方便后续处理
    fullconn_images = np.array(fullconn_images)
