Skip to content

Instantly share code, notes, and snippets.

@Coderx7
Last active January 12, 2017 18:05
Show Gist options
  • Save Coderx7/01b63e6553db8dea43ab113bf05d9b74 to your computer and use it in GitHub Desktop.
Save Coderx7/01b63e6553db8dea43ab113bf05d9b74 to your computer and use it in GitHub Desktop.
mean and std calculation with non-vectorized and semi-vectorized implementation, good candidates for times numpy.mean and numpy.std does not work! because of memory issues
#In the name of God, the most compassionate the most merciful
import math
import numpy as np
#English: a simple handy snippet which I specifically wrote for calculating mean for a batch of images,
#in semi-vectorized and unvectorized fashion, along with the fully numpy example to test the output!
#Farsi:
#mohasebe mean batchi az tasavir be sorate vectorized, unvectorized and semi vectorized
#age version vectorized error kambod hafeze dad, behtare az semi vectorized estefade beshe
#chon unvectorized ya hamoon loop mamoli sooratesh kheyli kame.
#[email protected]
#Seyyed Hossein Hasanpour
#1/12/2017 6:47 pm
#batch,channel,width,height
def calc_mean(a):
mean = 0.0
mean1 = np.mean(a, axis=0)
#mean channel wise, yanee, mean har channel jodagane hesab beshe
#0 batch hast, 2 va 3 ham andaze tasvir, ke vaghti hame element ha ba ham jam shodan taghsim bar zarb in 3 ta mishan
# chon in se ta tedade kole elementa ro moshakhaas mikonan.
#to version classic ke neveshtam kamelan moshakhase
mean2 = np.mean(a, axis=(0,2,3))
return mean1, mean2
#unvectorized version --very slow!
def calc_mean_classic(a):
#sum all elements in each channel and divide by the number of elements
batch = a.shape[0]
channel = a.shape[1]
width = a.shape[2]
height = a.shape[3]
sum = np.zeros((channel))
for i in range(batch):
for j in range(channel):
for w in range(width):
for h in range(height):
sum[j] += a[i,j,w,h]
return (sum/(width*height*batch))
#mesle balas ama dota halghe akhari vectori shodan va kheyly kheyly saritar mohasebe anjam mishe alan.
#semi-vectorized, very fast, almost as fast as the vectorized version
def calc_mean_classic2(a):
#sum all elements in each channel and divide by the number of elements
batch = a.shape[0]
channel = a.shape[1]
width = a.shape[2]
height = a.shape[3]
sum = np.zeros((channel))
for i in range(batch):
for j in range(channel):
sum[j] += np.sum(a[i,j,:,:])
return (sum/(width*height*batch))
#STD
#a non-vectorized and semi-vectorized implementation for calculating std for a batch of images.
#the semi-vectorized is the one, one should use, since its as fast as the numpy.std
#Seyyed Hossein Hasan pour
#[email protected]
#1/12/2017
#unvectorized version --very slow!
def calc_std_classic(a):
#sum all elements in each channel and divide by the number of elements
batch = a.shape[0]
channel = a.shape[1]
width = a.shape[2]
height = a.shape[3]
#this method is uploaded here : https://gist.github.com/Coderx7/de210643b15e206a4fd6a5da9f7c4d2b
mean = calc_mean_classic2(a)
#https://www.daniweb.com/programming/software-development/threads/348002/mean-and-standard-deviation-of-image
#https://docs.scipy.org/doc/numpy/reference/generated/numpy.std.html
sum = np.zeros((channel))
for i in range(batch):
for j in range(channel):
for w in range(width):
for h in range(height):
sum[j] += (abs(a[i,j,w,h] - mean[j])**2)
var = (sum/(width*height*batch))
std = [round(math.sqrt(x),8) for x in var ]
return std
#semi-vectorized, very fast.
#use this if you face memory shortage errors because of your dataset being too big for your memory
def calc_std_classic2(a):
batch = a.shape[0]
channel = a.shape[1]
width = a.shape[2]
height = a.shape[3]
mean = calc_mean_classic2(a)
sum = np.zeros((channel))
for i in range(batch):
for j in range(channel):
sum[j] += np.sum(abs(a[i,j,:,:] - mean[j])**2)
var = (sum/(width*height*batch))
std = [round(math.sqrt(x),8) for x in var ]
return std
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment