lambda_noobj * loss_noobj + loss_class) / batch_size total_loss += loss # backward optimizer.zero_grad() loss.backward() optimizer.step() if (i + 1) % 5 == 0: print( 'Epoch [%d/%d], Iter [%d/%d] Loss: %.4f, average_loss: %.4f' % (epoch + 1, max_epoch, i + 1, len(train_dloader), loss, total_loss / (i + 1))) num_iter += 1 model.eval() with torch.no_grad(): valid_loss = 0 test_accuracy = 0 test_num_data = 0 for x, y in test_dloader: # implement testing pipeline here x = x.to(device) y = y.to(device) logit = model(x) loss_xy, loss_wh, loss_obj, loss_noobj, loss_class = compute_loss( logit, y) loss = (lambda_coord * (loss_xy + loss_wh) + loss_obj + lambda_noobj * loss_noobj + loss_class) / batch_size valid_loss += loss
device = torch.device( 'cuda' if args.use_gpu and torch.cuda.is_available() else 'cpu') FloatTensor = torch.cuda.FloatTensor if args.use_gpu and torch.cuda.is_available( ) else torch.FloatTensor classes = utils.load_classes( args.class_path) # Extracts class labels from file # Set up model model = Yolo().to(device) if args.weights_path is not None: # Load darknet weights model.load_darknet_weights(args.weights_path) model.eval() # Set in evaluation mode # dataloader = DataLoader( # ImageFolder(args.image_folder, img_size=args.img_size), # batch_size=args.batch_size, # shuffle=False, # num_workers=args.n_cpu, # ) if not os.path.exists(args.output_path): os.makedirs(args.output_path) if not os.path.exists(args.image_folder): print('No file or directory with the name {}'.format( args.image_folder)) exit()