Giter VIP home page Giter VIP logo

razvandu / duck-net Goto Github PK

View Code? Open in Web Editor NEW
87.0 87.0 24.0 1.07 MB

Using DUCK-Net for polyp image segmentation. ( Nature Scientific Reports 2023 )

License: Creative Commons Attribution 4.0 International

Python 44.85% Jupyter Notebook 55.15%
cnn cnn-for-visual-recognition cnn-keras computer-vision convolutional-neural-network convolutional-neural-networks duck-net ducknet-keras ducknet-tensorflow image-segmentation image-segmentation-tensorflow machine-learning medical-image-processing medical-image-segmentation medical-imaging polyp-segmentation polyp-segmentation-task segmentation tensorflow2 u-net

duck-net's People

Contributors

catalincraciun2002 avatar maenstru56 avatar razvandu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

duck-net's Issues

Unable to Access Test Dataset

Hi,

I hope this message finds you well. I am currently facing an issue with accessing the test dataset provided. It seems that the test dataset is no longer accessible. Could you please provide it again?

Thank you very much for your assistance.

Could not achieve the accuracy on ETIS-LaribPolypDB as metioned in the corresponding paper

I was not able to reproduce the results on this particular dataset that were provided in the research paper.

Everything was kept on default settings with parameters double checked but still could not achieve the desired results

Dice Score
Proposed: 0.9324
Achieved: 0.8485


# Importing the necessary libraries

  import tensorflow as tf
  import albumentations as albu
  import numpy as np
  import gc
  import pickle
  import matplotlib.pyplot as plt
  from keras.callbacks import CSVLogger
  from datetime import datetime
  from sklearn.model_selection import train_test_split
  from sklearn.metrics import jaccard_score, precision_score, recall_score, accuracy_score, f1_score
  from ModelArchitecture.DiceLoss import dice_metric_loss
  from ModelArchitecture import DUCK_Net
  from ImageLoader import ImageLoader2D


# Checking the number of GPUs available

print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))

# Setting the model parameters

img_size = 352
dataset_type = 'etis-laribpolypdb' # Options: kvasir/cvc-clinicdb/cvc-colondb/etis-laribpolypdb
learning_rate = 1e-4
seed_value = 58800
filters = 17 # Number of filters, the paper presents the results with 17 and 34
optimizer = tf.keras.optimizers.RMSprop(learning_rate=learning_rate)

ct = datetime.now()

model_type = "DuckNet"

progress_path = 'ProgressFull/' + dataset_type + '_progress_csv_' + model_type + '_filters_' + str(filters) +  '_' + str(ct) + '.csv'
progressfull_path = 'ProgressFull/' + dataset_type + '_progress_' + model_type + '_filters_' + str(filters) + '_' + str(ct) + '.txt'
plot_path = 'ProgressFull/' + dataset_type + '_progress_plot_' + model_type + '_filters_' + str(filters) + '_' + str(ct) + '.png'
model_path = 'ModelSaveTensorFlow/' + dataset_type + '/' + model_type + '_filters_' + str(filters) + '_' + str(ct)

EPOCHS = 600
min_loss_for_saving = 0.2


# Loading the data

X, Y = ImageLoader2D.load_data(img_size, img_size, -1, 'etis-laribpolypdb')

# Splitting the data, seed for reproducibility

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.1, shuffle= True, random_state = seed_value)
x_train, x_valid, y_train, y_valid = train_test_split(x_train, y_train, test_size=0.111, shuffle= True, random_state = seed_value)

# Defining the augmentations

aug_train = albu.Compose([
    albu.HorizontalFlip(),
    albu.VerticalFlip(),
    albu.ColorJitter(brightness=(0.6,1.6), contrast=0.2, saturation=0.1, hue=0.01, always_apply=True),
    albu.Affine(scale=(0.5,1.5), translate_percent=(-0.125,0.125), rotate=(-180,180), shear=(-22.5,22), always_apply=True),
])

def augment_images():
    x_train_out = []
    y_train_out = []

    for i in range (len(x_train)):
        ug = aug_train(image=x_train[i], mask=y_train[i])
        x_train_out.append(ug['image'])  
        y_train_out.append(ug['mask'])

    return np.array(x_train_out), np.array(y_train_out)


# Creating the model

gpus = tf.config.list_physical_devices('GPU')
if gpus:
  # Restrict TensorFlow to only allocate 40GB of memory on the GPU
  try:
    tf.config.set_logical_device_configuration(
        gpus[0],
        [tf.config.LogicalDeviceConfiguration(memory_limit=40000)])
    logical_gpus = tf.config.list_logical_devices('GPU')
    print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
  except RuntimeError as e:
    # Virtual devices must be set before GPUs have been initialized
    print(e)


model = DUCK_Net.create_model(img_height=img_size, img_width=img_size, input_chanels=3, out_classes=1, starting_filters=filters)

# Compiling the model

model.compile(optimizer=optimizer, loss=dice_metric_loss)

# Training the model

step = 0

for epoch in range(0, EPOCHS):
    
    print(f'Training, epoch {epoch}')
    print('Learning Rate: ' + str(learning_rate))

    step += 1
        
    image_augmented, mask_augmented = augment_images()
    
    csv_logger = CSVLogger(progress_path, append=True, separator=';')
    
    model.fit(x=image_augmented, y=mask_augmented, epochs=1, batch_size=4, validation_data=(x_valid, y_valid), verbose=1, callbacks=[csv_logger])
    
    prediction_valid = model.predict(x_valid, verbose=0)
    loss_valid = dice_metric_loss(y_valid, prediction_valid)
    
    loss_valid = loss_valid.numpy()
    print("Loss Validation: " + str(loss_valid))
        
    prediction_test = model.predict(x_test, verbose=0)
    loss_test = dice_metric_loss(y_test, prediction_test)
    loss_test = loss_test.numpy()
    print("Loss Test: " + str(loss_test))
        
    with open(progressfull_path, 'a') as f:
        f.write('epoch: ' + str(epoch) + '\nval_loss: ' + str(loss_valid) + '\ntest_loss: ' + str(loss_test) + '\n\n\n')
    
    if min_loss_for_saving > loss_valid:
        min_loss_for_saving = loss_valid
        print("Saved model with val_loss: ", loss_valid)
        model.save(model_path)
        
    del image_augmented
    del mask_augmented

    gc.collect()


# Computing the metrics and saving the results

print("Loading the model")

model = tf.keras.models.load_model(model_path, custom_objects={'dice_metric_loss':dice_metric_loss})

prediction_train = model.predict(x_train, batch_size=4)
prediction_valid = model.predict(x_valid, batch_size=4)
prediction_test = model.predict(x_test, batch_size=4)

print("Predictions done")

dice_train = f1_score(np.ndarray.flatten(np.array(y_train, dtype=bool)),
                           np.ndarray.flatten(prediction_train > 0.5))
dice_test = f1_score(np.ndarray.flatten(np.array(y_test, dtype=bool)),
                          np.ndarray.flatten(prediction_test > 0.5))
dice_valid = f1_score(np.ndarray.flatten(np.array(y_valid, dtype=bool)),
                           np.ndarray.flatten(prediction_valid > 0.5))

print("Dice finished")


miou_train = jaccard_score(np.ndarray.flatten(np.array(y_train, dtype=bool)),
                           np.ndarray.flatten(prediction_train > 0.5))
miou_test = jaccard_score(np.ndarray.flatten(np.array(y_test, dtype=bool)),
                          np.ndarray.flatten(prediction_test > 0.5))
miou_valid = jaccard_score(np.ndarray.flatten(np.array(y_valid, dtype=bool)),
                           np.ndarray.flatten(prediction_valid > 0.5))

print("Miou finished")


precision_train = precision_score(np.ndarray.flatten(np.array(y_train, dtype=bool)),
                                  np.ndarray.flatten(prediction_train > 0.5))
precision_test = precision_score(np.ndarray.flatten(np.array(y_test, dtype=bool)),
                                 np.ndarray.flatten(prediction_test > 0.5))
precision_valid = precision_score(np.ndarray.flatten(np.array(y_valid, dtype=bool)),
                                  np.ndarray.flatten(prediction_valid > 0.5))

print("Precision finished")


recall_train = recall_score(np.ndarray.flatten(np.array(y_train, dtype=bool)),
                            np.ndarray.flatten(prediction_train > 0.5))
recall_test = recall_score(np.ndarray.flatten(np.array(y_test, dtype=bool)),
                           np.ndarray.flatten(prediction_test > 0.5))
recall_valid = recall_score(np.ndarray.flatten(np.array(y_valid, dtype=bool)),
                            np.ndarray.flatten(prediction_valid > 0.5))

print("Recall finished")


accuracy_train = accuracy_score(np.ndarray.flatten(np.array(y_train, dtype=bool)),
                                np.ndarray.flatten(prediction_train > 0.5))
accuracy_test = accuracy_score(np.ndarray.flatten(np.array(y_test, dtype=bool)),
                               np.ndarray.flatten(prediction_test > 0.5))
accuracy_valid = accuracy_score(np.ndarray.flatten(np.array(y_valid, dtype=bool)),
                                np.ndarray.flatten(prediction_valid > 0.5))


print("Accuracy finished")


final_file = 'results_' + model_type + '_' + str(filters) + '_' + dataset_type + '.txt'
print(final_file)

with open(final_file, 'a') as f:
    f.write(dataset_type + '\n\n')
    f.write('dice_train: ' + str(dice_train) + ' dice_valid: ' + str(dice_valid) + ' dice_test: ' + str(dice_test) + '\n\n')
    f.write('miou_train: ' + str(miou_train) + ' miou_valid: ' + str(miou_valid) + ' miou_test: ' + str(miou_test) + '\n\n')
    f.write('precision_train: ' + str(precision_train) + ' precision_valid: ' + str(precision_valid) + ' precision_test: ' + str(precision_test) + '\n\n')
    f.write('recall_train: ' + str(recall_train) + ' recall_valid: ' + str(recall_valid) + ' recall_test: ' + str(recall_test) + '\n\n')
    f.write('accuracy_train: ' + str(accuracy_train) + ' accuracy_valid: ' + str(accuracy_valid) + ' accuracy_test: ' + str(accuracy_test) + '\n\n\n\n')

print('File done')


Test/ Val Loss File: etis-laribpolypdb_progress_DuckNet_filters_17_2024-07-12 11:25:19.256742.txt

Result File: results_DuckNet_17_etis-laribpolypdb.txt

CPU: Intel(R) Xeon(R) Gold 5320 CPU @ 2.20GHz
GPU: NVIDIA A100 80GB

Pytorch implementation

Thanks for your interesting research. Do you have any plan to implement this research in Pytorch?

library dependency

I think there is a small error in the "requirements.txt" file containing the necessary libraries. The module tensorflow = 2.9.1 depends on keras<2.10, but keras=2.10 is specified in the file. Sorry if I overlooked something, I am quite new to this.

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

When I run into X, Y = ImageLoader2D.load_data(img_size, img_size, -1, 'kvasir'), I encountered the following error:

Resizing training images and masks: 1000
0it [00:00, ?it/s]
Traceback (most recent call last):
File "/home/gufei/rfi_code/DUCK-Net/train.py", line 41, in
X, Y = ImageLoader2D.load_data(img_size, img_size, -1, 'kvasir')
File "/home/gufei/rfi_code/DUCK-Net/ImageLoader/ImageLoader2D.py", line 64, in load_data
if mask_[i, j] >= 127:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Widescope

Widescope simulates a kernel size of 15 × 15,you use dilation_rate=1,2,3,How does it simulate 15 * 15 ? Isn't it 13 * 13

loss is always 1

Hi, when I run the notebook for training, I find the loss is always 1, is this normal?
Snipaste_2023-11-07_17-33-23
By the way, I couldn't install tensorflow2.10.0 for some reason with the server, so I used tensorflow2.3.0 instead and also made the code running.

Good jobs here!

It is really amazing that the U-net structure without using any attention mechanism that can improve the result so much! I can see that this will be a new trend that using multi-viewed field blocks as the connections between features since cfpnet-m.

Model training

In the model training in the 16th line

model.fit(x=image_augmented, y=mask_augmented, epochs=1, batch_size=4, validation_data=(x_valid, y_valid), verbose=1, callbacks=[csv_logger])

NotFoundError: ProgressFull/kvasir_progress_csv_DuckNet_filters_17_2023-12-01 20:02:22.394228.csv; No such file or directory.
Error is coming.
I am using KVASIR dataset for training.
Please help
image

result

In the CVC Colondb dataset, the obtained dice_ Test: 0.8902772313306522, why does it differ so much from the results in the paper

A question about the details of network training

Thank you for your wonderful work!
I was very impressed with your article, the network you proposed is concise and powerful.
I have a question about the details of network training: Could you please tell me that during the training phase, did you train on these datasets separately, or combined them into one large training set?
Your response would be highly appreciated, and thank you again for your great work!

Qualitative results

Dear Sir, Thank you for your code and results. After following all your instruction I could run the code, but I haven't understood with the qualitative results, in which folder it is saved? How do we get it.

Hi, I have a problem I can't solve

屏幕截图 2024-01-22 134306
It shows that there is a utf-8 encoding problem, but I do not know how to solve it.I haven't changed the program, Sir. Do you have a good solution?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.