In [1]:
from keras.layers import Input, Dense, Convolution2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras.callbacks import TensorBoard
from keras.models import model_from_json
from keras.models import load_model
from keras import regularizers

import os
from os import listdir
from os.path import isfile, join
import numpy as np
from matplotlib import pyplot as plt
import  cv2
import scipy.misc
from scipy import spatial
from PIL import Image
%matplotlib inline


Using TensorFlow backend.

In [21]:
# load json and create model
json_file = open('../model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model_dae.h5")
print("Loaded model from disk")

loaded_model.compile(loss='binary_crossentropy', optimizer='adagrad')
img = plt.imread('/home/arvind/MyStuff/Desktop/Manatee_dataset/cleaned_data/test/op_U3850_B.jpg.tif')
img = cv2.imread('/home/arvind/MyStuff/Desktop/Manatee_dataset/cleaned_data/test/op_U3850_B.jpg.tif')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img[img<100] = 20
img[img>=100] = 0

plt.imshow(img)
plt.show()
X = np.array([img])
X = X.astype('float32')#/ float(np.max(X))
X = np.reshape(X, (len(X),  224, 224, 1))
#X_ = np.array([np.invert(img)])
#X_ = X.astype('float32')#/ float(np.max(X_))
#plt.imshow(img)
#X_ = np.reshape(X_, (len(X_), 224, 224, 1))
#score = loaded_model.evaluate(X_, X_, verbose=0)
pred = loaded_model.predict(X, verbose=0)[0]
#pred = pred * 255.
plt.imshow(pred.reshape((224,224)))


Loaded model from disk
Out[21]:
<matplotlib.image.AxesImage at 0x7fdde4407d90>

In [5]:
img = Image.open(mypath+filen)
img = np.asarray(img, dtype=np.uint8)

img = cv2.imread(mypath+filen)
img = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)
img[img<200] = 1
img[img>=200] = 0

#img = np.invert(img.T)
images1=[]
images1.append(np.array([img]))
images_train = np.array(images1)
images_train = images_train.astype('float32')#/ float(np.max(images_train))


json_file = open('../model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model_right.h5")
print("Loaded model from disk")

loaded_model.compile(loss='binary_crossentropy', optimizer='adagrad')
X = images_train
#X = X.astype('float32') / float(np.max(X))
X = np.reshape(X, (len(X),  224, 224, 1))
#X_ = np.array([np.invert(img)])
#X_ = X_.astype('float32') / float(np.max(X_))
#plt.imshow(img)
#X_ = np.reshape(X_, (len(X_), 224, 224, 1))
#score = loaded_model.evaluate(X_, X_, verbose=0)
plt.imshow(loaded_model.predict(X, verbose=0)[0].reshape((224,224)))


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-7aa62a676c26> in <module>()
----> 1 img = Image.open(mypath+filen)
      2 img = np.asarray(img, dtype=np.uint8)
      3 
      4 img = cv2.imread(mypath+filen)
      5 img = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)

NameError: name 'mypath' is not defined

In [41]:
predicted_label


Out[41]:
array([ 0.        ,  0.0231612 ,  0.00370248, ...,  0.53082341,
        0.03798916,  0.26384863], dtype=float32)

In [8]:
input_img = Input(shape=(224, 224,1))
#plt.imshow(loaded_model.predict(X_).reshape(224,224))
x = Convolution2D(16, 3, 3, activation='relu', border_mode='same', input_shape=(224,224,1), weights=loaded_model.layers[1].get_weights())(input_img)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same', weights=loaded_model.layers[3].get_weights())(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same', weights=loaded_model.layers[5].get_weights())(x)
encoded = MaxPooling2D((4, 4), border_mode='same')(x)
model = Model(input_img, encoded)
#model.compile(loss='binary_crossentropy', optimizer='adagrad')
model.load_weights('modelnew1.h5', by_name=True)
#print score
predicted_label = model.predict(X_, verbose=0).flatten()


img = plt.imread('/home/arvind/Desktop/Manatee_dataset/cleaned_data/train/op_U1150.jpg.tif')

X_ = np.array([np.invert(img)])
X_ = X_.astype('float32') / float(np.max(X_))
X_ = np.reshape(X_, (len(X_), 224, 224, 1))
predicted_label1 = model.predict(X_, verbose=0)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-a3ff5e910b56> in <module>()
      9 model = Model(input_img, encoded)
     10 #model.compile(loss='binary_crossentropy', optimizer='adagrad')
---> 11 model.load_weights('modelnew1.h5', by_name=True)
     12 #print score
     13 predicted_label = model.predict(X_, verbose=0).flatten()

/home/arvind/.local/lib/python2.7/site-packages/keras/engine/topology.pyc in load_weights(self, filepath, by_name)
   2704             f = f['model_weights']
   2705         if by_name:
-> 2706             self.load_weights_from_hdf5_group_by_name(f)
   2707         else:
   2708             self.load_weights_from_hdf5_group(f)

/home/arvind/.local/lib/python2.7/site-packages/keras/engine/topology.pyc in load_weights_from_hdf5_group_by_name(self, f)
   2841                         weight_value_tuples.append((symbolic_weights[i],
   2842                                                     weight_values[i]))
-> 2843             K.batch_set_value(weight_value_tuples)
   2844 
   2845     def _updated_config(self):

/home/arvind/.local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.pyc in batch_set_value(tuples)
   1879             assign_ops.append(assign_op)
   1880             feed_dict[assign_placeholder] = value
-> 1881         get_session().run(assign_ops, feed_dict=feed_dict)
   1882 
   1883 

/home/arvind/.local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    765     try:
    766       result = self._run(None, fetches, feed_dict, options_ptr,
--> 767                          run_metadata_ptr)
    768       if run_metadata:
    769         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/home/arvind/.local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
    942                 'Cannot feed value of shape %r for Tensor %r, '
    943                 'which has shape %r'
--> 944                 % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
    945           if not self.graph.is_feedable(subfeed_t):
    946             raise ValueError('Tensor %s may not be fed.' % subfeed_t)

ValueError: Cannot feed value of shape (3, 3, 8, 16) for Tensor u'Placeholder_38:0', which has shape '(3, 3, 8, 8)'

In [42]:
1 - spatial.distance.cosine(predicted_label, predicted_label1)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-42-e57432e07660> in <module>()
----> 1 1 - spatial.distance.cosine(predicted_label, predicted_label1)

/home/arvind/.local/lib/python2.7/site-packages/scipy/spatial/distance.pyc in cosine(u, v)
    323     """
    324     u = _validate_vector(u)
--> 325     v = _validate_vector(v)
    326     dist = 1.0 - np.dot(u, v) / (norm(u) * norm(v))
    327     return dist

/home/arvind/.local/lib/python2.7/site-packages/scipy/spatial/distance.pyc in _validate_vector(u, dtype)
    153     u = np.atleast_1d(u)
    154     if u.ndim > 1:
--> 155         raise ValueError("Input vector should be 1-D.")
    156     return u
    157 

ValueError: Input vector should be 1-D.

In [43]:
img = plt.imread('/home/arvind/Desktop/Manatee_dataset/cleaned_data/train/op_U3427.jpg.tif')
X = np.array([img])
X = X.astype('float32') / float(np.max(X))
X = np.reshape(X, (len(X),  224, 224, 1))
X_ = np.array([np.invert(img)])
X_ = X_.astype('float32') / float(np.max(X_))
X_ = np.reshape(X_, (len(X_), 224, 224, 1))
predicted_label2 = model.predict(X_, verbose=1)


1/1 [==============================] - 0s

In [16]:
1 - spatial.distance.cosine(np.invert(np.array(predicted_label)), np.invert(np.array(predicted_label2)))


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-5153d6372794> in <module>()
----> 1 1 - spatial.distance.cosine(np.invert(np.array(predicted_label)), np.invert(np.array(predicted_label2)))

TypeError: ufunc 'invert' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

In [17]:
a = (predicted_label[predicted_label.astype(int)>0]).flatten()
b = (predicted_label2[predicted_label2.astype(int)>0]).flatten()
a = a[:min(len(a), len(b))]
b = b[:min(len(a), len(b))]

In [18]:
1 - spatial.distance.cosine(a,b)


---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-18-905ea184a260> in <module>()
----> 1 1 - spatial.distance.cosine(a,b)

/home/arvind/.local/lib/python2.7/site-packages/scipy/spatial/distance.pyc in cosine(u, v)
    324     u = _validate_vector(u)
    325     v = _validate_vector(v)
--> 326     dist = 1.0 - np.dot(u, v) / (norm(u) * norm(v))
    327     return dist
    328 

/home/arvind/.local/lib/python2.7/site-packages/scipy/linalg/misc.pyc in norm(a, ord, axis, keepdims)
    135             # use blas for fast and stable euclidean norm
    136             nrm2 = get_blas_funcs('nrm2', dtype=a.dtype)
--> 137             return nrm2(a)
    138 
    139         if a.ndim == 2 and axis is None and not keepdims:

error: (offx>=0 && offx<len(x)) failed for 2nd keyword offx: snrm2:offx=0

In [53]:
mypath = '/home/arvind/MyStuff/Desktop/Manatee_dataset/cleaned_data/train/'
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
images = []
masks = np.zeros((224,224))
for filen in files:
#       img = cv2.imread(mypath+filen)
        img = cv2.imread(mypath+filen)
        #img = np.asarray(img, dtype=np.uint8)
        #img = np.invert(img)
        masks += img[:,:,2]/255.
        images.append(np.array([img]))
images_train = np.array(images[:-100])
images_test = np.array(images[-100:])

images_train = images_train.astype('float32')# /255.# / float(np.max(images_train))
images_test = images_test.astype('float32')# -np.mean(images_test) # /255.# / float(np.max(images_test))

%matplotlib


Using matplotlib backend: TkAgg

In [56]:
i = np.copy(images_train[2][0])
i = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)
#_,i = cv2.threshold(i.astype('uint8'), 0, 1, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
i[i<200] = 1
i[i>=200] = 0
#i[masks>100] = 1
plt.imshow(i)


Out[56]:
<matplotlib.image.AxesImage at 0x7fd820207090>

In [69]:
np.average(masks)


Out[69]:
88.891748339961055

In [16]:
input_img = Input(shape=(224, 224,1))
x = Convolution2D(16, 3, 3, activation='relu', border_mode='same', input_shape=(224,224,1))(input_img)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same', activity_regularizer=regularizers.activity_l1(10e-5))(x)
encoded = MaxPooling2D((2, 2), border_mode='same')(x)
model = Model(input_img, encoded)
model.compile(loss='binary_crossentropy', optimizer='adagrad')
model.load_weights('model_dae.h5', by_name=True)
#model.load_weights('modelnew1.h5', by_name=True)

#print score
predicted_label = model.predict(X, verbose=0)

In [17]:
img = cv2.imread('/home/arvind/MyStuff/Desktop/Manatee_dataset/cleaned_data/test/op_U3996_B.jpg.tif')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img[img<100] = 20
img[img>=100] = 0

plt.imshow(img)
plt.show()

X = np.array([img])
X = X.astype('float32')#/ float(np.max(X))
X = np.reshape(X, (len(X),  224, 224, 1))

predicted_label1 = model.predict(X, verbose=0)
predicted_label1 = predicted_label1[0]



In [18]:
plt.imshow(np.sum(predicted_label1, axis=2))


Out[18]:
<matplotlib.image.AxesImage at 0x7fddf4367dd0>

In [18]:
#img = cv2.imread('/home/arvind/MyStuff/Desktop/Manatee_dataset/cleaned_data/train/op_U3427.jpg.tif')
img = cv2.imread('/home/arvind/MyStuff/Desktop/Manatee_dataset/cleaned_data/train/op_U065.jpg.tif')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img[img<100] = 20
img[img>=100] = 0

plt.imshow(img)
plt.show()

X = np.array([img])
X = X.astype('float32')#/ float(np.max(X))
X = np.reshape(X, (len(X),  224, 224, 1))

predicted_label2 = model.predict(X, verbose=0)
predicted_label2 = predicted_label2[0]



In [19]:
plt.imshow(np.sum(predicted_label2, axis=2))


Out[19]:
<matplotlib.image.AxesImage at 0x7f2268819610>

In [20]:
1 - spatial.distance.cosine(predicted_label1.flatten(), predicted_label2.flatten())


Out[20]:
0.5597676179420108

In [ ]: