I have two virtually identical scripts running in the same PyCharm IDE. They both call into a third script that uses matplotlib to output a Numpy array to a PNG. One of the scripts works fine and outputs the PNG. The other script raises the following error:
qt.qpa.plugin: Could not load the Qt platform plugin "windows" in "" even though it was found.
The differences between the scripts are minimal - they only vary in that they each import a different pytorch model (both models created by me). This leads me to think there might be some sort of environmental difference, but they are both launched from the same IDE and Conda environment.
Does anyone have any suggestions?
EDIT:
Here is a stripped down version of the two files. The bits I have removed are identical to each other down to the character and whitespace.
File 1:
import argparse
from average_meter import AverageMeter
import criteria # -> NOTE: This is not used, but was not removed in this file
import dataset
from densenet_mapmaker_model import DensenetMapMakerModel # -> Different model imported
import json
import math
import os
import time
import torch
from torch.autograd import Variable
import torch.nn as nn
from torchvision import transforms
import visualizer
parser = argparse.ArgumentParser(description='Densenet Map Maker Trainer for Steel')
# ...Define args...
##########################################################################################
# These args were defined here, but not in the other file. They are not actually used here, just defined and ignored.
parser.add_argument('--shrinkage_coef', dest='shrinkage_coef', type=int,
help='Use a shrinking gaussian loss and multiply its result by this amount.')
parser.add_argument('--multiply', dest='multiply', type=int,
help='If 1, in the shrinking gaussian loss, ' +
'multiply by 1000 before comparing and then divide the loss by 1000.')
parser.add_argument('--diff_coef', dest='diff_coef', type=int,
help='Use the count difference loss and multiply its result by this amount.')
parser.add_argument('--clustering_coef', dest='clustering_coef', type=int,
help='Use the clustering loss and multiply its result by this amount.')
##########################################################################################
use_cuda = torch.cuda.is_available()
def main():
best_mse = 1e6
version = 1
args = parser.parse_args()
# ...set up default arg values...
args.loss_calc_method = 'DenseNet' # Defined here, but not in the other file. Not used here and could be removed.
# ...load dataset lists...
model = DensenetMapMakerModel() # -> Note: Different model used
if use_cuda:
model = model.cuda()
# ...Create criteria and optimizer...
# ...Load weights from last training run...
for epoch in range(args.start_epoch, args.epochs):
train(train_list, model, criterion, optimizer, epoch, args)
mse = validate(val_list, model, criterion, epoch, args)
# ...Check result and save weights...
def train(...):
# ...Set up counters and dataset...
model.train()
end = time.time()
# img: torch.Tensor # Image data
# location : torch.Tensor. Target annotation locations for the image.
# image_id : str. Unique ID of image.
for i, (img, location, image_id) in enumerate(train_loader):
# ...Run input through model, check result and backpropagate...
if i % args.print_freq == 0:
# Note: Print statement has different whitespace formatting from other file...
print('Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
.format(
epoch, i, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=losses))
def validate(...) -> float:
# ...Set up dataset...
# img: torch.Tensor # Image data
# location : torch.Tensor. Target annotation locations for the image.
# image_id : str. Unique ID of image.
for i, (img, location, image_id) in enumerate(test_loader):
# ...Run input through model and check result...
# Output target and source the first time...
output_dir = args.output_dir
if args.output_images == 1 and epoch % 1 == 0 and i == 2: # -> NOTE: Other file does NOT have additional check i == 2
visualizer.display_tensor(output, output_dir + "/epoch_" + str(epoch) + "_" + str(i) + "_output")
if __name__ == '__main__':
main()
File 2:
import argparse
from average_meter import AverageMeter
import dataset
from steel_scanner_model import SteelScanner # -> Different model imported
import json
import math
import os
import time
import torch
from torch.autograd import Variable
import torch.nn as nn
from torchvision import transforms
import visualizer
parser = argparse.ArgumentParser(description='Densenet Map Maker Trainer for Steel')
# ...Define args...
use_cuda = torch.cuda.is_available()
def main():
best_mse = 1e6
version = 1
args = parser.parse_args()
# ...set up default arg values...
# ...load dataset lists...
model = SteelScanner() # -> Note: Different model used
if use_cuda:
model = model.cuda()
# ...Create criteria and optimizer...
# ...Load weights from last training run...
for epoch in range(args.start_epoch, args.epochs):
train(train_list, model, criterion, optimizer, epoch, args)
mse = validate(val_list, model, criterion, epoch, args)
# ...Check result and save weights...
def train(...):
# ...Set up counters and dataset...
model.train()
end = time.time()
# img: torch.Tensor # Image data
# location : torch.Tensor. Target annotation locations for the image.
# image_id : str. Unique ID of image.
for i, (img, location, image_id) in enumerate(train_loader):
# ...Run input through model, check result and backpropagate...
if i % args.print_freq == 0:
# Note: Print statement has different whitespace formatting from other file...
print('Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
.format(epoch, i, len(train_loader), batch_time=batch_time, data_time=data_time, loss=losses))
def validate(...) -> float:
# ...Set up dataset...
# img: torch.Tensor # Image data
# location : torch.Tensor. Target annotation locations for the image.
# image_id : str. Unique ID of image.
for i, (img, location, image_id) in enumerate(test_loader):
# ...Run input through model and check result...
# Output target and source the first time...
output_dir = args.output_dir
if args.output_images == 1 and epoch % 1 == 0: # -> NOTE: Other file has additional check i == 2
# NOTE: The error occurs inside 'display_tensor'
visualizer.display_tensor(output, output_dir + "/epoch_" + str(epoch) + "_" + str(i) + "_output")
if __name__ == '__main__':
main()
The error occurs inside visualizer.display_tensor
when called from the second file.