Commit 4ff6ba78 authored by Meet Narendra's avatar Meet Narendra 💬

Fmaps Loss Preprc

parent fd47d3e3
...@@ -32,6 +32,22 @@ class FeatureMaps: ...@@ -32,6 +32,22 @@ class FeatureMaps:
LOGGER.error("Could not fetch layer "+str(layer)) LOGGER.error("Could not fetch layer "+str(layer))
return weights return weights
def get_fmaps(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__": if __name__ == "__main__":
fmap = FeatureMaps() fmap = FeatureMaps()
model = fmap.get_model() model = fmap.get_model()
......
...@@ -10,11 +10,12 @@ class Loss: ...@@ -10,11 +10,12 @@ class Loss:
@params @params
F: 2D numpy array F: 2D numpy array
P: 2D numpy array P: 2D numpy array
Author: @meetdoshi
''' '''
l2_norm_sq = None l2_norm_sq = None
try: try:
diff = F-P diff = F-P
l2_norm_sq = np.sum(diff*diff) l2_norm_sq = np.sum(diff**2)
except Exception as e: except Exception as e:
LOGGER.error("Error computing loss",e) LOGGER.error("Error computing loss",e)
return l2_norm_sq/2.0 return l2_norm_sq/2.0
......
from distutils.log import Log from distutils.log import Log
from logger import Logger from logger import Logger
import numpy as np
LOGGER = Logger().logger() LOGGER = Logger().logger()
class Preprocessor: class Preprocessor:
@staticmethod
def subtract_mean(img):
'''
Function to subtract mean values of RGB channels computed over whole ImageNet dataset
@params
img: 3d numpy array
'''
mean = np.reshape([103.939, 116.779, 123.68],(1,1,3))#b,g,r
return img-mean
@staticmethod
def reshape_img(img):
'''
Function to reshpae image in 224x224xnum_of_channels shape
@params
img: 3d numpy array
'''
@staticmethod @staticmethod
def process(img): def process(img):
''' '''
Function to preprocess the image Function to preprocess the image
@params @params
img: 2d numpy a[103.939, 116.779, 123.68]rray
''' '''
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])
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