简体   繁体   English

我该如何解决这个项目的 shpe 和重新构建 CNN?

[英]How can I solve the shpe and reconstract CNN for this project?

when I train this network on medical images data -train -benign -normal -cancer -test -benign -normal -cancer -valid -benign -normal -cancer I get an error when I do training当我在医学图像数据上训练这个网络时 -train -benign -normal -cancer -test -benign -normal -cancer -valid -benign -normal -cancer 训练时出现错误

this is data loading.这是数据加载。 import os import torch from torchvision import datasets, transforms import os import torch from torchvision 导入数据集,转换

### TODO: Write data loaders for training, validation, and test sets
## Specify appropriate transforms, and batch_sizes
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

# number of subprocesses to use for data loading
num_workers = 0
# how many samples per batch to load
batch_size = 32

data_transform_train = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])
data_transform_test = transforms.Compose([
    transforms.Resize(234),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])

data_dir = '/content/drive/MyDrive/COVID-19  Database/COVID'
train_dir = os.path.join(data_dir, 'train')
valid_dir = os.path.join(data_dir, 'valid')
test_dir = os.path.join(data_dir, 'test')

train_data = datasets.ImageFolder(train_dir, transform=data_transform_train)
valid_data = datasets.ImageFolder(valid_dir, transform=data_transform_test)
test_data = datasets.ImageFolder(test_dir, transform=data_transform_test)

train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, num_workers=num_workers, shuffle=True)
valid_loader = torch.utils.data.DataLoader(valid_data, batch_size=batch_size, num_workers=num_workers, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, num_workers=num_workers, shuffle=True)

loaders_scratch = {
    'train' : train_loader,
    'valid' : valid_loader,
    'test'  : test_loader
}

make a model here from scratch从头开始制作 model

import torch.nn as nn
import torch.nn.functional as F

# define the CNN architecture
class Net(nn.Module):
    ### TODO: choose an architecture, and complete the class
    def __init__(self):
        super(Net, self).__init__()
        ## Define layers of a CNN
        self.conv1 = nn.Conv2d(1, 128, 3) #(224-3)/1+1= 222
        self.conv2 = nn.Conv2d(128, 64, 3) #110 after pooling with (2,2) ==>(110-3)/1+1=108
        self.conv3 = nn.Conv2d(64, 64, 3) # 54 after pooling with (2,2) ==> 110/2=54 ==>(54-3)/1+1=52
        self.conv4 = nn.Conv2d(64, 32, 3) # 26 after pooling with (2,2) ==> 52/2=26  ==>(26-3)/1+1=24
        self.conv5 = nn.Conv2d(32, 16, 3) # 12 after pooling with (2,2) ==> 24/2=12 ==> (12-3)/1+1=10
        self.conv6 = nn.Conv2d(16, 8, 3) # 5 after pooling with (2,2) ==> 10/2=2
        self.pool = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(8 * 5 * 5, 160) #8 is a out_channel(number of filter) of last conv layer and 5 is the output of last conv layer after pooling(200 input to fc1)
        self.fc2 = nn.Linear(160, 3) #166 is the output of the fc1 as input to fc2 and 133 output classes
        self.dropout25 = nn.Dropout(p=0.5) # 50% dropout of nodes
        self.softmax = nn.Softmax(dim = 1)
        
    
    def forward(self, x):
        ## Define forward behavior
        x = F.relu(self.conv1(x))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.pool(F.relu(self.conv3(x)))
        x = self.pool(F.relu(self.conv4(x)))
        x = self.pool(F.relu(self.conv5(x)))
        x = self.pool(F.relu(self.conv6(x)))
        x = x.view(x.size(0), -1)
        x = F.relu(self.fc1(x))
        x = self.dropout25(x)
        x = self.fc2(x)
        x = self.softmax(x)
        
        return x

#-#-# You so NOT have to modify the code below this line. #-#-#

# instantiate the CNN
model_scratch = Net()

use_cuda = torch.cuda.is_available()

# move tensors to GPU if CUDA is available
if use_cuda:
    model_scratch.cuda()
print(model_scratch)

here I define loss and optimizer在这里我定义损失和优化器

import torch.optim as optim

### TODO: select loss function
criterion_scratch = nn.CrossEntropyLoss()

### TODO: select optimizer
optimizer_scratch = optim.Adam(model_scratch.parameters(), lr = 0.001)

make a training and the error i appear here进行培训,我出现在这里的错误

import numpy as np 
def train(n_epochs, loaders, model, optimizer, criterion,use_cuda,save_path):
  """returns trained model"""
  # initialize tracker for maxi validation loss
  valid_loss_min = np.Inf 
    
    for epoch in range(1, n_epochs+1):
        # initialize variables to monitor training and validation loss
        train_loss = 0.0
        valid_loss = 0.0
        
        ###################
        # train the model #
        ###################
        model.train()
        for batch_idx, (data, target) in enumerate(loaders['train']):
           # move to GPU
            if use_cuda:
                data, target = data.cuda(), target.cuda()
            ## find the loss and update the model parameters accordingly
            ## record the average training loss, using something like
            ## train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - train_loss))
            
            optimizer.zero_grad()
            output = model(data)
            loss = criterion(output,target)
            loss.backward()
            optimizer.step()            
            train_loss += loss.item()*data.size(0)
        ######################    
        # validate the model #
        ######################
        model.eval()
        for batch_idx, (data, target) in enumerate(loaders['valid']):
            # move to GPU
            if use_cuda:
                data, target = data.cuda(), target.cuda()
            ## update the average validation loss
            
            output = model(data)
            loss = criterion(output,target)
            
            valid_loss += loss.item()*data.size(0)
        
        train_loss = train_loss/len(loaders['train'].dataset)
        valid_loss = valid_loss/len(loaders['valid'].dataset)
        
        # print training/validation statistics 
        print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
            epoch, 
            train_loss,
            valid_loss
            ))
        
        ## TODO: save the model if validation loss has decreased
        if valid_loss <= valid_loss_min:
            print('Validation loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(
            valid_loss_min,
            valid_loss))
            torch.save(model.state_dict(),save_path)
            valid_loss_min = valid_loss
            # return trained model
    return model


# train the model
model_scratch = train(15, loaders_scratch, model_scratch, optimizer_scratch, 
                      criterion_scratch, use_cuda, 'model_scratch.pt')

# load the model that got the best validation accuracy
model_scratch.load_state_dict(torch.load('model_scratch.pt'))

and this is an error这是一个错误

RuntimeError                              Traceback (most recent call last)
<ipython-input-4-63f181ccccc5> in <module>()
     66 # train the model
     67 model_scratch = train(15, loaders_scratch, model_scratch, optimizer_scratch, 
---> 68                       criterion_scratch, use_cuda, 'model_scratch.pt')
     69 
     70 # load the model that got the best validation accuracy

5 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
    394                             _pair(0), self.dilation, self.groups)
    395         return F.conv2d(input, weight, bias, self.stride,
--> 396                         self.padding, self.dilation, self.groups)
    397 
    398     def forward(self, input: Tensor) -> Tensor:

RuntimeError: Given groups=1, weight of size [128, 1, 3, 3], expected input[32, 3, 224, 224] to have 1 channels, but got 3 channels instead

its because you have a model definition which have 1 channel ...and your datasets class have images of 3 channels这是因为您有一个 model 定义,它有1 channel ......并且您的datasets class 有3 channels的图像
So in your model should be written as所以在你的 model 应该写成

import torch.nn as nn
import torch.nn.functional as F


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        ## Define layers of a CNN
        self.conv1 = nn.Conv2d(3, 128, 3) #(224-3)/1+1= 222
        self.conv2 = nn.Conv2d(128, 64, 3) #110 after pooling with (2,2) ==>(110-3)/1+1=108
        self.conv3 = nn.Conv2d(64, 64, 3)
        .
        .
        .
   

in short make self.conv1 = nn.Conv2d(1, 128, 3) to this self.conv1 = nn.Conv2d(3, 128, 3) #(224-3)/1+1= 222简而言之,将self.conv1 = nn.Conv2d(1, 128, 3)变成这个self.conv1 = nn.Conv2d(3, 128, 3) #(224-3)/1+1= 222

EDIT: Until you do this (below code),Your images will still be in 3 channel编辑:直到你这样做(下面的代码),你的图像仍将在 3 通道

data_transform = transforms.Compose([transforms.Grayscale(num_output_channels=1),
                                     transforms.ToTensor()])

dataset = ImageFolder(root, transform=data_transform)

Hence the above code is necessary to make single channel input因此,上面的代码对于进行single channel输入是必要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用 Keras 运行 CNN model 时如何解决此错误? - How can I solve this error during running CNN model with Keras? 如何解决 tensorflow CNN 中“:形状不兼容”的错误? - How can I solve error of ": Incompatible shapes" in tensorflow CNN? 如何解决 CNN 中的输入形状问题? - How can I solve the input shape issue in CNN? 如何使用 CNN 模型 Keras 解决错误? - How can solve Error with CNN Model Keras? 我在使用 cnn 对超过 10000 个 class 进行分类时遇到问题。 我该如何解决? - i have a problem while using cnn to classify more then 10000 class. how can i solve it? 我未能训练 CNN + LSTM model。 我怎么解决这个问题? 数据集有问题吗? 还是 model? (Python 3.8x) - I failed to train CNN + LSTM model. How can I solve this problem? Is it have problem in dataset? or model? (Python 3.8x) 如何解决项目“不是有效的 python 路径”中的路径错误 - How can I solve this path error in project 'not a valid python path' 我如何解决在heroku上托管的Django项目中的迁移问题? - How can i solve a migration issue in django project hosted on heroku? 我该如何解决这个 pipenv 安装错误 - vscode 上的 django 项目 - how can i solve this pipenv install error - django project on vscode 如何解决关于 CNN 训练的 Value Error - How to solve Value Error with regards to CNN training
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM