How to load and test a model in pytorch? - python

I am loading an already trained dataset. My dataset is a 3D image and i want to test that model now. So I created a different function but is showing an error at. i tried to save using state_dict() first but it showed an error, so I saved the whole model.
My = MyNet(data)
My = torch.load("model_par.pt")
The error is:
File "main_SUDF.py", line 238, in <module>
My = MyNet(data)
File "main_SUDF.py", line 148, in __init__
self.conv1 = nn.Conv2d(input_dim, nChannel, kernel_size=3, stride=1, padding=1)
File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/conv.py", line 410, in __init__
super(Conv2d, self).__init__(
File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/conv.py", line 50, in __init__
if in_channels % groups != 0:
RuntimeError: Boolean value of Tensor with more than one value is ambiguous
and my model is:
class MyNet(nn.Module):
def __init__(self, input_dim):
super(MyNet, self).__init__()
self.conv1 = nn.Conv2d(input_dim, nChannel, kernel_size=3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(nChannel)
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = []
self.bn2 = []
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
for i in range(nConv - 1):
self.conv2.append(nn.Conv2d(nChannel, nChannel, kernel_size=3, stride=1, padding=1))
self.bn2.append(nn.BatchNorm2d(nChannel))
self.conv3 = nn.Conv2d(nChannel, nChannel, kernel_size=1, stride=1, padding=0)
self.bn3 = nn.BatchNorm2d(nChannel)
self.UB1 = nn.UpsamplingBilinear2d(scale_factor=2)
self.deconv = nn.ConvTranspose2d(nChannel, nChannel, kernel_size=3, stride=1, padding=1)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.bn1(x)
x = self.pool1(x)
for i in range(nConv - 1):
x = self.conv2[i](x)
x = F.relu(x)
x = self.bn2[i](x)
if i == 0:
x = self.pool2(x)
x = self.conv3(x)
# x = F.relu(x)
x = self.bn3(x)
x = self.UB1(x)
x = self.deconv(x)
x = F.relu(x)
x = self.bn3(x)
x = self.UB1(x)
x = self.deconv(x)
x = F.relu(x)
x = self.bn3(x)
return x

Related

CycleGan only producing black images

I'm trying to follow a Cyclegan tutorial here:https://www.youtube.com/watch?v=4LktBHGCNfw. It also has the Github to his code. At first I thought I had a typo, but then I tried copying and pasting his code and still the results are only black images. Is anybody else able to copy the code and get actual images?
Generator model
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, down=True, use_act=True, **kwargs):
super().__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, padding_mode="reflect", **kwargs)
if down
else nn.ConvTranspose2d(in_channels, out_channels, **kwargs),
nn.InstanceNorm2d(out_channels),
nn.ReLU(inplace=True) if use_act else nn.Identity()
)
def forward(self, x):
return self.conv(x)
class ResidualBlock(nn.Module):
def __init__(self, channels):
super().__init__()
self.block = nn.Sequential(
ConvBlock(channels, channels, kernel_size=3, padding=1),
ConvBlock(channels, channels, use_act=False, kernel_size=3, padding=1),
)
def forward(self, x):
return x + self.block(x)
class Generator(nn.Module):
def __init__(self, img_channels, num_features = 64, num_residuals=9):
super().__init__()
self.initial = nn.Sequential(
nn.Conv2d(img_channels, num_features, kernel_size=7, stride=1, padding=3, padding_mode="reflect"),
nn.InstanceNorm2d(num_features),
nn.ReLU(inplace=True),
)
self.down_blocks = nn.ModuleList(
[
ConvBlock(num_features, num_features*2, kernel_size=3, stride=2, padding=1),
ConvBlock(num_features*2, num_features*4, kernel_size=3, stride=2, padding=1),
]
)
self.res_blocks = nn.Sequential(
*[ResidualBlock(num_features*4) for _ in range(num_residuals)]
)
self.up_blocks = nn.ModuleList(
[
ConvBlock(num_features*4, num_features*2, down=False, kernel_size=3, stride=2, padding=1, output_padding=1),
ConvBlock(num_features*2, num_features*1, down=False, kernel_size=3, stride=2, padding=1, output_padding=1),
]
)
self.last = nn.Conv2d(num_features*1, img_channels, kernel_size=7, stride=1, padding=3, padding_mode="reflect")
def forward(self, x):
x = self.initial(x)
for layer in self.down_blocks:
x = layer(x)
x = self.res_blocks(x)
for layer in self.up_blocks:
x = layer(x)
return torch.tanh(self.last(x))
Discriminator Model
class Block(nn.Module):
def __init__(self, in_channels, out_channels, stride):
super().__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 4, stride, 1, bias=True, padding_mode="reflect"),
nn.InstanceNorm2d(out_channels),
nn.LeakyReLU(0.2, inplace=True),
)
def forward(self, x):
return self.conv(x)
class Discriminator(nn.Module):
def __init__(self, in_channels=3, features=[64, 128, 256, 512]):
super().__init__()
self.initial = nn.Sequential(
nn.Conv2d(
in_channels,
features[0],
kernel_size=4,
stride=2,
padding=1,
padding_mode="reflect",
),
nn.LeakyReLU(0.2, inplace=True),
)
layers = []
in_channels = features[0]
for feature in features[1:]:
layers.append(Block(in_channels, feature, stride=1 if feature==features[-1] else 2))
in_channels = feature
layers.append(nn.Conv2d(in_channels, 1, kernel_size=4, stride=1, padding=1, padding_mode="reflect"))
self.model = nn.Sequential(*layers)
def forward(self, x):
x = self.initial(x)
return torch.sigmoid(self.model(x))
Train:
def train_fn(disc_H, disc_Z, gen_Z, gen_H, loader, opt_disc, opt_gen, l1, mse, d_scaler, g_scaler):
H_reals = 0
H_fakes = 0
loop = tqdm(loader, leave=True)
for idx, (zebra, horse) in enumerate(loop):
zebra = zebra.to(config.DEVICE)
horse = horse.to(config.DEVICE)
# Train Discriminators H and Z
with torch.cuda.amp.autocast():
fake_horse = gen_H(zebra)
D_H_real = disc_H(horse)
D_H_fake = disc_H(fake_horse.detach())
H_reals += D_H_real.mean().item()
H_fakes += D_H_fake.mean().item()
D_H_real_loss = mse(D_H_real, torch.ones_like(D_H_real))
D_H_fake_loss = mse(D_H_fake, torch.zeros_like(D_H_fake))
D_H_loss = D_H_real_loss + D_H_fake_loss
fake_zebra = gen_Z(horse)
D_Z_real = disc_Z(zebra)
D_Z_fake = disc_Z(fake_zebra.detach())
D_Z_real_loss = mse(D_Z_real, torch.ones_like(D_Z_real))
D_Z_fake_loss = mse(D_Z_fake, torch.zeros_like(D_Z_fake))
D_Z_loss = D_Z_real_loss + D_Z_fake_loss
# put it togethor
D_loss = (D_H_loss + D_Z_loss)/2
opt_disc.zero_grad()
d_scaler.scale(D_loss).backward()
d_scaler.step(opt_disc)
d_scaler.update()
# Train Generators H and Z
with torch.cuda.amp.autocast():
# adversarial loss for both generators
D_H_fake = disc_H(fake_horse)
D_Z_fake = disc_Z(fake_zebra)
loss_G_H = mse(D_H_fake, torch.ones_like(D_H_fake))
loss_G_Z = mse(D_Z_fake, torch.ones_like(D_Z_fake))
# cycle loss
cycle_zebra = gen_Z(fake_horse)
cycle_horse = gen_H(fake_zebra)
cycle_zebra_loss = l1(zebra, cycle_zebra)
cycle_horse_loss = l1(horse, cycle_horse)
# identity loss (remove these for efficiency if you set lambda_identity=0)
identity_zebra = gen_Z(zebra)
identity_horse = gen_H(horse)
identity_zebra_loss = l1(zebra, identity_zebra)
identity_horse_loss = l1(horse, identity_horse)
# add all togethor
G_loss = (
loss_G_Z
+ loss_G_H
+ cycle_zebra_loss * config.LAMBDA_CYCLE
+ cycle_horse_loss * config.LAMBDA_CYCLE
+ identity_horse_loss * config.LAMBDA_IDENTITY
+ identity_zebra_loss * config.LAMBDA_IDENTITY
)
opt_gen.zero_grad()
g_scaler.scale(G_loss).backward()
g_scaler.step(opt_gen)
g_scaler.update()
if idx % 200 == 0:
save_image(fake_horse*0.5+0.5, f"saved_images/horse_{idx}.png")
save_image(fake_zebra*0.5+0.5, f"saved_images/zebra_{idx}.png")
#loop.set_postfix(H_real=H_reals/(idx+1), H_fake=H_fakes/(idx+1))
def main():
disc_H = Discriminator(in_channels=3).to(config.DEVICE)
disc_Z = Discriminator(in_channels=3).to(config.DEVICE)
gen_Z = Generator(img_channels=3, num_residuals=9).to(config.DEVICE)
gen_H = Generator(img_channels=3, num_residuals=9).to(config.DEVICE)
opt_disc = optim.Adam(
list(disc_H.parameters()) + list(disc_Z.parameters()),
lr=config.LEARNING_RATE,
betas=(0.5, 0.999),
)
opt_gen = optim.Adam(
list(gen_Z.parameters()) + list(gen_H.parameters()),
lr=config.LEARNING_RATE,
betas=(0.5, 0.999),
)
L1 = nn.L1Loss()
mse = nn.MSELoss()
if config.LOAD_MODEL:
load_checkpoint(
config.CHECKPOINT_GEN_H, gen_H, opt_gen, config.LEARNING_RATE,
)
load_checkpoint(
config.CHECKPOINT_GEN_Z, gen_Z, opt_gen, config.LEARNING_RATE,
)
load_checkpoint(
config.CHECKPOINT_CRITIC_H, disc_H, opt_disc, config.LEARNING_RATE,
)
load_checkpoint(
config.CHECKPOINT_CRITIC_Z, disc_Z, opt_disc, config.LEARNING_RATE,
)
dataset = HorseZebraDataset(
root_horse=config.TRAIN_DIR+"/horses", root_zebra=config.TRAIN_DIR+"/zebras", transform=config.transforms
)
#val_dataset = HorseZebraDataset(
#root_horse="cyclegan_test/horse1", root_zebra="cyclegan_test/zebra1", transform=config.transforms
#)
#val_loader = DataLoader(
#val_dataset,
#batch_size=1,
#shuffle=False,
#pin_memory=True,
#)
loader = DataLoader(
dataset,
batch_size=config.BATCH_SIZE,
shuffle=True,
#num_workers=config.NUM_WORKERS,
pin_memory=True
)
g_scaler = torch.cuda.amp.GradScaler()
d_scaler = torch.cuda.amp.GradScaler()
for epoch in range(config.NUM_EPOCHS):
train_fn(disc_H, disc_Z, gen_Z, gen_H, loader, opt_disc, opt_gen, L1, mse, d_scaler, g_scaler)
if config.SAVE_MODEL:
save_checkpoint(gen_H, opt_gen, filename=config.CHECKPOINT_GEN_H)
save_checkpoint(gen_Z, opt_gen, filename=config.CHECKPOINT_GEN_Z)
save_checkpoint(disc_H, opt_disc, filename=config.CHECKPOINT_CRITIC_H)
save_checkpoint(disc_Z, opt_disc, filename=config.CHECKPOINT_CRITIC_Z)

When I used pytorch , I got the error:"" IndexError: index 4 is out of bounds for axis 0 with size 4 ""

the following code shows IndexError: index 4 is out of bounds for axis 0 with size 4:
I don't know what's wrong with it.
How can I solve this error? Thanks!
The following is the problem when I run Pycharm:
Epoch 1/10
Traceback (most recent call last):
File "C:/Users/ABCDfile/PycharmProjects/pythonProject1/main.py", line 376, in
train(model, train_loader, torch.optim.Adam(model.parameters(), lr=0.01), nn.BCEWithLogitsLoss(), 10, "_bce_e10")
File "C:/Users/ABCDfile/PycharmProjects/pythonProject1/main.py", line 251, in train
iou = IOU(output, y)
File "C:/Users/ABCDfile/PycharmProjects/pythonProject1/main.py", line 301, in IOU
if prediction[l][0][n][m] == 1 and groundtruth[l][0][n][m] == 1:
IndexError: index 4 is out of bounds for axis 0 with size 4
import torch
from torch import nn
from torch.utils.data import Dataset, DataLoader
from os import listdir
import numpy as np
import cv2 as cv
import torch.nn.functional as F
class NucleusDataset(Dataset):
def __init__(self, tot): # tot = train or test
super().__init__()
self.root = 'C:/Users/Desktop/Nucleus dataset/'
self.allfolderlist = listdir(self.root)
print("There are " + str(len(self.allfolderlist)) + " data totally")
self.folderlist = []
self.tot = tot
if self.tot == 'train':
print("Get training dataset")
for n in range(int(len(self.allfolderlist) / 2)):
self.folderlist.append(self.allfolderlist[n])
print("There are " + str(len(self.folderlist)) + " data in training dataset")
elif self.tot == 'test':
print("Get testing dataset")
for n in range(int(len(self.allfolderlist) / 2), int(len(self.allfolderlist))):
self.folderlist.append(self.allfolderlist[n])
print("There are " + str(len(self.folderlist)) + " data in testing dataset")
else:
print("Choose train or test")
def __len__(self):
return len(self.folderlist)
def __getitem__(self, index):
foldername = self.folderlist[index]
filename = foldername.split(".")[0] + ".png"
img = cv.imread(self.root + foldername + "/images/" + filename)
img = cv.resize(img, (224, 224), interpolation=cv.INTER_LINEAR)
img_np = np.array(img, dtype=np.float32)
flat_img_np = np.empty(shape=(3, 224, 224))
for x in range(224):
for y in range(224):
flat_img_np[0][x][y] = (img_np[x][y][0] + img_np[x][y][1] + img_np[x][y][2]) / 765
sum = 0
for x in range(224):
for y in range(224):
sum += flat_img_np[0][x][y]
flat_img_np = flat_img_np * 0.5 / sum * 224 * 224
outputpath = self.root + foldername + "/masks/"
isfirst = True
for objectpic in listdir(outputpath):
obimg = cv.imread(outputpath + objectpic)
obimg = cv.resize(obimg, (224, 224), interpolation=cv.INTER_LINEAR)
if isfirst:
obimg_np = np.array(obimg, dtype=np.float32)
isfirst = False
else:
obimg_np += np.array(obimg, dtype=np.float32)
obimg_np / 2
flat_obimg_np = np.empty(shape=(1, 224, 224))
for x in range(224):
for y in range(224):
if obimg_np[x][y][0] == 255:
flat_obimg_np[0][x][y] = 1
else:
flat_obimg_np[0][x][y] = 0
return flat_img_np, flat_obimg_np
class SegNet(nn.Module):
def __init__(self,input_nbr,label_nbr):
super(SegNet, self).__init__()
batchNorm_momentum = 0.1
self.conv11 = nn.Conv2d(input_nbr, 64, kernel_size=3, padding=1)
self.bn11 = nn.BatchNorm2d(64, momentum= batchNorm_momentum)
self.conv12 = nn.Conv2d(64, 64, kernel_size=3, padding=1)
self.bn12 = nn.BatchNorm2d(64, momentum= batchNorm_momentum)
self.conv21 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
self.bn21 = nn.BatchNorm2d(128, momentum= batchNorm_momentum)
self.conv22 = nn.Conv2d(128, 128, kernel_size=3, padding=1)
self.bn22 = nn.BatchNorm2d(128, momentum= batchNorm_momentum)
self.conv31 = nn.Conv2d(128, 256, kernel_size=3, padding=1)
self.bn31 = nn.BatchNorm2d(256, momentum= batchNorm_momentum)
self.conv32 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
self.bn32 = nn.BatchNorm2d(256, momentum= batchNorm_momentum)
self.conv33 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
self.bn33 = nn.BatchNorm2d(256, momentum= batchNorm_momentum)
self.conv41 = nn.Conv2d(256, 512, kernel_size=3, padding=1)
self.bn41 = nn.BatchNorm2d(512, momentum= batchNorm_momentum)
self.conv42 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.bn42 = nn.BatchNorm2d(512, momentum= batchNorm_momentum)
self.conv43 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.bn43 = nn.BatchNorm2d(512, momentum= batchNorm_momentum)
self.conv51 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.bn51 = nn.BatchNorm2d(512, momentum= batchNorm_momentum)
self.conv52 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.bn52 = nn.BatchNorm2d(512, momentum= batchNorm_momentum)
self.conv53 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.bn53 = nn.BatchNorm2d(512, momentum= batchNorm_momentum)
self.conv53d = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.bn53d = nn.BatchNorm2d(512, momentum= batchNorm_momentum)
self.conv52d = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.bn52d = nn.BatchNorm2d(512, momentum= batchNorm_momentum)
self.conv51d = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.bn51d = nn.BatchNorm2d(512, momentum= batchNorm_momentum)
self.conv43d = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.bn43d = nn.BatchNorm2d(512, momentum= batchNorm_momentum)
self.conv42d = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.bn42d = nn.BatchNorm2d(512, momentum= batchNorm_momentum)
self.conv41d = nn.Conv2d(512, 256, kernel_size=3, padding=1)
self.bn41d = nn.BatchNorm2d(256, momentum= batchNorm_momentum)
self.conv33d = nn.Conv2d(256, 256, kernel_size=3, padding=1)
self.bn33d = nn.BatchNorm2d(256, momentum= batchNorm_momentum)
self.conv32d = nn.Conv2d(256, 256, kernel_size=3, padding=1)
self.bn32d = nn.BatchNorm2d(256, momentum= batchNorm_momentum)
self.conv31d = nn.Conv2d(256, 128, kernel_size=3, padding=1)
self.bn31d = nn.BatchNorm2d(128, momentum= batchNorm_momentum)
self.conv22d = nn.Conv2d(128, 128, kernel_size=3, padding=1)
self.bn22d = nn.BatchNorm2d(128, momentum= batchNorm_momentum)
self.conv21d = nn.Conv2d(128, 64, kernel_size=3, padding=1)
self.bn21d = nn.BatchNorm2d(64, momentum= batchNorm_momentum)
self.conv12d = nn.Conv2d(64, 64, kernel_size=3, padding=1)
self.bn12d = nn.BatchNorm2d(64, momentum= batchNorm_momentum)
self.conv11d = nn.Conv2d(64, label_nbr, kernel_size=3, padding=1)
def forward(self, x):
# Stage 1
x11 = F.relu(self.bn11(self.conv11(x)))
x12 = F.relu(self.bn12(self.conv12(x11)))
x1p, id1 = F.max_pool2d(x12,kernel_size=2, stride=2,return_indices=True)
# Stage 2
x21 = F.relu(self.bn21(self.conv21(x1p)))
x22 = F.relu(self.bn22(self.conv22(x21)))
x2p, id2 = F.max_pool2d(x22,kernel_size=2, stride=2,return_indices=True)
# Stage 3
x31 = F.relu(self.bn31(self.conv31(x2p)))
x32 = F.relu(self.bn32(self.conv32(x31)))
x33 = F.relu(self.bn33(self.conv33(x32)))
x3p, id3 = F.max_pool2d(x33,kernel_size=2, stride=2,return_indices=True)
# Stage 4
x41 = F.relu(self.bn41(self.conv41(x3p)))
x42 = F.relu(self.bn42(self.conv42(x41)))
x43 = F.relu(self.bn43(self.conv43(x42)))
x4p, id4 = F.max_pool2d(x43,kernel_size=2, stride=2,return_indices=True)
# Stage 5
x51 = F.relu(self.bn51(self.conv51(x4p)))
x52 = F.relu(self.bn52(self.conv52(x51)))
x53 = F.relu(self.bn53(self.conv53(x52)))
x5p, id5 = F.max_pool2d(x53,kernel_size=2, stride=2,return_indices=True)
# Stage 5d
x5d = F.max_unpool2d(x5p, id5, kernel_size=2, stride=2)
x53d = F.relu(self.bn53d(self.conv53d(x5d)))
x52d = F.relu(self.bn52d(self.conv52d(x53d)))
x51d = F.relu(self.bn51d(self.conv51d(x52d)))
# Stage 4d
x4d = F.max_unpool2d(x51d, id4, kernel_size=2, stride=2)
x43d = F.relu(self.bn43d(self.conv43d(x4d)))
x42d = F.relu(self.bn42d(self.conv42d(x43d)))
x41d = F.relu(self.bn41d(self.conv41d(x42d)))
# Stage 3d
x3d = F.max_unpool2d(x41d, id3, kernel_size=2, stride=2)
x33d = F.relu(self.bn33d(self.conv33d(x3d)))
x32d = F.relu(self.bn32d(self.conv32d(x33d)))
x31d = F.relu(self.bn31d(self.conv31d(x32d)))
# Stage 2d
x2d = F.max_unpool2d(x31d, id2, kernel_size=2, stride=2)
x22d = F.relu(self.bn22d(self.conv22d(x2d)))
x21d = F.relu(self.bn21d(self.conv21d(x22d)))
# Stage 1d
x1d = F.max_unpool2d(x21d, id1, kernel_size=2, stride=2)
x12d = F.relu(self.bn12d(self.conv12d(x1d)))
x11d = self.conv11d(x12d)
return x11d
def load_from_segnet(self, model_path):
s_dict = self.state_dict()# create a copy of the state dict
th = torch.load(model_path).state_dict() # load the weigths
# for name in th:
# s_dict[corresp_name[name]] = th[name]
self.load_state_dict(th)
def train(model, dataloader, optimizer, loss_fn, epochs, filename):
model.cuda()
model.train(True) # Set trainind mode = true
f = open("C:/Users/Desktop/limf" + filename + ".txt", 'a')
for epoch in range(epochs):
print('-' * 10)
print('Epoch {}/{}'.format(epoch + 1, epochs))
step = 0
eloss = 0
eiou = 0
emae = 0
ef = 0
for x, y in dataloader:
x = x.float()
x.requires_grad = True
x = x.cuda()
y = y.float()
y = y.cuda()
step += 1
optimizer.zero_grad()
output = model(x)
output = output.cuda()
loss = loss_fn(output, y)
iou = IOU(output, y)
mae = MAE(output, y)
fmeasure = Fmeasure(output, y)
y = y.cpu().detach().numpy()
output = output.cpu().detach().numpy()
output = 1 * (output[:, :, :, :] > 0.5)
ys = ""
os = ""
for n in range(224):
for m in range(224):
ys += str(int(y[0][0][n][m]))
os += str(int(output[0][0][n][m]))
print(ys + " " + os, file=f)
ys = ""
os = ""
print("----------", file=f)
eloss += loss
eiou += iou
emae += mae
ef += fmeasure
loss.backward()
optimizer.step()
print('Current step: {} Loss: {} IOU: {} MAE: {} F: {}'.format(step, loss, iou, mae, fmeasure), file=f)
eloss /= step
eiou /= step
emae /= step
ef /= step
print('-----Epoch {} finish Loss: {} IOU: {} MAE: {} F: {}'.format(epoch, eloss, eiou, emae, ef), file=f)
print('-----Epoch {} finish Loss: {} IOU: {} MAE: {} F: {}'.format(epoch, eloss, eiou, emae, ef))
f.close()
def IOU(prediction, groundtruth, bs=15):
prediction = prediction.cpu().detach().numpy()
groundtruth = groundtruth.cpu().detach().numpy()
prediction = 1 * (prediction[:, :, :, :] > 0.5)
intersection = 0
union = 0
for l in range(bs):
for n in range(224):
for m in range(224):
if prediction[l][0][n][m] == 1 and groundtruth[l][0][n][m] == 1:
intersection += 1
if prediction[l][0][n][m] == 1 or groundtruth[l][0][n][m] == 1:
union += 1
iou_score = intersection / union
return iou_score
def MAE(prediction, groundtruth, bs=15):
prediction = prediction.cpu().detach().numpy()
groundtruth = groundtruth.cpu().detach().numpy()
prediction = 1 * (prediction[:, :, :, :] > 0.5)
error = 0
for l in range(bs):
for x in range(224):
for y in range(224):
if prediction[l][0][x][y] != groundtruth[l][0][x][y]:
error += 1
return error / 224 / 224 / bs
def Fmeasure(prediction, groundtruth, bs=15, b=1):
prediction = prediction.cpu().detach().numpy()
groundtruth = groundtruth.cpu().detach().numpy()
prediction = 1 * (prediction[:, :, :, :] > 0.5)
TP = 0
FP = 0
FN = 0
for l in range(bs):
for x in range(224):
for y in range(224):
if prediction[l][0][x][y] == 1 and groundtruth[l][0][x][y] == 1:
TP += 1
if prediction[l][0][x][y] == 1 and groundtruth[l][0][x][y] == 0:
FP += 1
if prediction[l][0][x][y] == 0 and groundtruth[l][0][x][y] == 1:
FN += 1
if (TP + FP) == 0:
precision = 0
else:
precision = TP / (TP + FP)
if (TP + FN) == 0:
recall = 0
else:
recall = TP / (TP + FN)
if precision + recall == 0:
return 0
return ((1 + b * b) * precision * recall) / (b * b * (precision + recall))
if __name__ == '__main__':
# see whether gpu can be used
# device = torch.device('cuda' if torch.cuda.is_available() else 'CPU')
# print("device: "+str(device))
# load dataset
train_dataset = NucleusDataset("train")
bs = 4 # batch size
train_loader = DataLoader(train_dataset, batch_size=bs, num_workers=1, drop_last=True, shuffle=False)
model = SegNet(3,1)
train(model, train_loader, torch.optim.Adam(model.parameters(), lr=0.01), nn.BCEWithLogitsLoss(), 10, "_bce_e10")
How can I solve this error?
Your batch size is 4, and you are using the default value for bs when calling IOU, which means bs=15, not 4. Therefore call IOU by passing the batch size: IOU(output, y, bs=bs). Better yet, you could remove the bs argument, and define bs as y.size(0) inside the function.

ValueError in model subclassing with tensorflow 2

I'm trying to implement a WideResnet using Model subclassing in keras. I cannot understand what's wrong in my code:
class ResidualBlock(layers.Layer):
def __init__(self, filters, kernel_size, dropout, dropout_percentage, strides=1, **kwargs):
super(ResidualBlock, self).__init__(**kwargs)
self.conv_1 = layers.Conv2D(filters, (1, 1), strides=strides)
self.bn_1 = layers.BatchNormalization()
self.rel_1 = layers.ReLU()
self.conv_2 = layers.Conv2D(filters, kernel_size, padding="same", strides=strides)
self.dropout = layers.Dropout(dropout_percentage)
self.bn_2 = layers.BatchNormalization()
self.rel_2 = layers.ReLU()
self.conv_3 = layers.Conv2D(filters, kernel_size, padding="same")
self.add = layers.Add()
self.dropout = dropout
self.strides = strides
def call(self, inputs):
x = inputs
if self.strides > 1:
x = self.conv_1(x)
res_x = self.bn_1(x)
res_x = self.rel_1(x)
res_x = self.conv_2(x)
if self.dropout:
res_x = self.dropout(x)
res_x = self.bn_2(x)
res_x = self.rel_2(x)
res_x = self.conv_3(x)
inputs = self.add([x, res_x])
return inputs
class WideResidualNetwork(models.Model):
def __init__(self, input_shape, n_classes, d, k, kernel_size=(3, 3), dropout=False, dropout_percentage=0.3, strides=1, **kwargs):
super(WideResidualNetwork, self).__init__(**kwargs)
if (d-4)%6 != 0:
raise ValueError('Please choose a correct depth!')
self.rel_1 = layers.ReLU()
self.conv_1 = layers.Conv2D(16, (3, 3), padding='same')
self.conv_2 = layers.Conv2D(16*k, (1, 1))
self.dense = layers.Dense(n_classes)
self.dropout = dropout
self.dropout_percentage = dropout_percentage
self.N = int((d - 4) / 6)
self.k = k
self.d = d
self.kernel_size = kernel_size
def build(self, input_shape):
self.bn_1 = layers.BatchNormalization(input_shape=input_shape)
def call(self, inputs):
x = self.bn_1(inputs)
x = self.rel_1(x)
x = self.conv_1(x)
x = self.conv_2(x)
for _ in range(self.N):
x = ResidualBlock(16*self.k, self.kernel_size, self.dropout, self.dropout_percentage)(x)
x = ResidualBlock( 32*self.k, self.kernel_size, self.dropout, self.dropout_percentage, strides=2)(x)
for _ in range(self.N-1):
x = ResidualBlock( 32*self.k, self.kernel_size, self.dropout, self.dropout_percentage)(x)
x = ResidualBlock( 64*self.k, self.kernel_size, self.dropout, self.dropout_percentage, strides=2)(x)
for _ in range(self.N-1):
x = ResidualBlock( 64*self.k, self.kernel_size, self.dropout, self.dropout_percentage)(x)
x = layers.GlobalAveragePooling2D()(x)
x = self.dense(x)
x = layers.Activation("softmax")(x)
return x
When i try to fit the model in this way:
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
model = WideResidualNetwork(x_train[0].shape, 10, 28, 1)
x_train, x_test = x_train/255. , x_test/255.
model = WideResidualNetwork(x_train[0].shape, 10, 28, 1)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
epochs = 40
batch_size = 64
validation_split = 0.2
h = model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_split=validation_split)
I got the following error:
...
<ipython-input-26-61c1bdb3546c>:31 call *
x = ResidualBlock(16*self.k, self.kernel_size, self.dropout, self.dropout_percentage)(x)
<ipython-input-9-3fea1e77cb6e>:23 call *
res_x = self.bn_1(x)
...
ValueError: tf.function-decorated function tried to create variables on non-first call.
So I didn't understand where is the problem, I also tried to move the initialization into the build, but without results, the error persists. Probably I have some gaps in my knowledge
Thank you in advance
You are initializing ResidualBlocks, GlobalAveragePooling2D, and Activation layers into the call method. Try to move them into the init, as you did for other layers, and it shouldn't give you that error.

Tensorflow: output of multi-step decay function returns a TypeError

We are trying to write a multi-step decay function in Tensorflow using tf.train.piecewise_constant() as suggested here. Tensorflow documentation here states that:
"When eager execution is enabled, this function returns a function which in turn returns the decayed learning rate Tensor"
However, when we tried running the code, it returned a TypeError.
It returns the same error even when lr() is used.
import tensorflow as tf
tf.enable_eager_execution()
import numpy as np
def conv3x3(out_planes, data_format ='channels_last', stride=1, padding='same', dilation=1, name = None,use_bias = False):
"""3x3 convolution with padding"""
return tf.keras.layers.Conv2D(filters = out_planes, kernel_size = 3,data_format= data_format,
strides=(stride, stride), padding='same', use_bias=use_bias,
dilation_rate = (dilation,dilation) , kernel_initializer=tf.initializers.he_normal(),name = name)
def conv1x1(out_planes,data_format ='channels_last', padding = 'same', stride=1):
"""1x1 convolution"""
return tf.keras.layers.Conv2D(filters = out_planes, kernel_size = 1, strides=(stride, stride),data_format= data_format,
padding=padding, use_bias=False, kernel_initializer=tf.initializers.he_normal())
class BasicBlock(tf.keras.Model):
expansion = 1
def __init__(self, planes=1, stride=1, data_format= 'channels_last', downsample=None, dilation=(1, 1), residual=True, key=None, stage = None):
super(BasicBlock, self).__init__()
self.data_format = data_format
bn_axis = 1 if self.data_format == 'channels_first' else 3
self.conv1 = conv3x3(out_planes= planes, stride = stride, padding='same' ,
data_format = self.data_format, dilation=dilation[0], name = '{}_{}_conv0'.format(key,stage))
self.bn1 = tf.keras.layers.BatchNormalization(axis=bn_axis, name = '{}_{}_BN0'.format(key,stage))
self.conv2 = conv3x3(out_planes =planes, padding='same',
data_format = self.data_format, dilation=dilation[0],name = '{}_{}_conv1'.format(key,stage))
self.bn2 = tf.keras.layers.BatchNormalization(axis=bn_axis,name = '{}_{}_BN1'.format(key,stage))
self.downsample = downsample
self.relu = tf.keras.layers.ReLU(name = '{}_{}_Relu'.format(key,stage))
self.stride = stride
self.residual = residual
def get_config(self):
base_config = {}
base_config['conv1'] = self.conv1.get_config()
base_config['bn1'] = self.bn1.get_config()
base_config['conv2'] = self.conv2.get_config()
base_config['bn2'] = self.bn2.get_config()
if self.downsample is not None:
base_config['downsample'] = self.downsample.get_config()
return base_config
def call(self, inputs, training=None):
residual = inputs
out = self.conv1(inputs)
out = self.bn1(out,training = training)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(inputs)
if self.residual:
out += residual
out = self.relu(out)
return out
class Bottleneck(tf.keras.Model):
expansion = 4
def __init__(self, planes, stride=1, data_format = 'channels_last',downsample=None,dilation=(1, 1)):
super(Bottleneck, self).__init__()
bn_axis = 1 if data_format == 'channels_first' else 3
self.conv1 = conv1x1(planes, data_format = data_format)
self.bn1 = tf.keras.layers.BatchNormalization(axis=bn_axis)
self.relu = tf.keras.layers.ReLU()
self.conv2 = conv3x3(planes, stride, padding= 'same', bias=False, data_format = data_format, dilation=dilation[1])
self.bn2 = tf.keras.layers.BatchNormalization(axis=bn_axis)
self.conv3 =conv1x1( planes * 4, data_format = data_format, )
self.bn3 = tf.keras.layers.BatchNormalization(axis=bn_axis) # nn.BatchNorm2d(planes * self.expansion)
self.downsample = downsample
self.stride = stride
def get_config(self):
base_config = {}
base_config['conv1'] = self.conv1.get_config()
base_config['bn1'] = self.bn1.get_config()
base_config['conv2'] = self.conv2.get_config()
base_config['bn2'] = self.bn2.get_config()
base_config['conv3'] = self.conv3.get_config()
base_config['bn3'] = self.bn3.get_config()
if self.downsample is not None:
base_config['downsample'] = self.downsample.get_config()
return base_config
def call(self, inputs, training=None):
identity = inputs
out = self.conv1(inputs)
out = self.bn1(out,training = training)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out,training = training)
out = tf.nn.relu(out)
out = self.conv3(out)
out = self.bn3(out,training = training)
if self.downsample is not None:
identity = self.downsample(inputs)
out += identity
out = self.relu(out)
return out
class pooling (tf.keras.Model):
def __init__(self, pool_size, stride = None, data_format='channels_last'):
super(pooling, self).__init__()
self.pool_size = pool_size
self.data_format = data_format
if stride is None:
self.stride =self.pool_size
else:
self.stride = stride
def call(self, inputs):
return tf.layers.average_pooling2d(inputs, strides =self.stride, pool_size = self.pool_size, data_format = self.data_format)
class DRN(tf.keras.Model):
def __init__(self, block, layers, data_format='channels_last', num_classes=7,channels=(16, 32, 64, 128, 256, 512, 512, 512),
out_map=False, out_middle=False, pool_size=28, arch='D'):
super(DRN, self).__init__()
self.inplanes = channels[0]
self.out_map = out_map
self.out_dim = channels[-1]
self.out_middle = out_middle
self.arch = arch
self.poolsize = pool_size
self.data_format = data_format
self.bn_axis = 1 if data_format == 'channels_first' else 3
self.conv0 = tf.keras.layers.Conv2D(filters=channels[0], kernel_size=7, strides=1, padding='same',
use_bias=False, data_format = self.data_format, kernel_initializer=tf.initializers.he_normal(), name ='L0_conv0' )
self.bn0 = tf.keras.layers.BatchNormalization(axis=self.bn_axis,name ='L0_BN0')
self.relu0 = tf.keras.layers.ReLU(name ='L0_Relu0')
if arch == 'C':
self.layer1 = self._make_layer(block = BasicBlock, planes = channels[0], blocks = layers[0], stride=1, data_format = self.data_format, key='CL1')
self.layer2 = self._make_layer(block = BasicBlock, planes = channels[1], blocks = layers[1], stride=2, data_format = self.data_format, key='CL2')
elif arch == 'D':
self.layer1 = self._make_conv_layers(channels = channels[0],convs = layers[0], stride=1, data_format = self.data_format, key='DL1')
self.layer2 = self._make_conv_layers(channels = channels[1],convs = layers[1], stride=2, data_format = self.data_format, key='DL2')
self.layer3 = self._make_layer(block = block, planes = channels[2], blocks = layers[2], stride=2, data_format = self.data_format, key='L3')
self.layer4 = self._make_layer(block = block, planes = channels[3], blocks = layers[3], stride=2, data_format = self.data_format, key='L4')
self.layer5 = self._make_layer(block = block, planes = channels[4], blocks = layers[4], dilation=2, new_level=False, data_format = self.data_format, key='L5')
self.layer6 = None if layers[5] == 0 else self._make_layer(block, channels[5], layers[5], dilation=4, new_level=False, data_format = self.data_format, key='L6')
if arch == 'C':
self.layer7 = None if layers[6] == 0 else self._make_layer(BasicBlock, channels[6], layers[6], dilation=2, new_level=False, residual=False, data_format = self.data_format, key='CL7')
self.layer8 = None if layers[7] == 0 else self._make_layer(BasicBlock, channels[7], layers[7], dilation=1, new_level=False, residual=False, data_format = self.data_format, key='CL8')
elif arch == 'D':
self.layer7 = None if layers[6] == 0 else self._make_conv_layers(channels[6], layers[6], dilation=2, data_format = self.data_format, key='DL7')
self.layer8 = None if layers[7] == 0 else self._make_conv_layers(channels[7], layers[7], dilation=1, data_format = self.data_format, key='DL8')
if num_classes > 0:
self.avgpool = tf.keras.layers.GlobalAveragePooling2D(data_format = self.data_format)
self.fc = tf.keras.layers.Dense(units=num_classes)
def _make_layer(self, block, planes, blocks, stride=1,dilation=1, new_level=True, data_format = 'channels_last', residual=True, key=None):
assert dilation == 1 or dilation % 2 == 0
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = tf.keras.Sequential([conv1x1(out_planes = planes * block.expansion,stride = stride, data_format = data_format),
tf.keras.layers.BatchNormalization(axis=self.bn_axis)], name = 'downsample')
#
layers = []
layers.append(block(planes= planes, stride = stride, downsample = downsample, dilation=(1, 1) if dilation == 1 else (
dilation // 2 if new_level else dilation, dilation), data_format=data_format, residual=residual, key = key, stage = '0'))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(planes, residual=residual,dilation=(dilation, dilation), data_format=data_format, key = key, stage = i))
return tf.keras.Sequential(layers, name = key)
def _make_conv_layers(self, channels, convs, stride=1, dilation=1 ,data_format = 'channels_last', key = None):
modules = []
for i in range(convs):
modules.extend([
conv3x3(out_planes= channels, stride=stride if i == 0 else 1,
padding= 'same' , use_bias=False, dilation=dilation, data_format = data_format,name ='{}_{}_Conv'.format(key,i)),
tf.keras.layers.BatchNormalization(axis=self.bn_axis,name ='{}_{}_BN'.format(key,i)),
tf.keras.layers.ReLU(name ='{}_{}_Relu'.format(key,i))])
self.inplanes = channels
return tf.keras.Sequential(modules,name=key)
def call(self, x, training=None):
x = self.conv0(x)
x = self.bn0(x,training = training)
x = self.relu0(x)
x = self.layer1(x,training = training)
x = self.layer2(x,training = training)
x = self.layer3(x,training = training)
x = self.layer4(x,training = training)
x = self.layer5(x,training = training)
if self.layer6 is not None:
x = self.layer6(x,training = training)
if self.layer7 is not None:
x = self.layer7(x)
if self.layer8 is not None:
x = self.layer8(x)
if self.out_map:
x = self.fc(x)
else:
x = self.avgpool(x)
x = self.fc(x)
return x
def loss(logits, labels):
return tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels))
def make_scheduler(policy, init_lr, n_step_epoch, global_step):
total_steps= n_step_epoch * 10 #10 epochs
milestones = policy.split('_')
milestones.pop(0)
milestones = list(map(lambda x: int(x), milestones))
boundaries = np.multiply(milestones,n_step_epoch)
values = [init_lr] + [init_lr/(0.1**-i) for i in range(1,len(milestones)+1)]
learning_rate = tf.train.piecewise_constant(global_step, boundaries, values)
return learning_rate
def train(model, optimizer, step_counter ):
"""Trains model on `dataset` using `optimizer`."""
for (batch, i) in enumerate(range(10)):
print('Training Loop {}'.format(i))
images = tf.random.uniform((4, 224, 224,3))
labels = tf.constant(np.random.randint(4, size=4))
with tf.contrib.summary.record_summaries_every_n_global_steps(10, global_step=step_counter):
with tf.GradientTape() as tape:
logits = model(images, training=True)
loss_value = loss(logits, labels)
grads = tape.gradient(loss_value, model.variables)
optimizer.apply_gradients(zip(grads, model.variables), global_step=step_counter)
def test(model):
"""Perform an evaluation of `model` on the examples from `dataset`."""
for i in (range(10)):
images = tf.random.uniform((4, 225, 225,3))
logits = model(images, training=False)
print(logits)
def main():
model = DRN(BasicBlock, [1, 1, 2, 2, 2, 2, 1, 1], arch='C',num_classes = 4)
device = '/gpu:0'
step_counter = tf.train.get_or_create_global_step()
lr = make_scheduler(policy='multistep_2_5',init_lr=0.1,n_step_epoch = 10,global_step= step_counter)
optimizer = tf.train.MomentumOptimizer(lr,momentum=0.5)
with tf.device(device):
for _ in range(10):
train(model, optimizer,step_counter)
print(optimizer._lr_t)
test(model)
if __name__ == '__main__':
main()
File "", line 1, in
runfile('/home/srijith/work/Tensorflow/SkinCaner_tensorflow/debug/stackoverflow.py', wdir='/home/srijith/work/Tensorflow/SkinCaner_tensorflow/debug')
File "/home/srijith/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 709, in runfile
execfile(filename, namespace)
File "/home/srijith/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/srijith/work/Tensorflow/SkinCaner_tensorflow/debug/stackoverflow.py", line 311, in
main()
File "/home/srijith/work/Tensorflow/SkinCaner_tensorflow/debug/stackoverflow.py", line 305, in main
train(model, optimizer,step_counter)
File "/home/srijith/work/Tensorflow/SkinCaner_tensorflow/debug/stackoverflow.py", line 284, in train
optimizer.apply_gradients(zip(grads, model.variables), global_step=step_counter)
File "/home/srijith/anaconda3/lib/python3.7/site-packages/tensorflow/python/training/optimizer.py", line 598, in apply_gradients
self._prepare()
File "/home/srijith/anaconda3/lib/python3.7/site-packages/tensorflow/python/training/momentum.py", line 87, in _prepare
learning_rate = learning_rate()
File "/home/srijith/anaconda3/lib/python3.7/site-packages/tensorflow/python/training/learning_rate_decay_v2.py", line 171, in decayed_lr
boundaries = ops.convert_n_to_tensor(boundaries)
File "/home/srijith/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1273, in convert_n_to_tensor
as_ref=False)
File "/home/srijith/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1228, in internal_convert_n_to_tensor
raise TypeError("values must be a list.")
TypeError: values must be a list.
The code works as expected when we provide a constant learning rate. Is there something that we are missing?

Re-train a deep learning by adding some more images to dataset

I have a model created by training with a dataset. Now I want to re-train the model by put some more images in that dataset with out deleting the already created model. I have some images which are slightly different from the images already exists in the dataset. The colour is the only different. So I want to train the model using these new images also. So how can I train the model again using new images without deleting the alredy trained data. I use Tensorflow in Python. How can I re-train the model?
import math
import numpy as np
import tensorflow as tf
from enum import Enum, unique
#unique
class InputType(Enum):
TENSOR = 1
BASE64_JPEG = 2
class OpenNsfwModel:
"""Tensorflow implementation of Yahoo's Open NSFW Model
Original implementation:
https://github.com/yahoo/open_nsfw
Weights have been converted using caffe-tensorflow:
https://github.com/ethereon/caffe-tensorflow
"""
def __init__(self):
self.weights = {}
self.bn_epsilon = 1e-5 # Default used by Caffe
def build(self, weights_path="open_nsfw-weights.npy",
input_type=InputType.TENSOR):
self.weights = np.load(weights_path, encoding="latin1").item()
self.input_tensor = None
if input_type == InputType.TENSOR:
self.input = tf.placeholder(tf.float32,
shape=[None, 224, 224, 3],
name="input")
self.input_tensor = self.input
elif input_type == InputType.BASE64_JPEG:
from image_utils import load_base64_tensor
self.input = tf.placeholder(tf.string, shape=(None,), name="input")
self.input_tensor = load_base64_tensor(self.input)
else:
raise ValueError("invalid input type '{}'".format(input_type))
x = self.input_tensor
x = tf.pad(x, [[0, 0], [3, 3], [3, 3], [0, 0]], 'CONSTANT')
x = self.__conv2d("conv_1", x, filter_depth=64,
kernel_size=7, stride=2, padding='valid')
x = self.__batch_norm("bn_1", x)
x = tf.nn.relu(x)
x = tf.layers.max_pooling2d(x, pool_size=3, strides=2, padding='same')
x = self.__conv_block(stage=0, block=0, inputs=x,
filter_depths=[32, 32, 128],
kernel_size=3, stride=1)
x = self.__identity_block(stage=0, block=1, inputs=x,
filter_depths=[32, 32, 128], kernel_size=3)
x = self.__identity_block(stage=0, block=2, inputs=x,
filter_depths=[32, 32, 128], kernel_size=3)
x = self.__conv_block(stage=1, block=0, inputs=x,
filter_depths=[64, 64, 256],
kernel_size=3, stride=2)
x = self.__identity_block(stage=1, block=1, inputs=x,
filter_depths=[64, 64, 256], kernel_size=3)
x = self.__identity_block(stage=1, block=2, inputs=x,
filter_depths=[64, 64, 256], kernel_size=3)
x = self.__identity_block(stage=1, block=3, inputs=x,
filter_depths=[64, 64, 256], kernel_size=3)
x = self.__conv_block(stage=2, block=0, inputs=x,
filter_depths=[128, 128, 512],
kernel_size=3, stride=2)
x = self.__identity_block(stage=2, block=1, inputs=x,
filter_depths=[128, 128, 512], kernel_size=3)
x = self.__identity_block(stage=2, block=2, inputs=x,
filter_depths=[128, 128, 512], kernel_size=3)
x = self.__identity_block(stage=2, block=3, inputs=x,
filter_depths=[128, 128, 512], kernel_size=3)
x = self.__identity_block(stage=2, block=4, inputs=x,
filter_depths=[128, 128, 512], kernel_size=3)
x = self.__identity_block(stage=2, block=5, inputs=x,
filter_depths=[128, 128, 512], kernel_size=3)
x = self.__conv_block(stage=3, block=0, inputs=x,
filter_depths=[256, 256, 1024], kernel_size=3,
stride=2)
x = self.__identity_block(stage=3, block=1, inputs=x,
filter_depths=[256, 256, 1024],
kernel_size=3)
x = self.__identity_block(stage=3, block=2, inputs=x,
filter_depths=[256, 256, 1024],
kernel_size=3)
x = tf.layers.average_pooling2d(x, pool_size=7, strides=1,
padding="valid", name="pool")
x = tf.reshape(x, shape=(-1, 1024))
self.logits = self.__fully_connected(name="fc_nsfw",
inputs=x, num_outputs=2)
self.predictions = tf.nn.softmax(self.logits, name="predictions")
"""Get weights for layer with given name
"""
def __get_weights(self, layer_name, field_name):
if not layer_name in self.weights:
raise ValueError("No weights for layer named '{}' found"
.format(layer_name))
w = self.weights[layer_name]
if not field_name in w:
raise (ValueError("No entry for field '{}' in layer named '{}'"
.format(field_name, layer_name)))
return w[field_name]
"""Layer creation and weight initialization
"""
def __fully_connected(self, name, inputs, num_outputs):
return tf.layers.dense(
inputs=inputs, units=num_outputs, name=name,
kernel_initializer=tf.constant_initializer(
self.__get_weights(name, "weights"), dtype=tf.float32),
bias_initializer=tf.constant_initializer(
self.__get_weights(name, "biases"), dtype=tf.float32))
def __conv2d(self, name, inputs, filter_depth, kernel_size, stride=1,
padding="same", trainable=False):
if padding.lower() == 'same' and kernel_size > 1:
if kernel_size > 1:
oh = inputs.get_shape().as_list()[1]
h = inputs.get_shape().as_list()[1]
p = int(math.floor(((oh - 1) * stride + kernel_size - h)//2))
inputs = tf.pad(inputs,
[[0, 0], [p, p], [p, p], [0, 0]],
'CONSTANT')
else:
raise Exception('unsupported kernel size for padding: "{}"'
.format(kernel_size))
return tf.layers.conv2d(
inputs, filter_depth,
kernel_size=(kernel_size, kernel_size),
strides=(stride, stride), padding='valid',
activation=None, trainable=trainable, name=name,
kernel_initializer=tf.constant_initializer(
self.__get_weights(name, "weights"), dtype=tf.float32),
bias_initializer=tf.constant_initializer(
self.__get_weights(name, "biases"), dtype=tf.float32))
def __batch_norm(self, name, inputs, training=False):
return tf.layers.batch_normalization(
inputs, training=training, epsilon=self.bn_epsilon,
gamma_initializer=tf.constant_initializer(
self.__get_weights(name, "scale"), dtype=tf.float32),
beta_initializer=tf.constant_initializer(
self.__get_weights(name, "offset"), dtype=tf.float32),
moving_mean_initializer=tf.constant_initializer(
self.__get_weights(name, "mean"), dtype=tf.float32),
moving_variance_initializer=tf.constant_initializer(
self.__get_weights(name, "variance"), dtype=tf.float32),
name=name)
"""ResNet blocks
"""
def __conv_block(self, stage, block, inputs, filter_depths,
kernel_size=3, stride=2):
filter_depth1, filter_depth2, filter_depth3 = filter_depths
conv_name_base = "conv_stage{}_block{}_branch".format(stage, block)
bn_name_base = "bn_stage{}_block{}_branch".format(stage, block)
shortcut_name_post = "_stage{}_block{}_proj_shortcut" \
.format(stage, block)
shortcut = self.__conv2d(
name="conv{}".format(shortcut_name_post), stride=stride,
inputs=inputs, filter_depth=filter_depth3, kernel_size=1,
padding="same"
)
shortcut = self.__batch_norm("bn{}".format(shortcut_name_post),
shortcut)
x = self.__conv2d(
name="{}2a".format(conv_name_base),
inputs=inputs, filter_depth=filter_depth1, kernel_size=1,
stride=stride, padding="same",
)
x = self.__batch_norm("{}2a".format(bn_name_base), x)
x = tf.nn.relu(x)
x = self.__conv2d(
name="{}2b".format(conv_name_base),
inputs=x, filter_depth=filter_depth2, kernel_size=kernel_size,
padding="same", stride=1
)
x = self.__batch_norm("{}2b".format(bn_name_base), x)
x = tf.nn.relu(x)
x = self.__conv2d(
name="{}2c".format(conv_name_base),
inputs=x, filter_depth=filter_depth3, kernel_size=1,
padding="same", stride=1
)
x = self.__batch_norm("{}2c".format(bn_name_base), x)
x = tf.add(x, shortcut)
return tf.nn.relu(x)
def __identity_block(self, stage, block, inputs,
filter_depths, kernel_size):
filter_depth1, filter_depth2, filter_depth3 = filter_depths
conv_name_base = "conv_stage{}_block{}_branch".format(stage, block)
bn_name_base = "bn_stage{}_block{}_branch".format(stage, block)
x = self.__conv2d(
name="{}2a".format(conv_name_base),
inputs=inputs, filter_depth=filter_depth1, kernel_size=1,
stride=1, padding="same",
)
x = self.__batch_norm("{}2a".format(bn_name_base), x)
x = tf.nn.relu(x)
x = self.__conv2d(
name="{}2b".format(conv_name_base),
inputs=x, filter_depth=filter_depth2, kernel_size=kernel_size,
padding="same", stride=1
)
x = self.__batch_norm("{}2b".format(bn_name_base), x)
x = tf.nn.relu(x)
x = self.__conv2d(
name="{}2c".format(conv_name_base),
inputs=x, filter_depth=filter_depth3, kernel_size=1,
padding="same", stride=1
)
x = self.__batch_norm("{}2c".format(bn_name_base), x)
x = tf.add(x, inputs)
return tf.nn.relu(x)
You can save your trained model. When you get new images in your dataset, reload the saved model and continue training from that point onwards.
Refer Tensorflow docs: here
Check out SO how to save and restore a model in tensorflow

Categories