# Simplified Machine Learning Model as an Intelligent Support for Safe Urban Cycling
## 3 Materials and Methods
### Cognitive Model Architecture
For example, when two images are input, the two inputs are fed into two neural networks; these two neural networks map the inputs to the newrepresentation space separately, allowing the input to be represented as a value within the new space. The similarity or dissimilarity between both inputs is evaluated by calculating the loss value itself. ```
0:\(S\)--Training dataset, \(f\)--CNN, \(z_{ij}\)--binary label \((z_{ij}\in 0,1)\), \(\mathbf{w}\)--shared weights, \(\eta\)--learning rate
0:\(\mathcal{L}(\mathbf{x}_{i},\mathbf{x}_{j},\mathbf{z}_{\mathrm{ij}})\)
1:for\(S\leftarrow\left\{(\mathbf{x}_{i},\mathbf{x}_{j},z_{ij}),(i,j)\in K\right\}\)do
2:\(h_{i}\gets f(x_{i})\) {feature vector of \(x_{i}\)}
3:\(h_{j}\gets f(x_{j})\) {feature vector of \(x_{j}\)}
4:\(d_{h_{i},h_{j}}\leftarrow\left\|h_{i}-h_{j}\right\|^{2}\) [Euclidean distance]
5: Contrastive loss function \(\mathcal{L}(\mathbf{x}_{i},\mathbf{x}_{j},\mathbf{z}_{\mathrm{ij}})\) (Equation (3))
6: Total error function to be minimized \(\mathcal{L}(w)\) (Equation (2))
7: Optimize and update weights \(\mathbf{w}_{n+1}\leftarrow\mathbf{w}_{n}-\eta\bigtriangledown f(\mathbf{w}_{n})\)
8:endfor
```  
**Algorithm 1** General training algorithm for a Siamese neural network  
Let us say that each pair of training images intrinsically has a binary label \(Y\) assigned to this pair. If \(Y=0\), \(x_{1}\) and \(x_{2}\) are considered similar; otherwise if \(Y=1\). Then the contrastive loss function in its general form can be defined as  
\[\mathcal{L}(\mathrm{W})=\sum_{\mathrm{i=1}}^{\mathrm{P}}\mathrm{L}(\mathrm{W},( Y,\vec{\mathbf{X}}_{1},\vec{\mathbf{X}}_{2})^{\mathrm{i}}) \tag{2}\]  
\[L(W,(Y,\vec{\mathbf{X}}_{1},\vec{\mathbf{X}}_{2})^{j})=(1-Y)L_{S}(D^{j})+YL_{D} (D^{j}) \tag{3}\]  
where \(\vec{\mathbf{X}}_{1}\) and \(\vec{\mathbf{X}}_{2}\) are a pair of input vectors shown to the system; in our case, they are the feature vectors of the input images, \((Y,\vec{\mathbf{X}}_{1},\vec{\mathbf{X}}_{2})^{j})\) is the \(i\)-th labeled pair of samples, \(L_{S}\) is the partial loss function for a pair of similar points, \(L_{D}\) is the partial loss function for a pair of different points, and \(D\) is to shorten the Euclidean distance notation \(D_{X_{1},X_{2}}\). Finally \(P\) is the number of training pairs. Both the function \(L_{S}\) and the function \(L_{D}\) must be designed such that minimizing \(L\) with respect to \(W\) results in low values of \(D\) for similar pairs and high values for \(D\) in different pairs. Specifically, the exact loss function will be the following Equation (4):  
Figure 7: Schematic diagram of the Siamese network. \[L_{c}(W,(Y,\vec{X}_{1},\vec{X}_{2})^{i})=(1-Y)\frac{1}{2}(D)^{2}+Y\frac{1}{2}\{max( 0,m-D)\}^{2} \tag{4}\]  
where \(m>0\) is a margin. The margin defines a radius around \(G_{W}(X)\), which is a set threshold. It can be known from the contrastive loss that the loss function can be used both to express the coincidence of pairs of samples and to train the model with the extracted features effectively. With all this, through the continuous reduction in the loss value, the distance between pairs of similar samples is continuously reduced, while the distance between pairs of dissimilar samples is continuously increased. The general description of the architecture of the proposed Siamese cognitive model is presented in Figure 8. We established as a basis for our Siamese network model an approach similar to the one shown in [28], but adjusting the convolutional neural networks (CNNs) to generate 1024 features instead of 4096, they will also share the same parameters since they are copies of the same CNN. The neural network architecture that learns image embeddings and attribute vectors in the same vector space (embedding space) was used in the implementation of the feature extractor; in this way, the distances between affinity features can be calculated. The two input images \((x_{i},x_{j})\) feed the CNNs, where the two fixed-length feature vectors, \(f(x_{i})\) and \(f(x_{j})\), are obtained.