Created
January 12, 2017 15:11
-
-
Save Coderx7/de210643b15e206a4fd6a5da9f7c4d2b to your computer and use it in GitHub Desktop.
a simple script for calculating the mean for a batch of images in a semi-vectorized version for situations where the memory is not large enough to accommodate all data and you need to loop through them!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
import numpy as np | |
#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 --really 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment