CNN对时间序列数据进行分类【Keras】

 

项目结构如上所示

tsv_cnnfile.py为训练的代码

inference.py为模型推理的代码

注意调用训练代码时候可以通过 

python tsv_cnnfile.py -- nb_epochs num 来改变训练epoch的次数。

网络结构

  

运行环境:

  • pip3 install tensorflow==1.13.1
  • pip3 install keras==2.3.1

数据集:37类,训练集390行,测试集391行,每行的数据长度有176。

# Adiac

The Automatic Diatom Identification and Classification (ADIAC) project was a pilot study concerning automatic identification of diatoms (unicellular algae) on the basis of images. The data was donated by Andrei Jalba, a PhD student on the project, which finished in the early 2000s. The outlines are extracted from thresholded images. Presumably the time series are generated as distance to a reference point (the centre being the obvious candidate). The data are very sinusoidal.

Train size: 390

Test size: 391

Missing value: No

Number of classses: 37

Time series length: 176

Data donated by Andrei Jalba (see [1], [2]).

[1] Jalba, Andrei C., Michael HF Wilkinson, and Jos BTM Roerdink. "Automatic segmentation of diatom images for classification." Microscopy research and technique 65.1‐2 (2004): 72-85.

[2] http://www.timeseriesclassification.com/description.php?Dataset=Adiac

 代码:

#!/usr/bin/env 
# -*- coding: utf-8 -*-
"""
Created base on author Stephen Sun Dec 1 8:00 2021

@author: MaoWei Jiang
"""
 
from __future__ import print_function
 
import matplotlib.pyplot as plt
from keras.models import Model
from keras.utils import np_utils
import numpy as np
import pandas as pd
import keras 
from keras.callbacks import ReduceLROnPlateau

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--nb_epochs', type=int,default=100)
parser.add_argument('--batch_size', type=int, default=64)
parser.add_argument('--epochs', type=int, default=100)
parser.add_argument('--weight_file', type=str)
parser.add_argument('--result_file', type=str)
parser.add_argument('--kernel_size', type=int)
parser.add_argument('--pooling_method', type=str)
parser.add_argument('--alpha', type=float)
parser.add_argument('--log_file', type=str)
args = parser.parse_args()
      
def readucr(filename):
    data = np.loadtxt(filename, delimiter = '\t')
    Y = data[:,0]
    X = data[:,1:]
    return X, Y
  
nb_epochs = args.nb_epochs

flist  = ['Adiac']
for each in flist:
    fname = each
    x_train, y_train = readucr(fname+'/'+fname+'_TRAIN.tsv')
    x_test, y_test = readucr(fname+'/'+fname+'_TEST.tsv')
    nb_classes = len(np.unique(y_test))
    batch_size = min(x_train.shape[0]/10, 16)
    
    y_train = (y_train - y_train.min())/(y_train.max()-y_train.min())*(nb_classes-1)
    y_test = (y_test - y_test.min())/(y_test.max()-y_test.min())*(nb_classes-1)
    
    
    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)
    
    x_train_mean = x_train.mean()
    x_train_std = x_train.std()
    x_train = (x_train - x_train_mean)/(x_train_std)
     
    x_test = (x_test - x_train_mean)/(x_train_std)
    x_train = x_train.reshape(x_train.shape + (1,1,))
    x_test = x_test.reshape(x_test.shape + (1,1,))

    x = keras.layers.Input(x_train.shape[1:])
#    drop_out = Dropout(0.2)(x)
    conv1 = keras.layers.Conv2D(128, 8, 1, border_mode='same')(x)
    conv1 = keras.layers.normalization.BatchNormalization()(conv1)
    conv1 = keras.layers.Activation('relu')(conv1)
    
#    drop_out = Dropout(0.2)(conv1)
    conv2 = keras.layers.Conv2D(256, 5, 1, border_mode='same')(conv1)
    conv2 = keras.layers.normalization.BatchNormalization()(conv2)
    conv2 = keras.layers.Activation('relu')(conv2)
    
