代码拉取完成,页面将自动刷新
# train.py
#!/usr/bin/env python3
""" train network using pytorch
Junde Wu
"""
import os
import sys
import argparse
from datetime import datetime
from collections import OrderedDict
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.metrics import roc_auc_score, accuracy_score, confusion_matrix
import torchvision
import torchvision.transforms as transforms
from skimage import io
from sklearn.model_selection import KFold
from torch.utils.data import DataLoader, DistributedSampler, ConcatDataset
#from dataset import *
from torch.autograd import Variable
from PIL import Image
# from tensorboardX import SummaryWriter
#from models.discriminatorlayer import discriminator
from dataset import *
from conf import settings
import time
import cfg
from tqdm import tqdm
from torch.utils.data import DataLoader, random_split
from utils import *
import function
'''定义了一些图像预处理的操作,用于在训练和测试过程中对图像进行变换'''
transform_train = transforms.Compose([
transforms.Resize((args.image_size,args.image_size)),
transforms.ToTensor(),
])
transform_train_seg = transforms.Compose([
transforms.Resize((args.out_size,args.out_size)),
transforms.ToTensor(),
])
random_dataset = RandomDataset('DualModal2019/RGB/Training')
skip_iterations = args.skip
kf = KFold(n_splits=5, shuffle=True, random_state=42)
for fold, (train_index, test_index) in enumerate(kf.split(random_dataset)):
if fold < skip_iterations:
continue
print('开始第' + str(fold) + '次交叉验证')
train_subset = torch.utils.data.Subset(random_dataset, [train_index[0]])
test_subset = torch.utils.data.Subset(random_dataset, test_index)
train_subset = list(train_subset)
test_temp = list(test_subset)
test_list = [f"{i.split('.png')[0]}-{j}.png" for i in test_temp for j in range(29, 30)]
train_list = [f"{i.split('.png')[0]}-{j}.png" for i in train_subset for j in range(29, 30)]
args.dataset = '3c'
train_dataset_rgb = DualModalMultNfoldRGB(args, data_list=train_list, transform=transform_train, transform_msk=transform_train_seg)
train_dataset_3c = DualModalMultNfold3C(args, data_list=train_list, transform=transform_train, transform_msk=transform_train_seg)
net = get_network(args, args.net, use_gpu=args.gpu, gpu_device=device, distribution=args.distributed)
for ind, pack in enumerate(train_dataset_rgb):
imgsw = pack['image'].to(dtype=torch.float32, device=device)
imgsw_2 = train_dataset_3c[ind]['image'].to(dtype=torch.float32, device=device).unsqueeze(0)
print(imgsw_2.shape)
name = pack['image_meta_dict']['filename_or_obj']
print(name)
buoy = 0
mask_type = torch.float32
ind += 1
imgsw = imgsw.to(dtype=mask_type, device=device).unsqueeze(0)
with torch.no_grad():
print(imgsw.shape)
a = net.image_encoder(imgsw, imgsw_2)
print(a.shape)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。