<jupyter_start><jupyter_text>Codes<jupyter_code># %cd ./CURL/

import numpy as np
from PIL import Image
import sys
from util import ImageProcessing

import torch
import torchvision.transforms.functional as TF
import util

import requests
from io import BytesIO
import model

np.set_printoptions(threshold=sys.maxsize)

import matplotlib.pyplot as plt
%matplotlib inline


checkpoint_filepath = "./pretrained_models/adobe_dpe/curl_validpsnr_23.073045286204017_validloss_0.0701291635632515_testpsnr_23.584083321292365_testloss_0.061363041400909424_epoch_510_model.pt"
inference_img_dirpath = "./adobe5k_dpe"
log_dirpath = './'

DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'

net = model.CURLNet()
checkpoint = torch.load(checkpoint_filepath, map_location=DEVICE)
net.load_state_dict(checkpoint['model_state_dict'])

net.eval()
if DEVICE == 'cuda':
  net.cuda()


def load_image(path):
    '''
    '''
    if ("https" in path) or ("http" in path):
      image = Image.open(BytesIO(requests.get(path).content))

    else:
      image = Image.open(path)


    if image.mode != 'RGB':
      image = image.convert('RGB')
               
    return TF.to_tensor(image).to(DEVICE)


def evaluate(img, convert_uint = False):
    """
    """
    # img = load_image(img)

    with torch.no_grad():

        # img = img.unsqueeze(0)
        img = torch.clamp(img, 0, 1)

        net_output_img_example , _ = net(img)

        net_output_img_example_numpy = net_output_img_example.squeeze(0).data.cpu().numpy()
        net_output_img_example_numpy = ImageProcessing.swapimdims_3HW_HW3(net_output_img_example_numpy)
        return (net_output_img_example_numpy* 255).astype(np.uint8) if convert_uint else net_output_img_example_numpy

        
img = torch.randn(1,3,1920,1080).to(DEVICE)
import os


paths = ["./image/"+i for i in os.listdir("./image/") if i.endswith(('jpeg', 'png', 'jpg'))]

path = "./adobe5k_dpe/curl_example_test_input/a2803-060810_075208_GM6A0020_input.png"
result = evaluate(path, convert_uint = False)

f, ax = plt.subplots(1,2, figsize = (20,10))
ax[0].imshow(np.array(Image.open(path)))
ax[1].imshow(result)
url = "https://2.img-dpreview.com/files/p/TS560x560~forums/62902641/8ec07adb32b04f5dababb8f24bf10fb3"
result = evaluate(url, convert_uint = False)

f, ax = plt.subplots(1,2, figsize = (20,10))
ax[0].imshow(np.array(Image.open(BytesIO(requests.get(url).content))))
ax[1].imshow(result)
!zip -r myfiles.zip ./image<jupyter_output>adding: image/ (stored 0%)
  adding: image/PE22P_K102U05_8Q1204_K09_00.jpg (deflated 6%)
  adding: image/8906032990369-Angle_1.jpg (deflated 26%)
  adding: image/IMG_20201224_145728__front__3CJ-78.jpg (deflated 1%)
  adding: image/8906032911623-Angle_1.jpg (deflated 31%)
  adding: image/8906032910091-Angle_2.jpg (deflated 27%)
  adding: image/PE22P_K103U48_1K1204_K02_00.jpg (deflated 5%)
  adding: image/PE22P_K303U25_0F1204_103_00.jpg (deflated 5%)
  adding: image/IMG_20210803_233050__20__DKS.png (deflated 3%)
  adding: image/IMG_20210803_232044__45__Chaap.png (deflated 1%)
  adding: image/8908000295902-101220669-Pillow_Packs_Front.jpg (deflated 34%)
  adding: image/8908013841080-Angle_2.jpg (deflated 27%)
  adding: image/PE22P_K303U25_0F1204_102_00.jpg (deflated 4%)
  adding: image/PE22P_K403U28_1K1204_A24_00.jpg (deflated 4%)
  adding: image/PE22P_K102U05_8Q1204_G35_00.jpg (deflated 3%)
  adding: image/8906032910497-Angle_1.jpg (deflated 36%)
  adding: image/PE22P_K403U28_1K1204_K0[...]
{'fraction_non_alphanumeric': 0.1263005780346821, 'fraction_numerical': 0.12572254335260116, 'mean_word_length': 4.946735395189004, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '333937', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1399, 'n_words': 220, 'path': 'Image_Enhancement_CURL.ipynb', 'repo_name': 'deshwalmahesh/CURL---cpu-gpu', 'size': '3460'}