Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
7ad62dc
Create Test_file
ShangGao-forever Oct 30, 2021
3410f1e
Create README.md
ShangGao-forever Oct 30, 2021
79fb898
Delete recognition/SG directory
ShangGao-forever Oct 30, 2021
df24814
Add files via upload
ShangGao-forever Oct 30, 2021
e7a6440
Add files via upload
ShangGao-forever Oct 30, 2021
b7997a1
Update README.md
ShangGao-forever Oct 30, 2021
71c3280
Update README.md
ShangGao-forever Oct 30, 2021
1aadbcb
Create Dataset.ipynb
ShangGao-forever Oct 30, 2021
fdb3c8e
Create Models.ipynb
ShangGao-forever Oct 30, 2021
6af5ab4
Update Models.ipynb
ShangGao-forever Oct 30, 2021
baad26b
Update Models.ipynb
ShangGao-forever Oct 30, 2021
5abb5e9
Update README.md
ShangGao-forever Oct 30, 2021
88e1091
Delete Dataset.ipynb
ShangGao-forever Oct 30, 2021
980da7f
Create Dataset.py
ShangGao-forever Oct 30, 2021
f2cb540
Update Models.ipynb
ShangGao-forever Oct 30, 2021
d4ef4ff
Create train_stylegan.ipynb
ShangGao-forever Oct 30, 2021
63b07df
Merge branch 'topic-recognition' of https://github.com/ShangGao-forev…
ShangGao-forever Oct 30, 2021
1fb94d5
Update Model1.py
ShangGao-forever Oct 30, 2021
b733c00
Update Model1.py
ShangGao-forever Oct 30, 2021
7ca9593
Update train_stylegan.ipynb
ShangGao-forever Oct 30, 2021
a0a1ecb
Update train_stylegan.ipynb
ShangGao-forever Oct 30, 2021
0d06e5d
Update train_stylegan.ipynb
ShangGao-forever Oct 31, 2021
b89f57b
Update Model1.py
ShangGao-forever Oct 31, 2021
df31f43
Update README.md
ShangGao-forever Oct 31, 2021
226daa7
Update README.md
ShangGao-forever Oct 31, 2021
ad760d3
Create Model2.py
ShangGao-forever Nov 1, 2021
9188279
Update train_stylegan.ipynb
ShangGao-forever Nov 1, 2021
95f6370
Update Model1.py
ShangGao-forever Nov 1, 2021
a66fd00
Update README.md
ShangGao-forever Nov 1, 2021
bb5952a
Update Dataset.py
ShangGao-forever Nov 2, 2021
b15f9a3
Update README.md
ShangGao-forever Nov 2, 2021
739c008
Update train_stylegan.ipynb
ShangGao-forever Nov 2, 2021
75fc2e3
Update Model1.py
ShangGao-forever Nov 3, 2021
febdd34
Update Model2.py
ShangGao-forever Nov 3, 2021
6c88231
Update train_stylegan.ipynb
ShangGao-forever Nov 3, 2021
620c51b
Create train_stylegan.py
ShangGao-forever Nov 3, 2021
6909f56
Update train_stylegan.ipynb
ShangGao-forever Nov 3, 2021
682218c
Update README.md
ShangGao-forever Nov 3, 2021
726d6d0
Create size_128.png
ShangGao-forever Nov 3, 2021
4524d24
Create size_64.png
ShangGao-forever Nov 3, 2021
67ec833
Create size_256.png
ShangGao-forever Nov 3, 2021
2a426cb
Create size_8.png
ShangGao-forever Nov 3, 2021
e5841d8
Create Generate_image.py
ShangGao-forever Nov 3, 2021
d521449
Update README.md
ShangGao-forever Nov 3, 2021
67bc280
Update README.md
ShangGao-forever Nov 3, 2021
15a2232
Update README.md
ShangGao-forever Nov 3, 2021
e4e90c3
Update README.md
ShangGao-forever Nov 3, 2021
5003adc
Update README.md
ShangGao-forever Nov 3, 2021
2dafca9
Update README.md
ShangGao-forever Nov 3, 2021
516d737
Update README.md
ShangGao-forever Nov 3, 2021
a048362
Update README.md
ShangGao-forever Nov 3, 2021
24c2c92
Update README.md
ShangGao-forever Nov 3, 2021
98457b7
Revert "Create Generate_image.py"
ShangGao-forever Nov 3, 2021
04e54e9
Create generate_mixing.py
ShangGao-forever Nov 3, 2021
88931b0
Delete Models.ipynb
ShangGao-forever Nov 3, 2021
df546b5
Update train_stylegan.ipynb
ShangGao-forever Nov 3, 2021
2d43cf6
Update train_stylegan.py
ShangGao-forever Nov 3, 2021
fd8aff5
Update generate_mixing.py
ShangGao-forever Nov 3, 2021
7d3f638
Update Model2.py
ShangGao-forever Nov 3, 2021
6eb7953
Update train_stylegan.py
ShangGao-forever Nov 3, 2021
5f45c53
Update Model2.py
ShangGao-forever Nov 3, 2021
65bb777
Update Model1.py
ShangGao-forever Nov 3, 2021
3aaa41f
Update README.md
ShangGao-forever Nov 3, 2021
5b45ada
Merge branch 'topic-recognition' of https://github.com/ShangGao-forev…
ShangGao-forever Nov 3, 2021
5723769
Create Structure.png
ShangGao-forever Nov 3, 2021
f474c19
Create ADAIN.png
ShangGao-forever Nov 3, 2021
4baacdc
Update README.md
ShangGao-forever Nov 3, 2021
14bb97a
Update README.md
ShangGao-forever Nov 3, 2021
3307972
Update README.md
ShangGao-forever Nov 3, 2021
cfde0e5
Create prepare_data.py
ShangGao-forever Nov 5, 2021
6deca79
Update train_stylegan.py
ShangGao-forever Nov 5, 2021
11ba208
Update train_stylegan.py
ShangGao-forever Nov 5, 2021
f59e9c6
Update README.md
ShangGao-forever Nov 5, 2021
b29bce9
Update train_stylegan.py
ShangGao-forever Nov 5, 2021
f3ebe14
Create sample-iter20500.png
ShangGao-forever Nov 5, 2021
a19fa58
Create sample-iter32500.png
ShangGao-forever Nov 5, 2021
2b4a83d
Create sample-iter5000.png
ShangGao-forever Nov 5, 2021
d99803d
Create sample-iter55000.png
ShangGao-forever Nov 5, 2021
8dd69f9
Update README.md
ShangGao-forever Nov 5, 2021
afb3c7a
Update README.md
ShangGao-forever Nov 5, 2021
b7fc52e
Update README.md
ShangGao-forever Nov 5, 2021
f19b3f4
Delete train_stylegan.ipynb
ShangGao-forever Nov 5, 2021
11a40b1
Create train_stylegan_new.ipynb
ShangGao-forever Nov 5, 2021
2dab6a6
Merge branch 'topic-recognition' of https://github.com/ShangGao-forev…
ShangGao-forever Nov 5, 2021
9aa0fd8
Update README.md
ShangGao-forever Nov 5, 2021
52759b5
Update README.md
ShangGao-forever Nov 5, 2021
208edb2
Update train_stylegan.py
ShangGao-forever Nov 5, 2021
399f7a4
Update Model1.py
ShangGao-forever Nov 5, 2021
c4f468e
Update Model2.py
ShangGao-forever Nov 5, 2021
63e3702
Merge branch 'topic-recognition' of https://github.com/ShangGao-forev…
ShangGao-forever Nov 5, 2021
f2d4dcd
Update train_stylegan.py
ShangGao-forever Nov 10, 2021
f424e94
Create sample
ShangGao-forever Nov 10, 2021
2b497a1
Delete .gitignore
ShangGao-forever Nov 22, 2021
f91d5e3
Create Models-checkpoint.ipynb
ShangGao-forever Nov 22, 2021
ffb3515
Create train_stylegan-checkpoint.ipynb
ShangGao-forever Nov 22, 2021
a9a7830
Create Dataset.cpython-38.pyc
ShangGao-forever Nov 22, 2021
5bb5988
Create Model1.cpython-38.pyc
ShangGao-forever Nov 22, 2021
4344824
Create Model1.cpython-39.pyc
ShangGao-forever Nov 22, 2021
393d9f3
Create Model2.cpython-38.pyc
ShangGao-forever Nov 22, 2021
228c1e0
Create Model2.cpython-39.pyc
ShangGao-forever Nov 22, 2021
ca36b40
Delete train_stylegan_new.ipynb
ShangGao-forever Nov 22, 2021
9e067d0
Delete Model2.cpython-39.pyc
ShangGao-forever Nov 22, 2021
c71e435
Delete Model2.cpython-38.pyc
ShangGao-forever Nov 22, 2021
fa3802a
Delete Model1.cpython-39.pyc
ShangGao-forever Nov 22, 2021
264f1bf
Delete Model1.cpython-38.pyc
ShangGao-forever Nov 22, 2021
34718de
Delete Dataset.cpython-38.pyc
ShangGao-forever Nov 22, 2021
0038e0e
Delete train_stylegan-checkpoint.ipynb
ShangGao-forever Nov 22, 2021
206ecb4
Delete Models-checkpoint.ipynb
ShangGao-forever Nov 22, 2021
4759f55
Revert "Delete .gitignore"
ShangGao-forever Nov 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 87 additions & 38 deletions recognition/ISICs_UNet/README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,101 @@
# Segmenting ISICs with U-Net
# Segment the ISICs data set with the U-net

COMP3710 Report recognition problem 3 (Segmenting ISICs data set with U-Net) solved in TensorFlow
## Project Overview
This project aim to solve the segmentation of skin lesian (ISIC2018 data set) using the U-net, with all labels having a minimum Dice similarity coefficient of 0.7 on the test set[Task 3].

Created by Christopher Bailey (45576430)
## ISIC2018
![ISIC example](imgs/example.jpg)

## The problem and algorithm
The problem solved by this program is binary segmentation of the ISICs skin lesion data set. Segmentation is a way to label pixels in an image according to some grouping, in this case lesion or non-lesion. This translates images of skin to masks representing areas of concern for skin lesions.
Skin Lesion Analysis towards Melanoma Detection

U-Net is a form of autoencoder where the downsampling path is expected to learn the features of the image and the upsampling path learns how to recreate the masks. Long skip connections between downpooling and upsampling layers are utilised to overcome the bottleneck in traditional autoencoders allowing feature representations to be recreated.
Task found in https://challenge2018.isic-archive.com/

## How it works
A four layer padded U-Net is used, preserving skin features and mask resolution. The implementation utilises Adam as the optimizer and implements Dice distance as the loss function as this appeared to give quicker convergence than other methods (eg. binary cross-entropy).

The utilised metric is a Dice coefficient implementation. My initial implementation appeared faulty and was replaced with a 3rd party implementation which appears correct. 3 epochs was observed to be generally sufficient to observe Dice coefficients of 0.8+ on test datasets but occasional non-convergence was observed and could be curbed by increasing the number of epochs. Visualisation of predictions is also implemented and shows reasonable correspondence. Orange bandaids represent an interesting challenge for the implementation as presented.
## U-net
![UNet](imgs/uent.png)

### Training, validation and testing split
Training, validation and testing uses a respective 60:20:20 split, a commonly assumed starting point suggested by course staff. U-Net in particular was developed to work "with very few training images" (Ronneberger et al, 2015) The input data for this problem consists of 2594 images and masks. This split appears to provide satisfactory results.
U-net is one of the popular image segmentation architectures used mostly in biomedical purposes. The name UNet is because it’s architecture contains a compressive path and an expansive path which can be viewed as a U shape. This architecture is built in such a way that it could generate better results even for a less number of training data sets.

## Using the model
### Dependencies required
* Python3 (tested with 3.8)
* TensorFlow 2.x (tested with 2.3)
* glob (used to load filenames)
* matplotlib (used for visualisations, tested with 3.3)
## Data Set Structure

### Parameter tuning
The model was developed on a GTX 1660 TI (6GB VRAM) and certain values (notably batch size and image resolution) were set lower than might otherwise be ideal on more capable hardware. This is commented in the relevant code.
data set folder need to be stored in same directory with structure same as below
```bash
ISIC2018
|_ ISIC2018_Task1-2_Training_Input_x2
|_ ISIC_0000000
|_ ISIC_0000001
|_ ...
|_ ISIC2018_Task1_Training_GroundTruth_x2
|_ ISIC_0000000_segmentation
|_ ISIC_0000001_segmentation
|_ ...
```

### Running the model
The model is executed via the main.py script.
## Dice Coefficient

### Example output
Given a batch size of 1 and 3 epochs the following output was observed on a single run:
Era | Loss | Dice coefficient
--- | ---- | ----------------
Epoch 1 | 0.7433 | 0.2567
Epoch 2 | 0.3197 | 0.6803
Epoch 3 | 0.2657 | 0.7343
Testing | 0.1820 | 0.8180
The Sørensen–Dice coefficient is a statistic used to gauge the similarity of two samples.

Further information in https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient

### Figure 1 - example visualisation plot
Skin images in left column, true mask middle, predicted mask right column
![Visualisation of predictions](visual.png)
## Dependencies

