In [1]:
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
import zipfile
from IPython.display import display, Image
from scipy import ndimage
from sklearn.linear_model import LogisticRegression
from six.moves.urllib.request import urlretrieve
from six.moves import cPickle as pickle

from skimage import color, io
from scipy.misc import imresize

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D, Activation, GlobalAveragePooling2D
from keras.callbacks import EarlyStopping
from keras.models import Model

np.random.seed(31337)

# Config the matplotlib backend as plotting inline in IPython
%matplotlib inline


Using TensorFlow backend.

In [2]:
X_train=np.load('catfish-X_train.npy')
Y_train=np.load('catfish-Y_train.npy')
import gc
gc.collect()


Out[2]:
8

In [3]:
X_hard=np.load('catfish-X_hard.npy')
Y_hard=np.load('catfish-Y_hard.npy')

In [4]:
import gc
gc.collect()


Out[4]:
8

In [5]:
def unprocess_image(im):
    import copy
    im=copy.copy(im)
    # restore mean (sueezenet mean values)
    im[:, :, 0] += 104.006
    im[:, :, 1] += 116.669
    im[:, :, 2] += 122.679
    
    # swap RBGR->RBG
    im = im[:, :, ::-1]
    
    return im

num=1185
plt.imshow(unprocess_image(X_hard[num])*255)
plt.show()

num=1698
plt.imshow(unprocess_image(X_train[num])*255)
plt.show()


num=1231
plt.imshow(unprocess_image(X_train[num])*255)
plt.show()



In [6]:
import sys
sys.path.append("keras-squeezenet")

from squeezenet import get_squeezenet
from keras.models import Model

squeezenet_model = get_squeezenet(1000, dim_ordering='tf')
squeezenet_model.compile(loss="categorical_crossentropy", optimizer="adam")
squeezenet_model.load_weights('keras-squeezenet/model/squeezenet_weights_tf_dim_ordering_tf_kernels.h5', by_name=True)

squeezenet_model.summary()


