Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
This is basically a Super Resolution Generative Adversarial Network (SRGAN) with the purpose of upscaling image resolutions by a factor of two using deep learning. This way, a picture which initially appears pixellated and/or blurry can be modified so that the features are quite more distinguishable. The model is trained on the COCO unlabeled2017 dataset. Download [here](http://cocodataset.org/#download).

## Requirements
- Tensorflow 2.0
- Scipy, Numpy
- PIL
- Matplotlib
- MS COCO unlabeled2017 Dataset (for training)

## Usage
To train model (which we highly reccomend doing some more):
```
python srgan.py
```
To run the model on an image:
```
python srgan.py -p image.jpg
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import scipy.misc
from glob import glob
import numpy as np
import matplotlib.pyplot as plt

class DataLoader():
def __init__(self, dataset_name, img_res=(200, 200)):
self.dataset_name = dataset_name
self.img_res = img_res
def load_data(self, batch_size=1, is_testing=False):
data_type = "train" if not is_testing else "test"

path = glob('../../datasets/%s/*' % (self.dataset_name))

batch_images = np.random.choice(path, size=batch_size)

imgs_hr = []
imgs_lr = []
for img_path in batch_images:
img = self.imread(img_path)

h, w = self.img_res
low_h, low_w = int(h / 2), int(w / 2)

img_hr = scipy.misc.imresize(img, self.img_res)
img_lr = scipy.misc.imresize(img, (low_h, low_w))

# If training => do random flip
if not is_testing and np.random.random() < 0.5:
img_hr = np.fliplr(img_hr)
img_lr = np.fliplr(img_lr)

imgs_hr.append(img_hr)
imgs_lr.append(img_lr)

imgs_hr = np.array(imgs_hr) / 127.5 - 1.
imgs_lr = np.array(imgs_lr) / 127.5 - 1.
return imgs_hr, imgs_lr
def load_pred(self, path):
img = self.imread(path)
imgs_hr = []
imgs_lr = []
h, w = self.img_res
low_h, low_w = int(h / 2), int(w / 2)
img_hr = scipy.misc.imresize(img, (self.img_res))
img_lr = scipy.misc.imresize(img, (low_h, low_w))
imgs_hr.append(img_hr)
imgs_lr.append(img_lr)
imgs_hr = np.array(imgs_hr) / 127.5 - 1.
imgs_lr = np.array(imgs_lr) / 127.5 - 1.
return imgs_hr, imgs_lr

def load_resize(self, path):
img = self.imread(path)
imgs_hr = []
imgs_lr = []
h, w = self.img_res
low_h, low_w = int(h/2), int(w/2)
img_hr = scipy.misc.imresize(img, (self.img_res))
img_lr = scipy.misc.imresize(img, (img_lr))
imgs_hr = np.array(imgs_hr) / 127.5 - 1.
imgs_lr = np.array(imgs_lr) / 127.5 - 1.
imgs_hr = np.resize(imgs_hr, (-1, 400,400,3))

def imread(self, path):
return scipy.misc.imread(path, mode='RGB').astype(np.float)
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import numpy as np
import matplotlib.pyplot as plt
import os
import math
path = '../../datasets/unlabeled2017/000000002272.jpg'

class ImageSlicer(object):
def __init__(self, source, size, strides=[None, None], BATCH = False, PADDING=False):
self.source = source
self.size = size
self.strides = strides
self.BATCH = BATCH
self.PADDING = PADDING

def __read_images(self):
Images = []
image_names = sorted(os.listdir(self.source))
for im in image_names:
image = plt.imread(os.path.join(dir_path,im))
Images.append(image)
return Images

def __offset_op(self, input_length, output_length, stride):
offset = (input_length) - (stride*((input_length - output_length)//stride)+output_length)
return offset

def __padding_op(self, Image):
if self.offset_x > 0:
padding_x = self.strides[0] - self.offset_x
else:
padding_x = 0
if self.offset_y > 0:
padding_y = self.strides[1] - self.offset_y
else:
padding_y = 0
Padded_Image = np.zeros(shape=(Image.shape[0]+padding_x, Image.shape[1]+padding_y, Image.shape[2]),dtype=Image.dtype)
Padded_Image[padding_x//2:(padding_x//2)+(Image.shape[0]),padding_y//2:(padding_y//2)+Image.shape[1],:] = Image
return Padded_Image

def __convolution_op(self, Image):
start_x = 0
start_y = 0
self.n_rows = Image.shape[0]//self.strides[0] + 1
self.n_columns = Image.shape[1]//self.strides[1] + 1
# print(str(self.n_rows)+" rows")
# print(str(self.n_columns)+" columns")
small_images = []
for i in range(self.n_rows-1):
for j in range(self.n_columns-1):
new_start_x = start_x+i*self.strides[0]
new_start_y= start_y+j*self.strides[1]
small_images.append(Image[new_start_x:new_start_x+self.size[0],new_start_y:new_start_y+self.size[1],:])
return small_images

def transform(self):

if not(os.path.exists(self.source)):
raise Exception("Path does not exist!")

else:
if self.source and not(self.BATCH):
Image = plt.imread(self.source)
Images = [Image]
else:
Images = self.__read_images()

im_size = Images[0].shape
num_images = len(Images)
transformed_images = dict()
Images = np.array(Images)

if self.PADDING:

padded_images = []

if self.strides[0]==None and self.strides[1]==None:
self.strides[0] = self.size[0]
self.strides[1] = self.size[1]
self.offset_x = Images.shape[1]%self.size[0]
self.offset_y = Images.shape[2]%self.size[1]
padded_images = list(map(self.__padding_op, Images))

elif self.strides[0]==None and self.strides[1]!=None:
self.strides[0] = self.size[0]
self.offset_x = Images.shape[1]%self.size[0]
if self.strides[1] <= Images.shape[2]:
self.offset_y = self.__offset_op(Images.shape[2], self.size[1], self.strides[1])
else:
raise Exception("stride_y must be between {0} and {1}".format(1,Images.shape[2]))
padded_images = list(map(self.__padding_op, Images))

elif self.strides[0]!=None and self.strides[1]==None:
self.strides[1] = self.size[1]
self.offset_y = Images.shape[2]%self.size[1]
if self.strides[0] <=Images.shape[1]:
self.offset_x = self.__offset_op(Images.shape[1], self.size[0], self.strides[0])
else:
raise Exception("stride_x must be between {0} and {1}".format(1,Images.shape[1]))
padded_images = list(map(self.__padding_op, Images))

else:
if self.strides[0] > Images.shape[1]:
raise Exception("stride_x must be between {0} and {1}".format(1,Images.shape[1]))

elif self.strides[1] > Images.shape[2]:
raise Exception("stride_y must be between {0} and {1}".format(1,Images.shape[2]))

else:
self.offset_x = self.__offset_op(Images.shape[1], self.size[0], self.strides[0])
self.offset_y = self.__offset_op(Images.shape[2], self.size[1], self.strides[1])
padded_images = list(map(self.__padding_op, Images))

for i, Image in enumerate(padded_images):
transformed_images[str(i)] = self.__convolution_op(Image)

else:
if self.strides[0]==None and self.strides[1]==None:
self.strides[0] = self.size[0]
self.strides[1] = self.size[1]

elif self.strides[0]==None and self.strides[1]!=None:
if self.strides[1] > Images.shape[2]:
raise Exception("stride_y must be between {0} and {1}".format(1,Images.shape[2]))
self.strides[0] = self.size[0]

elif self.strides[0]!=None and self.strides[1]==None:
if self.strides[0] > Images.shape[1]:
raise Exception("stride_x must be between {0} and {1}".format(1,Images.shape[1]))
self.strides[1] = self.size[1]
else:
if self.strides[0] > Images.shape[1]:
raise Exception("stride_x must be between {0} and {1}".format(1,Images.shape[1]))
elif self.strides[1] > Images.shape[2]:
raise Exception("stride_y must be between {0} and {1}".format(1,Images.shape[2]))

for i, Image in enumerate(Images):
transformed_images[str(i)] = self.__convolution_op(Image)

return transformed_images

def save_images(self,transformed):
self.r,self.c = self.n_rows-1, self.n_columns-1
for key, val in transformed.items():
val = np.array(val, dtype=np.float64) /127.5 -1.
val = .5 * val + 0.5
return val
Loading
Loading