-
Notifications
You must be signed in to change notification settings - Fork 410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Classification code with modification of train.py and datasets/brains18.py #79
Comments
Thank you for your reply and for trying the codes. |
I updated all the codes and four files were modified including 1. datasets/brains18.py 2. models/resnet.py 3. model.py and 4. train.py. |
''' import math class BrainS18Dataset(Dataset):
|
import torch all = [ def conv3x3x3(in_planes, out_planes, stride=1, dilation=1): def downsample_basic_block(x, planes, stride, no_cuda=False):
class BasicBlock(nn.Module):
class Bottleneck(nn.Module):
class ResNet(nn.Module):
def resnet10(**kwargs): def resnet18(**kwargs): def resnet34(**kwargs): def resnet50(**kwargs): def resnet101(**kwargs): def resnet152(**kwargs): def resnet200(**kwargs): |
import torch def generate_model(opt):
|
''' from setting import parse_opts def train(data_loader, model, optimizer, scheduler, total_epochs, save_interval, save_folder, sets):
if name == 'main':
|
thnak you for your responding File "train.py", line 69, in train out_masks = tensor([[0.0005, 0.0046]], device='cuda:0', grad_fn=) |
This err rises if the input format of LOSS and label mismatch. |
The new layer's name should be "conv_seg" in resnet.py (for not bothering to change the setting.py file). |
thank you very much. |
thx for your work but how about test? |
Thank you for your work. What is your version of Pytorch? Torch0.4.1 reported an error AttributeError: module 'torch. nn' has no attribute 'Flatten'。 @loopnownow |
how about your result?My result is bad and makes me confused... |
Hi
My result was too bad ...
So I did not use this model but I wrote it's result in my thesis.
…On Thu, Jan 18, 2024, 2:42 PM Shaozihan ***@***.***> wrote:
thank you very much. The error fixed.
how about your result?My result is bad and makes me confused...
—
Reply to this email directly, view it on GitHub
<#79 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQR6RB2O47DCYTWK7DW3W2TYPD7Q3AVCNFSM6AAAAAASSS6E7CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJYGI3TMOJWGE>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
|
Hi |
The result are not reproducible please provide some insights on the f1 score |
I'd like to share the code of classification with modification of train.py and datasets/brains18.py
The code is based on JasperHG90's commented at #58
To deal with the issue of Classification, there are three main problems should be resolved.
First, the format of the class labels.
Second, how to add the labels of the class into the codes.
Third, how to define an appropriate LOSS.
For the fist one, I simply add the class labels in the training data lists under data/train after the address of the masks
Such as:
MRBrainS18/images/75.nii.gz MRBrainS18/labels/75.nii.gz 0
MRBrainS18/images/14.nii.gz MRBrainS18/labels/14.nii.gz 0
MRBrainS18/images/148.nii.gz MRBrainS18/labels/148.nii.gz 0
MRBrainS18/images/4.nii.gz MRBrainS18/labels/4.nii.gz 0
MRBrainS18/images/5.nii.gz MRBrainS18/labels/5.nii.gz 0
MRBrainS18/images/7.nii.gz MRBrainS18/labels/7.nii.gz 1
MRBrainS18/images/71.nii.gz MRBrainS18/labels/71.nii.gz 1
MRBrainS18/images/72.nii.gz MRBrainS18/labels/72.nii.gz 1
MRBrainS18/images/73.nii.gz MRBrainS18/labels/73.nii.gz 1
MRBrainS18/images/74.nii.gz MRBrainS18/labels/74.nii.gz 1
For the second one, I modified the file of datasets/brains18.py. The modified code were followed by #####
The modified code were:
import torch #####
line 43, label_name = os.path.join(self.root_dir, ith_info[1])
class_array = np.zeros((1)) ######
class_array[0] = ith_info[2] #####
class_array = torch.tensor(class_array, dtype=torch.float32) ######
line 59, return img_array, mask_array, class_array #####
For the third one, I modified the file of train.py. The modified code were followed by #####
The modified code were:
from models import resnet #####
#the code below is from JasperHG90's commented at #58, any modification was followed by ##### (notification that I used model of resnet50 rather than resnet 34)
class MedicalNet(nn.Module):
line 24, loss_seg = nn.BCELoss() ##### loss_seg = nn.CrossEntropyLoss(ignore_index=-1)
line 43, volumes, label_masks, class_array = batch_data ##### volumes, label_masks = batch_data
line 47, class_array = class_array.cuda() #####
line 66, loss_value_seg = loss_seg(out_masks, class_array) ##### loss_value_seg = loss_seg(out_masks, new_label_masks)
line 67, loss = loss_value_seg
line 68, loss.requires_grad_(True) #####
line 118, model = MedicalNet(path_to_weights="pretrain/resnet_50.pth", device='cuda') #####model, parameters = generate_model(sets)
line 119, model = MedicalNet(path_to_weights="pretrain/resnet_50.pth", device='cuda') #####print (model)
line 120, model.cuda() ##### # optimizer
line 128, optimizer = torch.optim.SGD(model.parameters(), lr=1e-5,momentum=0.9, weight_decay=1e-3) #####optimizer = torch.optim.SGD(params, momentum=0.9, weight_decay=1e-3)
The text was updated successfully, but these errors were encountered: