这是我的火车。 我对网络使用骰子丢失,输出的大小为[4,2,224,224],其中4表示批处理大小,2表示通道,224表示h和w。output_c1大小为[4,224,224],标签大小为[4,224,224 ]。
function sort_cart_specific_product_at_bottom( $cart ) {
// Product id's to to display at tbe bottom of the product list
$product_ids_last = array( 30, 815 );
// Set empty arrays
$products_in_cart = array();
$products_last = array();
$cart_contents = array();
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get product id
$product_id = $cart_item['data']->get_id();
// In_array — checks if a value exists in an array
if ( in_array( $product_id, $product_ids_last) ) {
// Add to products last array
$products_last[ $cart_item_key ] = $product_id;
} else {
// Add to products in cart array
$products_in_cart[ $cart_item_key ] = $product_id;
}
}
// Merges the elements together so that the values of one are appended to the end of the previous one.
$products_in_cart = array_merge( $products_in_cart, $products_last );
// Assign sorted items to cart
foreach ( $products_in_cart as $cart_item_key => $product_id ) {
$cart_contents[ $cart_item_key ] = $cart->cart_contents[ $cart_item_key ];
}
// Cart contents
$cart->cart_contents = $cart_contents;
}
add_action( 'woocommerce_cart_loaded_from_session', 'sort_cart_specific_product_at_bottom', 10, 1 );
我的Dice_loss.py是:
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from model import U_net
import visdom
from dataset import driveDateset
from torch import optim
from Dice_loss import DiceLoss
from Dice_loss import MulticlassDiceLoss
import matplotlib.pylab as plt
import numpy as np
import time
if __name__ == '__main__':
DATA_DIRECTORY = "F:\\experiment_code\\U-net\\DRIVE\\training"
DATA_LIST_PATH = "F:\\experiment_code\\U-net\DRIVE\\training\\images_id.txt"
Batch_size = 4
epochs = 100
#MEAN = (104.008, 116.669, 122.675)
dst = driveDateset(DATA_DIRECTORY, DATA_LIST_PATH)
# Initialize model
device = torch.device("cuda")
model = U_net()
model.to(device)
#Initial optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
best_acc, best_epoch =0, 0
global_step = 0
start_time = time.time()
viz = visdom.Visdom()
for epoch in range(epochs):
running_corrects = 0
since_epoch = time.time()
trainloader = torch.utils.data.DataLoader(dst, batch_size=Batch_size) #,shuffle =True
for step, data in enumerate(trainloader):
imgs, labels, _, _ = data
imgs, labels = imgs.to(device), labels.to(device)
labels = labels.float()
# labels = labels / 255.0
# labels[labels > 0.5] = 1.
# labels[labels < 0.5] = 0.
# imgs = imgs / 255.0
model.train()
outputs = model(imgs) # output B * C * H *W
output_c1 = outputs[:,1,:,:] # C are 2 channels
# Dice loss
binary_output_c1 = torch.round(output_c1)
# Network output visualization
binary_output_c1_show = binary_output_c1.cpu().detach().numpy().astype(np.uint8)
plt.figure()
plt.suptitle('Network visualization output')
plt.subplot(1, 4, 1)
plt.imshow(binary_output_c1_show[0,:,:] ),plt.axis('off')
plt.subplot(1, 4, 2)
plt.imshow(binary_output_c1_show[1, :, :] ),plt.axis('off')
plt.subplot(1, 4, 3)
plt.imshow(binary_output_c1_show[2, :, :] ),plt.axis('off')
plt.subplot(1, 4, 4)
plt.imshow(binary_output_c1_show[3, :, :] ),plt.axis('off')
plt.show()
plt.pause(0.5)
#dice = MulticlassDiceLoss()
dice = DiceLoss()
loss = dice(binary_output_c1,labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
viz.line([loss.item()],[global_step], win='loss', update='append',opts=dict(title='train_loss'))
running_corrects = torch.sum(binary_output_c1 == labels).float()
labels_size = labels.size(1) * labels.size(2) * 4
training_acc = running_corrects / labels_size
time_elapsed_epoch = time.time() - since_epoch
print('epoch :', epoch, '\t', 'loss:', loss.item(),'\t','training_acc',training_acc,'\t','{:.0f}m {:.0f}s'.format(time_elapsed_epoch // 60, time_elapsed_epoch % 60))
global_step += 1
time_elapsed = time.time() - start_time
print('Training complate in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
我测试输入的数据没有问题,所有训练数据都已遍历。这样的训练数据和标签
,结果如下:
import torch
import torch.nn as nn
class DiceLoss(nn.Module):
def __init__(self):
super(DiceLoss, self).__init__()
def forward(self, input, target):
N = target.size(0)
smooth = 1
input_flat = input.view(N, -1)
target_flat = target.view(N, -1)
#target_flat = torch.t(target_flat)
intersection = input_flat * target_flat
loss = 2 * (intersection.sum(1) + smooth) / (input_flat.sum(1) + target_flat.sum(1) + smooth)
loss = 1 - loss.sum() / N
return loss
class MulticlassDiceLoss(nn.Module):
"""
requires one hot encoded target. Applies DiceLoss on each class iteratively.
requires input.shape[0:1] and target.shape[0:1] to be (N, C) where N is
batch size and C is number of classes
"""
def __init__(self):
super(MulticlassDiceLoss, self).__init__()
def forward(self, input, target, weights=None):
C = target.shape[1]
if weights is None:
weights = torch.ones(C) #uniform weights for all classes
dice = DiceLoss()
totalLoss = 0
for i in range(C):
diceLoss = dice(input[:, i], target[:, i])
if weights is not None:
diceLoss *= weights[i]
totalLoss += diceLoss
return totalLoss
我不知道如何解决这个问题 我尝试使用一键编码来转换输出和交叉熵损失
epoch : 0 loss: -0.8808882236480713 training_acc tensor(0.3331, device='cuda:0') 0m 2s
epoch : 0 loss: -0.478731632232666 training_acc tensor(0.4376, device='cuda:0') 0m 3s
epoch : 0 loss: -0.42925024032592773 training_acc tensor(0.4485, device='cuda:0') 0m 4s
epoch : 0 loss: -0.60923171043396 training_acc tensor(0.4187, device='cuda:0') 0m 5s
epoch : 0 loss: -0.4442058801651001 training_acc tensor(0.4383, device='cuda:0') 0m 6s
epoch : 1 loss: -0.8808882236480713 training_acc tensor(0.3331, device='cuda:0') 0m 1s
epoch : 1 loss: -0.478731632232666 training_acc tensor(0.4376, device='cuda:0') 0m 2s
epoch : 1 loss: -0.42925024032592773 training_acc tensor(0.4485, device='cuda:0') 0m 2s
epoch : 1 loss: -0.60923171043396 training_acc tensor(0.4187, device='cuda:0') 0m 3s
epoch : 1 loss: -0.4442058801651001 training_acc tensor(0.4383, device='cuda:0') 0m 4s
epoch : 2 loss: -0.8808882236480713 training_acc tensor(0.3331, device='cuda:0') 0m 1s
并得到相同的现象
imgs, labels = imgs.to(device), labels.to(device) # imgs size [4,224,224]
criteon = nn.CrossEntropyLoss() #reduce=False
model.train()
outputs = model(imgs) # output B * C * H *W
output_c1 = outputs[:,1,:,:] # outputs have 2 channels ,I choose the second channels
Rounding_output_c1 = torch.round(output_c1)
Rounding_output_c11 = torch.stack([(Rounding_output_c1 == i).float() for i in range(256)]) # [4,224,224]-> [256,4,224,224] ,256 means the number of classes
Rounding_output_c11 = Rounding_output_c11.permute(1,0,2,3) #[256,4,224,224]->[4,256,224,224]
loss = criteon(Rounding_output_c11,labels)
loss.requires_grad = True
optimizer.zero_grad()
loss.backward()
optimizer.step()
labels_float = labels.float()
running_corrects = torch.sum(Rounding_output_c1 == labels_float).float()
labels_size = labels.size(1) * labels.size(2) * 4
training_acc = running_corrects / labels_size
我不知道如何解决这个问题