X = torch.from_numpy(np.array(old_data)).float()
Y = torch.from_numpy(np.array(old_label)).long()
print(X.size(),Y.size())
CT_dataset = TensorDataset(X,Y)
kwargs = {‘num_workers’: 4, ‘pin_memory’: True} if torch.cuda.is_available() else {}
train_loader = torch.utils.data.DataLoader(
CT_dataset,

batch_size=1, shuffle=True, **kwargs)

pretrained_net = models.resnet34(pretrained=True)
num_classes = 2 #len(classes)
class fcn(nn.Module):
def __init__(self, num_classes):

    super(fcn, self).__init__()

    self.stage1 = nn.Sequential(*list(pretrained_net.children())[:-4])

    # change input channels to 9

    self.stage1[0] = nn.Conv2d(9, 64, kernel_size=7, stride=2, padding=3, bias=False)

    self.stage2 = list(pretrained_net.children())[-4]

    self.stage3 = list(pretrained_net.children())[-3]

    self.scores1 = nn.Conv2d(512, num_classes, 1)

    self.scores2 = nn.Conv2d(256, num_classes, 1)

    self.scores3 = nn.Conv2d(128, num_classes, 1)

    self.upsample_8x = nn.ConvTranspose2d(num_classes, num_classes, 16, 8, 4, bias=False)

    self.upsample_4x = nn.ConvTranspose2d(num_classes, num_classes, 4, 2, 1, bias=False)

    self.upsample_2x = nn.ConvTranspose2d(num_classes, num_classes, 4, 2, 1, bias=False)

def forward(self, x):

    x = self.stage1(x)

    s1 = x  # 1/8

    x = self.stage2(x)

    s2 = x  # 1/16

    x = self.stage3(x)

    s3 = x  # 1/32

    s3 = self.scores1(s3)

    s3 = self.upsample_2x(s3)

    s2 = self.scores2(s2)

    s2 = s2 + s3

    s1 = self.scores3(s1)

    s2 = self.upsample_4x(s2)

    s = s1 + s2

    s = self.upsample_8x(s2)

    return s

num_classes = 2 #len(classes)
X = torch.randn(2, 9, 224, 224)
Y = torch.randint(0, num_classes, (2, 224, 224))
CT_dataset = TensorDataset(X,Y)
kwargs = {‘num_workers’: 4, ‘pin_memory’: True} if torch.cuda.is_available() else {}
train_loader = torch.utils.data.DataLoader(
CT_dataset,

batch_size=1, shuffle=True, **kwargs)

pretrained_net = models.resnet34(pretrained=True)
#model = fcn(num_classes).cuda()
model = fcn(num_classes)
criterion = nn.NLLLoss()
basic_optim = torch.optim.SGD(model.parameters(), lr=1e-2, weight_decay=1e-4)
optimizer = basic_optim
for e in range(50):
if e > 0 and e % 50 == 0:

    optimizer.set_learning_rate(optimizer.learning_rate * 0.1)

train_loss = 0

model= model.train()

for idx, data in tqdm (enumerate(train_loader)):

    optimizer.zero_grad()

    x, y_true = data

    if torch.cuda.is_available():

        x, y_true = x.cuda(), y_true.cuda()

    # forward

    out = model(x)

    out = F.log_softmax(out, dim=1)  # (b, n, h, w)

    loss = criterion(out, y_true)

    # backward

    optimizer.zero_grad()

    loss.backward()

    optimizer.step()

    train_loss += loss.item()

    label_pred = out.max(dim=1)[1].data.cpu().numpy()

    label_true = y_true .data.cpu().numpy()

    acc= get_accuracy(label_true,label_pred)

    print(acc)