Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Apple Silicon support #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions main_test_bsrgan.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# from utils import utils_model
from models.network_rrdbnet import RRDBNet as net


"""
Spyder (Python 3.6-3.7)
PyTorch 1.4.0-1.8.1
Expand All @@ -33,46 +32,49 @@


def main():

utils_logger.logger_info('blind_sr_log', log_path='blind_sr_log.log')
logger = logging.getLogger('blind_sr_log')

# print(torch.__version__) # pytorch version
# print(torch.version.cuda) # cuda version
# print(torch.backends.cudnn.version()) # cudnn version
# print(torch.__version__) # pytorch version
# print(torch.version.cuda) # cuda version
# print(torch.backends.cudnn.version()) # cudnn version

testsets = 'testsets' # fixed, set path of testsets
testsets = 'testsets' # fixed, set path of testsets
testset_Ls = ['RealSRSet'] # ['RealSRSet','DPED']

model_names = ['RRDB','ESRGAN','FSSR_DPED','FSSR_JPEG','RealSR_DPED','RealSR_JPEG']
model_names = ['BSRGAN'] # 'BSRGANx2' for scale factor 2


model_names = ['RRDB', 'ESRGAN', 'FSSR_DPED', 'FSSR_JPEG', 'RealSR_DPED', 'RealSR_JPEG']
model_names = ['BSRGAN'] # 'BSRGANx2' for scale factor 2

save_results = True
sf = 4
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if torch.has_mps:
device = torch.device('mps')
else:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

for model_name in model_names:
if model_name in ['BSRGANx2']:
sf = 2
model_path = os.path.join('model_zoo', model_name+'.pth') # set model path
model_path = os.path.join('model_zoo', model_name + '.pth') # set model path
logger.info('{:>16s} : {:s}'.format('Model Name', model_name))

# torch.cuda.set_device(0) # set GPU ID
logger.info('{:>16s} : {:<d}'.format('GPU ID', torch.cuda.current_device()))
torch.cuda.empty_cache()
try:
logger.info('{:>16s} : {:<d}'.format('GPU ID', torch.cuda.current_device()))
torch.cuda.empty_cache()
except:
logger.info('{:>16s} : {:<s}'.format('GPU ID', 'None'))

# --------------------------------
# define network and load model
# --------------------------------
model = net(in_nc=3, out_nc=3, nf=64, nb=23, gc=32, sf=sf) # define network

# model_old = torch.load(model_path)
# state_dict = model.state_dict()
# for ((key, param),(key2, param2)) in zip(model_old.items(), state_dict.items()):
# state_dict[key2] = param
# model.load_state_dict(state_dict, strict=True)
# model_old = torch.load(model_path)
# state_dict = model.state_dict()
# for ((key, param),(key2, param2)) in zip(model_old.items(), state_dict.items()):
# state_dict[key2] = param
# model.load_state_dict(state_dict, strict=True)

model.load_state_dict(torch.load(model_path), strict=True)
model.eval()
Expand All @@ -84,8 +86,8 @@ def main():
for testset_L in testset_Ls:

L_path = os.path.join(testsets, testset_L)
#E_path = os.path.join(testsets, testset_L+'_'+model_name)
E_path = os.path.join(testsets, testset_L+'_results_x'+str(sf))
# E_path = os.path.join(testsets, testset_L+'_'+model_name)
E_path = os.path.join(testsets, testset_L + '_results_x' + str(sf))
util.mkdir(E_path)

logger.info('{:>16s} : {:s}'.format('Input Path', L_path))
Expand All @@ -99,7 +101,7 @@ def main():
# --------------------------------
idx += 1
img_name, ext = os.path.splitext(os.path.basename(img))
logger.info('{:->4d} --> {:<s} --> x{:<d}--> {:<s}'.format(idx, model_name, sf, img_name+ext))
logger.info('{:->4d} --> {:<s} --> x{:<d}--> {:<s}'.format(idx, model_name, sf, img_name + ext))

img_L = util.imread_uint(img, n_channels=3)
img_L = util.uint2tensor4(img_L)
Expand All @@ -115,9 +117,8 @@ def main():
# --------------------------------
img_E = util.tensor2uint(img_E)
if save_results:
util.imsave(img_E, os.path.join(E_path, img_name+'_'+model_name+'.png'))
util.imsave(img_E, os.path.join(E_path, img_name + '_' + model_name + '.png'))


if __name__ == '__main__':

main()