昇思25天学习打卡营第25 天|Resnet for transition learning

first some params.

batch_size = 18
image_size = 224
num_epochs = 5
lr = 0.001
momentum = 0.9
workers = 4    #parallel thread

we try to classify dogs and wolves.

def create_dataset_canidae(dataset_path, usage):
    data_set = ds.ImageFolderDataset(dataset_path, 
                                    num_parallel_workers = workers
                                    shuffle = True)
#augmentation    
    mean = [0.485*255, 0.456*255, 0.496*255]
    std = [0.229*255, 0.224*255, 0.225*255]
    scale = 32 
    if usage == 'train':
        trains = [
            vision.RandomCropDecodeResize(size=image_size, scale = (0.08,1.0), ratio = (0.75, 1.333)),
            vision.RandomHorizontalFilp(prob = 0.5),
            vision.Normalize(mean = mean, std = std),
            vision.HWC2CHW()
]
    else :
        trans= [
            vision.Decode(),
            vision.Resize(image_size + scale),
            vision.CenterCrop(image_size),
            vision.Normalize(mean = mean, std = std),
            vision.HWC2CHW()
]
    data_set = data_set.map(
        operations = trans,
        input_columns = 'image',
        num_parallel_workers = workers
)
    #batch op
    data_set = data_set.batch(batch_size)
    return data_set
dataset_train = create_dataset_canidae(data_path_train, 'train')
step_size_train = dataset_train.get_dataset_size()
dataset_val= create_dataest_canidae(data_path_val, 'val')
step_size_val = dataset_val.get_dataset_size()

we visualize the data.

class_name = {0:'dogs', 1:'wolves'}
plt.figure(figsize = (5,5))
for i in range(4):
    data_image = images[i].asnumpy()
    data_label = labels[i]
    data_image = np.transpose(data_image, (1,2,0))
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    data_image = std*data_image + mean
    data_image = np.clip(data_image, 0, 1)
    plt.subplot(2,2,i+1)
    plt.imshow(data_image)
    plt.title(class_name[int(labels[i].asnumpy())])
    plt.axis('off')
plt.show()

model.

weight_init = Normal(mean = 0, sigma = 0.02)
gamma_init = Normal(mean = 1, sigma = 0.02)

 

class ResidualBlockBase(nn.Cell):
    expansion : int = 1 #the number of  last conv kernel == that of the first
    def __init__(self, in_channel: int, out_channel: int,
                stride: int = 1, norm: Optional[nn.Cell] = None,
                down_sample: Optional[nn.Cell] = None) ->None:
        super(ResidualBlockBase,self).__init__()
        if not norm:
            self.norm = nn.BatchNorm2d(out_channel)
        else:
            self.norm = norm
        self.conv1 = nn.Conv2d(in_channel, out_channel, kernel_size = 3, stride = stride, weight_init = weight_init)
        self.conv2 = nn.Conv2d(in_channel, out_channel, kernel_size =3, weight_init  =weight_init)
        self.relu = nn.ReLU()
        self.down_sample = down_sample
    def construct(self, x):
        identity =x
        out = self.conv1(x) #3x3
        out = self.norm1(out)
        out = self.relu(out)
        out = self.conv2(out)  #3x3
        out = self.norm(out)
        if self.down_sample is not None:
            identity = self.down_sample(x)
        out += identity #shortcut added
        out = self.relu(x)
        return out
class ResidualBlock(nn.Cell):
    expansion = 4 # means the number of last conv is 4 x that of the first
    def __init__(self, in_channel:int, out_channel:int, stride: int = 1, down_sample: Optional[nn.Cell] = None) -> None:
        super(ResidualBlock, self).__init__()
        self.conv1 = nn.Conv2d(in_channel, out_channel, kernel_size =1, weight_init = weight_init)
        self.norm1 = nn.BatchNorm2d(out_channel)
        self.conv2 = nn.Conv2d(out_channel, out_chanmel, kernel_size= 3, stride = stride, weight_init = weight_init)
        self.norm2 = nn.BatchNorm2d(out_channel)
        self.conv3 = nn.Conv2d(out_channel, out_channel * self.expansion,
                                    kernel_size = 1, weight_init = weight_init )
        self.norm3 = nn.BatchNorm2d(out_channel*self.expansion)
        self.relu = nn.ReLU()
        self.down_sample = down_sample
    def construct(self, x):
        identity = x
        out = self.conv1(x)
        out = self.norm1(out)
        out = self.relu(out)
        out = self.conv2(out)
        out = self.norm2(out)
        out = self.relu(out)
        out = self.conv3(out)
        out = self.norm3(out)
        if self.down_sample is not None:
            identity = self.down_sample(x)
        out += identity    
        out = self.relu(out)
        return out

def make_layer(last_out_channel, block: Type[Unionp[ResidualBlockBase, ResidualBlock]], channel:int, block_nums:int, stride: int  = 1):
    down_sample = None
    if stride != 1 or last_out_channel != channel * block.expansion:
        down_sample  = nn.SequentialCell([
            nn.Conv2d(last_out_channel, channel*block.expansion, kernel_size = 1, stride = stride, weight_init = weight_init),
            nn.BatchNorm2d(channel*block.expansion, gamma_init = gamma_init)
])
    layers = []
    layers.append(block(last_out_channel,channel, stride= stride, down_sample = down_sample))
    in_channel = channel * block.expansion
    for _ in range(1, block_nums):
        layers.append(block(in_channel, channel))
    return nn.SequentialCell(layers)


construct the resnet50.

class ResNet(nn.Cell):
    def __init__(self, block:Type[Union[ResidualBlockBase , ResidualBlock]], 
                layer_nums: List[int], num_classes: int, input_channel:int) ->None:
        super(ResNet, self).__init__()
        self.relu = nn.ReLU()    
        self.conv1 = nn.Conv2d(3, 64, kernel_size = 7, stride = 2, weight_init = weight_init)
        self.norm = nn.BatchNorm2d(64)
        self.max_pool = nn.MaxPool2d(kernel_size = 3, stride = 2, pad_mode = 'same')
        self.layer1 = make_layer(64, block, 64, layer_nums[0])
        self.layer2 = make_layer(64* block.expansion, block, 128, layer_nums[1], stride = 2)
        self.layer3 = make_layer(128 * block.expansion, block, 256, layer_nums[2], stride = 2)
        self.layer4 = make_layer(256* block.expansion, block, 512, layer_nums[3], stride = 2)
        self.avg_pool = nn.AvgPool2d()
        self.flatten = nn.Flatten()
        self.fc = nn.Dense(in_channels = input_channel, out_channels = num_classes)
    def construct(self, x):
        x = self.conv1(x)    
        x = self.norm(x)
        x= self.relu(x)    
        x = self.max_pool(x)
        x= self.layer1(x)
        x = self.layer2(x)    
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avg_pool (x)
        x = self.flatten(x)
        x = self.fc(x)
        return x
def _resnet(model_url:str, block:Type[Union[ResidualBlockBase, ResidualBlock]],
                layers:List[int] , num_classes:int, pretrained:bool, pretrained_ckpt: str, input_channel:int):
        model = ResNet(block, layers, num_classes, input_channel)
        if pretrained:
            download(url = model_url, path =pretrained_ckpt, replace = True)
            param_dict = load_checkpoint(pretrained_ckpt)
            load_param_into_net(model, param_dict)
        return model
def resnet50(num_classe: int  = 1000, pretrained: bool = False):
        resnet50_url = "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/models/application/resnet50_224_new.ckpt"
        resnet50_ckpt = './LoadPretrainedModel/resent50_224_new.ckpt'
        return _resnet(resnet50_url, ResidualBlock, [3,4,6,3], num_classes, pretrained, resnet50_ckpt, 2048)
