Body: <p>As part of my masters thesis, I'm developing generative models for ECGs.
  Right now, I have a Denoising Diffusion Implicit model (DDIM), that transforms random noise into a valid ECG (2s long, or 1024 sample points with a 512Hz sample frequency). It works quite well, but by itself, it is only capable of producing random ECGs. It would be of much more scientific value to produce a realistic ECG that has R-peaks at specific, predetermined points.</p>
  <p>The solution would be a guided diffusion. This relies on an auxillary model, that takes the output of the diffusion model, and gives a prediction of where the peaks currently are. Then I calculate the gradient of the desired peak locations w.r.t the input data (i.e. the output of the DDIM). I add the gradients to the data, and get a slightly transformed noise. Then I can feed it back into the DDIM, and repeat the process until I get a clean ECG with peaks at the desired positions.</p>
  <p>The problem is, I can't figure out a good way to do peak prediction using NNs.
  Researching the problem, people usually recommend other algorithms for peak detection. But in my case, I have to do it via a NN, so I can calculate the gradients. Papers on the matter usually do Peak classification, or segmentation, not predicting the exact location.</p>
  <p>Right now I have the following setup:</p>
  <ul>
  <li><p>A sample ECG, that consists of 1024 points representing 2 seconds. It has 2 to 4 peaks (which is a physiological range for humans), of varying height, normalized to 1 being the highest R-peak.</p>
  </li>
  <li><p>My peak identifier network is a convolutional NN, that takes the ECG as input, and has 128 output nodes. Each node represents an 8 point range. So if node 5 has the output value of close to 1, it means the model predicts there is a peak somewhere between point 40 and point 48.</p>
  </li>
  </ul>
  <p>My problem now is actually the loss function. The ECG is annotated, so I know the actual peak location and can encode them into the same 128 8-point segments.
  Right now I'm using simple categorical cross entropy between the real location and my networks predictions. However, this loses the aspect of &quot;distance&quot;. If the peak really is in the 5th segment, and my network predicts a peak in the 4th segment, this should result in a lower loss than if the network predicts a peak in the 80th segment.</p>
  <p>And because there is a variable amount of peaks (between 2 and 4), I can't simply calculate the distances between the predicted peaks and the real ones, because I don't know how to handle the case where there is 3 peaks, but my model predicts 5. Which peaks do I then use for distance calculation?</p>
  <p>The cross entropy loss almost works. The model gets quite close, but it defaults to a strategy where if the real peaks are say at 24, 58 and 100, it predicts 23, 24 and 58 as the 3 highest output nodes. So regions around a peak often have higher outputs than another peak far away.</p>
  <p>I'd really appreciate some input here. I'm not fixed on segmenting the peaks into blocks of lengths 8, I just thought it would be easier for a model to approximate the peak location, since ECGs can be quite noisy. Also, with the current architecture, I'd need 1024 output nodes, and cross entropy between all of them, which would make for quite a long training, since my loss does not reward &quot;distance&quot; right now.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-08-18T16:45:16.560Z
Comments.Text: Please note that we have a [Medical Sciences](https://medicalsciences.stackexchange.com/questions/tagged/electrocardiogram)) site where more people would understand the question, though perhaps know less about NN; be certain to provide a link to this question. Also GitHub has some software available: https://github.com/topics/ecg-classification
Comments.UserDisplayName: Rob
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-08-24T16:38:28.960Z
Comments.Text: @Rob, I provided all the additional information to show that I really HAVE to use a NN for peak detection. It's not really needed to answer the question. All I want is a way to detect an arbitrary amount of peaks in timeseries via NNs. I really doubt the medical Sciences site will be of much use, as my question is not about ecgs in general.
Comments.UserDisplayName: Jackilion
Tags: <neural-networks><convolutional-neural-networks><loss>
Title: How to detect peak locations via Neural Networks?
UserDisplayName: Jackilion
ThreadText: # How to detect peak locations via Neural Networks?
  
  (Asked by: username_0 on 2022-08-18)
  
  As part of my masters thesis, I'm developing generative models for ECGs.
  Right now, I have a Denoising Diffusion Implicit model (DDIM), that transforms random noise into a valid ECG (2s long, or 1024 sample points with a 512Hz sample frequency). It works quite well, but by itself, it is only capable of producing random ECGs. It would be of much more scientific value to produce a realistic ECG that has R-peaks at specific, predetermined points.
  
  The solution would be a guided diffusion. This relies on an auxillary model, that takes the output of the diffusion model, and gives a prediction of where the peaks currently are. Then I calculate the gradient of the desired peak locations w.r.t the input data (i.e. the output of the DDIM). I add the gradients to the data, and get a slightly transformed noise. Then I can feed it back into the DDIM, and repeat the process until I get a clean ECG with peaks at the desired positions.
  
  The problem is, I can't figure out a good way to do peak prediction using NNs.
  Researching the problem, people usually recommend other algorithms for peak detection. But in my case, I have to do it via a NN, so I can calculate the gradients. Papers on the matter usually do Peak classification, or segmentation, not predicting the exact location.
  
  Right now I have the following setup:
  
  * A sample ECG, that consists of 1024 points representing 2 seconds. It has 2 to 4 peaks (which is a physiological range for humans), of varying height, normalized to 1 being the highest R-peak.
  * My peak identifier network is a convolutional NN, that takes the ECG as input, and has 128 output nodes. Each node represents an 8 point range. So if node 5 has the output value of close to 1, it means the model predicts there is a peak somewhere between point 40 and point 48.
  
  My problem now is actually the loss function. The ECG is annotated, so I know the actual peak location and can encode them into the same 128 8-point segments.
  Right now I'm using simple categorical cross entropy between the real location and my networks predictions. However, this loses the aspect of "distance". If the peak really is in the 5th segment, and my network predicts a peak in the 4th segment, this should result in a lower loss than if the network predicts a peak in the 80th segment.
  
  And because there is a variable amount of peaks (between 2 and 4), I can't simply calculate the distances between the predicted peaks and the real ones, because I don't know how to handle the case where there is 3 peaks, but my model predicts 5. Which peaks do I then use for distance calculation?
  
  The cross entropy loss almost works. The model gets quite close, but it defaults to a strategy where if the real peaks are say at 24, 58 and 100, it predicts 23, 24 and 58 as the 3 highest output nodes. So regions around a peak often have higher outputs than another peak far away.
  
  I'd really appreciate some input here. I'm not fixed on segmenting the peaks into blocks of lengths 8, I just thought it would be easier for a model to approximate the peak location, since ECGs can be quite noisy. Also, with the current architecture, I'd need 1024 output nodes, and cross entropy between all of them, which would make for quite a long training, since my loss does not reward "distance" right now.
  
  **Tags:** neural-networks, convolutional-neural-networks, loss
  
  > **Comments:**
  > **username_1**: Please note that we have a [Medical Sciences](https://medicalsciences.stackexchange.com/questions/tagged/electrocardiogram)) site where more people would understand the question, though perhaps know less about NN; be certain to provide a link to this question. Also GitHub has some software available: https://github.com/topics/ecg-classification  
  > **username_0**: @username_1, I provided all the additional information to show that I really HAVE to use a NN for peak detection. It's not really needed to answer the question. All I want is a way to detect an arbitrary amount of peaks in timeseries via NNs. I really doubt the medical Sciences site will be of much use, as my question is not about ecgs in general.  
  
  ---
  
  ## 0 answers
  
  