# BLVC Googlenet, model from the paper: # "Going Deeper with Convolutions" # Original source: # https://github.com/BVLC/caffe/tree/master/models/bvlc_googlenet # License: unrestricted use # Download pretrained weights from: # https://s3.amazonaws.com/lasagne/recipes/pretrained/imagenet/blvc_googlenet.pkl from lasagne.layers import InputLayer from lasagne.layers import DenseLayer from lasagne.layers import ConcatLayer from lasagne.layers import NonlinearityLayer from lasagne.layers import GlobalPoolLayer from lasagne.layers.dnn import Conv2DDNNLayer as ConvLayer from lasagne.layers.dnn import MaxPool2DDNNLayer as PoolLayerDNN from lasagne.layers import MaxPool2DLayer as PoolLayer from lasagne.layers import LocalResponseNormalization2DLayer as LRNLayer from lasagne.nonlinearities import softmax, linear import lasagne import pickle import pandas as pd import numpy as np import sklearn import csv import os from PIL import Image #DATA I/O STUFF """ Returns all labels for the training data as pandas data frame """ def all_labels(base_directory=None): basedir = base_directory or os.path.dirname(os.path.abspath(__file__)) train_d = pd.read_csv(os.path.join(basedir,'./train.csv')) train_to_biz_id_data = pd.read_csv(os.path.join(basedir,'./train_photo_to_biz_ids.csv')) X_train = pd.merge(train_d, train_to_biz_id_data, on = 'business_id') Y_train = X_train['labels'].str.get_dummies(sep=' ') del(X_train['labels']) X_test = pd.read_csv(os.path.join(basedir,'./test_photo_to_biz.csv')) for i in range(9): X_train[str(i)] = Y_train[str(i)] X_train = X_train.set_index('photo_id') del X_train['business_id'] return X_train """ Given a list of image ids and a directory containing the images, resizes the images and loads them into a numpy array of shape [len(set_imgs), img_size[0], img_size[1], num_channels]. Also returns the image ids of the set. """ def image_set(set_imgs, img_size, X_dir, gray_scale): if (gray_scale): image_set = np.zeros((len(set_imgs),1, img_size[0], img_size[1]), dtype = 'uint8') else: image_set = np.zeros((len(set_imgs), img_size[0], img_size[1],3), dtype = 'uint8') #Fill the batch for idx, img in enumerate(set_imgs): print("Processesing Images: " + str(idx+1) + " of " + str(len(set_imgs)) + ", path: "+str(X_dir)+str(img)+".jpg"); try: im = Image.open(str(X_dir) + str(img)+".jpg"); if (gray_scale): im = im.convert('L') image_set[idx]= np.asarray(im.resize(img_size)); except IOError: print(str(X_dir)+ str(img)+".jpg"+" is not an image"); #Strip the filenames to get the ids. image_ids = [str(i).split('.', 1)[0] for i in set_imgs]; return image_set, image_ids; """ Returns the training labels for a specific set of image ids """ def labels(label_dir, image_set_ids): all_y = all_labels(label_dir) y = np.zeros((len(image_set_ids), 9), dtype='uint8') for idx, image_id in enumerate(image_set_ids): y[idx] = all_y.loc[int(image_id)] return y # ORIGINAL GOOGLE CODE def build_inception_module(name, input_layer, nfilters): # nfilters: (pool_proj, 1x1, 3x3_reduce, 3x3, 5x5_reduce, 5x5) net = {} net['pool'] = PoolLayerDNN(input_layer, pool_size=3, stride=1, pad=1) net['pool_proj'] = ConvLayer( net['pool'], nfilters[0], 1, flip_filters=False) net['1x1'] = ConvLayer(input_layer, nfilters[1], 1, flip_filters=False) net['3x3_reduce'] = ConvLayer( input_layer, nfilters[2], 1, flip_filters=False) net['3x3'] = ConvLayer( net['3x3_reduce'], nfilters[3], 3, pad=1, flip_filters=False) net['5x5_reduce'] = ConvLayer( input_layer, nfilters[4], 1, flip_filters=False) net['5x5'] = ConvLayer( net['5x5_reduce'], nfilters[5], 5, pad=2, flip_filters=False) net['output'] = ConcatLayer([ net['1x1'], net['3x3'], net['5x5'], net['pool_proj'], ]) print 'name: '+name print({'{}/{}'.format(name, k): v for k, v in net.items()}) return {'{}/{}'.format(name, k): v for k, v in net.items()} def build_model(): net = {} net['input'] = InputLayer((None, 3, None, None)) net['conv1/7x7_s2'] = ConvLayer( net['input'], 64, 7, stride=2, pad=3, flip_filters=False) net['pool1/3x3_s2'] = PoolLayer( net['conv1/7x7_s2'], pool_size=3, stride=2, ignore_border=False) net['pool1/norm1'] = LRNLayer(net['pool1/3x3_s2'], alpha=0.00002, k=1) net['conv2/3x3_reduce'] = ConvLayer( net['pool1/norm1'], 64, 1, flip_filters=False) net['conv2/3x3'] = ConvLayer( net['conv2/3x3_reduce'], 192, 3, pad=1, flip_filters=False) net['conv2/norm2'] = LRNLayer(net['conv2/3x3'], alpha=0.00002, k=1) net['pool2/3x3_s2'] = PoolLayer(net['conv2/norm2'], pool_size=3, stride=2) net.update(build_inception_module('inception_3a', net['pool2/3x3_s2'], [32, 64, 96, 128, 16, 32])) net.update(build_inception_module('inception_3b', net['inception_3a/output'], [64, 128, 128, 192, 32, 96])) net['pool3/3x3_s2'] = PoolLayer(net['inception_3b/output'], pool_size=3, stride=2) net.update(build_inception_module('inception_4a', net['pool3/3x3_s2'], [64, 192, 96, 208, 16, 48])) net.update(build_inception_module('inception_4b', net['inception_4a/output'], [64, 160, 112, 224, 24, 64])) net.update(build_inception_module('inception_4c', net['inception_4b/output'], [64, 128, 128, 256, 24, 64])) net.update(build_inception_module('inception_4d', net['inception_4c/output'], [64, 112, 144, 288, 32, 64])) net.update(build_inception_module('inception_4e', net['inception_4d/output'], [128, 256, 160, 320, 32, 128])) net['pool4/3x3_s2'] = PoolLayer(net['inception_4e/output'], pool_size=3, stride=2) net.update(build_inception_module('inception_5a', net['pool4/3x3_s2'], [128, 256, 160, 320, 32, 128])) net.update(build_inception_module('inception_5b', net['inception_5a/output'], [128, 384, 192, 384, 48, 128])) net['pool5/7x7_s1'] = GlobalPoolLayer(net['inception_5b/output']) net['loss3/classifier'] = DenseLayer(net['pool5/7x7_s1'], num_units=1000, nonlinearity=linear) net['prob'] = NonlinearityLayer(net['loss3/classifier'], nonlinearity=softmax) return net #END ORIGINAL GOOGLE CODE """ Returns the image ids belonging to each business in a grouped DataFrame """ def load_business_datasets(filename): biz_ids_grouped = pd.read_csv(filename) biz_ids_grouped = biz_ids_grouped.groupby(by='business_id',squeeze=False) return biz_ids_grouped if __name__ == '__main__': net = build_model() output_layer = net['prob'] values = pickle.load(open('blvc_googlenet.pkl','rb'))['param values'] lasagne.layers.set_all_param_values(output_layer,values) #Prepare sets of image labels for each business as grouped object biz_ids = load_business_datasets('./train_photo_to_biz_ids.csv') # Fetch image data for each business, put it in the inception network and grab the representations # Store representations in a Dataframe for averaging and to create training data for the second stage business_representations = pd.DataFrame(columns=('business_id','avg_representation')) for name, group in biz_ids: #print(name) #print 'length: %r' % len(group['photo_id']) group_length = len(group['photo_id']) image_ids_group = group['photo_id'] print image_ids_group image_data_group,image_ids_group = image_set(image_ids_group,(150,150),'./train_preprocessed/',False) group_representations = pd.DataFrame(columns=('photo_id','i4a','i4b','i4c','i4d','i4e')) for i in range(len(image_data_group)): hl1,hl2,hl3,hl4 = lasagne.layers.get_output([net['inception_4a/output'],net['inception_4b/output'],net['inception_4c/output'],net['inception_4d/output'],net['inception_4e/output']],image_data_group[i]) rep = [image_ids_group[i],hl1,hl2,hl3,hl4] group_representations.loc[len(group_representations)] = rep #Calculate "business representations" for inception to feed to Random Forest (or other module) business_representations.loc[len(business_representations)] = [name,business_representations[['i4a','i4b','i4c','i4d','i4e']].mean(axis=1)] # Write business representations to CSV file business_representations.to_csv(path_or_buf='./inceptionNet/business_representations_train.csv') # Now do the same thing for the test data #Prepare sets of image labels for each business as grouped object biz_ids = load_business_datasets('./test_photo_to_biz.csv') # Fetch image data for each business, put it in the inception network and grab the representations # Store representations in a Dataframe for averaging and to create training data for the second stage business_representations_test = pd.DataFrame(columns=('business_id','avg_representation')) for name, group in biz_ids: #print(name) #print 'length: %r' % len(group['photo_id']) group_length = len(group['photo_id']) image_ids_group_test = group['photo_id'] image_data_group_test = image_set(image_ids_group_test,(150,150),'./test_preprocessed/',False) group_representations_test = pd.DataFrame(columns=('photo_id','i4a','i4b','i4c','i4d','i4e')) for i in range(len(image_data_group_test)): hl1,hl2,hl3,hl4 = lasagne.layers.get_output([net['inception_4a/output'],net['inception_4b/output'],net['inception_4c/output'],net['inception_4d/output'],net['inception_4e/output']],image_data_group_test(i)) rep = [image_ids_group_test[i],hl1,hl2,hl3,hl4] group_representations_test.loc[len(group_representations_test)] = rep #Calculate "business representations" for inception to feed to Random Forest (or other module) business_representations_test.loc[len(business_representations_test)] = [name,business_representations_test[['i4a','i4b','i4c','i4d','i4e']].mean(axis=1)] # Write business representations to CSV file business_representations_test.to_csv(path_or_buf='./inceptionNet/business_representations_test.csv')
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question