import os import argparse # import cv2 ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", type=str, default="", help="path to input image file") args = vars(ap.parse_args()) images = args["image"] # image_path_output = '/Users/sukshi/Downloads/t_r' rdn = RDN(arch_params={'C': 6, 'D': 20, 'G': 64, 'G0': 64, 'x': 2}) rdn.model.load_weights( 'weights/sample_weights/rdn-C6-D20-G64-G064-x2/ArtefactCancelling/rdn-C6-D20-G64-G064-x2_ArtefactCancelling_epoch219.hdf5' ) for fl in os.listdir(images): #print(fl) if fl == ".DS_Store" or fl == "_DS_Store": #print(fl) print("stupid files") else: try: images2 = os.path.join(images, fl) img = Image.open(str(images2)) lr_img = np.array(img) fl2 = fl.split(".")[0] sr_img = rdn.predict(lr_img)
owd = os.getcwdb() os.chdir(r'image-super-resolution') get_ipython().system('python setup.py install') os.chdir(owd) # In[7]: img = Image.open('team_reduced.jpg') lr_img = np.array(img) # In[8]: model = RDN(weights='noise-cancel') def upscale(img): sr_img = model.predict(np.array(img)) return Image.fromarray(sr_img) # In[ ]:
import isr import sys import os from os.path import join import numpy as np import glob from PIL import Image from ISR.models import RDN, RRDN model_list = [ RDN(weights='psnr-small'), # RDN(weights='psnr-large'), # '', # '', # RDN(weights='noise-cancel'), # RRDN(weights='gans'), ] dataset_list = [ # join('data', 'input', 'chest_xray', 'test', 'NORMAL'), # join('data', 'input', 'chest_xray', 'test', 'PNEUMONIA'), # join('data', 'input', 'chest_xray', 'train', 'NORMAL'), # join('data', 'input', 'chest_xray', 'train', 'PNEUMONIA'), # join('data', 'input', 'chest_xray', 'val', 'NORMAL'), join('data', 'input', 'chest_xray', 'val', 'PNEUMONIA'), ] result_dir = 'results/chest_xray' resize_list = [ 256,
def __init__(self, config): #self.model = RRDN(weights='gans') self.model = RDN(weights='noise-cancel')
tock = datetime.now() print( f"[info] complete, time elapsed: {(tock - tick).total_seconds():.1f}s.\n" ) for image in tqdm(images): enhanced = m2.predict(image) # enhanced = m2.predict(enhanced, by_patch_of_size=500) yield enhanced if __name__ == '__main__': print('[info] initializing models...') tick = datetime.now() # m1 = RRDN(weights='gans') m1 = RDN(weights='psnr-large') m2 = RDN(weights='noise-cancel') tock = datetime.now() print( f"[info] complete, time elapsed: {(tock - tick).total_seconds():.1f}s.\n" ) index = 497 folder = f'c{index}' pattern = os.path.join('./exports', folder, '*.jpg') image_paths = glob(pattern) images = [cv2.imread(p) for p in image_paths] for i, origin in enumerate(images): e1 = m1.predict(origin) e2 = m2.predict(e1, by_patch_of_size=400) show_image(origin, e1, e2, col=2, width=1200)
import numpy as np from PIL import Image img = Image.open('/home/user/env2/GAN_hack/biggan/beach1.png') lr_img = np.array(img) from ISR.models import RDN rdn = RDN(weights='psnr-small') sr_img = rdn.predict(lr_img) sr = Image.fromarray(sr_img) sr.show() sr = sr.save("beach1_sr.jpg")
import numpy as np from os.path import expanduser from PIL import Image from ISR.models import RDN, RRDN # RDN: psnr-large, psnr-small, noise-cancel # RRDN: gans model = RDN(weights='psnr-large') img = Image.open(expanduser('/home/minhhoang/image-super-resolution/data/input/sample/test.jpg')) lr_img = np.array(img) sr_img = model.predict(lr_img,) output = Image.fromarray(sr_img) output.save(expanduser('data/test_psnr-large.png'))
import numpy as np from PIL import Image from ISR.models import RDN import matplotlib.pyplot as plt from skimage import io path = './usage/tar_images/IMG_1006.jpg' img = Image.open(path) lr_img = np.array(img) rdn = RDN(weights='noise-cancel') sr_img = rdn.predict(lr_img) lr_img = np.array(img.resize((1024, 1024))) f, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.imshow(lr_img, cmap='gray') ax2.imshow(sr_img, cmap='gray') plt.show() io.imsave(path, lr_img)
from ISR.models import RDN from ISR.models import Discriminator from ISR.models import Cut_VGG19 from ISR.train import Trainer lr_train_patch_size = 40 layers_to_extract = [5, 9] scale = 2 hr_train_patch_size = lr_train_patch_size * scale rdn = RDN(arch_params={ 'C': 4, 'D': 3, 'G': 64, 'G0': 64, 'x': scale }, patch_size=lr_train_patch_size) f_ext = Cut_VGG19(patch_size=hr_train_patch_size, layers_to_extract=layers_to_extract) discr = Discriminator(patch_size=hr_train_patch_size, kernel_size=3) loss_weights = { 'generator': 0.0, 'feature_extractor': 0.0833, 'discriminator': 0.01 } losses = { 'generator': 'mae', 'feature_extractor': 'mse', 'discriminator': 'binary_crossentropy'
# Michael Langford # 1/4/20 # simple script for superresolution import numpy as np from PIL import Image from ISR.models import RDN import scipy.misc import os rdn = RDN(arch_params={'C': 3, 'D': 10, 'G': 64, 'G0': 64, 'x': 2}) rdn.model.load_weights('weights/rdn-C3-D10-G64-G064-x2_PSNR_epoch134.hdf5') # just going to name them by the order they were given in ugh i = 0 # loop through all the files in the images folder for file in os.listdir('data/input/images'): if file == '.DS_Store': continue i += 1 img = Image.open(os.path.join('data/input/images', file)) lr_img = np.array(img) print('Predicting on {}'.format(file)) sr_img = rdn.predict(lr_img, by_patch_of_size=2) scipy.misc.imsave('results/{}.png'.format(i), sr_img)
with open('G:/Deecamp/STGAN_ZW/STGAN/makeup/7.png', 'rb') as f: a8 = base64.b64encode(f.read()).decode() with open('history.jpg', 'rb') as f: history = base64.b64encode(f.read()).decode() with open('marval.jpg', 'rb') as f: marval = base64.b64encode(f.read()).decode() with open('springFastival.jpg', 'rb') as f: springFestival = base64.b64encode(f.read()).decode() # 全局化 # keras可能会出现的bug:在第一次predict的时候有问题,这里初始化后,选取一张图片让model执行一次predict,后面predict就不会报错了 cgclr = changeFace() stgan = demo2.STGAN() cgstyle = changestyle() rdn = RDN(arch_params={'C': 6, 'D': 20, 'G': 64, 'G0': 64, 'x': 2}) rdn.model.load_weights( 'G:/Deecamp/STGAN_ZW/STGAN/ImageSuperResolution/ArtefactCancelling/rdn-C6-D20-G64-G064-x2_ArtefactCancelling_epoch219.hdf') img = np.array(Image.open('G:/Deecamp/STGAN_ZW/STGAN/ImageSuperResolution/image/20121.jpg')) rdn.predict(img) cgclr.getMask(img, 'skin') im = cv2.imread("G:/Deecamp/STGAN_ZW/STGAN/transform_folder/fj2.png") body = cgstyle.inference(im, 1) print("init successfully") @app.route('/Hello') def hello_world(): return "Hello"
else: import pickle from cachelib import SimpleCache cache = SimpleCache() cfg_from_file(r'E:\Projects\digital_writer\AttnGAN\code\cfg\eval_coco.yml') # otherwise it allocates all memory and no memory is left for pytorch gpu_devices = tf.config.experimental.list_physical_devices('GPU') tf.config.experimental.set_memory_growth(gpu_devices[0], True) config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True session = tf.compat.v1.Session(config=config) rdn = RDN(weights='psnr-large') # rdn = RDN(weights='psnr-small') def vectorize_caption(wordtoix, caption, copies=2): # create caption vector tokens = caption.split(' ') cap_v = [] for t in tokens: t = t.strip().encode('ascii', 'ignore').decode('ascii') if len(t) > 0 and t in wordtoix: cap_v.append(wordtoix[t]) # expected state for single generation captions = np.zeros((copies, len(cap_v))) for i in range(copies):
def plot_saliency_map(image_path, model_path, output_class_index, custom_preprocessing=None, sr=True): loaded_image = load_image(image_path) orig_image_size = loaded_image.size if not model_path: print('Loading Mobilenet pretrained on imagenet dataset ....') from tensorflow.keras.applications import MobileNet cnn = MobileNet() inp_to_cnn = mobilenet_preprocess(inp_image=loaded_image) else: from tensorflow.keras.models import load_model # Assumption: model in h5 cnn = load_model(model_path) inp_to_cnn = custom_preprocessing( inp_image=loaded_image ) #ToDo: Error handling for absence of custom_preprocessing function inp_tensor = tf.constant(inp_to_cnn) cnn_model = models.Model([cnn.inputs], [cnn.output]) grad = _get_grads_wrt_input(cnn_model=cnn_model, inp_tensor=inp_tensor, output_class_index=output_class_index) if sr: inp_size = tuple(inp_tensor.shape[1:]) # 0-th index is for batch width_upsample_factor = int(np.floor(orig_image_size[0] / inp_size[0])) height_upsample_factor = int(np.floor(orig_image_size[1] / inp_size[1])) upsampling_factor = min(width_upsample_factor, height_upsample_factor) if upsampling_factor != 0: from ISR.models import RDN rdn = RDN( arch_params={ 'C': 6, 'D': 20, 'G': 64, 'G0': 64, 'x': upsampling_factor }, weights='psnr-small' ) # 'x' is the upsampling factor. Other values are required to load # pre-trained model img_grad = preprocessing.image.array_to_img(grad[0]) lr_img = np.array(img_grad) sr_img = rdn.predict(lr_img) full_size_salmap = tf.image.resize( sr_img, orig_image_size ) # Since upsampling factor may not fit to exact # original image dimensions sal_image = preprocessing.image.array_to_img(full_size_salmap) else: full_size_grad = tf.image.resize(grad, orig_image_size)[0] sal_image = preprocessing.image.array_to_img(full_size_grad) map_plotter(grad_image=sal_image, original_image=loaded_image)
""" This file is based off this project: https://github.com/idealo/image-super-resolution#installation Since we don't have direct video upscaling, we convert a video into a list of images, and predict each one, then merge them back into a video """ import numpy as np from PIL import Image from ISR.models import RDN import sys sys.path.append('..') #Set model - One of [noise-cancel, psnr-small, psnr-large] #There's also a 'gan' model based off ESRGAN. Import RRDN to use it model_name = "noise-cancel" #this seems to work the best model = RDN(weights=model_name) #Upscales an image 2^scale times def predict(array, scale=1): pred = model.predict(array) for i in range(scale - 1): pred = model.predict(np.array(pred)) return pred
import numpy as np from PIL import Image from ISR.models import RDN #rdn = RDN(weights='psnr-small') rdn = RDN(weights='noise-cancel') def upsample(filename_in, filename_out): img = Image.open(filename_in) print(img) lr_img = np.array(img) sr_img = rdn.predict(lr_img) #sr_img = rdn.predict(lr_img, by_patch_of_size=50) res = Image.fromarray(sr_img) res.save(filename_out) img_list = [ 83, 177, 181, 185, 207, 289, 314, 317, 366, 367, 368, 369, 370, 371, 381, 387, 389, 392, 393, 394, 396, 400, 430, 542, 543, 544, 545, 636, 637, 638, 640, 643, 646, 667, 669, 670, 750, 751, 753, 775, 776, 802, 829, 843, 845, 848, 850, 851, 854, 856, 889, 890, 891, 892, 914, 916, 918, 922, 1002, 1005, 1098, 1104, 1105, 1106, 1108, 1109, 1128, 1129, 1130, 1133, 1213, 1216, 1217, 1240, 1290, 1294, 1295, 1302, 1309, 1310, 1314, 1315, 1316, 1319, 1320, 1352, 1375, 1379, 1381 ] for id in img_list: upsample('datasets/room1_us/frames/ZMojNkEp431/color/' + str(id) + '.jpg', 'datasets/room1_us/images/' + str(id) + '.jpg')
img = Image.open( io.BytesIO(file_byte) ) # https://stackoverflow.com/questions/18491416/pil-convert-bytearray-to-image sr_img = rdn.predict( np.array(img)[:100, :100, :3]) # crop to standardize input. output_str += 'overall time: {}\n'.format(time.time() - entry_time) output_str += 'output image size: {} Bytes\n'.format(len(sr_img)) return web.Response(text="%s" % output_str) app = web.Application() # model prepare rdn = RDN(arch_params={ 'C': 3, 'D': 10, 'G': 64, 'G0': 64, 'x': 2 }) # infer: 4.16 s rdn.model.load_weights( '/data/weights/rdn-C3-D10-G64-G064-x2_PSNR_epoch134.hdf5' ) # tensorflow as backend app['rdn'] = rdn # input prepare image_weblink = "https://s3.amazonaws.com/model-server/inputs/dogs-before.png" # <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=758x520 at 0x7FDBDCFA9E48> r = requests.get(image_weblink) file_byte = r.content redis_number = int(os.getenv('NUM_REDIS', 0)) self_port = int(os.getenv('SELF_PORT', 0))
global upload_directory, result_directory global model_directory global rdn result_directory = '/src/results/' create_directory(result_directory) upload_directory = '/src/upload/' create_directory(upload_directory) model_directory = '/src/weights/' create_directory(model_directory) url_prefix = 'http://pretrained-models.auth-18b62333a540498882ff446ab602528b.storage.gra5.cloud.ovh.net/image/' model_file_rar = 'weights.rar' get_model_bin(url_prefix + 'super-resolution/' + model_file_rar , os.path.join('/src', model_file_rar)) unrar(model_file_rar, '/src') rdn = RDN(arch_params={'C':6, 'D':20, 'G':64, 'G0':64, 'x':2}) rdn.model.load_weights('weights/sample_weights/rdn-C6-D20-G64-G064-x2/ArtefactCancelling/rdn-C6-D20-G64-G064-x2_ArtefactCancelling_epoch219.hdf5') port = 5000 host = '0.0.0.0' app.run(host=host, port=port, threaded=False)
) args = parser.parse_args() # Handle user-specified input file inputfile = str(args.inputFile) file_root, file_ext = os.path.splitext(inputfile) print("Input file is {}".format(inputfile)) img = Image.open(inputfile) lr_img = np.array(img) # Handle user-specified model if args.model == 1: model = RRDN(weights='gans') tagout = '_gan' elif args.model == 2: model = RDN(weights='psnr-large') tagout = '_psnr-large' elif args.model == 3: model = RDN(weights='psnr-small') tagout = '_psnr-small' elif args.model == 4: model = RDN(weights='noise-cancel') tagout = '_denoise' else: print("Couldn't resolve model choice to valid value") # Enlarge the image using ISR print("Enlarging {} using {} now\n".format(args.inputFile, tagout)) # Handle patch size selection patchsize = args.patchSize if patchsize == 0:
import io import base64 import numpy as np from PIL import Image from ISR.models import RDN from message_protocol.upscale_input import UpscaleInput from message_protocol.upscale_output import UpscaleOutput MODEL = RDN(weights='noise-cancel') class ResourceUpscale: def main(self, upscaleInput: UpscaleInput) -> UpscaleOutput: # Parse base64 string into bytes array inputImageBytesArray = base64.b64decode(upscaleInput.imageBase64) # Open the image imagePil = Image.open(io.BytesIO(inputImageBytesArray)) imageNumpy = np.array(imagePil) # Scale the image scaledNumpy = MODEL.predict(imageNumpy) scaledPil = Image.fromarray(scaledNumpy) # Write scaled image as bytes array outputImageBytesArrayIO = io.BytesIO() scaledPil.save(outputImageBytesArrayIO, format=imagePil.format, quality=100) outputImageBytesArray = outputImageBytesArrayIO.getvalue()
#Unzip and putting data into dictionary !mkdir div2k !unzip -q DIV2K_valid_LR_bicubic_X2.zip -d div2k !unzip -q DIV2K_train_LR_bicubic_X2.zip -d div2k !unzip -q DIV2K_train_HR.zip -d div2k !unzip -q DIV2K_valid_HR.zip -d div2k #loading the model from ISR.models import RRDN, RDN from ISR.models import Discriminator from ISR.models import Cut_VGG19 model1 = RDN(weights='noise-cancel') model = RRDN(weights='gans') model2 = RDN(weights='psnr-small') model3 = RDN(weights='psnr-large') """ rdn = RDN(arch_params={'C': 5, 'D':16, 'G':48, 'G0':52, 'x':3}) rdn.model.load_weights('PATH/TO/WEIGHTS') """ #setting the perameters lr_train_patch_size = 40 layers_to_extract = [5, 9] scale = 2 hr_train_patch_size = lr_train_patch_size * scale
'png,jpg,jpeg').split(",") ALLOWED_EXTENSIONS = set(ALLOWED_EXTENSIONS) result_directory = os.environ.get("RESULTS_PATH", '/src/results/') create_directory(result_directory) upload_directory = os.environ.get("UPLOADS_PATH", '/src/uploads/') create_directory(upload_directory) model_directory = os.environ.get("WEIGHTS_PATH", '/src/weights/') create_directory(model_directory) rdn = RDN(weights='noise-cancel', arch_params={ 'C': 6, 'D': 20, 'G': 64, 'G0': 64, 'x': 1 }) rrdn = RRDN(weights="gans", arch_params={ 'C': 4, 'D': 3, 'G': 32, 'G0': 32, 'x': 1, 'T': 10 }) if check_cuda: print("- cuda: ", tf.test.is_built_with_cuda())
from PIL import Image from ISR.models import RDN, RRDN # trigger a download of the models rdn = RDN(weights='psnr-small') rdn = RDN(weights='psnr-large') rdn = RDN(weights='noise-cancel') rrdn = RRDN(weights='gans')