#    drop_out = Dropout(0.2)(conv2)
    conv3 = keras.layers.Conv2D(128, 3, 1, border_mode='same')(conv2)
    conv3 = keras.layers.normalization.BatchNormalization()(conv3)
    conv3 = keras.layers.Activation('relu')(conv3)
    
    full = keras.layers.pooling.GlobalAveragePooling2D()(conv3)    
    out = keras.layers.Dense(nb_classes, activation='softmax')(full)
    
    
    model = Model(input=x, output=out)
     
    optimizer = keras.optimizers.Adam()
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])
     
    reduce_lr = ReduceLROnPlateau(monitor = 'loss', factor=0.5,
                      patience=50, min_lr=0.0001) 
    history = model.fit(x_train, Y_train, batch_size=batch_size, nb_epoch=nb_epochs,
              verbose=1, validation_data=(x_test, Y_test), callbacks = [reduce_lr])
    keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, verbose=0, mode='auto') 
    model.save('FCN_CBF_1500.h5')
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
print(x_train.shape[1:])#input_shape
epochs = range(len(acc))

plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()

plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()


# inference.py
#   Created by Jew,Dec 1,2021 9:07

from keras.models import Model,load_model
from keras.utils import np_utils
import numpy as np
import pandas as pd
import keras 


def readucr(filename):
    data = np.loadtxt(filename, delimiter = '\t')
    Y = data[:,0]
    X = data[:,1:]
    return X, Y
'''
模型推理
'''
flist  = ['Adiac']
for each in flist:
    fname = each
    x_train, y_train = readucr(fname+'/'+fname+'_TRAIN.tsv')
    x_test, y_test = readucr(fname+'/'+fname+'_TEST.tsv')
    nb_classes = len(np.unique(y_test))
    batch_size = min(x_train.shape[0]/10, 16)
    
    y_train = (y_train - y_train.min())/(y_train.max()-y_train.min())*(nb_classes-1)
    y_test = (y_test - y_test.min())/(y_test.max()-y_test.min())*(nb_classes-1)
    
    
    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)
    
    x_train_mean = x_train.mean()
    x_train_std = x_train.std()
    x_train_1 = (x_train - x_train_mean)/(x_train_std)
     
    x_test_1 = (x_test - x_train_mean)/(x_train_std)
    x_train = x_train.reshape(x_train_1.shape + (1,1))
    x_test = x_test.reshape(x_test_1.shape + (1,1))
    # x_train = x_train.reshape(x_train_1.shape + (1,1,))
    # x_test = x_test.reshape(x_test_1.shape + (1,1,))

print(x_test_1.shape)   
print(x_test.shape) 


# print(input)

model = load_model('FCN_CBF_1500.h5')
output = model.predict(x_test)
output = np.argmax(output,axis=1)+1
print(output)

        期间要是遇见什么问题,欢迎评论下方一起讨论。

参考资料:

基于深度学习的时间序列分类[含代码]_格拉迪沃的博客-CSDN博客

to_categorical()Keras

keras中to_categorical函数解析_文科升的博客-CSDN博客_to_categorical

cnn_keras

深度学习:Keras入门(二)之卷积神经网络(CNN)_天为我蓝的博客-CSDN博客

Keras学习(四)——CNN卷积神经网络_cchangcs-CSDN博客

Keras搭建多分类模型

【Python与机器学习】:利用Keras进行多类分类 - Arkenstone - 博客园

sparse_categorical_crossentropy的使用(精品)

sparse_categorical_crossentropy的使用_vivi_and_qiao的博客-CSDN博客_sparse_categorical_crossentropy

基于深度学习的时间序列分类[含代码]

基于深度学习的时间序列分类[含代码]_格拉迪沃的博客-CSDN博客

numpy.shape的用法辨析shape[1:],shape[:-1]的区别

numpy.shape的用法辨析shape[1:],shape[:-1]的区别_道一斩的博客-CSDN博客_shape[-1]

[深度学习] kerasEarlyStopping使用与技巧

[深度学习] keras的EarlyStopping使用与技巧_小墨鱼的专栏-CSDN博客_earlystopping

Keras model.fit()参数详解+Keras回调函数+Earlystopping

Keras model.fit()参数详解+Keras回调函数+Earlystopping - 知乎

keras模型训练与保存的call_back的设置

keras模型训练与保存的call_back的设置_Mr_不想起床的博客-CSDN博客

Tensorflow2.0实践常见问题&解决方法

Tensorflow2.0实践常见问题&解决方法 - 知乎

np.argmax()函数的作用

np.argmax()函数的作用_Vaxue的博客-CSDN博客_np.argmax函数

Keras BP-处理手写数据集

Keras保存模型并载入模型继续训练 - 凌逆战 - 博客园

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:书香水墨 设计师:CSDN官方博客 返回首页

打赏作者

思考实践

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值