Commit 12038f26 authored by Meet Narendra's avatar Meet Narendra 💬

Merge branch 'DEV_meetdoshi' into 'DEV'

Dev meetdoshi merged with DEV.

See merge request !1
parents 9dfa23ae 6cece00c
*pycache*
*.pdf
*.csv
*.ipynb
*Logs*
This diff is collapsed.
import torch
import torch.nn as NN
from logger import Logger
LOGGER = Logger().logger()
LOGGER.info("Started Feature Maps")
device=torch.device( "cuda" if (torch.cuda.is_available()) else 'cpu')
LOGGER.info("Running the model cuda_available = "+str(torch.cuda.is_available()))
#Author: @meetdoshi
class FeatureMaps:
class FeatureMaps():
def __init__(self,arch="vgg19"):
'''
Init function
@params
arch: str {vgg11,vgg13,vgg16,vgg19,vgg19bn}
'''
super()
try:
self.model = torch.hub.load('pytorch/vision:v0.10.0',arch,pretrained=True)
except:
LOGGER.error("Could not load model")
except Exception as e:
LOGGER.error("Could not load model" + str(e))
return
def get_model(self):
......@@ -33,7 +37,7 @@ class FeatureMaps:
LOGGER.error("Could not fetch layer "+str(layer))
return weights
def get_fmaps(self,img,layer=[0,5,10,19,28]):
def get_fmaps_content(self,img,layer=[21]):
'''
Function which will pass the image through the model and get the respective fmaps
@params
......@@ -49,6 +53,22 @@ class FeatureMaps:
layer_num+=1
return fmaps
def get_fmaps_style(self,img,layer=[0,5,10,19,28]):
'''
Function which will pass the image through the model and get the respective fmaps
@params
img: numpy image f64
layer: list
'''
fmaps = []
layer_num = 0
for layer_i in self.model.features:
img = layer_i(img)
if layer_num in layer:
fmaps.append(img)
layer_num+=1
return fmaps
'''
if __name__ == "__main__":
fmap = FeatureMaps()
model = fmap.get_model()
......@@ -57,3 +77,4 @@ if __name__ == "__main__":
print(len(weights))
for weight in weights:
print(type(weight),weight.shape)
'''
import imageio
import os
fnames = []
newNameFolder = 'content-4_2'
path = 'styled_images/'+newNameFolder
for img in os.listdir(path):
fnames.append(os.path.join(path+"/", img))
fnames.sort()
with imageio.get_writer('gifs/'+newNameFolder+".gif", mode='I',duration = 0.2) as writer:
for fname in fnames:
image = imageio.imread(fname)
writer.append_data(image)
import logging
import os
from xml.dom.minidom import Identified
#Author: @meetdoshi
class Logger:
'''
......@@ -10,11 +11,12 @@ class Logger:
_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
def __new__(cls,*args,**kwargs):
if not cls._instance:
os.system("rm -rf Logs/")
identifier = 'content-4_2'
if not os.path.isdir("Logs/"):
os.mkdir("Logs/")
logHandler = logging.FileHandler("Logs/style_transfer.log")
logHandler = logging.FileHandler("Logs/style_transfer_"+identifier+".log")
logHandler.setFormatter(cls._formatter)
cls._logHandler = logging.getLogger("Logs/style_transfer.log")
cls._logHandler = logging.getLogger("Logs/style_transfer_"+identifier+".log")
cls._logHandler.setLevel(logging.INFO)
cls._logHandler.addHandler(logHandler)
cls._instance = super(Logger, cls).__new__(cls,*args,**kwargs)
......
......@@ -14,10 +14,9 @@ class Loss:
Author: @meetdoshi
'''
l2_norm_sq = None
for i in range(len(F)):
try:
diff = F[i]-P[i]
l2_norm_sq = torch.mean(diff**2)
diff = F-P
l2_norm_sq = torch.mean((diff)**2)
except Exception as e:
LOGGER.error("Error computing loss",e)
return l2_norm_sq
......@@ -41,24 +40,33 @@ class Loss:
@params
Author: @soumyagupta
'''
for i in range(len(F)):
num_channels = F[i][1]
h = F[i][2]
w = F[i][3]
style_gram_matrix = Loss.gram_matrix(F[i])
target_gram_matrix = Loss.gram_matrix(A[i])
loss_s = torch.sum((style_gram_matrix-target_gram_matrix)**2)
constant = 1/(4.0*(num_channels**2)*((h*w)**2))
return constant*loss_s
num_channels = F.shape[1]
h = F.shape[2]
w = F.shape[3]
style_gram_matrix = Loss.gram_matrix(F)
target_gram_matrix = Loss.gram_matrix(A)
loss_s = torch.mean((style_gram_matrix-target_gram_matrix)**2)
#constant = 1/(4.0*(num_channels**2)*((h*w)**2))
return loss_s
@staticmethod
def total_loss(alpha,beta,cont_fmap_real,cont_fmap_noise,style_fmap_real,style_fmap_noise):
def total_loss(alpha,beta,cont_fmap_real,style_fmap_real,generated_fmaps_content,generated_fmaps_style):
'''
Function which computes total loss and returns it
@params
Author: @jiteshg
'''
content_loss = Loss.content_loss(cont_fmap_real, cont_fmap_noise)
style_loss = Loss.style_loss(style_fmap_real, style_fmap_noise)
loss_t = alpha*content_loss + beta*style_loss
return loss_t
\ No newline at end of file
loss_t = 0.0
a = 0.0
b = 0.0
for cont,gen_cont in zip(cont_fmap_real,generated_fmaps_content):
loss_cont = Loss.content_loss(cont,gen_cont)
a+= loss_cont
for gen_style,sty in zip(generated_fmaps_style,style_fmap_real):
loss_style = Loss.style_loss(sty,gen_style)
b+= loss_style
loss_t += alpha*a + beta*b
return loss_t,a,b
\ No newline at end of file
from loss import Loss
from feature_maps import FeatureMaps
from feature_maps import LOGGER, FeatureMaps
import torch.optim as optim
from torchvision.utils import save_image
import matplotlib.pyplot as plt
import os
plt.ion()
class Optimizer:
@staticmethod
def gradient_descent(content_img, style_img, content_img_clone):
......@@ -14,24 +16,28 @@ class Optimizer:
content_img_clone: Copy of Original Image
Author: @gaurangathavale
'''
epoch = 1000
learning_rate = 0.001
alpha = 10
beta = 100
LOGGER.info("Running gradient descent with the following parameters")
epoch = 4000
learning_rate = 0.01
alpha = 1
beta = 0.01
identifier = "content-4_2"
os.mkdir("styled_images/"+identifier)
LOGGER.info(f"{epoch},{learning_rate},{alpha},{beta}")
optimizer=optim.Adam([content_img_clone],lr=learning_rate)
print(optimizer)
LOGGER.info("Optimizer = " + str(optimizer))
#fig = plt.figure()
#ax = fig.add_subplot(111)
feature_maps = FeatureMaps()
for e in range(epoch):
feature_maps = FeatureMaps()
content_fmaps = feature_maps.get_fmaps(content_img)
style_fmaps = feature_maps.get_fmaps(style_img)
# content_clone_fmaps = feature_maps.get_fmaps(content_img_clone)
content_white_noise_fmaps = feature_maps.get_fmaps(content_img, [21])
style_white_noise_fmaps = feature_maps.get_fmaps(style_img, [21])
content_fmaps = feature_maps.get_fmaps_content(content_img)
style_fmaps = feature_maps.get_fmaps_style(style_img)
generated_fmaps_content = feature_maps.get_fmaps_content(content_img_clone)
generated_fmaps_style = feature_maps.get_fmaps_style(content_img_clone)
total_loss = Loss.total_loss(alpha, beta, content_fmaps, content_white_noise_fmaps, style_fmaps, style_white_noise_fmaps)
total_loss,total_cont_loss,total_style_loss = Loss.total_loss(alpha, beta, content_fmaps, style_fmaps,generated_fmaps_content,generated_fmaps_style)
# clears x.grad for every parameter x in the optimizer.
# It’s important to call this before total_loss.backward(), otherwise it will accumulate the gradients from multiple passes.
......@@ -42,8 +48,9 @@ class Optimizer:
# Optimization Step / Update Rule
optimizer.step()
if(not (e%100)):
print(total_loss)
save_image(content_img_clone,"styled.png")
\ No newline at end of file
#plt.clf()
#plt.plot(content_img_clone)
if(e%10 == 0):
LOGGER.info(f"Epoch = {e} Total Loss = {total_loss} content Loss = {total_cont_loss} style Loss = {total_style_loss}")
name = "styled_images/"+identifier+"/styled_" + str(e) +".png"
save_image(content_img_clone,name)
\ No newline at end of file
from os import device_encoding
from logger import Logger
from torch import transforms
import torch
import torchvision.transforms as transforms
from PIL import Image
import numpy as np
LOGGER = Logger().logger()
device=torch.device( "cuda" if (torch.cuda.is_available()) else 'cpu')
#Author: @meetdoshi
class Preprocessor:
@staticmethod
......@@ -32,9 +35,11 @@ class Preprocessor:
@params
img: 3d numpy array
'''
loader = transforms.Compose([transforms.ToTensor(),transforms.Resize([224,224]),transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225],),])
#loader = transforms.Compose([transforms.ToTensor(),transforms.Resize([224,224]),transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225],),])
loader = transforms.Compose([transforms.ToTensor(),transforms.Resize([224,224])])
img = loader(img).unsqueeze(0)
return img
#assert img.shape == (1,3,224,224)
return img.to(device,torch.float)
@staticmethod
......@@ -46,15 +51,12 @@ class Preprocessor:
'''
img = Preprocessor.load_image(path)
img = Preprocessor.reshape_img(img)
img = Preprocessor.subtract_mean(img)
#img = Preprocessor.subtract_mean(img)
return img
'''
if __name__=="__main__":
prec = Preprocessor()
img = np.zeros(shape=(4,4,3))
print(img.shape)
for i in range(img.shape[2]):
print(img[:,:,i])
img = prec.subtract_mean(img)
for i in range(img.shape[2]):
print(img[:,:,i])
img = prec.process('test/sem8.jpeg')
'''
matplotlib==3.5.1
numpy==1.21.5
Pillow==9.2.0
torch==1.12.1+cpu
torchvision==0.13.1+cpu
import os
import os
import warnings
from optimizer import Optimizer
from loss import Loss
from preprocess import Preprocessor
......@@ -11,10 +12,10 @@ import argparse
import torchvision.models as models
import torch.optim as optim
from torchvision.utils import save_image
warnings.filterwarnings('ignore')
from logger import Logger
LOGGER = Logger().logger()
LOGGER.info("Started Style Transfer")
class StyleTransfer:
'''
Style Transfer Base Class
......@@ -32,8 +33,8 @@ class StyleTransfer:
device = torch.device( "cuda" if (torch.cuda.is_available()) else 'cpu')
content_img_path = 'Nikola-Tesla.jpg'
style_img_path = 'style-Image.jpg'
content_img_path = 'test/content.jpg'
style_img_path = 'test/style.jpg'
content_img = Preprocessor.process(content_img_path)
style_img = Preprocessor.process(style_img_path)
......@@ -41,3 +42,10 @@ class StyleTransfer:
content_img_clone = content_img.clone().requires_grad_(True)
Optimizer.gradient_descent(content_img, style_img, content_img_clone)
if __name__ == "__main__":
stf = StyleTransfer()
stf.pipeline()
\ No newline at end of file
import logging
import os
#Author: @meetdoshi
class Logger:
'''
Singleton logger class
'''
_instance = None
_logHandler = None
_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
def __new__(cls,*args,**kwargs):
if not cls._instance:
os.system("rm -rf Logs/")
os.mkdir("Logs/")
logHandler = logging.FileHandler("Logs/style_transfer.log")
logHandler.setFormatter(cls._formatter)
cls._logHandler = logging.getLogger("Logs/style_transfer.log")
cls._logHandler.setLevel(logging.INFO)
cls._logHandler.addHandler(logHandler)
cls._instance = super(Logger, cls).__new__(cls,*args,**kwargs)
return cls._instance
def logger(self):
return self._logHandler
'''
#Demo use
if __name__ == "__main__":
a = Logger()
b = Logger()
print(a is b)
INFO = a.logger()
ERROR = b.logger()
INFO.info("TEST")
ERROR.info("ERROR")
'''
import numpy as np
import torch
from logger import Logger
LOGGER = Logger().logger()
class Loss:
@staticmethod
def adversarial_G():
'''
L_gan(G,Dy,X,Y) =
'''
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment