import json, sys, random
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Flatten, Activation
from keras.layers import Dropout
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.utils import np_utils
from keras.optimizers import SGD
import keras.callbacks
Using TensorFlow backend.
from PIL import Image, ImageDraw
from matplotlib import pyplot as plt
# download dataset from json object
f = open(r'../input/ships-in-satellite-imagery/shipsnet.json')
dataset = json.load(f)
f.close()
input_data = np.array(dataset['data']).astype('uint8')
output_data = np.array(dataset['labels']).astype('uint8')
INPUT
The dataset contains 2800 images. One image is represented as a vector of length 19200 elements.
input_data.shape
(4000, 19200)
n_spectrum = 3 # color chanel (RGB)
weight = 80
height = 80
X = input_data.reshape([-1, n_spectrum, weight, height])
X[0].shape
(3, 80, 80)
# get one chanel
pic = X[0]
rad_spectrum = pic[0]
green_spectrum = pic[1]
blue_spectum = pic[2]
plt.figure(2, figsize = (5*3, 5*1))
plt.set_cmap('jet')
# show each channel
plt.subplot(1, 3, 1)
plt.imshow(rad_spectrum)
plt.subplot(1, 3, 2)
plt.imshow(green_spectrum)
plt.subplot(1, 3, 3)
plt.imshow(blue_spectum)
plt.show()
OUTPUT
The output is a vector of lenght 2800 elements.
output_data.shape
(4000,)
The vector contains int 0 and 1
output_data
array([1, 1, 1, ..., 0, 0, 0], dtype=uint8)
Vector contains of 2100 zeros and 700 units. This means that in a dataset of 700 images tagged with "ship" and 2100 images marked as "not ship".
np.bincount(output_data)
array([3000, 1000])
# output encoding
y = np_utils.to_categorical(output_data, 2)
# shuffle all indexes
indexes = np.arange(4000)
np.random.shuffle(indexes)
X_train = X[indexes].transpose([0,2,3,1])
y_train = y[indexes]
# normalization
X_train = X_train / 255
np.random.seed(42)
# network design
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same', input_shape=(80, 80, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2))) #40x40
model.add(Dropout(0.25))
model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2))) #20x20
model.add(Dropout(0.25))
model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2))) #10x10
model.add(Dropout(0.25))
model.add(Conv2D(32, (10, 10), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2))) #5x5
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. Instructions for updating: Colocations handled automatically by placer. WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version. Instructions for updating: Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
# optimization setup
sgd = SGD(lr=0.01, momentum=0.9, nesterov=True)
model.compile(
loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
# training
model.fit(
X_train,
y_train,
batch_size=32,
epochs=18,
validation_split=0.2,
shuffle=True,
verbose=2)
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version. Instructions for updating: Use tf.cast instead. Train on 3200 samples, validate on 800 samples Epoch 1/18 - 30s - loss: 0.4134 - acc: 0.8125 - val_loss: 0.2515 - val_acc: 0.8925 Epoch 2/18 - 30s - loss: 0.2323 - acc: 0.9041 - val_loss: 0.1507 - val_acc: 0.9475 Epoch 3/18 - 30s - loss: 0.1996 - acc: 0.9209 - val_loss: 0.1800 - val_acc: 0.9313 Epoch 4/18 - 29s - loss: 0.1592 - acc: 0.9306 - val_loss: 0.1129 - val_acc: 0.9688 Epoch 5/18 - 29s - loss: 0.1400 - acc: 0.9441 - val_loss: 0.0949 - val_acc: 0.9688 Epoch 6/18 - 29s - loss: 0.1348 - acc: 0.9453 - val_loss: 0.0891 - val_acc: 0.9700 Epoch 7/18 - 29s - loss: 0.1063 - acc: 0.9594 - val_loss: 0.0689 - val_acc: 0.9700 Epoch 8/18 - 29s - loss: 0.1083 - acc: 0.9644 - val_loss: 0.0929 - val_acc: 0.9712 Epoch 9/18 - 29s - loss: 0.0931 - acc: 0.9675 - val_loss: 0.0697 - val_acc: 0.9800 Epoch 10/18 - 29s - loss: 0.0740 - acc: 0.9744 - val_loss: 0.0391 - val_acc: 0.9800 Epoch 11/18 - 29s - loss: 0.0898 - acc: 0.9656 - val_loss: 0.0703 - val_acc: 0.9750 Epoch 12/18 - 29s - loss: 0.0685 - acc: 0.9781 - val_loss: 0.0669 - val_acc: 0.9800 Epoch 13/18 - 29s - loss: 0.0613 - acc: 0.9819 - val_loss: 0.0852 - val_acc: 0.9725 Epoch 14/18 - 29s - loss: 0.0612 - acc: 0.9806 - val_loss: 0.0581 - val_acc: 0.9788 Epoch 15/18 - 30s - loss: 0.0554 - acc: 0.9794 - val_loss: 0.0572 - val_acc: 0.9850 Epoch 16/18 - 30s - loss: 0.0525 - acc: 0.9797 - val_loss: 0.0530 - val_acc: 0.9838 Epoch 17/18 - 29s - loss: 0.0584 - acc: 0.9803 - val_loss: 0.0478 - val_acc: 0.9862 Epoch 18/18 - 30s - loss: 0.0475 - acc: 0.9831 - val_loss: 0.0468 - val_acc: 0.9875
<keras.callbacks.History at 0x7f32643da668>
image = Image.open('../input/sfbay/sfbay_1.png')
pix = image.load()
n_spectrum = 3
width = image.size[0]
height = image.size[1]
# creat vector
picture_vector = []
for chanel in range(n_spectrum):
for y in range(height):
for x in range(width):
picture_vector.append(pix[x, y][chanel])
picture_vector = np.array(picture_vector).astype('uint8')
picture_tensor = picture_vector.reshape([n_spectrum, height, width]).transpose(1, 2, 0)
picture_tensor.shape
(1777, 2825, 3)
plt.figure(1, figsize = (15, 30))
plt.subplot(3, 1, 1)
plt.imshow(picture_tensor)
plt.show()
picture_tensor = picture_tensor.transpose(2,0,1)
picture_tensor.shape
(3, 1777, 2825)
def cutting(x, y):
area_study = np.arange(3*80*80).reshape(3, 80, 80)
for i in range(80):
for j in range(80):
area_study[0][i][j] = picture_tensor[0][y+i][x+j]
area_study[1][i][j] = picture_tensor[1][y+i][x+j]
area_study[2][i][j] = picture_tensor[2][y+i][x+j]
area_study = area_study.reshape([-1, 3, 80, 80])
area_study = area_study.transpose([0,2,3,1])
area_study = area_study / 255
sys.stdout.write('\rX:{0} Y:{1} '.format(x, y))
return area_study
def not_near(x, y, s, coordinates):
result = True
for e in coordinates:
if x+s > e[0][0] and x-s < e[0][0] and y+s > e[0][1] and y-s < e[0][1]:
result = False
return result
def show_ship(x, y, acc, thickness=5):
for i in range(80):
for ch in range(3):
for th in range(thickness):
picture_tensor[ch][y+i][x-th] = -1
for i in range(80):
for ch in range(3):
for th in range(thickness):
picture_tensor[ch][y+i][x+th+80] = -1
for i in range(80):
for ch in range(3):
for th in range(thickness):
picture_tensor[ch][y-th][x+i] = -1
for i in range(80):
for ch in range(3):
for th in range(thickness):
picture_tensor[ch][y+th+80][x+i] = -1
step = 10; coordinates = []
for y in range(int((height-(80-step))/step)):
for x in range(int((width-(80-step))/step) ):
area = cutting(x*step, y*step)
result = model.predict(area)
if result[0][1] > 0.90 and not_near(x*step,y*step, 88, coordinates):
coordinates.append([[x*step, y*step], result])
print(result)
plt.imshow(area[0])
plt.show()
X:810 Y:10 [[0.04458108 0.95541894]]
X:820 Y:350 [[0.08825494 0.9117451 ]]
X:1640 Y:420 [[0.02345004 0.9765499 ]]
X:1890 Y:430 [[0.01587618 0.98412377]]
X:2190 Y:470 [[1.4625650e-05 9.9998534e-01]]
X:880 Y:490 [[0.07363806 0.926362 ]]
X:950 Y:690 [[0.04267973 0.9573203 ]]
X:1830 Y:1180 [[0.09686951 0.9031305 ]]
X:2160 Y:1200 [[0.03439297 0.96560705]]
X:1060 Y:1260 [[0.06018276 0.9398173 ]]
X:2460 Y:1260 [[0.00666098 0.993339 ]]
X:940 Y:1270 [[0.09059117 0.9094088 ]]
X:1230 Y:1590 [[0.00807857 0.9919214 ]]
X:1320 Y:1650 [[6.5420769e-05 9.9993455e-01]]
X:2740 Y:1690
for e in coordinates:
show_ship(e[0][0], e[0][1], e[1][0][1])
#picture_tensor = picture_tensor.transpose(2,0,1)
picture_tensor = picture_tensor.transpose(1,2,0)
picture_tensor.shape
(1777, 2825, 3)
plt.figure(1, figsize = (15, 30))
plt.subplot(3,1,1)
plt.imshow(picture_tensor)
plt.show()