% This program will connect Visual and LSTM 
% to classify time sequenced framed images 
% 7/29/2024
% Coded in MATLAB R2024b
% Place file under MATLAB folder NIS/Visualfaults

%Remove previous settings
clear
close all

%Load saved trained visual tool
load VisMotionFaultNet.mat

%Set environment
cnnLayers = layerGraph(netCNN);

layerNames = ["data" "pool5-drop_7x7_s1" "loss3-classifier" "prob" "output"];
cnnLayers = removeLayers(cnnLayers,layerNames);
inputSize = netCNN.Layers(1).InputSize(1:2);
averageImage = netCNN.Layers(1).Mean;

inputLayer = sequenceInputLayer([inputSize 3], ...
    'Normalization','zerocenter', ...
    'Mean',averageImage, ...
    'Name','input');

layers = [
    inputLayer
    sequenceFoldingLayer('Name','fold')];

lgraph = addLayers(cnnLayers,layers);
lgraph = connectLayers(lgraph,"fold/out","conv1-7x7_s2");

lstmLayers = netLSTM.Layers;
lstmLayers(1) = [];

layers = [
    sequenceUnfoldingLayer('Name','unfold')
    flattenLayer('Name','flatten')
    lstmLayers];

lgraph = addLayers(lgraph,layers);
lgraph = connectLayers(lgraph,"pool5-7x7_s1","unfold/in");
lgraph = connectLayers(lgraph,"fold/miniBatchSize","unfold/miniBatchSize");

%Connect
VisMFN = assembleNetwork(lgraph);

%Save connected network VIsual Motion Fault Network (VisMFN)
save VisMFNonly.mat VisMFN