____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_1 (InputLayer)             (None, 227, 227, 3)   0                                            
____________________________________________________________________________________________________
conv1 (Convolution2D)            (None, 113, 113, 64)  1792        input_1[0][0]                    
____________________________________________________________________________________________________
relu_conv1 (Activation)          (None, 113, 113, 64)  0           conv1[0][0]                      
____________________________________________________________________________________________________
pool1 (MaxPooling2D)             (None, 56, 56, 64)    0           relu_conv1[0][0]                 
____________________________________________________________________________________________________
fire2/squeeze1x1 (Convolution2D) (None, 56, 56, 16)    1040        pool1[0][0]                      
____________________________________________________________________________________________________
fire2/relu_squeeze1x1 (Activatio (None, 56, 56, 16)    0           fire2/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire2/expand1x1 (Convolution2D)  (None, 56, 56, 64)    1088        fire2/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire2/expand3x3 (Convolution2D)  (None, 56, 56, 64)    9280        fire2/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire2/relu_expand1x1 (Activation (None, 56, 56, 64)    0           fire2/expand1x1[0][0]            
____________________________________________________________________________________________________
fire2/relu_expand3x3 (Activation (None, 56, 56, 64)    0           fire2/expand3x3[0][0]            
____________________________________________________________________________________________________
fire2/concat (Merge)             (None, 56, 56, 128)   0           fire2/relu_expand1x1[0][0]       
                                                                   fire2/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
fire3/squeeze1x1 (Convolution2D) (None, 56, 56, 16)    2064        fire2/concat[0][0]               
____________________________________________________________________________________________________
fire3/relu_squeeze1x1 (Activatio (None, 56, 56, 16)    0           fire3/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire3/expand1x1 (Convolution2D)  (None, 56, 56, 64)    1088        fire3/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire3/expand3x3 (Convolution2D)  (None, 56, 56, 64)    9280        fire3/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire3/relu_expand1x1 (Activation (None, 56, 56, 64)    0           fire3/expand1x1[0][0]            
____________________________________________________________________________________________________
fire3/relu_expand3x3 (Activation (None, 56, 56, 64)    0           fire3/expand3x3[0][0]            
____________________________________________________________________________________________________
fire3/concat (Merge)             (None, 56, 56, 128)   0           fire3/relu_expand1x1[0][0]       
                                                                   fire3/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
pool3 (MaxPooling2D)             (None, 27, 27, 128)   0           fire3/concat[0][0]               
____________________________________________________________________________________________________
fire4/squeeze1x1 (Convolution2D) (None, 27, 27, 32)    4128        pool3[0][0]                      
____________________________________________________________________________________________________
fire4/relu_squeeze1x1 (Activatio (None, 27, 27, 32)    0           fire4/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire4/expand1x1 (Convolution2D)  (None, 27, 27, 128)   4224        fire4/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire4/expand3x3 (Convolution2D)  (None, 27, 27, 128)   36992       fire4/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire4/relu_expand1x1 (Activation (None, 27, 27, 128)   0           fire4/expand1x1[0][0]            
____________________________________________________________________________________________________
fire4/relu_expand3x3 (Activation (None, 27, 27, 128)   0           fire4/expand3x3[0][0]            
____________________________________________________________________________________________________
fire4/concat (Merge)             (None, 27, 27, 256)   0           fire4/relu_expand1x1[0][0]       
                                                                   fire4/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
fire5/squeeze1x1 (Convolution2D) (None, 27, 27, 32)    8224        fire4/concat[0][0]               
____________________________________________________________________________________________________
fire5/relu_squeeze1x1 (Activatio (None, 27, 27, 32)    0           fire5/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire5/expand1x1 (Convolution2D)  (None, 27, 27, 128)   4224        fire5/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire5/expand3x3 (Convolution2D)  (None, 27, 27, 128)   36992       fire5/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire5/relu_expand1x1 (Activation (None, 27, 27, 128)   0           fire5/expand1x1[0][0]            
____________________________________________________________________________________________________
fire5/relu_expand3x3 (Activation (None, 27, 27, 128)   0           fire5/expand3x3[0][0]            
____________________________________________________________________________________________________
fire5/concat (Merge)             (None, 27, 27, 256)   0           fire5/relu_expand1x1[0][0]       
                                                                   fire5/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
pool5 (MaxPooling2D)             (None, 13, 13, 256)   0           fire5/concat[0][0]               
____________________________________________________________________________________________________
fire6/squeeze1x1 (Convolution2D) (None, 13, 13, 48)    12336       pool5[0][0]                      
____________________________________________________________________________________________________
fire6/relu_squeeze1x1 (Activatio (None, 13, 13, 48)    0           fire6/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire6/expand1x1 (Convolution2D)  (None, 13, 13, 192)   9408        fire6/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire6/expand3x3 (Convolution2D)  (None, 13, 13, 192)   83136       fire6/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire6/relu_expand1x1 (Activation (None, 13, 13, 192)   0           fire6/expand1x1[0][0]            
____________________________________________________________________________________________________
fire6/relu_expand3x3 (Activation (None, 13, 13, 192)   0           fire6/expand3x3[0][0]            
____________________________________________________________________________________________________
fire6/concat (Merge)             (None, 13, 13, 384)   0           fire6/relu_expand1x1[0][0]       
                                                                   fire6/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
fire7/squeeze1x1 (Convolution2D) (None, 13, 13, 48)    18480       fire6/concat[0][0]               
____________________________________________________________________________________________________
fire7/relu_squeeze1x1 (Activatio (None, 13, 13, 48)    0           fire7/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire7/expand1x1 (Convolution2D)  (None, 13, 13, 192)   9408        fire7/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire7/expand3x3 (Convolution2D)  (None, 13, 13, 192)   83136       fire7/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire7/relu_expand1x1 (Activation (None, 13, 13, 192)   0           fire7/expand1x1[0][0]            
____________________________________________________________________________________________________
fire7/relu_expand3x3 (Activation (None, 13, 13, 192)   0           fire7/expand3x3[0][0]            
____________________________________________________________________________________________________
fire7/concat (Merge)             (None, 13, 13, 384)   0           fire7/relu_expand1x1[0][0]       
                                                                   fire7/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
fire8/squeeze1x1 (Convolution2D) (None, 13, 13, 64)    24640       fire7/concat[0][0]               
____________________________________________________________________________________________________
fire8/relu_squeeze1x1 (Activatio (None, 13, 13, 64)    0           fire8/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire8/expand1x1 (Convolution2D)  (None, 13, 13, 256)   16640       fire8/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire8/expand3x3 (Convolution2D)  (None, 13, 13, 256)   147712      fire8/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire8/relu_expand1x1 (Activation (None, 13, 13, 256)   0           fire8/expand1x1[0][0]            
____________________________________________________________________________________________________
fire8/relu_expand3x3 (Activation (None, 13, 13, 256)   0           fire8/expand3x3[0][0]            
____________________________________________________________________________________________________
fire8/concat (Merge)             (None, 13, 13, 512)   0           fire8/relu_expand1x1[0][0]       
                                                                   fire8/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
fire9/squeeze1x1 (Convolution2D) (None, 13, 13, 64)    32832       fire8/concat[0][0]               
____________________________________________________________________________________________________
fire9/relu_squeeze1x1 (Activatio (None, 13, 13, 64)    0           fire9/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire9/expand1x1 (Convolution2D)  (None, 13, 13, 256)   16640       fire9/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire9/expand3x3 (Convolution2D)  (None, 13, 13, 256)   147712      fire9/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire9/relu_expand1x1 (Activation (None, 13, 13, 256)   0           fire9/expand1x1[0][0]            
____________________________________________________________________________________________________
fire9/relu_expand3x3 (Activation (None, 13, 13, 256)   0           fire9/expand3x3[0][0]            
____________________________________________________________________________________________________
fire9/concat (Merge)             (None, 13, 13, 512)   0           fire9/relu_expand1x1[0][0]       
                                                                   fire9/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
drop9 (Dropout)                  (None, 13, 13, 512)   0           fire9/concat[0][0]               
____________________________________________________________________________________________________
conv10 (Convolution2D)           (None, 13, 13, 1000)  513000      drop9[0][0]                      
____________________________________________________________________________________________________
relu_conv10 (Activation)         (None, 13, 13, 1000)  0           conv10[0][0]                     
____________________________________________________________________________________________________
globalaveragepooling2d_1 (Global (None, 1000)          0           relu_conv10[0][0]                
____________________________________________________________________________________________________
loss (Activation)                (None, 1000)          0           globalaveragepooling2d_1[0][0]   
====================================================================================================
Total params: 1,235,496
Trainable params: 1,235,496
Non-trainable params: 0
____________________________________________________________________________________________________

In [7]:
# trim after fire9/concat
squeezenet_model = Model(input=squeezenet_model.inputs, output=squeezenet_model.layers[61].output)
squeezenet_model.summary()


____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_1 (InputLayer)             (None, 227, 227, 3)   0                                            
____________________________________________________________________________________________________
conv1 (Convolution2D)            (None, 113, 113, 64)  1792        input_1[0][0]                    
____________________________________________________________________________________________________
relu_conv1 (Activation)          (None, 113, 113, 64)  0           conv1[0][0]                      
____________________________________________________________________________________________________
pool1 (MaxPooling2D)             (None, 56, 56, 64)    0           relu_conv1[0][0]                 
____________________________________________________________________________________________________
fire2/squeeze1x1 (Convolution2D) (None, 56, 56, 16)    1040        pool1[0][0]                      
____________________________________________________________________________________________________
fire2/relu_squeeze1x1 (Activatio (None, 56, 56, 16)    0           fire2/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire2/expand1x1 (Convolution2D)  (None, 56, 56, 64)    1088        fire2/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire2/expand3x3 (Convolution2D)  (None, 56, 56, 64)    9280        fire2/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire2/relu_expand1x1 (Activation (None, 56, 56, 64)    0           fire2/expand1x1[0][0]            
____________________________________________________________________________________________________
fire2/relu_expand3x3 (Activation (None, 56, 56, 64)    0           fire2/expand3x3[0][0]            
____________________________________________________________________________________________________
fire2/concat (Merge)             (None, 56, 56, 128)   0           fire2/relu_expand1x1[0][0]       
                                                                   fire2/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
fire3/squeeze1x1 (Convolution2D) (None, 56, 56, 16)    2064        fire2/concat[0][0]               
____________________________________________________________________________________________________
fire3/relu_squeeze1x1 (Activatio (None, 56, 56, 16)    0           fire3/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire3/expand1x1 (Convolution2D)  (None, 56, 56, 64)    1088        fire3/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire3/expand3x3 (Convolution2D)  (None, 56, 56, 64)    9280        fire3/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire3/relu_expand1x1 (Activation (None, 56, 56, 64)    0           fire3/expand1x1[0][0]            
____________________________________________________________________________________________________
fire3/relu_expand3x3 (Activation (None, 56, 56, 64)    0           fire3/expand3x3[0][0]            
____________________________________________________________________________________________________
fire3/concat (Merge)             (None, 56, 56, 128)   0           fire3/relu_expand1x1[0][0]       
                                                                   fire3/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
pool3 (MaxPooling2D)             (None, 27, 27, 128)   0           fire3/concat[0][0]               
____________________________________________________________________________________________________
fire4/squeeze1x1 (Convolution2D) (None, 27, 27, 32)    4128        pool3[0][0]                      
____________________________________________________________________________________________________
fire4/relu_squeeze1x1 (Activatio (None, 27, 27, 32)    0           fire4/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire4/expand1x1 (Convolution2D)  (None, 27, 27, 128)   4224        fire4/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire4/expand3x3 (Convolution2D)  (None, 27, 27, 128)   36992       fire4/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire4/relu_expand1x1 (Activation (None, 27, 27, 128)   0           fire4/expand1x1[0][0]            
____________________________________________________________________________________________________
fire4/relu_expand3x3 (Activation (None, 27, 27, 128)   0           fire4/expand3x3[0][0]            
____________________________________________________________________________________________________
fire4/concat (Merge)             (None, 27, 27, 256)   0           fire4/relu_expand1x1[0][0]       
                                                                   fire4/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
fire5/squeeze1x1 (Convolution2D) (None, 27, 27, 32)    8224        fire4/concat[0][0]               
____________________________________________________________________________________________________
fire5/relu_squeeze1x1 (Activatio (None, 27, 27, 32)    0           fire5/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire5/expand1x1 (Convolution2D)  (None, 27, 27, 128)   4224        fire5/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire5/expand3x3 (Convolution2D)  (None, 27, 27, 128)   36992       fire5/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire5/relu_expand1x1 (Activation (None, 27, 27, 128)   0           fire5/expand1x1[0][0]            
____________________________________________________________________________________________________
fire5/relu_expand3x3 (Activation (None, 27, 27, 128)   0           fire5/expand3x3[0][0]            
____________________________________________________________________________________________________
fire5/concat (Merge)             (None, 27, 27, 256)   0           fire5/relu_expand1x1[0][0]       
                                                                   fire5/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
pool5 (MaxPooling2D)             (None, 13, 13, 256)   0           fire5/concat[0][0]               
____________________________________________________________________________________________________
fire6/squeeze1x1 (Convolution2D) (None, 13, 13, 48)    12336       pool5[0][0]                      
____________________________________________________________________________________________________
fire6/relu_squeeze1x1 (Activatio (None, 13, 13, 48)    0           fire6/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire6/expand1x1 (Convolution2D)  (None, 13, 13, 192)   9408        fire6/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire6/expand3x3 (Convolution2D)  (None, 13, 13, 192)   83136       fire6/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire6/relu_expand1x1 (Activation (None, 13, 13, 192)   0           fire6/expand1x1[0][0]            
____________________________________________________________________________________________________
fire6/relu_expand3x3 (Activation (None, 13, 13, 192)   0           fire6/expand3x3[0][0]            
____________________________________________________________________________________________________
fire6/concat (Merge)             (None, 13, 13, 384)   0           fire6/relu_expand1x1[0][0]       
                                                                   fire6/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
fire7/squeeze1x1 (Convolution2D) (None, 13, 13, 48)    18480       fire6/concat[0][0]               
____________________________________________________________________________________________________
fire7/relu_squeeze1x1 (Activatio (None, 13, 13, 48)    0           fire7/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire7/expand1x1 (Convolution2D)  (None, 13, 13, 192)   9408        fire7/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire7/expand3x3 (Convolution2D)  (None, 13, 13, 192)   83136       fire7/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire7/relu_expand1x1 (Activation (None, 13, 13, 192)   0           fire7/expand1x1[0][0]            
____________________________________________________________________________________________________
fire7/relu_expand3x3 (Activation (None, 13, 13, 192)   0           fire7/expand3x3[0][0]            
____________________________________________________________________________________________________
fire7/concat (Merge)             (None, 13, 13, 384)   0           fire7/relu_expand1x1[0][0]       
                                                                   fire7/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
fire8/squeeze1x1 (Convolution2D) (None, 13, 13, 64)    24640       fire7/concat[0][0]               
____________________________________________________________________________________________________
fire8/relu_squeeze1x1 (Activatio (None, 13, 13, 64)    0           fire8/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire8/expand1x1 (Convolution2D)  (None, 13, 13, 256)   16640       fire8/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire8/expand3x3 (Convolution2D)  (None, 13, 13, 256)   147712      fire8/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire8/relu_expand1x1 (Activation (None, 13, 13, 256)   0           fire8/expand1x1[0][0]            
____________________________________________________________________________________________________
fire8/relu_expand3x3 (Activation (None, 13, 13, 256)   0           fire8/expand3x3[0][0]            
____________________________________________________________________________________________________
fire8/concat (Merge)             (None, 13, 13, 512)   0           fire8/relu_expand1x1[0][0]       
                                                                   fire8/relu_expand3x3[0][0]       
____________________________________________________________________________________________________
fire9/squeeze1x1 (Convolution2D) (None, 13, 13, 64)    32832       fire8/concat[0][0]               
____________________________________________________________________________________________________
fire9/relu_squeeze1x1 (Activatio (None, 13, 13, 64)    0           fire9/squeeze1x1[0][0]           
____________________________________________________________________________________________________
fire9/expand1x1 (Convolution2D)  (None, 13, 13, 256)   16640       fire9/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire9/expand3x3 (Convolution2D)  (None, 13, 13, 256)   147712      fire9/relu_squeeze1x1[0][0]      
____________________________________________________________________________________________________
fire9/relu_expand1x1 (Activation (None, 13, 13, 256)   0           fire9/expand1x1[0][0]            
____________________________________________________________________________________________________
fire9/relu_expand3x3 (Activation (None, 13, 13, 256)   0           fire9/expand3x3[0][0]            
____________________________________________________________________________________________________
fire9/concat (Merge)             (None, 13, 13, 512)   0           fire9/relu_expand1x1[0][0]       
                                                                   fire9/relu_expand3x3[0][0]       
====================================================================================================
Total params: 722,496
Trainable params: 722,496
Non-trainable params: 0
____________________________________________________________________________________________________

In [8]:
train_preds=squeezenet_model.predict(X_hard, verbose=0)
print(train_preds.shape)

pickle_file = 'squeezenet_hard_preds-86528D.npy'
np.save(pickle_file,train_preds)
    
statinfo = os.stat(pickle_file)
print('File size:', statinfo.st_size)


(1186, 13, 13, 512)
File size: 410488928

In [9]:
train_preds=squeezenet_model.predict(X_train, verbose=0)
print(train_preds.shape)

pickle_file = 'squeezenet_train_preds-86528D.npy'
np.save(pickle_file,train_preds)

statinfo = os.stat(pickle_file)
print('File size:', statinfo.st_size)


(23814, 13, 13, 512)
File size: 8242311264

In [11]:
X_train=''
Y_train=''
X_hard=''
Y_hard=''
import gc
gc.collect()


Out[11]:
12049

In [12]:
X_test=np.load('catfish-X_test.npy')

In [13]:
train_preds=squeezenet_model.predict(X_test, verbose=0)

pickle_file = 'squeezenet_test_preds-86528D.npy'
np.save(pickle_file,train_preds)


statinfo = os.stat(pickle_file)
print('File size:', statinfo.st_size)


File size: 4326400096

In [ ]: