Skip to content

Commit

Permalink
normalize image by per-color mean and std(#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
hma02 committed Aug 2, 2017
1 parent 76f5016 commit 80580c0
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 23 deletions.
10 changes: 6 additions & 4 deletions theanompi/models/alex_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,10 @@ def train_iter(self, count,recorder):

else:

img_mean = self.data.rawdata[-1]
img_mean = self.data.rawdata[4]
img_std = self.data.rawdata[5]
import hickle as hkl
arr = hkl.load(img[self.current_t]) - img_mean
arr = (hkl.load(img[self.current_t]) - img_mean)/255./img_std

from theanompi.models.data.utils import crop_and_mirror

Expand Down Expand Up @@ -520,9 +521,10 @@ def val_iter(self, count,recorder):
else:


img_mean = self.data.rawdata[-1]
img_mean = self.data.rawdata[4]
img_std = self.data.rawdata[5]
import hickle as hkl
arr = hkl.load(img[self.current_v]) - img_mean
arr = (hkl.load(img[self.current_v]) - img_mean)/255./img_std

from theanompi.models.data.utils import crop_and_mirror

Expand Down
5 changes: 3 additions & 2 deletions theanompi/models/be_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@

# load data

img_mean=model.data.rawdata[-1]
img_mean=model.data.rawdata[4]
img_std=model.data.rawdata[5]

import hickle as hkl
arr = hkl.load(model.data.train_img_shard[0]) - img_mean
arr = (hkl.load(model.data.train_img_shard[0]) - img_mean)/255./img_std

from theanompi.models.data.utils import crop_and_mirror

Expand Down
14 changes: 10 additions & 4 deletions theanompi/models/data/imagenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@

sc=False # if the architecture already includes subtract mean and cropping

debug=False

class ImageNet_data(object):

def __init__(self, verbose):

# data hyperparams

self.data_path = '/scratch/ilsvrc12/'
self.train_folder = 'train_hkl_b256_b_128'
self.val_folder = 'val_hkl_b256_b_128'
self.data_path = dir_head #'/scratch/ilsvrc12/'
self.train_folder = train_folder #'train_hkl_b256_b_128/'
self.val_folder = val_folder #'val_hkl_b256_b_128/'

self.channels = 3
self.width =256
Expand Down Expand Up @@ -89,11 +91,14 @@ def get_data(self, file_batch_size=128):
#print 'BGR_mean %s' % image_mean #[ 122.22585297 116.20915222 103.56548309]
import numpy as np
image_mean = image_mean[:,np.newaxis,np.newaxis,np.newaxis]

img_std = np.array([0.229, 0.224, 0.225]).astype(np.float32)
img_std = img_std[:,np.newaxis,np.newaxis,np.newaxis]



self.rawdata = [train_filenames,train_labels,\
val_filenames,val_labels,img_mean] # 5 items
val_filenames,val_labels,img_mean,img_std] # 6 items



Expand Down Expand Up @@ -286,6 +291,7 @@ def para_load_init(self, shared_x, input_width, input_height,
config['rand_crop'] = rand_crop
config['batch_crop_mirror'] = batch_crop_mirror
config['img_mean'] = self.rawdata[4]
config['img_std'] = self.rawdata[5]

import os
_sock_data = ((sock_data + int(os.getpid())) % 64511)+1024
Expand Down
3 changes: 2 additions & 1 deletion theanompi/models/data/proc_load_mpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
rand_crop = config['rand_crop']
batch_crop_mirror = config['batch_crop_mirror']
img_mean = config['img_mean']
img_std=config['img_std']
import os
if "CPULIST_train" in os.environ:
cpulist = os.environ['CPULIST_train']
Expand Down Expand Up @@ -95,7 +96,7 @@

arr = hkl.load(str(filename)).astype('float32')

arr = arr - img_mean
arr = (arr - img_mean)/255./img_std

arr = crop_and_mirror(arr, mode,
rand_crop,
Expand Down
10 changes: 6 additions & 4 deletions theanompi/models/googlenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,10 @@ def train_iter(self, count, recorder):

else:

img_mean = self.data.rawdata[-1]
img_mean = self.data.rawdata[4]
img_std = self.data.rawdata[5]
import hickle as hkl
arr = hkl.load(img[self.current_t]) - img_mean
arr = (hkl.load(img[self.current_t]) - img_mean)/255./img_std

from theanompi.models.data.utils import crop_and_mirror

Expand Down Expand Up @@ -866,9 +867,10 @@ def val_iter(self, count,recorder):
else:


img_mean = self.data.rawdata[-1]
img_mean = self.data.rawdata[4]
img_std = self.data.rawdata[5]
import hickle as hkl
arr = hkl.load(img[self.current_v]) - img_mean
arr = (hkl.load(img[self.current_v]) - img_mean)/255./img_std

from theanompi.models.data.utils import crop_and_mirror

Expand Down
13 changes: 9 additions & 4 deletions theanompi/models/lasagne_model_zoo/resnet50.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ def build_model_resnet50(input_shape):

batch_crop_mirror = True
rand_crop = True

image_mean = np.array([103.939, 116.779, 123.68],dtype='float32')[:,np.newaxis,np.newaxis,np.newaxis]
dataname = 'imagenet'

monitor_grad = False
Expand Down Expand Up @@ -254,6 +256,7 @@ def __init__(self, config):
# data
from theanompi.models.data import ImageNet_data
self.data = ImageNet_data(verbose=False)
self.data.rawdata[4] = image_mean
self.channels = self.data.channels # 'c' mean(R,G,B) = (103.939, 116.779, 123.68)
self.input_width = input_width # '0' single scale training 224
self.input_height = input_height # '1' single scale training 224
Expand Down Expand Up @@ -494,9 +497,10 @@ def train_iter(self, count,recorder):

else:

img_mean = self.data.rawdata[-1]
img_mean = self.data.rawdata[4]
img_std = self.data.rawdata[5]
import hickle as hkl
arr = hkl.load(img[self.current_t]) - img_mean
arr = (hkl.load(img[self.current_t]) - img_mean)/255./img_std

from theanompi.models.data.utils import crop_and_mirror

Expand Down Expand Up @@ -595,10 +599,11 @@ def val_iter(self, count,recorder):
else:


img_mean = self.data.rawdata[-1]
img_mean = self.data.rawdata[4]
img_std = self.data.rawdata[5]

import hickle as hkl
arr = hkl.load(img[self.current_v]) - img_mean
arr = (hkl.load(img[self.current_v]) - img_mean)/255./img_std

from theanompi.models.data.utils import crop_and_mirror

Expand Down
10 changes: 6 additions & 4 deletions theanompi/models/lasagne_model_zoo/vgg16.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,10 @@ def train_iter(self, count,recorder):

else:

img_mean = self.data.rawdata[-1]
img_mean = self.data.rawdata[4]
img_std = self.data.rawdata[5]
import hickle as hkl
arr = hkl.load(img[self.current_t]) - img_mean
arr = (hkl.load(img[self.current_t]) - img_mean)/255./img_std

from theanompi.models.data.utils import crop_and_mirror

Expand Down Expand Up @@ -493,9 +494,10 @@ def val_iter(self, count,recorder):
else:


img_mean = self.data.rawdata[-1]
img_mean = self.data.rawdata[4]
img_std = self.data.rawdata[5]
import hickle as hkl
arr = hkl.load(img[self.current_v]) - img_mean
arr = (hkl.load(img[self.current_v]) - img_mean)/255./img_std

from theanompi.models.data.utils import crop_and_mirror

Expand Down

0 comments on commit 80580c0

Please sign in to comment.