Problem
def a(...):
model = b(...)
I running a(...) but model is not defined.
the b(...) looks like:
def b(...):
...
model=...
...
return model
My question: What is my problem called in python? So I can solve it. Something like global/local, or nested function, recursive, static, calling functions inside function, or declaring/instantiating/ initializing/ assigning from another function?
Below is the same question but with my real code because i had google it, so I maybe need help for my concrete case.
What I run:
start_parameter_searching(lrList, momentumList, wdList )
Function:
def start_parameter_searching(lrList, wdList, momentumList):
for i in lrList:
for k in momentumList:
for j in wdListt:
set_train_validation_function(i, k, j)
trainFunction()
lrList = [0.001, 0.01, 0.1]
wdList = [0.001, 0.01, 0.1]
momentumList = [0.001, 0.01, 0.1]
Error
NameError Traceback (most recent call last)
<ipython-input-20-1d7a642788ca> in <module>()
----> 1 start_parameter_searching(lrList, momentumList, wdList)
1 frames
<ipython-input-17-cd25561c1705> in trainFunction()
10 for epoch in range(num_epochs):
11 # train for one epoch, printing every 10 iterations
---> 12 _, loss = train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
13 # update the learning rate
14 lr_scheduler.step()
NameError: name 'model' is not defined
Problem
I run def set_train_validation_function(i, k, j):
in my def start_parameter_searching(lrList, wdList, momentumList):
Inside def set_train_validation_function(i, k, j):
I have model = get_instance_segmentation_model(num_classes)
and the model is not defined.
The get_instance_segmentation_model(num_classes)
is probably not being called/declared/instatiated again. The function is also inside another function.
Everything put together in a pseudo code file
def set_train_validation_function(i, k, j):
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
# our dataset has two classes only - background and person
num_classes = 2
# get the model using our helper function
model = get_instance_segmentation_model(num_classes)
# move model to the right device
model.to(device)
# construct an optimizer
params = [p for p in model.parameters() if p.requires_grad]
optimizer = torch.optim.SGD(params, lr=i,
momentum=k, weight_decay=j)
# and a learning rate scheduler which decreases the learning rate by
# 10x every 3 epochs
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
step_size=3,
gamma=0.1)
def start_parameter_searching(lrList, wdList, momentumList):
for i in lrList:
for k in momentumList:
for j in wdListt:
set_train_validation_function(i, k, j)
trainFunction()
lrList = [0.001, 0.01, 0.1]
wdList = [0.001, 0.01, 0.1]
momentumList = [0.001, 0.01, 0.1]
#start training
start_parameter_searching(lrList, momentumList, wdList )
and from the problem with model = get_instance_segmentation_model(num_classes)
def get_instance_segmentation_model(num_classes):
# load an instance segmentation model pre-trained on COCO
model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)
# get the number of input features for the classifier
in_features = model.roi_heads.box_predictor.cls_score.in_features
# replace the pre-trained head with a new one
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
# now get the number of input features for the mask classifier
in_features_mask = model.roi_heads.mask_predictor.conv5_mask.in_channels
hidden_layer = 256
# and replace the mask predictor with a new one
model.roi_heads.mask_predictor = MaskRCNNPredictor(in_features_mask,
hidden_layer,
num_classes)
return model
It sounds like you are not returning model
and passing it on.
Did you mean:
model = set_train_validation_function(i, k, j)
trainFunction(model)
This will mean that def set_train_validation_function(...):
will need to return model
and then you will need def trainFunction(model):