Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Keras、Python纠正基于CNN、LSTM的分类器的输入维度_Python_Tensorflow_Keras_Deep Learning - Fatal编程技术网

使用Keras、Python纠正基于CNN、LSTM的分类器的输入维度

使用Keras、Python纠正基于CNN、LSTM的分类器的输入维度,python,tensorflow,keras,deep-learning,Python,Tensorflow,Keras,Deep Learning,我正在致力于实现一个2D(也许是1D)CNN+LSTM分类器,用于网络流量分类。CNN基本上将用作特征提取器,LSTM将用于分类 我使用了时间分布层来帮助将CNN和LSTM层结合在一起(代码附在后面) 由于输入大小是动态变化的,因此数据点的数量以无表示。 no_rows=20(每个流考虑用于分类的数据包数) 否=7(每个数据包考虑的功能数量) 尽管使用了TimeDistributed layer wrap,但我仍面临一些输入维度问题。不太清楚如何解决这个问题 使用“重塑”作为层来解决此问题是我遇

我正在致力于实现一个2D(也许是1D)CNN+LSTM分类器,用于网络流量分类。CNN基本上将用作特征提取器,LSTM将用于分类

我使用了时间分布层来帮助将CNN和LSTM层结合在一起(代码附在后面) 由于输入大小是动态变化的,因此数据点的数量以无表示。 no_rows=20(每个流考虑用于分类的数据包数) 否=7(每个数据包考虑的功能数量)

尽管使用了TimeDistributed layer wrap,但我仍面临一些输入维度问题。不太清楚如何解决这个问题

使用“重塑”作为层来解决此问题是我遇到的许多修复方法之一,但都不起作用。请让我知道如何建立这个结构,以及如何修复我的代码

谢谢

(使用基于Linux的AWS实例、Ubuntu 16.04和Tensorflow后端来实现代码)

  • 使用Keras核心层的重塑层修复CNN的输出,但没有解决问题
  • 由于存在动态变化的输入大小,必须删除展平层,并将其替换为GlobalMapooling2D层
  • 运行上述代码段时,我会遇到以下错误消息:

    "Input tensor must be of rank 3, 4 or 5 but was {}.".format(n + 2))
    ValueError: Input tensor must be of rank 3, 4 or 5 but was 2.
    
    

    您可以做的一件事是从您的视频源生成一批固定的输入(帧数),并对其进行处理。这样做的准则是:

    def get_data(video_source, batch_size):
        x_data = []
        #Reading the Video from file path
        cap = cv2.VideoCapture(video_source)    
        for i in range(batch_size):
            #To Store Frames
            frames = []
            for j in range(frame_to_process): #here we get frame_to_process
                ret, frame = cap.read()
                if not ret:
                    # print('No frames found!')
                    break
                # converting to frmae gray
                # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
                # resizing frame to a particular input shape
                # frame = cv2.resize(frame,(30,30),interpolation=cv2.INTER_AREA)
                frames.append(frame)
            # appending each batch
            x_data.append(frames)
        return x_data
    
    # number of frames in each batch
    frame_to_process = 30
    # size of each batch
    batch_size = 32
    # make batch of video inputs
    X_data = np.array(get_data(video_source, batch_size, frame_to_process))
    
    提示:另外,您可以使用
    timedistributed
    来代替
    Conv2D
    ,这样可以稍微提高性能

    无论如何,如果您想动态地处理帧,您可以将代码转换为Pytorch,Pytorch具有,您可以在其中提供具有可变批量大小的输入


    请检查xtrain的输入形状,它应该是输入形状=(无,20,7,1)的矩阵
    def get_data(video_source, batch_size):
        x_data = []
        #Reading the Video from file path
        cap = cv2.VideoCapture(video_source)    
        for i in range(batch_size):
            #To Store Frames
            frames = []
            for j in range(frame_to_process): #here we get frame_to_process
                ret, frame = cap.read()
                if not ret:
                    # print('No frames found!')
                    break
                # converting to frmae gray
                # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
                # resizing frame to a particular input shape
                # frame = cv2.resize(frame,(30,30),interpolation=cv2.INTER_AREA)
                frames.append(frame)
            # appending each batch
            x_data.append(frames)
        return x_data
    
    # number of frames in each batch
    frame_to_process = 30
    # size of each batch
    batch_size = 32
    # make batch of video inputs
    X_data = np.array(get_data(video_source, batch_size, frame_to_process))