## References
Segments of code in this assignment were used from or based on the following sources:
1. COMP3710-demo-code.ipynb from Guest Lecture
1. https://www.tensorflow.org/tutorials/load_data/images
1. https://www.tensorflow.org/guide/gpu
1. Karan Jakhar (2019) https://medium.com/@karan_jakhar/100-days-of-code-day-7-84e4918cb72c
- python 3
- tensorflow 2.1.0
- pandas 1.1.4
- numpy 1.19.2
- matplotlib 3.3.2
- scikit-learn 0.23.2
- pillow 8.0.1


## Usages

- Run `train.py` for training the UNet on ISIC data.
- Run `evaluation.py` for evaluation and case present.

## Advance

- Modify `setting.py` for custom setting, such as different batch size.
- Modify `unet.py` for custom UNet, such as different kernel size.

## Algorithm

- data set:
- The data set we used is the training set of ISIC 2018 challenge data which has segmentation labels.
- Training: Validation: Test = 1660: 415: 519 = 0.64: 0.16 : 0.2 (Training: Test = 4: 1 and in Training, further split 4: 1 for Training: Validation)
- Training data augmentations: rescale, rotate, shift, zoom, grayscale
- model:
- Original UNet with padding which can keep the shape of input and output same.
- The first convolutional layers has 16 output channels.
- The activation function of all convolutional layers is ELU.
- Without batch normalization layers.
- The inputs is (384, 512, 1)
- The output is (384, 512, 1) after sigmoid activation.
- Optimizer: Adam, lr = 1e-4
- Loss: dice coefficient loss
- Metrics: accuracy & dice coefficient

## Results

Evaluation dice coefficient is 0.805256724357605.

plot of train/valid Dice coefficient:

![img](imgs/train_and_valid_dice_coef.png)

case present:

![case](imgs/case%20present.png)

## Reference
Manna, S. (2020). K-Fold Cross Validation for Deep Learning using Keras. [online] Medium. Available at: https://medium.com/the-owl/k-fold-cross-validation-in-keras-3ec4a3a00538 [Accessed 24 Nov. 2020].

zhixuhao (2020). zhixuhao/unet. [online] GitHub. Available at: https://github.com/zhixuhao/unet.

GitHub. (n.d.). NifTK/NiftyNet. [online] Available at: https://github.com/NifTK/NiftyNet/blob/a383ba342e3e38a7ad7eed7538bfb34960f80c8d/niftynet/layer/loss_segmentation.py [Accessed 24 Nov. 2020].

Team, K. (n.d.). Keras documentation: Losses. [online] keras.io. Available at: https://keras.io/api/losses/#creating-custom-losses [Accessed 24 Nov. 2020].

262588213843476 (n.d.). unet.py. [online] Gist. Available at: https://gist.github.com/abhinavsagar/fe0c900133cafe93194c069fe655ef6e [Accessed 24 Nov. 2020].

Stack Overflow. (n.d.). python - Disable Tensorflow debugging information. [online] Available at: https://stackoverflow.com/questions/35911252/disable-tensorflow-debugging-information [Accessed 24 Nov. 2020].
66 changes: 66 additions & 0 deletions recognition/SG_45762402/Dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python
# coding: utf-8

# In[3]:


#!pip install lmdb


# In[4]:


from io import BytesIO

import lmdb
from PIL import Image
from torch.utils.data import Dataset




'''
Create lmdb Dataset for training:

path:Path of image data

'''
class MultiResolutionDataset(Dataset):
def __init__(self, path, transform, resolution=8):
self.env = lmdb.open(
path,
max_readers=32,
readonly=True,
lock=False,
readahead=False,
meminit=False,
)

if not self.env:
raise IOError('Cannot open lmdb dataset', path)

with self.env.begin(write=False) as txn:
self.length = int(txn.get('length'.encode('utf-8')).decode('utf-8'))

self.resolution = resolution
self.transform = transform

def __len__(self):
return self.length

def __getitem__(self, index):
with self.env.begin(write=False) as txn:
key = f'{self.resolution}-{str(index).zfill(5)}'.encode('utf-8')
img_bytes = txn.get(key)

buffer = BytesIO(img_bytes)
img = Image.open(buffer)
img = self.transform(img)

return img






Binary file added recognition/SG_45762402/Images/ADAIN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added recognition/SG_45762402/Images/Structure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added recognition/SG_45762402/Images/size_128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added recognition/SG_45762402/Images/size_256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added recognition/SG_45762402/Images/size_64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added recognition/SG_45762402/Images/size_8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading