Open tejukasula opened 2 years ago
first you have to run the train_cnn.py but in that aslo you will find some errors so i myself correct the error and fixed it i hereby pasting the code for train_cnn.py you can run this and let me know if you facing any error and most importantly this code uses the frozen modules to deactivate that and run run this commad "python -Xno_frozen_main train_cnn.py" Code: import os import torch import torch.optim as optim from torch.utils.data import DataLoader from functions import overlapScore from cnn_model import from training_dataset import
def train_model(net, dataloader, batchSize, lr_rate, momentum, optimizer, scheduler): criterion = nn.MSELoss()
for epoch in range(10):
for i, data in enumerate(dataloader):
optimizer.zero_grad()
inputs, labels = data
inputs, labels = inputs.view(batchSize, 1, 100, 100), labels.view(batchSize, 4)
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
pbox = outputs.detach().numpy()
gbox = labels.detach().numpy()
score, _ = overlapScore(pbox, gbox)
print('[epoch %5d, step: %d, loss: %f, Average Score = %f' % (epoch+1, i+1, loss.item(), score/batchSize))
scheduler.step() # Move this line here to update learning rate after each epoch
print('Finish Training')
if name == 'main':
learning_rate = 0.000001
momentum = 0.9
batch = 100
no_of_workers = 2
shuffle = True
trainingdataset = training_dataset()
dataLoader = DataLoader(
dataset=trainingdataset,
batch_size=batch,
shuffle=shuffle,
num_workers=no_of_workers
)
model = cnn_model()
model.train()
optimizer = optim.SGD(model.parameters(), lr=learning_rate, momentum=momentum)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1)
train_model(model, dataLoader, batch, learning_rate, momentum, optimizer, scheduler)
# Ensure the directory exists before saving the model
model_directory = './Model/'
os.makedirs(model_directory, exist_ok=True) # This line creates the directory if it does not exist
torch.save(model.state_dict(), os.path.join(model_directory, 'cnn_model.pth'))
FileNotFoundError Traceback (most recent call last) in
16 model = cnn_model()
17 model.eval()
---> 18 model.load_state_dict(torch.load('Model/cnn_model.pth'))
19
20
2 frames /usr/local/lib/python3.7/dist-packages/torch/serialization.py in init(self, name, mode) 209 class _open_file(_opener): 210 def init(self, name, mode): --> 211 super(_open_file, self).init(open(name, mode)) 212 213 def exit(self, *args):
FileNotFoundError: [Errno 2] No such file or directory: 'Model/cnn_model.pth'