pytorch之inception_v3的实现

pytorch之inception_v3的实现

from __future__ import print_function 
from __future__ import division
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import os
import copy
import argparse
print("PyTorch Version: ",torch.__version__)
print("Torchvision Version: ",torchvision.__version__)


# Top level data directory. Here we assume the format of the directory conforms 
#   to the ImageFolder structure

数据集路径,路径下的数据集分为训练集和测试集,也就是train 以及val,train下分为两类数据1,2,val集同理

data_dir = "/home/dell/Desktop/data/切割图像"
# Models to choose from [resnet, alexnet, vgg, squeezenet, densenet, inception]
model_name = "inception" 
# Number of classes in the dataset
num_classes = 2#两类数据1,2

# Batch size for training (change depending on how much memory you have)
batch_size = 32#batchsize尽量选取合适,否则训练时会内存溢出

# Number of epochs to train for 
num_epochs = 1000

# Flag for feature extracting. When False, we finetune the whole model, 
#   when True we only update the reshaped layer params
feature_extract = True

# 参数设置,使得我们能够手动输入命令行参数,就是让风格变得和Linux命令行差不多
parser = argparse.ArgumentParser(description='PyTorch inception')
parser.add_argument('--outf', default='/home/dell/Desktop/dj/inception/', help='folder to output images and model checkpoints') #输出结果保存路径
parser.add_argument('--net', default='/home/dell/Desktop/dj/inception/inception.pth', help="path to net (to continue training)")  #恢复训练时的模型路径
args = parser.parse_args()

训练函数

def train_model(model, dataloaders, criterion, optimizer, num_epochs=25,is_inception=False):

    since = time.time()

    val_acc_history = []
    
    best_model_wts = copy.deepcopy(model.state_dict())
    best_acc = 0.0
    print("Start Training, InceptionV3!") 
    with open("acc.txt", "w") as f1:
        with open("log.txt", "w")as f2:
            for epoch in range(num_epochs):
                print('Epoch {}/{}'.format(epoch+1, num_epochs))
                print('*' * 10)
                # Each epoch has a training and validation phase
                for phase in ['train', 'val']:
                    if phase == 'train':
                        model.train()  # Set model to training mode
   
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值