# Deep Learning within a DICOM WSI Viewer for Histopathology
## 2 Materials and Methods
### Architecture of the Proposed System
In order for the CNNs to be able to infer, it is necessary to obtain the tiles that conform to the image at a certain magnification and split them into patches of the size required by the CNN, and, finally, the inference will be performed by applying a diagnostic rule. It was decided to develop the server that manages the DPL module using Python 3 and Flask as it natively allows us to respond to multiple requests at the same time. This allows the system to be able to have several users simultaneously or to receive different requests from the same user. This server consists of the following endpoints:  
* _getAvailableModels_: Retrieves a list of all available inference models. * _inferUsingModel_: Receives a list of URLs to tiles and a model to perform inference. * _getClassificationResults_: Accesses inference results with an ID provided by the same server. * _getHeatmap_: Returns a heatmap of the WSI and overlays the inference results on the WSI by setting the \(\alpha\) channel. * _getModelParameters_: Retrieves the model parameters. * _getAvailableViews_: Retrieves the types of outputs of each model. As can be observed, the _inferUsingModel_ endpoint does not return the results of the inference. It was decided to follow this approach since, in some cases, this process could take up to several minutes, reaching the point where the connection would time out. To solve this problem, the inferUsingModel endpoint returns a JSON object containing a Universally Unique Identifier (UUID), such as _uuid: \"bd4181 cc-780e-4963-b35c-df476b58 ee03\"_, which identifies the inference process to subsequently be able to access the results using the _getClassificationResults_ or _getHeatmap_ endpoints, also retrieving a JSON object with the UID and the structure containing the final results; in the case of _getClassificationResults_, an example would be (_result: null, status: \"Classifying\", uuid: \"bd4181 cc-780e-4963-b35c-df476b58 ee03\"_). Regarding the models, the framework used is _Pytorch_ stored in the path specified in the _models_folder_ variable of the _constants.py_ file contained in the application. Inside this folder, the following files will be present. * _*.pth_--File storing the architecture and weights of the CNN. * _mean.nyv_--File storing the media to be subtracted. * _labels.txt_--List of all classes that can be assigned by the CNN. * _params.json_--ISON file containing the following attributes: {_patch_width_, _patch_height_, _nclasses_ (number of classes), _X_class_ index for each class, starting with \(X=0\) for the background}. * _diagnosis.py_--Python 3 code file containing the code that performs the diagnostic once the results of the inference of the patches are obtained; it contains the _diagnosis_ function with the following parameters: (i) _results_--list of lists that contain the class assigned to each patch and the probability that it is the correct class; (ii) _nclasses_--number of possible classes; and (iii) _labels_--labels assigned by CNN. * _diagnosis.rule_--Internal function that performs the diagnosis. The DPL inference server can be configured to use either the CPU or an NVIDIA GPU. This configuration is set in the Viewer/envs/dpl.env file, in which the following two parameters are found: (i) _USE_GPU_, a boolean value where 1 indicates GPU usage and 0 indicates CPU usage; (ii) _GPU_DEVICE_, to identify the GPU to be used. By default, the value of this parameter is cuda:0, corresponding to the first NVIDIA GPU detected by the system. In order to use the GPU, nvidiadocker version 2 must be installed. On the other hand, version 20.10 of docker and 1.29 for docker-compose are required as a minimum, which allows the system to run the NVIDIA runtime as configured in the _docker-compose.yaml_ file.