##we freeze the backbone and finetune
net_work = resnet50(pretrained= True)
in_channels= net_work.fc.in_channels # the input of dense layer
head = nn.Dense(in_channels, 2) #2:dogs and wolves
net_work.fc  = head # reset
avg_pool = nn.AvgPool2d(kernel_size = 7)
net_work.avg_pool  =avg_pool#reset
for param in net_work.get_parameters():
        if param.name not in ['fc.weight', 'fc.bias']:
            param.requires_grad = False
opt = nn.Momentum(params = net_work.work.trainable_params(), learning_rate = lr, momentum = 0.5)
loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse  = True , reduction = 'mean')
def forward_fn (inputs, targets):
        logits = net_work(inputs)
        loss = loss_fn(logits, targets)
        return loss
grad_fn = ms.value_and_grad(forward_fn, None, opt.parameters)
def train_step(inputs, targets):
    loss, grads = grad_fn (inputs, targets)
    opt(grads)
    return loss
model1 = train.Model(net_work, loss_fn, opt, metrics = {'Accuarcy':train_Accuracy()})


start to train the model. Time has been dramatically saved since it is based on rhe pretrained model.

dataset_train = create_dataset_canidae(data_path_train, 'train')
step_size_train = dataset_train.get_dataset_size()
dataset_val = create_dataset_canidae(data_path_val, 'val')
step_size_val = dataset_val.get_dataset_size()

num_epochs = 5
data_loader_train = dataset_train.create_tuple_iterator(num_epochs= num_epochs)
data_loader_val  = dataset_val.create_tuple_iterator(num_epochs = num_epochs)
best_ckpt_dir = './BestCheckpoint'
best_ckpt_path = './BestCheckpoint/resnet50-best-freezing-param.ckpt'
best_acc =0 
for epoch in range(num_epochs):
    losses = []
    net_work.set_train()
    epoch_start = time.time()
    for i , (images, labels) in enumerate(data_loader_train):
        labels = labels.astype(float32)
        loss = train_step(images, labels)
        losses.append(loss)

acc = model1.eval(dataset_val)['Accuracy']
epoch_end = time.time()
epochs_seconds = (epoch_end - epoch_start) * 1000
step_seconds = epoch_seconds / step_size_train
print('-' * 20)
print('Epoch:[%3d/%3d], Average Train Loss:[%5.3f], Accuarcy:[%5.3f]' %(epoch + 1, num_epochs, sum(losses) / len(losses), acc))
print('epoch time: %5.3f ms, per step time : %5.3f ms' %(epoch_seconds, step_seconds))
if acc> best_acc:    
    best_acc =acc
    if not os.path.exists(best_ckpt_dir):
        os.mkdir(best_ckpt_dir)
    ms.save_checkpoint(net_work, best_ckpt_path)
print('=' * 80)
print(f'End of validation the best Accuracy is :{best_acc: 5.3f},'
        f'save the best ckpt file in {best_ckpt_path}', flush = True)

we visualize the prediction to make blue color represents correction.

def visualize_model(best_ckpt_path, val_ds):
    net = resnet50()
    in_channels = net.fc.in_channels
    head = nn.Dense(in_channels, 2)
    net.fc = head
    avg_pool = nn.AvgPool2d(kernel_size = 7)
    net.fc = head
    avg_pool = nn.AvgPool2d(kernel_size = 7)
    net.avg_pool = avg_pool
    param_dict = ms.load_checkpoint(best_ckpt_path)
    ms.load_param_into_net(net, param_dict)
    mdoel = train.Model(net)
    data = next(val_ds.create_dict_iterator())
    images = data['image'].asnumpy()
    labels = data['label'].asnumpy()
    class_name = {0:'dogs', 1:"wolves"}
    output = model.predict(ms.Tensor(data['image']))
    pred = np.argmax(output.asnumpy(), axis = 1)
    plt.figure(figsize = (5,5))
    for i in range(4):
        plt.subplot(2,2,i+1)
        color = 'blue' if pred[i] == labels[i] else 'red'
        picture_show = np.transpose(images[i], (1,2,0))
        mean = np.array([0.485,0.456,0.406])
        std = np.array([0.229, 0.224, 0.225])
        picture_show = std*picture_show + mean
        picture_show = np.clip(picture_show, 0,1)
        plt.imshow(picture_show)
        plt.axis('off')
    plt.show()


visualize_model(best_ckpt_path, dataset_val)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以通过以下步骤安装Deep Learning Toolbox Model for ResNet-50 Network和Deep Learning Toolbox Model for Inception-v3 Network: 1. 打开MATLAB软件并进入主界面。 2. 点击“Add-Ons”选项卡,然后选择“Get Add-Ons”。 3. 在搜索栏中输入“Deep Learning Toolbox Model for ResNet-50 Network”或“Deep Learning Toolbox Model for Inception-v3 Network”。 4. 点击“Install”按钮,等待安装完成。 5. 安装完成后,您可以在MATLAB中使用这些模型进行深度学习任务。 希望这个回答对您有所帮助! ### 回答2: 安装MATLAB中的Deep Learning Toolbox Model for ResNet-50 Network和Deep Learning Toolbox Model for Inception-v3 Network是非常简单的。 首先,确保你已经安装了MATLAB软件,并具有有效的许可证。 然后,打开MATLAB软件,点击工具栏上的“Add-Ons”按钮,它位于主界面的右上角。 在弹出的界面中,点击左侧的“Get Add-Ons”选项卡。 在搜索框中,输入"Deep Learning Toolbox Model for ResNet-50 Network"并点击搜索按钮。 在搜索结果中找到对应的模型,点击右侧的"Add From GitHub"按钮。 稍等几秒钟,MATLAB会自动下载并安装所需的模型。 重复以上步骤,以同样的方式安装“Deep Learning Toolbox Model for Inception-v3 Network”。 安装完成后,你可以在MATLAB的命令窗口中使用这些模型。例如,你可以通过以下命令加载已安装的ResNet-50模型: ```matlab net = resnet50; ``` 或者加载已安装的Inception-v3模型: ```matlab net = inceptionv3; ``` 这样就可以使用这些预训练的深度学习模型进行各种任务,如图像分类、目标检测等。记得在使用这些模型之前,先要明确自己的目标并适当调整模型以适应任务要求。 ### 回答3: 要安装Matlab中的Deep Learning Toolbox Model for ResNet-50 Network和Deep Learning Toolbox Model for Inception-v3 Network,您可以按照以下步骤进行操作: 1. 首先,确保您已经安装了Deep Learning Toolbox和Matlab软件。这些工具是使用这些深度学习模型的前提条件。 2. 打开Matlab软件,在主界面的"HOME"选项卡下,选择"Get Add-Ons"。这将打开Matlab Add-On Explorer。 3. 在搜索框中输入"Deep Learning Toolbox Model for ResNet-50 Network",然后点击搜索按钮。 4. 在搜索结果中找到"Deep Learning Toolbox Model for ResNet-50 Network",然后点击"Add"按钮进行安装。等待安装过程完成。 5. 重复步骤3和步骤4,这一次搜索"Deep Learning Toolbox Model for Inception-v3 Network",然后点击"Add"按钮进行安装。同样,等待安装过程完成。 6. 安装完成后,您可以在Matlab的工具箱中找到这些深度学习模型。打开"APPS"选项卡,在"Deep Learning Toolbox"部分下,您会看到"ResNet-50"和"Inception-v3"模型。 7. 单击所需的模型,Matlab将加载相应的模型并打开一个图形用户界面。 8. 在这个界面上,您可以使用这些预训练模型进行不同的深度学习任务,如图像分类、特征提取等。 请注意,这些模型的安装过程可能会因您的Matlab版本和操作系统而有所不同。确保您的Matlab版本兼容并满足相应的系统要求。此外,确保您的计算机具有足够的计算资源来运行这些深度学习模型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值