【深度学习】LSTM/CNN+LSTM实现图片分类

毕设实验设计部分:作为对照的单LSTM模型实现模型以及CNN+LSTM模型实现
注意:一般LSTM单模型不会用来实现图片分类,只做教学用途。

LSTM

主要需要注意的是,LSTM单元输入的是(num_timesteps, num_features)
X = X.reshape(X.shape[0],X.shape[1]*X.shape[2],-1)将图片尺寸转换为可以输入的尺寸

完整代码


import tensorflow as tf
import pathlib
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
import os 
import numpy as np 
from sklearn.model_selection import  train_test_split
from tensorflow.keras.utils import  to_categorical
import cv2 
'''
使用图片数据作为输入数据集,使用lstm作为网络模型
'''
# 图片文件夹
DATA_PATH = os.path.join() # 自行进行填充
# 定义类别名称
actions = np.array([])# 自行进行填充

no_sequences = 30
# 
sequence_length = 30

images_path = []
images,labels = [],[]
for action in actions:
  for sequence in range(no_sequences):
    for frame_num in range(sequence_length):
      img_path = os.path.join(DATA_PATH,action,str(sequence),"{}.jpg".format(frame_num))
      print(img_path)
      # 读入文件路径
      img = cv2.imread(img_path)
      # 改变输入尺寸
      resized_image = cv2.resize(img, (180, 180)) 
      images.append(resized_image)
      # images_path.append(img_path)
      labels.append(action)

print(len(images),len(labels),)


for i in range(len(labels)):
  if labels[i]=='':
    labels[i]= 0 
  elif labels[i] == '':
    labels[i] = 1 
  elif labels[i]=='':
    labels[i] =2 
  elif labels[i]=='':
    labels[i]=3 
  elif labels[i]=='':
    labels[i] = 4 

X = np.array(images)
print(X.shape)
# 转换输入尺寸,以进入rnn进行训练
X = X.reshape(X.shape[0],X.shape[1]*X.shape[2],-1)
Y = to_categorical(labels)
print(Y.shape)
print(X.shape)
X_train,X_test,y_train,y_test = train_test_split(X,Y,test_size=0.2)


from tensorflow.keras.models import  Sequential
from  tensorflow.keras.layers import LSTM,Dense
import  tensorflow as tf

def setupModel():

    model = Sequential()
    model.add(LSTM(64,input_shape=(180*180,3),return_sequences=True,activation='relu'))
    model.add(LSTM(128,return_sequences=True,activation='relu'))
    model.add(LSTM(64,return_sequences=False,activation='relu'))
    model.add(Dense(64,activation='relu'))
    model.add(Dense(32, activation='relu'))
    model.add(Dense(5, activation='softmax'))
    return model 

model = setupModel()

model.compile(
    optimizer='adam',
    loss = 'categorical_crossentropy',
    metrics = ['accuracy']
)
history = model.fit(X_train,y_train,epochs=50)

实现截图

在我的实现中,代码是可以跑通的。
在这里插入图片描述

CNN-LSTM

这里直接给出模型构建,详细的图片分类流程可以参考https://www.tensorflow.org/tutorials/images/classification

实现代码

# CNN-LSTM模型
model = Sequential([
  layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
  layers.Conv2D(16, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Flatten(),
  layers.Reshape((-1, 16)),
  layers.LSTM(64, return_sequences=True),
  layers.LSTM(32),
  layers.Dense(num_classes)
])
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