Skip to content

Commit ecb2b14

Browse files
committed
add en readme
1 parent f5c2620 commit ecb2b14

File tree

2 files changed

+256
-1
lines changed

2 files changed

+256
-1
lines changed

README.en.md

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
# Image-Recognition-system
2+
3+
**Alzheimer's Intelligent Diagnosis Web Application based on 3D Convolutional Neural Network and the ADNI Dataset**
4+
5+
A simple medical image recognition system, image recognition visualization interface, OCR, fast deployment of deep learning models as web applications, web prediction system, image recognition front-end web page, image recognition Demo display-Pywebio. AI artificial intelligence image recognition-Pytorch; nii medical image processing; ADNI dataset. 100% pure Python code, lightweight, easy to reproduce
6+
7+
[简体中文文档](./README.md)
8+
9+
[Personal website: www.bytesc.top](http://www.bytesc.top) includes online demonstrations
10+
11+
## Function introduction
12+
13+
- 1. Intelligent diagnosis of Alzheimer's disease based on brain MRI medical images
14+
- 2. Written in pure python, lightweight, easy to reproduce and deploy
15+
- 3. High code readability, with extremely detailed comments in the core part
16+
17+
## Interface display
18+
19+
- Upload image
20+
![image](./readme_img/1.png)
21+
- Return result
22+
![image](./readme_img/2.png)
23+
- Model output chart
24+
![image](./readme_img/3.png)
25+
- View uploaded images
26+
![image](./readme_img/3-1.png)
27+
28+
## How to use
29+
30+
python version 3.9
31+
32+
Requires `4GB` or more memory
33+
34+
First install dependencies
35+
36+
```bash
37+
pip install -r requirement.txt
38+
```
39+
40+
zlzheimer-diagnostic-system.py is the project entry point, run this file to start the server
41+
42+
```bash
43+
python zlzheimer-diagnostic-system.py
44+
```
45+
46+
Copy the link to the browser and open it
47+
48+
![image](./readme_img/4.png)
49+
50+
Click "Demo" to enter the Web interface
51+
52+
![image](./readme_img/5.png)
53+
54+
After that, you can click "Use example images" to use the default test cases. You can also upload .nii image files for testing.
55+
I provide a small number of sample image data in the [`lqdata` repository](https://github.com/bytesc/lqdata).
56+
57+
```bash
58+
git clone https://github.com/bytesc/lqdata.git
59+
```
60+
61+
- If an error is reported after uploading the image, you may need to manually create a folder `uploaded_img` in the root directory.
62+
63+
## Project structure
64+
65+
```
66+
.
67+
│ zlzheimer-diagnostic-system.py
68+
│ datasets.py
69+
│ model.py
70+
│ train.py
71+
│ myModel_109.pth
72+
│ README.md
73+
│ requirements.txt
74+
75+
├─demodata
76+
│ │ demo.nii
77+
├─readme_img
78+
└─uploaded_img
79+
```
80+
81+
- zlzheimer-diagnostic-system.py main project file for starting Web applications
82+
- datasets.py processing dataset
83+
- model.py defining model
84+
- train.py training model
85+
- myModel_109.pth trained model
86+
- readme_img folder for storing uploaded medical images and rendered pictures
87+
- demodata folder for storing some medical image files for testing.
88+
89+
## Core code of the classifier
90+
91+
```python
92+
from torch import nn
93+
import torch
94+
95+
class ClassificationModel3D(nn.Module):
96+
"""Classification model"""
97+
def __init__(self, dropout=0.4, dropout2=0.4):
98+
nn.Module.__init__(self)
99+
100+
# Define four Conv3d layers
101+
self.Conv_1 = nn.Conv3d(1, 8, 3) # Input channel is 1, output channel is 8, kernel size is 3x3x3
102+
self.Conv_2 = nn.Conv3d(8, 16, 3) # Input channel is 8, output channel is 16, kernel size is 3x3x3
103+
self.Conv_3 = nn.Conv3d(16, 32, 3) # Input channel is 16, output channel is 32, kernel size is 3x3x3
104+
self.Conv_4 = nn.Conv3d(32, 64, 3) # Input channel is 32, output channel is 64, kernel size is 3x3x3
105+
106+
# Define four BatchNorm3d layers, one after each convolution layer
107+
self.Conv_1_bn = nn.BatchNorm3d(8)
108+
self.Conv_2_bn = nn.BatchNorm3d(16)
109+
self.Conv_3_bn = nn.BatchNorm3d(32)
110+
self.Conv_4_bn = nn.BatchNorm3d(64)
111+
112+
# Define four MaxPool3d layers, one after each convolution layer
113+
self.Conv_1_mp = nn.MaxPool3d(2) # Pooling kernel size is 2
114+
self.Conv_2_mp = nn.MaxPool3d(3) # Pooling kernel size is 3
115+
self.Conv_3_mp = nn.MaxPool3d(2) # Pooling kernel size is 2
116+
self.Conv_4_mp = nn.MaxPool3d(3) # Pooling kernel size is 3
117+
118+
# Define two fully connected layers
119+
self.dense_1 = nn.Linear(4800, 128) # Input dimension is 4800, output dimension is 128
120+
self.dense_2 = nn.Linear(128, 5) # Input dimension is 128, output dimension is 5. Since this is a five-class problem, the final output dimension must be 5
121+
122+
# Define ReLU activation function and dropout layer
123+
self.relu = nn.ReLU()
124+
self.dropout = nn.Dropout(dropout) # Prevent overfitting
125+
self.dropout2 = nn.Dropout(dropout2) # Enhance robustness
126+
127+
def forward(self, x):
128+
# First convolutional layer
129+
x = self.relu(self.Conv_1_bn(self.Conv_1(x)))
130+
"""
131+
This line of code performs convolutional, batch normalization and ReLU activation operations on the input x.
132+
133+
self.Conv_1(x) performs a 3D convolution operation on the input x and outputs a feature map.
134+
135+
self.Conv_1_bn(...) performs batch normalization on the feature map output by the convolution operation to obtain a normalized feature map.
136+
137+
self.relu(...) performs a ReLU activation function operation on the normalized feature map to obtain an activated feature map.
138+
139+
The purpose of this operation is to extract features from the input x and nonlinearize them so that the network can better learn these features. The batch normalization technique used here can accelerate the training process of the model and improve its generalization ability. The final output result is a feature map x processed by convolutional, batch normalization and ReLU activation functions.
140+
"""
141+
# Max pooling of the first convolutional layer
142+
x = self.Conv_1_mp(x)
143+
"""
144+
This line of code performs a maximum pooling operation on the input x to reduce the size of the feature map by half.
145+
146+
self.Conv_1_mp(...) performs a maximum pooling operation on the input x with a pooling kernel size of 2.
147+
148+
The pooling operation extracts the maximum value in each pooling window in the feature map as the value at the corresponding position in the output feature map, thereby reducing the size of the feature map by half.
149+
150+
Maximum pooling can help the network achieve spatial invariance so that it can recognize the same features when there are slight changes in the input. In this model, after maximum pooling, the feature map x will be passed to the next convolutional layer for feature extraction and nonlinear processing.
151+
"""
152+
153+
# Second convolutional layer
154+
x = self.relu(self.Conv_2_bn(self.Conv_2(x)))
155+
# Max pooling of second convolutional layer
156+
x = self.Conv_2_mp(x)
157+
# Third convolutional layer
158+
x = self.relu(self.Conv_3_bn(self.Conv_3(x)))
159+
# Max pooling of third convolutional layer
160+
x = self.Conv_3_mp(x)
161+
# Fourth convolutional layer
162+
x = self.relu(self.Conv_4_bn(self.Conv_4(x)))
163+
# Max pooling of fourth convolutional layer
164+
x = self.Conv_4_mp(x)
165+
# Flatten tensor into a one-dimensional vector
166+
x = x.view(x.size(0), -1)
167+
"""
168+
This line of code flattens the input tensor x into a one-dimensional vector.
169+
170+
x.size(0) gets the size of the first dimension of the input tensor x, which is the batch size of the tensor.
171+
172+
-1 means to flatten the second dimension and all dimensions after it into one dimension.
173+
174+
x.view(...) performs a shape transformation on the input tensor x, flattening it into a one-dimensional vector.
175+
176+
The purpose of this operation is to transform the feature map x processed by convolution and pooling into a one-dimensional vector so that it can be passed to the fully connected layer for classification or regression tasks. The size of the flattened vector is (batch_size, num_features), where batch_size is the batch size of the input tensor and num_features is the number of elements in the flattened vector, which is also the number of features after convolution and pooling processing.
177+
"""
178+
179+
# dropout layer
180+
x = self.dropout(x)
181+
"""
182+
This line of code performs a dropout operation on the input tensor x, i.e., sets some elements of the input tensor to zero with a certain probability.
183+
184+
self.dropout(...) performs a dropout operation on the input tensor x, with a dropout probability of dropout.
185+
186+
The dropout operation sets some elements of the input tensor to zero with a certain probability, achieving the purpose of random deactivation. This can reduce overfitting and enhance the generalization ability of the model.
187+
188+
In this model, the dropout operation is applied before the fully connected layer, which can help the model better learn the features of the data and prevent overfitting. The resulting x tensor is the result after the dropout operation and will be passed to the next fully connected layer for processing.
189+
"""
190+
# fully connected layer 1
191+
x = self.relu(self.dense_1(x))
192+
"""
193+
This line of code performs a fully connected operation on the input tensor x and applies the ReLU activation function.
194+
195+
self.dense_1(x) performs a fully connected operation on the input tensor x, mapping it to a feature space of size 128.
196+
197+
self.relu(...) applies the ReLU activation function to the output of the fully connected layer to obtain an activated feature vector.
198+
199+
In this model, the role of the fully connected layer is to map the feature vector processed by convolution, pooling, and dropout to a new feature space for classification or regression tasks. The role of the ReLU activation function is to nonlinearize the feature vector so that the network can better learn the nonlinear correlation in the data. The resulting x tensor is the result after processing by the fully connected layer and ReLU activation function and will be passed to the next dropout layer for processing.
200+
"""
201+
# dropout2 layer
202+
x = self.dropout2(x)
203+
# fully connected layer 2
204+
x = self.dense_2(x)
205+
# return output result
206+
return x
207+
208+
209+
if __name__ == "__main__":
210+
# create an instance of ClassificationModel3D class named model, i.e., create a 3D image classification model
211+
model = ClassificationModel3D()
212+
213+
# create a test tensor test_tensor with shape (1, 1, 166, 256, 256),
214+
# where 1 represents batch size, 1 represents input channel number, 166, 256 and 256 represent depth, height and width of input data respectively
215+
test_tensor = torch.ones(1, 1, 166, 256, 256)
216+
217+
# perform forward propagation on test tensor test_tensor to obtain output result output from model
218+
output = model(test_tensor)
219+
220+
# print shape of output result, i.e., (batch_size, num_classes), where batch_size is batch size of test tensor and num_classes is number of classes in classification task
221+
print(output.shape)
222+
223+
```
224+
225+
If you need to train the model yourself, please go to the [ADNI official website](https://adni.loni.usc.edu) to obtain complete data. The dataset file structure should be as follows:
226+
227+
```txt
228+
/lqdata/date/
229+
├─AD
230+
│ 1.nii
231+
│ 2.nii
232+
│ ...
233+
234+
├─CN
235+
│ 1.nii
236+
│ ...
237+
├─EMCI
238+
├─LMCI
239+
├─MCI
240+
241+
└─test
242+
├─AD
243+
│ 1.nii
244+
│ ...
245+
├─CN
246+
├─EMCI
247+
├─LMCI
248+
└─MCI
249+
```
250+
251+
ref: https://github.com/moboehle/Pytorch-LRP
252+
253+
Dataset: https://adni.loni.usc.edu

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Image-Recognition-system
22

3-
基于 ADNI 数据集的阿尔兹海默智能诊断 Web 应用
3+
**基于 3D 卷积神经网络(CNN)的阿尔兹海默智能诊断 Web 应用**
44

55
简单医学影像识别系统,图像识别可视化界面,OCR,快速部署深度学习模型为网页应用,Web 预测系统,图像识别前端网页,图像识别 Demo 展示-Pywebio。AI 人工智能图像识别-Pytorch;nii 医学影像处理;ADNI 数据集。100%纯 Python 代码,轻量化,易复现
66

7+
🚩[Readme in English](./README.en.md)
8+
79
[个人网站:www.bytesc.top](http://www.bytesc.top) 包含在线演示
810

911
## 功能简介

0 commit comments

Comments
 (0)